gerbil
gerbil copied to clipboard
expanding define-syntax
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.)
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.
You can easily get the expanded source however from the module context -- module-context-code
, which you could write to a file.
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.
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?
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.
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?
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.
If you need syntax-case at runtime, you can import <syntax-case>
.
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.