Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl does not track locals: User-defined functions are always nil #23

Open
beakr opened this issue Feb 11, 2013 · 6 comments
Open

repl does not track locals: User-defined functions are always nil #23

beakr opened this issue Feb 11, 2013 · 6 comments
Assignees
Milestone

Comments

@beakr
Copy link
Contributor

beakr commented Feb 11, 2013

I'm on OSX, and cloned down a fresh copy of Potion. Whenever I try to evaluate a function in the repl
like so:

>> add = (x, y):
    x + y string print.

>> add (1, 2)

It will always return nil. It's very strange, because if I monkeypatch (e.g. String length(): 10.), or load the built-in readline lib, it will work perfectly.

It seems native extensions will work, but not potion functions themselves.

By the way, I've running OSX Mountain Lion if that somehow makes any difference.

@gildo
Copy link

gildo commented Apr 18, 2013

Also here on Ubuntu. :(

@jkleiser
Copy link

I think it's not only user-defined functions. If I enter "a = 1+2" in the REPL, I get the value 3 back, but if I then type just "a", I get nil.
I get this both with OSX 10.6.8 and 10.8.5.

$ bin/potion 
>> a=1+2
=> 3
>> a
=> nil
>> 

@beakr
Copy link
Contributor Author

beakr commented Oct 24, 2013

@jkleiser It seems as if all user-defined values are nil. It's a considerably serious issue considering it renders the language pretty unusable.

@rurban
Copy link
Member

rurban commented Oct 24, 2013

@beakr Only in the repl, not in scripts. The repl is similar to the debugger pretty limited yet.

@robotii
Copy link
Contributor

robotii commented Oct 17, 2015

Hi @rurban

This is due to the way the repl works.

Each line is executed as its own block/closure, with its own local variables, which go out of scope when the block has finished executing.

Any variable that doesn't start with uppercase is considered local to the block. If you use uppercase variable names, they are treated as globals, and are available to the next block that is executed.

To fix this, we would need to change the way the repl works, to execute the expression(s) contained in the block, rather than the block itself.

This would require changes to the potion_eval function, with probably a lot of other changes to surrounding code, including potion_source_compile, which will refuse to compile a bare expression.

@rurban
Copy link
Member

rurban commented Oct 17, 2015

yes, the repl is very limited. @robotii 's analysis is correct, eval cannot be used for the repl, or eval needs an optional env argument to track the local environment, as in a real lisp.

I'm working on it in the branch cl_eval

rurban pushed a commit that referenced this issue Oct 18, 2015
A closure captures the environment, the locals and lexical variables.
eval should extend it and not re-create it afresh.
See GH #23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants