google / j2cl

Java to Closure JavaScript transpiler
Apache License 2.0
1.24k stars 146 forks source link

Question on "repurposing" the transpiler #110

Closed lwhite1 closed 4 years ago

lwhite1 commented 4 years ago

Apologies in advance as this question is off-topic, but I made it multiple choice so it shouldn't take too long. :)

I'm implementing a simple language for educational purpose. I'm writing the lexer and parser in java, and I'd like to transpile to Javascript.

What I'm considering is trying to use J2CL as my transpiler. My thought was to walk my AST, translating to the J2CL AST. Then ask J2CL to compile. So,

A. That's nuts. the compiler is tightly integrated and has to start from Java source. B. That's not entirely nuts, but it would be a heck-of-a lotta work. C. Don't bother unless you're an absolute master in both languages, and you're not. D. Sure, why not.

Thanks very much for any help/guidance.

gkdn commented 4 years ago

No that's not crazy :)

We already have two frontends; one uses JDT and the other one uses javac: https://github.com/google/j2cl/tree/master/transpiler/java/com/google/j2cl/frontend/

You can see the amount of work required which was around 3.5K / 4K SLOC.

Only thing is; don't expect much support for your use case or stability of the APIs :)

rluble commented 4 years ago

On Wed, Sep 2, 2020 at 8:29 AM Larry White notifications@github.com wrote:

Apologies in advance as this question is off-topic, but I made it multiple choice so it shouldn't take too long. :)

I'm implementing a simple language for educational purpose. I'm writing the lexer and parser in java, and I'd like to transpile to Javascript.

What I'm considering is trying to use J2CL as my transpiler. My thought was to walk my AST, translating to the J2CL AST. Then ask J2CL to compile. So,

Depending on the complexity of your simple language it might be easier to just emit JavaScript directly.

So if it is a simple educational language that does not have classes, i.e. a simple imperative language, then in my opinion, once you have the parser creating the AST of your choice it would be trivial to emit JavaScript. If the input language is much more complex and has many of the characteristics of Java (i.e. classes, maybe interfaces, ....) then it makes sense to write a new frontend to J2CL and take advantage of the backend. Keep in mind that the AST that J2CL uses as its internal representation of programs matches very closely the Java constructs in Java (plus some additions that are close to JavaScript).

A. That's nuts. the compiler is tightly integrated and has to start from Java source. B. That's not entirely nuts, but it would be a heck-of-a lotta work. C. Don't bother unless you're an absolute master in both languages, and you're not. D. Sure, why not.

If I was implementing a simple compiler for a simple language (imperative, no subtyping, etc) I would just do the whole pipeline from scratch, since I think it is less work than trying to fit into the J2CL design. But if I were implementing a language that has class hierarchies (and features that are similar to Java, e.g. if I was considering C# or Kotlin) and I would care to produce output that is modular and optimizable by the Closure compiler, then I would definitely write a frontend and reuse J2CL.

All the best, Roberto.