forked from aasmundeldhuset/Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaScriptCodeGenerator.cs
257 lines (227 loc) · 7.69 KB
/
JavaScriptCodeGenerator.cs
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Compiler
{
public class JavaScriptCodeGenerator
: ICodeGenerator, ISyntaxNodeVisitor
{
private readonly TextWriter _output;
private int _indentationLevel = 0;
public JavaScriptCodeGenerator(TextWriter output)
{
_output = output;
}
public void Generate(ProgramNode program)
{
Visit(program);
}
public void Visit(ProgramNode program)
{
Emit("var stack = [];");
foreach (var function in program.Functions)
{
function.Accept(this);
}
Emit("Main();");
}
public void Visit(FunctionNode function)
{
Emit("function {0}() {{", function.Name.Name);
++_indentationLevel;
Emit("var locals = [];");
Emit("var params = [];");
for (int i = function.Parameters.Count - 1; i >= 0; --i)
{
Emit("params[{0}] = stack.pop();", i);
}
function.Body.Accept(this);
--_indentationLevel;
Emit("}");
}
public void Visit(BlockStatementNode block)
{
EmitComment("Begin block");
foreach (var statement in block.Statements)
{
statement.Accept(this);
}
EmitComment("End block");
}
public void Visit(AssignmentStatementNode assignment)
{
assignment.Expression.Accept(this);
EmitComment("Assign to {0}", assignment.Variable.Name);
if (assignment.SymbolTableEntry.Type == SymbolTableEntryType.Variable)
Emit("locals[{0}] = stack.pop();", assignment.SymbolTableEntry.Index);
else
Emit("params[{0}] = stack.pop();", assignment.SymbolTableEntry.Index);
}
public void Visit(ReturnStatementNode statement)
{
statement.Expression.Accept(this);
Emit("return;");
}
public void Visit(NullStatementNode statement)
{
}
public void Visit(IfStatementNode statement)
{
statement.Condition.Accept(this);
Emit("if (stack.pop()) {");
++_indentationLevel;
statement.ThenBody.Accept(this);
--_indentationLevel;
Emit("}");
if (statement.ElseBody != null)
{
Emit("else {");
++_indentationLevel;
statement.ElseBody.Accept(this);
--_indentationLevel;
Emit("}");
}
}
public void Visit(WhileStatementNode statement)
{
Emit("do {");
++_indentationLevel;
statement.Condition.Accept(this);
Emit("if (!stack.pop()) break;");
statement.Body.Accept(this);
--_indentationLevel;
Emit("} while (1);");
}
public void Visit(PrintStatementNode print)
{
var reversedItems = ((IEnumerable<IPrintItemNode>) print.Items).Reverse();
foreach (var item in reversedItems)
{
item.Accept(this);
}
var parameters = string.Join(" + ", Enumerable.Repeat("stack.pop()", print.Items.Count));
Emit("console.log(\"\" + {0});", parameters);
}
public void Visit(InputStatementNode input)
{
string pattern = "{0}[{1}] = +prompt(\"Please enter an integer:\");";
foreach (var target in input.TargetVariables)
{
if (target.SymbolTableEntry.Type == SymbolTableEntryType.Variable)
Emit(pattern, "locals", target.SymbolTableEntry.Index);
else
Emit(pattern, "params", target.SymbolTableEntry.Index);
}
}
public void Visit(BinaryExpressionNode expression)
{
expression.Right.Accept(this);
expression.Left.Accept(this);
string op;
bool comparison = false;
switch (expression.Operator)
{
case Operator.Equal:
op = "===";
comparison = true;
break;
case Operator.NotEqual:
op = "!==";
comparison = true;
break;
case Operator.LessThan:
op = "<";
comparison = true;
break;
case Operator.LessThanOrEqual:
op = "<=";
comparison = true;
break;
case Operator.GreaterThan:
op = ">";
comparison = true;
break;
case Operator.GreaterThanOrEqual:
op = ">=";
comparison = true;
break;
case Operator.Add:
op = "+";
break;
case Operator.Subtract:
op = "-";
break;
case Operator.Multiply:
op = "*";
break;
case Operator.Divide:
op = "/";
break;
default:
throw new Exception("Unsupported binary operator: " + expression.Operator);
}
if (comparison)
Emit("stack.push(stack.pop() {0} stack.pop() ? 1 : 0);", op);
else if (expression.Operator == Operator.Divide)
Emit("stack.push(Math.floor(stack.pop() {0} stack.pop()));", op);
else
Emit("stack.push(stack.pop() {0} stack.pop());", op);
}
public void Visit(UnaryExpressionNode expression)
{
}
public void Visit(FunctionCallNode call)
{
foreach (var arg in call.Arguments)
{
arg.Accept(this);
}
Emit("{0}();", call.Name.Name);
}
public void Visit(ConstantExpressionNode constant)
{
constant.Value.Accept(this);
}
public void Visit(VariableReferenceNode reference)
{
EmitComment("Read {0}", reference.Variable.Name);
if (reference.SymbolTableEntry.Type == SymbolTableEntryType.Variable)
Emit("stack.push(locals[{0}]);", reference.SymbolTableEntry.Index);
else
Emit("stack.push(params[{0}]);", reference.SymbolTableEntry.Index);
}
public void Visit(DeclarationNode declaration)
{
}
public void Visit(IdentifierNode identifier)
{
}
public void Visit(IntegerNode integer)
{
EmitComment("Constant integer {0}", integer.Value);
Emit("stack.push({0});", integer.Value);
}
public void Visit(StringNode str)
{
Emit("stack.push(\"{0}\");", str.Value);
}
private void Emit(string code)
{
Emit("{0}", code);
}
private void Emit(string pattern, params object[] args)
{
_output.WriteLine(new string(' ', 4 * _indentationLevel) + pattern, args);
}
private void EmitComment(string comment)
{
EmitComment("{0}", comment);
}
private void EmitComment(string comment, params object[] args)
{
Emit("// " + comment, args);
}
}
}