transpiler
v1.2.0
Published
AST-based transpiler wrapper
Downloads
3,216
Readme
Transpiler
AST-based transpiler wrapper.
Usage
var myTranspiler = require('transpiler').create(<spec>);
var myResult = myTranspiler.transpile(<AST>, <DATA|null>, [<OPTIONS>]);
Here's a transpiler for a very simple AST spec that represents expressions:
var transpilerSpec = {
nodes: {
'EXPR': function (node, transpile) {
return transpile(node.left) + ' ' + node.operator + ' ' + transpile(node.right);
},
'OPERAND': function (node) {
return node.value;
}
}
};
We can create a transpiler that uses this spec like so:
var expressionTranspiler = require('transpiler').create(transpilerSpec);
Now we're ready to perform a transpile. Let's start with a simple example, here's my AST:
var myAST = {
name: 'EXPR',
left: {
name: 'OPERAND',
value: 21
},
operator: '+',
right: {
name: 'OPERAND',
value: 27
}
};
And let's transpile:
console.log(expressionTranspiler.transpile(myAST)); // Logs '21 + 27'
Overrides
It's possible to override or customise the way certain nodes are transpiled.
As an example, let's change it so that operands with the value 27 are wrapped in yes(...)
.
Here's my AST again:
var myAST = {
name: 'EXPR',
left: {
name: 'OPERAND',
value: 21
},
operator: '+',
right: {
name: 'OPERAND',
value: 27
}
};
Now let's transpile, but this time we'll specify a custom way to transpile OPERAND
nodes:
console.log(expressionTranspiler.transpile(myAST, null, {
nodes: {
'OPERAND': function (node, transpile, context, original) {
if (node.value === 27) {
return 'yes(' + original(node) + ')';
}
return original(node);
}
}
})); // Logs '21 + yes(27)'
Keeping up to date
- Follow me on Twitter for updates: https://twitter.com/@asmblah