forked from clarkenheim/tykeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTykeGenerate.js
87 lines (79 loc) · 1.94 KB
/
TykeGenerate.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
const TykeGenerate = function(tree){
this.tree = tree;
};
TykeGenerate.prototype.generate = function(){
var statements = this.tree.map(function(statement){
return this._parseExpr(statement);
}.bind(this));
return statements.join(';\n') + ';';
};
TykeGenerate.prototype._parseExpr = function(expr){
var output = [];
switch(expr.type){
case 'bool':
case 'number':
case 'label':
case 'string':
output.push(expr.symbol);
break;
case 'arg_list':
{
let list = expr.args.map(function(arg){
return this._parseExpr(arg);
}.bind(this)).join(', ');
output.push(list);
}
break;
case 'label_list':
{
let list = expr.labels.map(function(label){
return this._parseExpr(label);
}.bind(this)).join(', ');
output.push(list);
}
break;
case 'bool_compare':
output.push(this._parseExpr(expr.left));
output.push(expr.comparison === 'is' ? '===' : '!==');
output.push(this._parseExpr(expr.right));
break;
case 'assignment':
output.push('var');
output.push(this._parseExpr(expr.label));
output.push('=');
output.push(this._parseExpr(expr.expr));
break;
case 'statement':
output.push(this._parseExpr(expr.statement));
break;
case 'function':
output.push('function');
{
let name = this._parseExpr(expr.label);
let parens = '(' + this._parseExpr(expr.arguments) + ')';
output.push(name + parens);
}
output.push('{\n\t');
{
let statements = expr.statements.map(function(statement){
return this._parseExpr(statement)
}.bind(this));
output.push(statements.join(';\n\t') + ';');
}
output.push('\n}');
break;
case 'function_call':
{
let name = this._parseExpr(expr.label);
let args = '(' + this._parseExpr(expr.args) + ')';
output.push(name + args);
}
break;
default:
throw new Error('Unknown Type : ' + expr.type);
break;
}
return output.join(' ');
};
module.exports = TykeGenerate;