~/test:$ cat lookup-var.scm (define (lookup-var var-name)
(##define-macro (macro-code-cte c) `(##vector-ref ,c 2))
(##define-macro (macro-rte-up rte) `(##vector-ref ,rte 0))
(define (the-env) (##continuation-capture (lambda (cont) (if (##interp-continuation? cont) (let (($code (##interp-continuation-code cont)) (rte (##interp-continuation-rte cont))) (cons (macro-code-cte $code) rte)) (error "the-env must be called from interpreted code")))))
(define (env-ref env var) (let loop1 ((c (car env)) (r (cdr env))) (cond ((##cte-top? c) (##global-var-ref (##make-global-var var))) ((##cte-frame? c) (let loop2 ((vars (##cte-frame-vars c)) (i 1)) (if (pair? vars) (if (eq? var (##car vars)) (vector-ref r i) (loop2 (cdr vars) (+ i 1))) (loop1 (##cte-parent-cte c) (macro-rte-up r))))) (else (loop1 (##cte-parent-cte c) r)))))
(env-ref (the-env) var-name)) ~/test:$ cat test.scm (define-macro (foo) (define x 20) (load "./lookup-var") (pp `(foo ,(lookup-var 'x))))
(define-macro (bar) (define x 20) (include "./lookup-var.scm") (pp `(bar ,(lookup-var 'x))))
(foo) (bar)
~/test:$ gsi test.scm (foo #!unbound) (bar 20)
note that lookup-var is just a function ... i'm surprised that load/include gives me different results
Afficher les réponses par date
note that lookup-var is just a function ... i'm surprised that load/include gives me different results
Why are you surprised? If two functions exist, then they do two different things.
The fact is that LOAD is a function (eval'd at runtime, as one explained you), and that INCLUDE is not. Include is a reader-macro. When the parser sees an INCLUDE, it parses the source code of the other file, and then inserts it instead of that form.
No magic here.
P!
They're loading/including the same function. Then I execute them, and I get different results. Isn't that kind of weird? If you loaded or included a function for +, wouldn't you expect it to give you the same results?
On Sat, Aug 15, 2009 at 12:28 AM, Adrien Piérardpierarda@iro.umontreal.ca wrote:
note that lookup-var is just a function ... i'm surprised that load/include gives me different results
Why are you surprised? If two functions exist, then they do two different things.
The fact is that LOAD is a function (eval'd at runtime, as one explained you), and that INCLUDE is not. Include is a reader-macro. When the parser sees an INCLUDE, it parses the source code of the other file, and then inserts it instead of that form.
No magic here.
P!
-- Français, English, 日本語, 한국어
My bad. The reason is (a little bit) different.
LOAD does not access the same current environment when in a macro. It loaded the file in a fresh environment, where x was indeed unbound.
A simpler example is
$ cat tata.scm (define (f x) (+ y x))
$ cat toto.scm (define-macro (foo) (define y 1) (load "tata") (pp (f 3))) (foo)
$ gsi toto.scm *** ERROR IN f, "tata.scm"@2.6 -- Unbound variable: y
The solution? Refactor all your code. I really believe that you are doing things with macros that should be done some other way…
P!
2009/8/15 lowly coder lowlycoder@huoyanjinjing.com:
They're loading/including the same function. Then I execute them, and I get different results. Isn't that kind of weird? If you loaded or included a function for +, wouldn't you expect it to give you the same results?
On Sat, Aug 15, 2009 at 12:28 AM, Adrien Piérardpierarda@iro.umontreal.ca wrote:
note that lookup-var is just a function ... i'm surprised that load/include gives me different results
Why are you surprised? If two functions exist, then they do two different things.
The fact is that LOAD is a function (eval'd at runtime, as one explained you), and that INCLUDE is not. Include is a reader-macro. When the parser sees an INCLUDE, it parses the source code of the other file, and then inserts it instead of that form.
No magic here.
P!
-- Français, English, 日本語, 한국어
I don't quite get this. There are no free variables in lookup-var.
On Sat, Aug 15, 2009 at 1:19 AM, Adrien Piérardpierarda@iro.umontreal.ca wrote:
My bad. The reason is (a little bit) different.
LOAD does not access the same current environment when in a macro. It loaded the file in a fresh environment, where x was indeed unbound.
A simpler example is
$ cat tata.scm (define (f x) (+ y x))
$ cat toto.scm (define-macro (foo) (define y 1) (load "tata") (pp (f 3))) (foo)
$ gsi toto.scm *** ERROR IN f, "tata.scm"@2.6 -- Unbound variable: y
The solution? Refactor all your code. I really believe that you are doing things with macros that should be done some other way…
P!
2009/8/15 lowly coder lowlycoder@huoyanjinjing.com:
They're loading/including the same function. Then I execute them, and I get different results. Isn't that kind of weird? If you loaded or included a function for +, wouldn't you expect it to give you the same results?
On Sat, Aug 15, 2009 at 12:28 AM, Adrien Piérardpierarda@iro.umontreal.ca wrote:
note that lookup-var is just a function ... i'm surprised that load/include gives me different results
Why are you surprised? If two functions exist, then they do two different things.
The fact is that LOAD is a function (eval'd at runtime, as one explained you), and that INCLUDE is not. Include is a reader-macro. When the parser sees an INCLUDE, it parses the source code of the other file, and then inserts it instead of that form.
No magic here.
P!
-- Français, English, 日本語, 한국어
-- Français, English, 日本語, 한국어
The environment containing the variable x seems unknown to the body of lookup-var.
P!
2009/8/15 lowly coder lowlycoder@huoyanjinjing.com:
I don't quite get this. There are no free variables in lookup-var.