Hi all,
The following program runs correctly in the interpreter, but fails when run with a generated executable.
(define-syntax foo (syntax-rules () ((_) "hi!\n"))) (display (eval '(foo)))
Running the generated executable fails with "*** ERROR -- Unbound variable: foo".
Am I doing something wrong?
Afficher les réponses par date
On 2012-04-19, at 3:04 PM, Kirill Zorin wrote:
Hi all,
The following program runs correctly in the interpreter, but fails when run with a generated executable.
(define-syntax foo (syntax-rules () ((_) "hi!\n"))) (display (eval '(foo)))
Running the generated executable fails with "*** ERROR -- Unbound variable: foo".
Am I doing something wrong?
Normally, when using define-macro, macros defined in a file are local to that file and they are not visible to eval. So the following will generate an error in the interpreter and compiler:
(define-macro (foo) "hi!\n") (display (eval '(foo)))
The reason your code works in the interpreter is that you are using the psyntax implementation of define-syntax which works by mutating the expansion-time global environment. Because the interpreter's expansion-time global environment is also the run-time global environment used by eval, it is possible to use the foo macro in the code evaluated.
To get your code working in the interpreter and compiler, you should do something like this:
(eval '(load "~~lib/syntax-case")) (eval '(define-syntax foo (syntax-rules () ((_) "hi!\n")))) (display (eval '(foo)))
Marc
On 2012-04-19, at 6:26 PM, Marc Feeley wrote:
To get your code working in the interpreter and compiler, you should do something like this:
(eval '(load "~~lib/syntax-case")) (eval '(define-syntax foo (syntax-rules () ((_) "hi!\n")))) (display (eval '(foo)))
Or if you prefer using define-macro, which is more lightweight than define-syntax, you can do:
(define-macro (eval-in-all-environments expr) (eval expr) ;; in the expansion-time environment `(begin (eval ',expr) ;; in the expansion-time environment of the run-time environment ,expr)) ;; in the run-time environment
(eval-in-all-environments (define-macro (foo) "hi!\n"))
(display (eval '(foo)))
Marc
That makes sense. For my purposes I think it'll suffice to simply put all macros in a "macros.ss" file and just load it inside+outside eval in my prelude. Thanks!
On 2012-04-19, at 6:42 PM, Marc Feeley wrote:
On 2012-04-19, at 6:26 PM, Marc Feeley wrote:
To get your code working in the interpreter and compiler, you should do something like this:
(eval '(load "~~lib/syntax-case")) (eval '(define-syntax foo (syntax-rules () ((_) "hi!\n")))) (display (eval '(foo)))
Or if you prefer using define-macro, which is more lightweight than define-syntax, you can do:
(define-macro (eval-in-all-environments expr) (eval expr) ;; in the expansion-time environment `(begin (eval ',expr) ;; in the expansion-time environment of the run-time environment ,expr)) ;; in the run-time environment
(eval-in-all-environments (define-macro (foo) "hi!\n"))
(display (eval '(foo)))
Marc