Transpiler
AST-based transpiler wrapper.
Usage
var myTranspiler = ; var myResult = myTranspiler;
Here's a transpiler for a very simple AST spec that represents expressions:
var transpilerSpec = nodes: { return + ' ' + nodeoperator + ' ' + ; } { return nodevalue; } ;
We can create a transpiler that uses this spec like so:
var expressionTranspiler = ;
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; // 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; // Logs '21 + yes(27)'
Keeping up to date
- Follow me on Twitter for updates: https://twitter.com/@asmblah