-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
241 lines (202 loc) · 7.26 KB
/
generate.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const fs = require("mz/fs");
async function main() {
const filename = process.argv[2];
if (!filename) {
console.log("Please provide a .ast file.");
return;
}
const astJson = (await fs.readFile(filename)).toString();
const statements = JSON.parse(astJson);
const jsCode = generateJsForStatements(statements);
const outputFilename = filename.replace(".ast", ".js");
await fs.writeFile(outputFilename, jsCode);
console.log(`Wrote ${outputFilename}.`);
}
function generateJsForStatements(statements) {
const lines = [];
// console.log(statements)
for (let statement of statements) {
const line = generateJsForStatement(statement);
lines.push(line);
}
return lines.join("");
}
function generateJsForStatement(node) {
let js = ``;
if (node.type === "method") {
const methodName = node.signature.name.value;
if (methodName === "loop") {
console.log("\nFound loop method\n");
const body = node.body;
body.statements.forEach((statement, index) => {
const statementType = statement.type
switch (statementType) {
case "fun_call":
js += handleFunCall(statement, EL = true);
break;
case "control":
js += handleControl(statement);
break;
case "loop":
js += handleLoop(statement);
break;
}
})
}
// const varName = node.var_name.value;
// const jsExpr = generateJsForStatementOrExpr(node.value);
// const js = `var ${varName} = ${jsExpr};`;
// return js;
}
// else if (node.type === "fun_call") {
// const funName = node.fun_name.value;
// const argList = node.arguments.map((arg) => {
// return generateJsForStatementOrExpr(arg);
// }).join(", ");
// return `${funName}(${argList})`; }
else {
throw new Error(`Unhandled AST node type: ${node.type}`);
}
return js;
}
main().catch(err => console.log(err.stack));
function handleControl(statement) {
const { condition, body } = statement;
const { checks, logicals: logical_ops, control_keys } = condition;
let left = checks.map(check => check.left)
let conditional = checks.map(check => check.conditional.value)
let right = checks.map(check => check.right)
let logicals = logical_ops.map(logical => logical.value)
let key = control_keys.map(key => key.value).join(" ")
let str = `${key} (`;
str += handleConditional(left, conditional, right, logicals);
const { statements } = body;
str += handleStatements(statements);
return str;
}
function handleConditional(left, conditional, right, logicals = []) {
/**
*
* Ingest @left @right @conditional and @logicals arrays.
*
* Produces output string @str as
*
* @expression @conditional @value (@logical ...) {
*
* e.g., digitalRead(4) == 0 && digitalRead(5) >= 0 && digitalRead(7) < 0) {
*
*/
let str = ``;
for (let i = 0, n = left.length; i < n; i++) {
str += `${left[i].type === "fun_call__" ? handleFunCall(left[i]) : (left[i].value ? left[i].value : left[i])}`;
str += ` ${conditional[i]} `;
str += `${right[i].type === "fun_call__" ? handleFunCall(right[i]) : (right[i].value ? right[i].value : right[i])}`;
if (logicals[i]) {
str += ` ${logicals[i]} `;
} else if (i === n - 1) {
str += `) {\n`;
}
}
return str;
}
function handleFunCall(statement, EL = false) {
/**
*
* Produces output string @str as
*
* functionName(arg1, arg2, ...)
*
* OR
*
* functionName(arg1, arg2, ...);\n // if @EL === truthy
*
*/
const { fun_name, arguments } = statement;
let functionName = fun_name.value;
let args = arguments.arg_values.map(arg => arg.value);
let str = `${functionName}(`;
if (args.length === 0) (EL === true ? str += `);\n` : str += str += `)`);
else args.forEach((arg, index) => {
if (index === args.length - 1) (EL === true ? str += `${arg});\n` : str += `${arg})`)
else str += `${arg}, `
})
return str;
}
function handleLoop(statement) {
const { loop_params, body } = statement;
const { loop_key, counter_var, stop_condition, incrementor } = loop_params;
const { statements } = body;
let str = `${loop_key.value} (`;
if (counter_var) { // Handle for loop
const counterName = counter_var.var_name.value;
const initialCounterValue = counter_var.value.value
let left = [stop_condition.left.primary.value];
let right = [stop_condition.right.value];
let conditional = [stop_condition.conditional.value];
str += `let ${counterName} = ${initialCounterValue}; `;
str += handleConditional(left, conditional, right);
str = str.substring(0, str.length - 4);
str += `; ${incrementor.identifier.value}${incrementor.op.value}) {\n`
str += handleStatements(statements);
} else { // Handle while loop
let left = statement.loop_params.check.left;
let right = statement.loop_params.check.right;
let conditional = statement.loop_params.check.conditional;
str += handleConditional([left], [conditional.value], [right], [])
str += handleStatements(statements);
}
return str;
}
function handleStatements(statements, indent = true) {
/**
*
* Produces output string @str as
*
* method1(arg1, arg2, ...); // i.e., fun_call
* let var; // i.e., var_declare
* let var = x; // i.e., var_assign
* var2 = y; // i.e., var_assign
* ...
* } <---- NB!
*
*/
let str = ``;
statements.forEach((statement, index) => {
if (statement.type === "fun_call") {
indent ? str += " " + handleFunCall(statement) : str += handleFunCall(statement);
if (index === statements.length - 1)
str += ";\n}\n";
else
str += `;\n`;
} else if (statement.type === "var_assign") {
str += " " + handleVarAssign(statement);
if (index === statements.length - 1)
str += ";\n}\n";
} else if (statement.type === "var_declare") {
str += " " + handleVarDeclare(statement);
if (index === statements.length - 1)
str += ";\n}\n";
}
});
return str;
}
function handleVarAssign(statement) {
const { var_types, var_name, value } = statement;
let str = ``;
if (var_types) {
str += `let ${var_name.value} = ${value.value};\n`
} else {
str += `${var_name.value} = ${value.value};\n`
}
return str;
}
function handleVarDeclare(statement) {
const { var_types, var_name, } = statement
let str = ``;
if (var_types) {
str += `let ${var_name.value};\n`
} else {
str += `${var_name.value} = ${value.value};\n`
}
return str;
}