-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeGenerator.cs
390 lines (354 loc) · 11.2 KB
/
CodeGenerator.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MiniC {
class CodeGenerator : MiniCBaseVisitor<int> {
public CodeASTElement TranslatedFile { get; set; }
private readonly Stack<ValueTuple<CodeASTElement, int>> _stack = new Stack<ValueTuple<CodeASTElement, int>>();
public void PrintStructure(StreamWriter file) { }
public void EmitToStdout() {
Console.Write(TranslatedFile.AssemblyCode().ToString());
}
public void EmitToFile(StreamWriter file) { }
public override int VisitCompileUnit(CCompileUnit node) {
TranslatedFile = new GFile();
TranslatedFile.AddCode("#include <stdio.h>", GFile.Directives);
TranslatedFile.AddCode("#include <stdlib.h>", GFile.Directives);
var mainFunc = new GMainFuncDef();
TranslatedFile.AddChild(mainFunc, GFile.FuncDefs);
_stack.Push((mainFunc, GMainFuncDef.Body));
VisitContext(node, CCompileUnit.Stmts);
_stack.Pop();
_stack.Push((TranslatedFile, GFile.FuncDefs));
VisitContext(node, CCompileUnit.FuncDefs);
_stack.Pop();
return 0;
}
public override int VisitFuncDef(CFuncDef node) {
var nn = new GFuncDef();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
var funcName = (node.GetChild(CFuncDef.FName, 0) as CID).Label;
nn.AddCode(funcName, CFuncDef.FName);
var decl = $"float {funcName}(";
var n = node.ChildrenNumber(CFuncDef.Params);
for (int i = 0; i < n; ++i) {
var paramName = (node.GetChild(CFuncDef.Params, i) as CID).Label;
parent.DeclareVar(paramName, "float");
nn.AddCode(paramName, GFuncDef.Params);
decl += $"float {paramName}";
if (i != n - 1)
decl += ", ";
}
decl += ")";
parent.AddCode(decl, GFile.ForwardDecls);
_stack.Push((nn, GFuncDef.Body));
VisitContext(node, CFuncDef.Body);
_stack.Pop();
return 0;
}
public override int VisitAsgn(CAsgn node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
var cid = node.GetChild(CAsgn.Left, 0) as CID;
var varname = cid.Label;
string type = "na";
switch (cid.Type) {
case MiniCLitType.Int:
type = "int";
break;
case MiniCLitType.Float:
type = "float";
break;
}
parent.DeclareVar(varname, type);
parent.AddChild(nn, context);
nn.AddCode($"{varname} = ");
_stack.Push((nn, 0));
VisitContext(node, CAsgn.Right);
_stack.Pop();
return 0;
}
public override int VisitBlock(CBlock node) {
var nn = new GBlock();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CBlock.Body);
_stack.Pop();
return 0;
}
public override int VisitIf(CIf node) {
var nn = new GIf();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, GIf.Cond));
VisitContext(node, CIf.Cond);
_stack.Pop();
_stack.Push((nn, GIf.BodyTrue));
VisitContext(node, CIf.BodyTrue);
_stack.Pop();
_stack.Push((nn, GIf.BodyFalse));
VisitContext(node, CIf.BodyFalse);
_stack.Pop();
return 0;
}
public override int VisitRet(CRet node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
nn.AddCode("return ");
_stack.Push((nn, 0));
VisitContext(node, CRet.Expr);
_stack.Pop();
return 0;
}
public override int VisitWhile(CWhile node) {
var nn = new GWhile();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, GWhile.Cond));
VisitContext(node, CWhile.Cond);
_stack.Pop();
_stack.Push((nn, GWhile.Body));
VisitContext(node, CWhile.Body);
_stack.Pop();
return 0;
}
public override int VisitNot(CNot node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
nn.AddCode("! ");
_stack.Push((nn, 0));
VisitContext(node, CNot.Operant);
_stack.Pop();
return 0;
}
public override int VisitPlus(CPlus node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
nn.AddCode("+ ");
_stack.Push((nn, 0));
VisitContext(node, CPlus.Operant);
_stack.Pop();
return 0;
}
public override int VisitMinus(CMinus node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
nn.AddCode("- ");
_stack.Push((nn, 0));
VisitContext(node, CMinus.Operant);
_stack.Pop();
return 0;
}
public override int VisitMult(CMult node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CMult.Left);
_stack.Pop();
nn.AddCode(" * ");
_stack.Push((nn, 0));
VisitContext(node, CMult.Right);
_stack.Pop();
return 0;
}
public override int VisitDiv(CDiv node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CDiv.Left);
_stack.Pop();
nn.AddCode(" / ");
_stack.Push((nn, 0));
VisitContext(node, CDiv.Right);
_stack.Pop();
return 0;
}
public override int VisitAdd(CAdd node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CAdd.Left);
_stack.Pop();
nn.AddCode(" + ");
_stack.Push((nn, 0));
VisitContext(node, CAdd.Right);
_stack.Pop();
return 0;
}
public override int VisitSub(CSub node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CSub.Left);
_stack.Pop();
nn.AddCode(" - ");
_stack.Push((nn, 0));
VisitContext(node, CSub.Right);
_stack.Pop();
return 0;
}
public override int VisitAnd(CAnd node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CAnd.Left);
_stack.Pop();
nn.AddCode(" && ");
_stack.Push((nn, 0));
VisitContext(node, CAnd.Right);
_stack.Pop();
return 0;
}
public override int VisitOr(COr node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, COr.Left);
_stack.Pop();
nn.AddCode(" || ");
_stack.Push((nn, 0));
VisitContext(node, COr.Right);
_stack.Pop();
return 0;
}
public override int VisitGt(CGt node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CGt.Left);
_stack.Pop();
nn.AddCode(" > ");
_stack.Push((nn, 0));
VisitContext(node, CGt.Right);
_stack.Pop();
return 0;
}
public override int VisitGte(CGte node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CGte.Left);
_stack.Pop();
nn.AddCode(" >= ");
_stack.Push((nn, 0));
VisitContext(node, CGte.Right);
_stack.Pop();
return 0;
}
public override int VisitLt(CLt node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CLt.Left);
_stack.Pop();
nn.AddCode(" < ");
_stack.Push((nn, 0));
VisitContext(node, CLt.Right);
_stack.Pop();
return 0;
}
public override int VisitLte(CLte node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CLte.Left);
_stack.Pop();
nn.AddCode(" <= ");
_stack.Push((nn, 0));
VisitContext(node, CLte.Right);
_stack.Pop();
return 0;
}
public override int VisitEq(CEq node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CEq.Left);
_stack.Pop();
nn.AddCode(" == ");
_stack.Push((nn, 0));
VisitContext(node, CEq.Right);
_stack.Pop();
return 0;
}
public override int VisitNeq(CNeq node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CNeq.Left);
_stack.Pop();
nn.AddCode(" != ");
_stack.Push((nn, 0));
VisitContext(node, CNeq.Right);
_stack.Pop();
return 0;
}
public override int VisitFuncCall(CFuncCall node) {
var nn = new GCodeRepo();
var (parent, context) = _stack.Peek();
parent.AddChild(nn, context);
_stack.Push((nn, 0));
VisitContext(node, CFuncCall.FName);
_stack.Pop();
nn.AddCode("(");
_stack.Push((nn, 0));
for (int i = 0; i < node.ChildrenNumber(CFuncCall.Args); ++i) {
//ASTVisitableElement child in node.GetChildren(CFuncCall.Args)) {
Visit(node.GetChild(CFuncCall.Args, i) as ASTVisitableElement);
if (i < node.ChildrenNumber(CFuncCall.Args) - 1)
nn.AddCode(", ");
}
_stack.Pop();
nn.AddCode(")");
return 0;
}
public override int VisitTerminal(MiniCASTElement node) {
var (parent, context) = _stack.Peek();
switch (node.Nt) {
case MiniCNodeType.ID:
var cid = node as CID;
var varname = cid.Label;
var parentType = (node.Parents.Last() as MiniCASTElement).Nt;
if (parentType == MiniCNodeType.FuncCall) {
if (context != 0) {
parent.DeclareVar(varname, "float");
}
}
else if (parentType != MiniCNodeType.FuncDef) {
parent.DeclareVar(varname, "float");
}
parent.AddCode(varname, context);
break;
case MiniCNodeType.BREAK:
parent.AddCode("break", context);
break;
case MiniCNodeType.INT:
parent.AddCode((node as CINT).Value, context);
break;
case MiniCNodeType.FLOAT:
parent.AddCode((node as CFLOAT).Value, context);
break;
}
return 0;
}
}
}