-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.js
27 lines (26 loc) · 1.01 KB
/
Generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Generator {
generate(node) {
switch (node.constructor.name) {
case 'DefNode':
const defFunctionName = node.name;
const defArgNames = node.argNames.join(', ');
const defReturnValue = this.generate(node.body);
return `function ${defFunctionName}(${defArgNames}) { return ${defReturnValue} };`;
case 'FunctionCallNode':
const callFunctionName = node.name;
const callArgNames = node.argExpressions
.map(expression => {
return this.generate(expression);
})
.join(', ');
return `${callFunctionName}(${callArgNames})`;
case 'IntegerNode':
return node.value;
case 'VariableReferenceNode':
return node.value;
default:
throw 'Unexpected node type: ' + node.constructor.name;
}
}
}
module.exports = Generator;