From: jeffbz <je...@co...> - 2008-01-08 00:43:29
|
Thanks for the reply. The code is written as intended. And indeed, this problem is confined to the compiler; in the REPL everything is fine. I'll try moving some definitions to another file to see if (load ...) helps. In general this wouldn't help too much though, since I'd like to be able to run an arbitrary portion of my code during macroexpansion. -Jeff Kyle R. Burton wrote: > >> I'm getting an error that seems to indicate the compiler can't see my >> toplevel function definitions during macroexpansion. I have a function >> foo, >> and a macro M that uses it. I get an error compiling function bar, which >> uses M, saying undefined variable "foo". How can I make my definitions >> visible during compilation? >> Example code that exhibits this issue: >> (define (foo x) (+ 1 x)) >> (define-macro (M y) (foo y)) >> (define (bar z) (M 3)) >> >> output: >> first pass of compiler >> (compiling foo) >> (compiling bar) >> (Error while compiling SchemeException: ERROR: undefined variable "foo") >> SchemeException: ERROR: undefined variable "foo" >> at jsint.E.error(E.java:14) >> >> thank you, and thank you for JScheme, which I am greatly enjoying!! > > Do you want foo to be part of the expansoin? In other words, do you > want M to expand to the call to foo, or do you want it to be 'compiled > away'? > > eg, do you want bar to be equivalent to: > > (define (bar z) (foo 3)) > > Or do you want it to be > > (define (bar z) 4) > > ? > > If it is the former, then M should probably look like: > > (define-macro (M y) `(foo ,y)) > > Either way, when I type in your expressions, I receive no error with > jscheme 7.2: > > $ java -jar jscheme-7.2.jar > JScheme 7.2 (2/2/06 9:47 PM) http://jscheme.sourceforge.net >> (define (foo x) (+ 1 x)) > $1 = (lambda foo (x)...) >> (foo 3) > $2 = 4 >> (define-macro (M y) (foo y)) > $3 = (macro M (y)...) >> (define (bar z) (M 3)) > $4 = (lambda bar (z)...) >> (bar 1) > $5 = 4 >> (bar 2) > $6 = 4 >> > > If you're after constant math (making bar be the integer 4), actually > regardless, it looks like if you put the macro definition in a > separate file and load it, it succeeds: > > $ cat mac.scm > (define-macro (M y) (foo y)) > > kburton@lap0029 ~/projects/sandbox/jscheme > $ cat Foo.scm > (load "mac.scm") > (define (foo x) (+ 1 x)) > (define (bar z) (M 3)) > > (define (main args) > (write "bar ") (write (bar 10)) (newline)) > > I'm not very familiar with the compiler...I hope this helps. > > Regards, > > Kyle Burton > -- View this message in context: http://www.nabble.com/definitions-available-at-compile-time--tp14673314p14679936.html Sent from the JScheme - User mailing list archive at Nabble.com. |