[gambit-list] Compiling "post-reader" forms?

Marc Feeley feeley at iro.umontreal.ca
Sun Jun 2 12:28:32 EDT 2013


On 2013-06-02, at 1:02 AM, Nathan Sorenson <nathan.d.sorenson at gmail.com> wrote:

> I don't think so because the interpreter and compiler unwrap symbols when checking if they refer to macros.  Do you have an example where this is not happening?
> 
> This demonstrates the problem I'm having in using a macro to attach source info to symbols that name macros:
> 
> ;OK
> (eval `(#(,##source2-marker lambda "foo" 1) () 'hi))
> 
> ;Also OK
> (eval `(,(source-at "foo" 1 2 'lambda) () 'hi))
> 
> ;fails
> (eval '((source-at "foo" 1 2 lambda) () 'hi))
> *** ERROR IN "foo.cljscm"@1.2 -- Macro name can't be used as a variable: lambda
> 
> ;likewise when compiling:
> ;;;; fail.scm ;;;;;;
> (define fail ((source-at "foo" 1 2 lambda) () 'hi))
> 
>  $ gsc -debug fail.scm
> *** ERROR IN "/tmp/foo"@1.2 -- Macro name can't be used as a variable: lambda
> 
> I think the unquote in the two OK examples suggests that some kind of pre-macro-expansion evaluation stage is needed for attaching source information to symbols that refer to macros.

Thanks for clarifying.  Now I see what the issue is.  It is due to Gambit's macro expansion algorithm which does not recursively expand the head of a form before determining if it is a special form.

There are a few solutions I can think of.  But let me ask this first, can't you simply avoid wrapping the head of special forms and macros?  That location information will be (mostly) ignored anyway.  What matters is the location of the complete expression.  In other words the code generated by your compiler for (lambda (x) x) should look like:

   (source-at "foo" 1 1 (lambda (x) (source-at "foo" 1 13 x))

instead of

   ((source-at "foo" 1 2 lambda) (x) (source-at "foo" 1 13 x))

or

   ((source-at "foo" 1 2 lambda) (source-at 1 9 (x)) (source-at "foo" 1 13 x))

If you can't help wrap the heads of special forms, then the source-at macro could recursively traverse the wrapped expression to process all the nested source-at forms.

Marc




More information about the Gambit-list mailing list