chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Macro use at runtime in compiled code


From: Andrew Hunter
Subject: [Chicken-users] Macro use at runtime in compiled code
Date: Thu, 30 Aug 2007 21:05:10 -0700
User-agent: Mutt/1.5.13 (2006-08-11)

Hello all,

I was hoping I could get an explanation as to some things I'm not
clear about w/r/t using macros at runtime in compiled code.

The (a bit lengthy) background:

I'm writing a program (a toy compiler for a research project, if for
some reason it's important) where I need to do some transformation on
input data in the form of scheme code.  I figured the easiest and best
way to do this, in some cases at least, would be to use macros.  After
looking around, I realized that there's no standard way (I think?) to
do macro-expansion on data at runtime, but there were some things that
would do it.  The best I could find was sc-expand from the psyntax
code base/syntax-case egg.  As an example of the code I tried to
write:

test1.scm:
(use syntax-case)
(use utils)
(port-for-each
 (lambda (x)
  (pp
   (sc-expand
    `(let-syntax (
                  (foo (syntax-rules ()
                         ((foo x ...) (* x ...)
                          )
                         )
                       )
                  )
       ,x
       )
    )
   )
  )
 read)

(The let-syntax splicing is a way I can apply a macro to expand
without making it global syntax, since I didn't want to apply it to my
code--just my input.)

This worked in the interpreter:
% csi -s test.scm
(foo 1 2 3)
(* '1 '2 '3)

But not in the compiler:
csc test.scm && ./test
(foo 1 2 3)
Error: unbound variable: sc-expand

Some inspection of the wiki implied my problem lay in the interaction
of the compiler with macros--while I'm not certain, my unclear
understanding was that the compiler did not support the use of macros
at runtime.  I tried (probably naively, but it seemed by name and
brief description that it could have been relevant) the
-run-time-macros switch, but that didn't help.

I did some poking around on this list and found this:
http://lists.gnu.org/archive/html/chicken-users/2007-04/msg00091.html

Following the instructions there (hand compiling syntax-case and
linking it in, and switching to (declare (uses ...)) instead of (use))
made it work:
csc test.scm syntax-case.o -o test && ./test
(foo 1 2 3)
(* '1 '2 '3)

All well and good.

Sorry for the long exposition, but I want to make sure it's clear what
I'm trying to do here and so on.

Anyway, I'm really unclear as to why the first version (just invoking
the egg) didn't work and linking directly to syntax-case.o did.
Shouldn't the sc-expand procedure be available the same way in either
case?  I know (I think) that all macros in compiled code are fully
expanded and the macro definitions are eliminated, but is sc-expand
itself also removed for some odd?  What exactly is the interaction
between macros and compiled code at run-time?

Anything that would give me some insight as to what exactly's going on
would be greatly appreciated.

Thanks for your time,
Andrew Hunter




reply via email to

[Prev in Thread] Current Thread [Next in Thread]