-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate.js
119 lines (109 loc) · 3.09 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
function generateJSForExpression(expression, declaredVariables) {
const operatorMap = {
"+": "+",
"-": "-",
"*": "*",
"/": "/",
">": ">",
"<": "<",
">=": ">=",
"<=": "<=",
"=": "===",
};
if (typeof expression === "object") {
if (expression.type === "binary_expression") {
const left = generateJSForExpression(expression.left, declaredVariables);
const right = generateJSForExpression(
expression.right,
declaredVariables
);
const operator = operatorMap[expression.operator];
return `${left} ${operator} ${right}`;
}
if(expression.type === "literal") {
return `\"${expression.value}\"`;
}
} else {
//identifier or number
return expression;
}
}
// handle variable assignment
function varAssignment(statement, declaredVariables) {
const value = generateJSForExpression(statement.value, declaredVariables);
let line;
if (declaredVariables.indexOf(statement.varname) === -1) {
line = `let ${statement.varname} = ${value};`;
declaredVariables.push(statement.varname);
} else {
line = `${statement.varname} = ${value};`;
}
return line;
}
// handle print statement
function printStatement(statement, declaredVariables) {
const arguments = statement.expression.map((arg) => {
if(typeof arg === "object" ) {
return generateJSForExpression(arg, declaredVariables);
}
return arg;
});
return `console.log(${arguments})`;
}
// handle while loop
function whileLoop(statement, declaredVariables) {
const condition = generateJSForExpression(
statement.condition,
declaredVariables
);
const body = generateJS(statement.body, declaredVariables)
.split("\n")
.map((line) => " " + line)
.join("\n");
return `while (${condition}) {\n ${body} \n}`;
}
// handle if statement
function ifStatement(statement, declaredVariables) {
const lines = [];
const condition = generateJSForExpression(
statement.condition,
declaredVariables
);
const body = generateJS(statement.body, declaredVariables);
lines.push(`if (${condition}) {\n ${body} \n}`);
// if it has else if's
if (statement.elifBody) {
for (const elif of statement.elifBody) {
const cond = generateJSForExpression(elif.condition, declaredVariables);
const body = generateJS(elif.body, declaredVariables);
lines.push(`else if (${cond}) {\n ${body} \n}`);
}
}
// if it has a final else
if (statement.else) {
const body = generateJS(statement.else, declaredVariables);
lines.push(`else {\n ${body} \n}`);
}
return lines.join("\n");
}
const visitors = {
var_assignment: varAssignment,
print_statement: printStatement,
while_loop: whileLoop,
if_statement: ifStatement,
};
function generateJS(statements, declaredVariables) {
return statements
.map((stmt) => visitors[stmt.type](stmt, declaredVariables))
.join("\n");
}
function generator(ASTCode) {
try {
const jsCode = generateJS(ASTCode, []);
console.log(jsCode);
return jsCode;
} catch (e) {
console.log(`generator Failed ${e.message}`);
}
}
module.exports = generator;