lapots
lapots

Reputation: 13395

internal code representation in java and groovy

What does java use to represent the code internally?

As far as I understand it builds a tree - as it has Compiler Tree API but at the same time it seems not be the same as Abstract Syntax Tree that Groovy builds and that can be modified via AST transformations. (code in Clojure supposed to be represented as is I think)

What is the difference between trees of Java and tree of Groovy? Why does Groovy allows to modify it out of box, while Java doesn't?

Upvotes: 1

Views: 66

Answers (1)

Mene
Mene

Reputation: 3799

The Abstract Syntax Tree of different languages are usually not compatible. This is the case because the AST represents the code that has been written in the respective language. That means you could traverse the tree and format it to code with identical syntax again (i.e. the whitespaces will differ, the rest is the same). However they cannot be compatible, because the languages have different constructs. For example Groovy has closures which is not the case for Java. You can usually find a mapping to different concepts that will be equivalent, but that's not the point of an AST.

The reason AST transformations are a part of Groovy whereas they are not part of Java is the same closures are part of Groovy but not of Java: different design decisions. Java was designed to be simple. Easy to get into and easy to read albeit often verbose. Groovy had a different focus. The syntax is more concise and things like domain specific languages are desired.

If you are more interested in the internals of compilers I recommend the "Dragon Book". It's as far as I know the standard you read in academics (I read it when I was studying).

Upvotes: 1

Related Questions