-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.js
178 lines (174 loc) · 4.42 KB
/
codegen.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var util = require("util");
const indent = x =>
x
.split("\n")
.map(x => "\t" + x)
.join("\n");
const nodeHandlers = {
BreakStatement(node) {
return "break;";
},
ReturnStatement(node) {
let out = "";
out += "return";
if (node.arguments.length) out += ` ${node.arguments.map(gen).join(", ")}`;
out += ";";
return out;
},
VarargLiteral(node) {
return node.raw;
},
NilLiteral(node) {
return node.raw;
},
BooleanLiteral(node) {
return node.raw;
},
NumericLiteral(node) {
return node.raw;
},
StringLiteral(node) {
return ` ${node.raw} `; // prevent `[[[` in code
},
StringCallExpression(node) {
return `${gen(node.base)}${gen(node.argument)}`;
},
TableCallExpression(node) {
return `${gen(node.base)}${gen(node.arguments)}`;
},
CallExpression(node) {
return `${gen(node.base)}(${node.arguments.map(gen).join(", ")})`;
},
CallStatement(node) {
return `${gen(node.expression)};`;
},
GotoStatement(node) {
return `goto ${gen(node.label)};`;
},
LabelStatement(node) {
return `::${gen(node.label)}::`;
},
TableConstructorExpression(node) {
return `({ ${node.fields.map(gen).join(", ")} })`;
},
TableValue(node) {
return gen(node.value);
},
TableKey(node) {
return `[ ${gen(node.key)} ] = ${gen(node.value)}`;
},
TableKeyString(node) {
return `${gen(node.key)} = ${gen(node.value)}`;
},
FunctionDeclaration(node) {
let out = "";
if (node.isLocal) out += "local ";
out += "function ";
if (node.identifier) out += `${gen(node.identifier)} `;
out += `(${node.parameters.map(gen).join(", ")})\n`;
out += indent(node.body.map(gen).join("\n"));
out += "\nend";
return out;
},
UnaryExpression(node) {
return `(${node.operator} ${gen(node.argument)})`;
},
BinaryExpression(node) {
return `(${gen(node.left)} ${node.operator} ${gen(node.right)})`;
},
LogicalExpression(node) {
return `(${gen(node.left)} ${node.operator} ${gen(node.right)})`;
},
DoStatement(node) {
let out = "";
out += `do\n`;
out += indent(node.body.map(gen).join("\n"));
out += "\nend;";
return out;
},
WhileStatement(node) {
let out = "";
out += `while ${gen(node.condition)} do\n`;
out += indent(node.body.map(gen).join("\n"));
out += "\nend;";
return out;
},
RepeatStatement(node) {
let out = "";
out += "repeat\n";
out += indent(node.body.map(gen).join("\n"));
out += `\nuntil ${gen(node.condition)};`;
return out;
},
ForGenericStatement(node) {
let out = "";
out += `for ${node.variables.map(gen).join(", ")} in ${node.iterators
.map(gen)
.join(", ")} do\n`;
out += indent(node.body.map(gen).join("\n"));
out += "\nend;";
return out;
},
ForNumericStatement(node) {
let out = "";
out += `for ${gen(node.variable)} = ${gen(node.start)}, ${gen(node.end)}`;
if (node.step) out += `, ${gen(node.step)}`;
out += " do\n";
out += indent(node.body.map(gen).join("\n"));
out += "\nend;";
return out;
},
IfStatement(node) {
return `${node.clauses.map(gen).join("\n")}\nend;`;
},
IfClause(node) {
return `if ${gen(node.condition)} then\n${indent(
node.body.map(gen).join("\n")
)}`;
},
ElseifClause(node) {
return `elseif ${gen(node.condition)} then\n${indent(
node.body.map(gen).join("\n")
)}`;
},
ElseClause(node) {
return `else\n${indent(node.body.map(gen).join("\n"))}`;
},
Identifier(node) {
return node.name;
},
MemberExpression(node) {
return `${gen(node.base)}${node.indexer}${gen(node.identifier)}`;
},
IndexExpression(node) {
return `${gen(node.base)}[${gen(node.index)}]`;
},
LocalStatement(node) {
let out = "";
out += `local ${node.variables.map(gen).join(", ")}`;
if (node.init.length) out += ` = ${node.init.map(gen).join(", ")}`;
out += ";";
return out;
},
AssignmentStatement(node) {
return `${node.variables.map(gen).join(", ")} = ${node.init
.map(gen)
.join(", ")};`;
},
Comment(node) {
return node.raw;
},
Chunk(node) {
return [...node.comments.map(gen), ...node.body.map(gen)].join("\n");
}
};
function gen(node) {
const formatter = nodeHandlers[node.type];
/* istanbul ignore if */
if (!formatter) {
throw new Error(`Unhandled: ${util.inspect(node, false, 5, true)}`);
}
let text = formatter(node);
return text;
}
module.exports = gen;