[Chicken-users] Macros and loading compiled code
Hi, I defined a macro. I compiled it separately and built it into my main program. When my main program calls the load procedure on normal Scheme source files, the procedures in these files use the macro without complaint. However, when it loads the dynamic object code created from compiling those same source files, I get an error about an unbound value in a subexpression of a macro expression. This seems to be because the macro expressions are being evaluated as normal procedure applications. If I insert the macro definition verbatim into each source file before I compile it, the errors do not occur. I tried passing both the source file and the macro definition file as input to the compiler. The errors remain. What else can I do to ensure that the dynamic object code knows about the macro definition? --Alex ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Macros and loading compiled code
On Wed, Sep 10, 2014 at 12:01:54AM -0700, alex wrote: Hi, Hello Alex, I defined a macro. I compiled it separately and built it into my main program. When my main program calls the load procedure on normal Scheme source files, the procedures in these files use the macro without complaint. However, when it loads the dynamic object code created from compiling those same source files, I get an error about an unbound value in a subexpression of a macro expression. This seems to be because the macro expressions are being evaluated as normal procedure applications. If I insert the macro definition verbatim into each source file before I compile it, the errors do not occur. I'm having a hard time picturing exactly what you're doing, so it would be nice if you could show us some code along with the csi/csc commands you've tried (it doesn't have to be exactly your code; in fact, a simplified dummy example is preferable). One important thing to remember about macros is that they must be available at compile time, not at runtime. For example, you can't use (load ...) to load the code for the macro in a script you're compiling, but it _will_ work if you interpret the same script because every expression will be evaluated one-by-one, whereas the compiler will read and macro-expand all the forms before compiling them, without evaluating them. This can sometimes be a little confusing. Cheers, Peter -- http://www.more-magic.net ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Macros and loading compiled code
On Wed, 10 Sep 2014 00:01:54 -0700 alex a...@cs.utah.edu wrote: Hi, I defined a macro. I compiled it separately and built it into my main program. When my main program calls the load procedure on normal Scheme source files, the procedures in these files use the macro without complaint. However, when it loads the dynamic object code created from compiling those same source files, I get an error about an unbound value in a subexpression of a macro expression. This seems to be because the macro expressions are being evaluated as normal procedure applications. If I insert the macro definition verbatim into each source file before I compile it, the errors do not occur. I tried passing both the source file and the macro definition file as input to the compiler. The errors remain. What else can I do to ensure that the dynamic object code knows about the macro definition? --Alex ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users Try wrapping a module around the code to be compiled separately. Compile it using the '-j module-name' flag then compile the generated scheme file: module-name.import.scm $ cat test.scm (module test * (import chicken scheme) (define-syntax a-macro (syntax-rules () ((_ l r) (+ l r (define (a-function l r) (+ l r)) ) compile like so: $ csc -s test.scm -j test $ csc -s test.import.scm use it like so: $ csi #;1 (load test.so) #;2 (import test) #;3 (a-function 1 2) 3 #;4 (a-macro 1 2) 3 I hope this is useful to you, greetings, Richard ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Macros and loading compiled code
I think you need to define the import library when building both test.scm and test.import.scm. If I do so, your test works: dleslie@marvin:~$ csc -s test.scm -j test dleslie@marvin:~$ csc -s test.import.scm -j test dleslie@marvin:~$ csi CHICKEN (c) 2008-2014, The Chicken Team (c) 2000-2007, Felix L. Winkelmann Version 4.9.0 (rev 3f195ba) linux-unix-gnu-x86-64 [ 64bit manyargs dload ptables ] compiled 2014-06-02 on yves (Linux) #;1 (import test) ; loading ./test.import.so ... ; loading /var/lib//chicken/7/chicken.import.so ... #;2 (a-macro 1 2) 3 #;3 -Dan On Wed, Sep 10, 2014 at 3:26 AM, Richard plui...@freeshell.de wrote: On Wed, 10 Sep 2014 00:01:54 -0700 alex a...@cs.utah.edu wrote: Hi, I defined a macro. I compiled it separately and built it into my main program. When my main program calls the load procedure on normal Scheme source files, the procedures in these files use the macro without complaint. However, when it loads the dynamic object code created from compiling those same source files, I get an error about an unbound value in a subexpression of a macro expression. This seems to be because the macro expressions are being evaluated as normal procedure applications. If I insert the macro definition verbatim into each source file before I compile it, the errors do not occur. I tried passing both the source file and the macro definition file as input to the compiler. The errors remain. What else can I do to ensure that the dynamic object code knows about the macro definition? --Alex ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users Try wrapping a module around the code to be compiled separately. Compile it using the '-j module-name' flag then compile the generated scheme file: module-name.import.scm $ cat test.scm (module test * (import chicken scheme) (define-syntax a-macro (syntax-rules () ((_ l r) (+ l r (define (a-function l r) (+ l r)) ) compile like so: $ csc -s test.scm -j test $ csc -s test.import.scm use it like so: $ csi #;1 (load test.so) #;2 (import test) #;3 (a-function 1 2) 3 #;4 (a-macro 1 2) 3 I hope this is useful to you, greetings, Richard ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Macros and loading compiled code
On Wed, 10 Sep 2014 07:05:43 -0700 Daniel Leslie d...@ironoxide.ca wrote: I think you need to define the import library when building both test.scm and test.import.scm. If I do so, your test works: dleslie@marvin:~$ csc -s test.scm -j test dleslie@marvin:~$ csc -s test.import.scm -j test dleslie@marvin:~$ csi CHICKEN (c) 2008-2014, The Chicken Team (c) 2000-2007, Felix L. Winkelmann Version 4.9.0 (rev 3f195ba) linux-unix-gnu-x86-64 [ 64bit manyargs dload ptables ] compiled 2014-06-02 on yves (Linux) #;1 (import test) ; loading ./test.import.so ... ; loading /var/lib//chicken/7/chicken.import.so ... #;2 (a-macro 1 2) 3 #;3 -Dan On Wed, Sep 10, 2014 at 3:26 AM, Richard plui...@freeshell.de wrote: On Wed, 10 Sep 2014 00:01:54 -0700 alex a...@cs.utah.edu wrote: Hi, I defined a macro. I compiled it separately and built it into my main program. When my main program calls the load procedure on normal Scheme source files, the procedures in these files use the macro without complaint. However, when it loads the dynamic object code created from compiling those same source files, I get an error about an unbound value in a subexpression of a macro expression. This seems to be because the macro expressions are being evaluated as normal procedure applications. If I insert the macro definition verbatim into each source file before I compile it, the errors do not occur. I tried passing both the source file and the macro definition file as input to the compiler. The errors remain. What else can I do to ensure that the dynamic object code knows about the macro definition? --Alex ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users Try wrapping a module around the code to be compiled separately. Compile it using the '-j module-name' flag then compile the generated scheme file: module-name.import.scm $ cat test.scm (module test * (import chicken scheme) (define-syntax a-macro (syntax-rules () ((_ l r) (+ l r (define (a-function l r) (+ l r)) ) compile like so: $ csc -s test.scm -j test $ csc -s test.import.scm use it like so: $ csi #;1 (load test.so) #;2 (import test) #;3 (a-function 1 2) 3 #;4 (a-macro 1 2) 3 I hope this is useful to you, greetings, Richard ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users Hello Dan, From the docs: -j -emit-import-library: MODULE write compile-time module information into separate file It does not seem logical to me why you need that -j test for the second compilation. On my computer it works without. Richard ps. the (load test.so) in my previous message was unneeded. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Macros and loading compiled code
Thank you, everyone, for responding so quickly. You helped me fix my problems. I'm now passing my source file with my macro definition to the compiler with the -extend option. Here was my first problem: I defined a macro. I compiled it separately and built it into my main program. If the macro expressions are compiled, then the compiler must be aware of the macro definition at the same time it is compiling the macro expression, just as Peter said. The compiled macro definition can't be linked after the fact. Here was my second problem: I tried passing both the source file and the macro definition file as input to the compiler. I figured that the compiler might need to know the macro definition when it compiled the macro expression. But the right way to do this is not literally passing two source files, as in csc macro-def.scm main.scm. The right way is to use the -extend option, as in csc -extend macro-def.scm main.scm. --Alex P.S. I also tried making a module of the macro definition for the same purpose, and this too was successful. However, the way I have my compilation process set up, leaving it as ordinary source code is more convenient. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users