Transformation
The next type of stage for a compiler is transformation. Again, this just takes the AST from the last step and makes changes to it. It can manipulate the AST in the same language or it can translate it into an entirely new language.
Let’s look at how we would transform an AST.
You might notice that our AST has elements within it that look very similar. There are these objects with a type property. Each of these are known as an AST Node. These nodes have defined properties on them that describe one isolated part of the tree.
We can have a node for a NumberLiteral:
{
type: 'NumberLiteral',
value: '2'
}
Or maybe a node for a CallExpression:
{
type: 'CallExpression',
name: 'subtract',
params: [...nested nodes go here...]
}
When transforming the AST we can manipulate nodes by adding/removing/replacing properties, we can add new nodes, remove nodes, or we could leave the existing AST alone and create an entirely new one based on it.
Since we’re targeting a new language, we’re going to focus on creating an entirely new AST that is specific to the target language.
In order to navigate through all of these nodes, we need to be able to traverse through them. This traversal process goes to each node in the AST depth-first.
Let us first see how a typical depth first search is run.
{
type: 'Program',
body: [{
type: 'CallExpression',
name: 'add',
params: [{
type: 'NumberLiteral',
value: '2'
}, {
type: 'CallExpression',
name: 'subtract',
params: [{
type: 'NumberLiteral',
value: '4'
}, {
type: 'NumberLiteral',
value: '2'
}]
}]
}]
}
So for the above AST we would go:
If we were manipulating this AST directly, instead of creating a separate AST, we would likely introduce all sorts of abstractions here. But just visiting each node in the tree is enough.
The reason I use the word “visiting” is because there is this pattern of how to represent operations on elements of an object structure.
The basic idea here is that we are going to create a “visitor” object that has methods that will accept different node types.
var visitor = {
NumberLiteral() {},
CallExpression() {}
};
When we traverse our AST we will call the methods on this visitor whenever we encounter a node of a matching type.
In order to make this useful we will also pass the node and a reference to the parent node.
var visitor = {
NumberLiteral(node, parent) {},
CallExpression(node, parent) {}
};
Let us now view the code, where we will create the new AST.