Re: Quick question: eval and read-from-string scoping



Jeremy Smith wrote:
Hi,

When I run this code in CLisp:

    	(let ((left 5) (top 10))
    	    	(eval(read-from-string "(eq left (margin top 20))")))

When executed, it complains that "*** - EVAL: variable left has no value". At first I wondered why the local variables were not in scope for the eval'd code and was going to post, but I found this thread:

(eval) gets its own scope. It would be nice if you could pass the current environment with all its local variables to eval. However, I don't know a way to do this. What you might use is closures, but they more or less just replace global variables with global functions.

(let ((left 5)) (top 10))
  (defun get-left nil left)
  (defun get-top nil top)
  (eval (read-from-string "(eq left top)")))


http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/846c4f 53e91ef251/69239105712fed0d?lnk=st&q=lisp+eval+global+variables&rnum=2# 69239105712fed0d

And now I realise that eval is like a function, and local variables are not passed from function to function. So the question is revised: How do I pass variables to code executed by eval without declaring them as special?

I remember a scheme where you could do this with eval-env or something and pass the environment in the second parameter. You could write your own eval-env. Not trivial, though.


Finally, my reasons for knowing this is that I am implementing a small description language for OCR (optical character recognition) software but I'm implementing it in Lisp before I write a parser, so I 'eval' the descriptions as if read in from a text file. The reason I need to be able to pass data to the eval'd code is because they are the attributes of the current character object (such as top,bottom,left,right) and thus should not be global.

You could make lisp your declaration language. Syntax sucks anyway. If your program is used and extended for a long time, the syntax will grow. After a few years today's improvisations will haunt you and will force you and your users to live with cruft. .



Relevant Pages

  • Re: newLISP is simple, terse, and well documented
    ... or, if you insist on "local variables", we can fix it with a simple macro ... will this macro make CL easier to use? ... The idiotic processing of a symbol as a string in your newLISP implementation ... namely that eval takes place in a null lexical environment. ...
    (comp.lang.lisp)
  • Re: {{ }} Unnamed local vars binding
    ... it can be dangerous if you have nested LAM environments. ... How would you reference a LAM in an outer environment, ... implemented as unnamed local variables, ... This works in debug4x, but with reverse order: ...
    (comp.sys.hp48)
  • Re: Problem With eval and each_index
    ...  I have no problem splitting the string into an array, ... call it e, or in putting the variable names in a second array, call it ... local variables are local to the block. ... and they therefore capture the environment ...
    (comp.lang.ruby)
  • Re: Could someone please explain the magic in this code
    ... A binding is the environment in which variables are evaluated. ... it allows the local variables in your environment to be ...
    (comp.lang.ruby)
  • Quick question: eval and read-from-string scoping
    ... And now I realise that eval is like a function, and local variables are ... I realise that special variables can be re-defined using 'let' and they ... my reasons for knowing this is that I am implementing a small ...
    (comp.lang.lisp)