gerbil icon indicating copy to clipboard operation
gerbil copied to clipboard

expanding define-syntax

Open svenha opened this issue 5 years ago • 9 comments

Is there a way in Gerbil to expand a source file to another file so that all macro uses (define-syntax would be enough for the moment) are expanded? (Like alexpander does, but without the artifacts that alexpander introduces.)

svenha avatar Dec 25 '18 17:12 svenha

You still want it in Gerbil source? There is no easy way to save the expanded output of the compilation process, best we have is expanding to Gambit source.

vyzo avatar Dec 25 '18 20:12 vyzo

You can easily get the expanded source however from the module context -- module-context-code, which you could write to a file.

vyzo avatar Dec 25 '18 20:12 vyzo

So to recap: import your source file directoy with import-module and then use module-context-code in the resulting module context object to get the code, which you can then write to a file.

vyzo avatar Dec 25 '18 20:12 vyzo

You still want it in Gerbil source?

Little more complicated. The input and the output should be some Scheme dialect as defined by implementations like Chicken or Bigloo.

import-module

Thanks! This works if the source is Gerbil-compatible. For other Scheme dialects, I get such errors for identifiers not defined in Gerbil:

Syntax Error: Reference to unbound identifier

I cannot define these identifiers because the input file to be macro-expanded is for other Scheme dialects. Can these errors be relaxed to warnings? Or any other ideas?

svenha avatar Dec 26 '18 09:12 svenha

You need to somehow define these identifiers, perhaps with an extern. You can have a custom prelude for each dialect, which provides the necessary symbols.

vyzo avatar Dec 26 '18 09:12 vyzo

So to recap: import your source file directly with import-module and then use module-context-code in the resulting module context object to get the code, which you can then write to a file.

What does module-context-code return? https://cons.io/reference/core-expander.html#module-context-code is empty. How do I get the s-expressions?

svenha avatar Dec 30 '18 15:12 svenha

Yeah, the documentation is not there for the expander yet. It returns a syntax object containing the source code that was expanded. You can process the syntax object with the regular expander facilities (syntax-case, stx-* procedures, etc). You can import the expander runtime (<expander-runtime>) or the expander itself (:gerbil/expander) to get them in scope. If you want to turn it into a datum, say for pretty printing, then (syntax->datum code) will do.

vyzo avatar Dec 30 '18 15:12 vyzo

If you need syntax-case at runtime, you can import <syntax-case>.

vyzo avatar Dec 30 '18 15:12 vyzo

Thanks, that worked for me. But I must say that the source is already transformed quite a lot. OK, now I know how it looks like :-) and it definitely helps in certain situations.

svenha avatar Dec 30 '18 18:12 svenha