Re: Quick question: eval and read-from-string scoping
- From: Tin Gherdanarra <tinman31337@xxxxxxxxx>
- Date: Thu, 29 Dec 2005 23:06:25 +0100
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. .
- Follow-Ups:
- Re: Quick question: eval and read-from-string scoping
- From: Jeremy Smith
- Re: Quick question: eval and read-from-string scoping
- References:
- Quick question: eval and read-from-string scoping
- From: Jeremy Smith
- Quick question: eval and read-from-string scoping
- Prev by Date: Quick question: eval and read-from-string scoping
- Next by Date: Re: Quick question: eval and read-from-string scoping
- Previous by thread: Quick question: eval and read-from-string scoping
- Next by thread: Re: Quick question: eval and read-from-string scoping
- Index(es):
Relevant Pages
|