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



Tin Gherdanarra <tinman31337@xxxxxxxxx> wrote in
news:41j4tfF1cpgeiU1@xxxxxxxxxxxxxx:

> 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.

Ah, okay, thanks for the confirmation.

> What you might use is
> closures, but they more or less just replace global variables
> with global functions.

I understand the idea. I think the best thing will be something in one of
the other posts, about eval'ing the code with the variables and initial
values inserted before the code.

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

>> 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.

I suppose you're saying to use Lisp as the language. That sounds like a
good idea, partly because it has minimal syntax, as long as I can get
around the problem of malicious code. Also, it's not necessarily "today's
improvisations" - I'm just experimenting at the moment, and nothing is
set in stone. The basic idea is to get it working first. Also, I'm going
to read On Lisp once it works enough to be improved.

Thanks for your help, have a happy new year!

Cheers,

Jeremy.
.



Relevant Pages

  • Re: Ruby performance woes
    ... You'd think there would be some way for Lisp to fill this gap. ... Rails offers a clean, prescribed and predictable framework that - like ... That's why Ruby ... making up names for local variables and for the functions being ...
    (comp.lang.lisp)
  • Re: Theories on why Lisp source *seems* to have less comments?
    ... I think it is because of functional part of lisp. ... In mostly functional code, the only description that is needed is ... For example quicksort. ... rather than a joggling with local variables with intent to ...
    (comp.lang.lisp)
  • Re: multi-core software
    ... local variables - sure we take them for granted in Lisp (special ... variables rebound in each thread), but I don't think C + phtreads has ... allow for plain old variable syntax: ...
    (comp.lang.lisp)
  • Re: Default argument values for blocks
    ... |Proc that comes from the lambda method? ... syntax of method arguments. ... |a variable of the same name exists in the defining scope). ... In 1.9, you can declare block local variables explicitly, by the ...
    (comp.lang.ruby)
  • Re: [SPARK] Code safety and information hiding
    ... subroutines) in the original has its own local variables. ... distributed system as a whole. ... But I will argue that a variable which is visible everywhere in the program is effectively global in that program - it's the visibility in any given scope the makes something global or local, all the rest is only syntax. ...
    (comp.lang.ada)