Silly Freak
Silly Freak

Reputation: 4231

Efficient ways to reuse a Java compiler's backend for a new JVM language

I'm writing a language targeted at the JVM, and I'm currently putting the compiler together. It becomes apparent to me that, logically, my new language has many of the same needs as Java when it comes to creating the bytecode. Just to give a few examples:

There's probably much more, and I somehow don't want to reinvent these wheels (making a new language, of course I'm reinventing some wheels already...), so I wondered what would be the best ways to reuse (parts of) an existing Java compiler, passing it the pieces of the AST it needs to figure out the above.

I do already have lexer and parser (ANTLR) in place, so I'm really looking for advice on what compilers are out there that would make it relatively easy for me to work with them (for example, I have looked a little into ECJ, but if someone told me that it's not capable of what I want or another compiler would be easier to use that would be great).

to sum it up with a definite question: Which Java compilers out there have an easily accessible backend that is suiteable to be used with a frontend for a non-Java JVM language?

Upvotes: 4

Views: 507

Answers (2)

ewernli
ewernli

Reputation: 38615

Here are two extensible compiler backend that I heard of while reading academic papers:

For domain-specific language engineering, I would suggest

  • Xtext and for instance Xtend, a variation of Java

These projects seem mature. I never had a close look to them, but I really would like to ;)

Upvotes: 3

Paul Stansifer
Paul Stansifer

Reputation: 1269

At the risk of being a little obtuse, my answer is Scala.

Scala is a JVM-based language which has, as of the latest release, a macro system. Macros (when they're more powerful than the ones associated with C) are a technique for building domain-specific languages on top of existing languages, without having to start from scratch. A macro system lets you write code in the existing language, enhanced by new constructs, and will compile the new constructs down into the base language.

If you want to build straight off of Java, it might be worth considering the Java Syntax Extender, but JSE is not extensively used, and may be pretty rough. (There may, in fact, be more mature Java macro systems I haven't heard of.)

Upvotes: 2

Related Questions