Access the AST
Daniele Benegiamo
danielebenegiamo@fastwebnet.it
Fri Jul 9 10:18:00 GMT 2010
> On 07/08/2010 11:02 AM, Daniele Benegiamo wrote:
>> I would like to know if it is possible, using some Kawa class, to access
>> (directly or indirectly, e.g. rebuilding it in some way) the AST of a
>> source Scheme file.
>
> There are 3-4 things you could mean by "the AST":
>
> (1) The raw reader-level S-expressions: lists, symbols, etc.
> These do include line-number positions, though not quite as detailed
> as one might like.
>
>
> (2) The "syntax object" level, which is basically S-expressions
> annotated with lexical context. This is the input to (hygienic)
> macro expansion.
>
>
> (3) The output of macro expansion, which is a dynamic combination of (2)
> (for further processing) and (4). (A macro may return an Expression
> directly.)
>
>
> (4) The Expression tree, which is a non-Scheme-specific data structure
> with macros expanded, identifiers bound to their definitions, etc.
> This is the input to further analysis and then code generation.
My comment below, but I think point 2 or 4 is what I need (e.g. I don't
want the output of macros).
>> I'm trying using Translator.formStack, and checking for implementation
>> of Iterable interface to distinguish expressions and terminals; but it
>> seems more an hack than a viable solution.
>
> I don't understand this - Kawa's compiler doesn't distinguish Iterable -
> I can't even find the name Iterable in the Kawa source code.
I mean the standard java.lang.Iterable interface. It /seems/ that all
expressions implement (usually indirectly) that interface, while
terminal symbols don't. But I've done only few very simple tests.
> I think just using form (4) makes most sense, though I'm not quite sure
> what you're trying to do. Form (4) does map each identifier to its definition,
> assuming one is lexically available, and this seems useful in an IDE/editor.
> However, it does have some limitations - for example it doesn't track
> macro expansions, so it may be tricky to figure out where something came from;
> we could perhaps add some annotation mechanism for this.
I should don't need macro expanded, I try to explain better my
application because I don't have to create a generic Scheme GUI, but
only a GUI for a subset of it (for a DSL): I have to "encode" trading
agreements, this can be done using only a few expression types (mainly
some conditionals, "set!", and some custom functions). Who will usually
edit those agreements of course don't know how/why a computer works, so
we have to implement a GUI to allow them to read/edit those encoded
agreements without showing them the Scheme source code. A short
hypothetic agreement could be encoded as:
(output-var vat percentage EUR "VAT to apply")
(output-var profit value EUR "Absolute profit")
(case destination-location-country
((IT FR ES) .2)
((US) .175))
(set! profit 100)
(if (is-festival-day delivery-date)
(set! profit (* profit 1.1))
Each of these expressions have to be mapped to some GUI control. E.g.
"output-var" could be a Scheme macro or Java function, but the user have
to see a simple block that, if clicked, open a form to insert the
currency, the variable type and a comment. So I've to parse the same
expression that appears into the source code, without expansions: if the
expression maps to some known name (e.g. "output-var", "case", "set!",
etc.) I know what GUI controls to use, otherwise a nice black-box will
be used. Is this the form (4) that you have described? Or the (2)?
Daniele
More information about the Kawa
mailing list