Manipulating AST node for code generation #800
-
Hey guys, I'm trying to implement a code generator for my DSL. Automatons are the fist-class citizens in this language, and they can be nested, for example, you can instantiate one automaton in another one, so I need to unfold all automatons into only one automaton, and then transform it into some target language. (This is very like unfolding every inline functions to get a big function, but it is a bit more complicated). Therefore, I need to manipulate AST nodes, creating new ones and relate them together. For example, in some cases, I need to take two expressions from two statements, and create a disjunction of them. Then I need to write something like const disjunct: BinaryExpression = {
$container: container,
$type: 'BinaryExpression'
operator: '||',
left: expr1,
right: expr2
} There are two things bothering me:
Given enough time, I can find workarounds for these problems, but the progress is very slow because I'm not familiar with TypeScript (nor JavaScript). Do you have any advice on dealing with these inconveniences? Both suggestions on how to do code generation and tips on useful TypeScript features are appreciated. By the way, I have a prototype of the generator written in Java, it looks like tnew.setGuard(new BinaryOperatorTerm()
.setParent(tnew)
.setOpr(EnumBinaryOperator.LAND)
.setLeft(
new SingleOperatorTerm()
.setOpr(EnumSingleOperator.LNOT)
.setTerm(cond.copy(parent))
)
.setRight(tnew.getGuard())
); which is more intuitive to me (although I'm not familiar with Java either). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hey @luan-xiaokun, for cases such as these, we do have a |
Beta Was this translation helpful? Give feedback.
Hey @luan-xiaokun,
for cases such as these, we do have a
linkContentToContainer
function inlangium
, which automatically sets the$container
property on all its direct descendants (not it's transitive descendants!). You can ignore all the$container/$containerProperty/$containerIndex
properties during construction and the mentioned function will take care of it.