Directly Generating ASTs

View: New views
2 Messages — Rating Filter:   Alert me  

Directly Generating ASTs

by Matt Fowles :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

All~

I am attempting to use Janino in a project of mine and have come up
with a few questions.

First a bit of background,

My current setup has a hand rolled java dom that I then serialize to
java code which I compile using javac.  I am currently contemplating
two different approaches to migrating to Janino.

1) Rip out my entire DOM and target Janino's DOM directly
2) Teach my DOM how to spit out a Janino DOM.

I haven't decided which I will follow yet and will likely experiment
with both in parallel while deciding.

In the meant time though, I have a few simple questions about the Janino DOM.

Java.FieldDeclaration takes an array of VariableDeclarators.  Why is that?

From looking at the source, I think it has something to do with inline
array initializers, but I am having a little trouble discerning
exactly what.

If I am trying to create the statement:

int foo = 2;

Do I do that as

FieldDeclaration foo = new FieldDeclaration(
        null, //location
        null, //doc
        Mod.PUBLIC,
        new BasicType(null, BasicType.INT),
        new VariableDeclarator[] {
                new VariableDeclarator(
                            null, //location
                            "foo",
                            0, //brackets
                            new Java.Literal(null, 1))
});


Any pointers you can provide would be most appreciated.

Thanks,
Matt

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Directly Generating ASTs

by Arno Unkrig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Matt,

Matt Fowles schrieb:
> All~
>
> I am attempting to use Janino in a project of mine and have come up
> with a few questions.
>
> First a bit of background,
>
> My current setup has a hand rolled java dom that I then serialize to
> java code which I compile using javac.

No ideal... unparsing and then parsing again is neither elegant, nor
reliable or fast.

 > I am currently contemplating
> two different approaches to migrating to Janino.
>
> 1) Rip out my entire DOM and target Janino's DOM directly
> 2) Teach my DOM how to spit out a Janino DOM.
>
> I haven't decided which I will follow yet and will likely experiment
> with both in parallel while deciding.

Difficult to tell from afar. If your DOM/AST is quite different from
Java, then it would make sense to build up that AST first, then
transform it into Java/JANINO and have JANINO compile it. If both ASTs
are more or less the same, then fumbling around with two is a waste. I'd
say.

> In the meant time though, I have a few simple questions about the Janino DOM.
>
> Java.FieldDeclaration takes an array of VariableDeclarators.  Why is that?
>
> From looking at the source, I think it has something to do with inline
> array initializers, but I am having a little trouble discerning
> exactly what.

No, not really. A field declaration can have multiple variable
declarators because Java allows

      public int a = 3, b, c = 7;

See also

 
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#40898

> If I am trying to create the statement:
>
> int foo = 2;
>
> Do I do that as
>
> FieldDeclaration foo = new FieldDeclaration(
>         null, //location
>         null, //doc
>         Mod.PUBLIC,
>         new BasicType(null, BasicType.INT),
>         new VariableDeclarator[] {
>                 new VariableDeclarator(
>                             null, //location
>                             "foo",
>                             0, //brackets
>                             new Java.Literal(null, 1))
> });

Exactly. (Except that your code implements "int foo = 1", not "int foo =
  2" ;-)


CU

Arno

> Any pointers you can provide would be most appreciated.
>
> Thanks,
> Matt
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email