This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: a local eval



> BTW, what I really need personally is a way of taking a
> symbol and getting the value bound to that symbol in the
> current environment.  If there is some clever way of
> doing this in Guile, _please_ let me know.  

the following is not really clever, but does work.  the first form uses
`eval2', a guile-ism:

 - primitive: eval2 EXP LOOKUP
     Evaluate EXP, a Scheme expression, in the environment designated
     by LOOKUP, a symbol-lookup function.  `(eval exp)' is equivalent
     to `(eval2 exp *top-level-lookup-closure*)'.

the second form does the lookup.  `d-ref` is really `hashq-ref'; thus
`thud-eval-lookup' consults obarrays `vob' and `fob' before using the
provided `*top-level-lookup-closure*'.

this approach requires you to explicitly create and populate an obarray
(which may or may not be local).  in my code, the obarrays are global to
minimize initialization overhead.  the last two forms show how `fob' is
made and initialized (as with `d-ref', `d-set!' is really `hashq-set!').
`thud-plus' is a function.

check out ice-9/boot-9.scm.  the (current) module system is defined
there, and provides more dances-with-obarrays stuff.  i initially wanted
to use the given mechanisms, but gave up due to the complexity.  (please
no flames -- i am a simple programmer!)

if you find (or define) a good `local-lookup', i would love to hear
about it.

happy hacking,
thi

--------------------------------------------------------------
(eval2 ef thud-eval-lookup)

(define (thud-eval-lookup symbol define?)
  (or (d-ref vob symbol)
      (d-ref fob symbol)
      (*top-level-lookup-closure* symbol #f)
      (builtin-variable symbol)
      (die "failed!")))

(define fob (make-array '() 431))
(d-set! fob '+ (make-variable thud-plus 'thud-plus))