-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeASTElement.cs
388 lines (349 loc) · 13.1 KB
/
CodeASTElement.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
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MiniC {
public enum CodeNodeType {
Na,
File,
FuncDef,
MainFuncDef,
While,
If,
Block,
CodeRepo
}
public abstract class CodeASTElement : ASTVisitableElement {
public CodeNodeType Nt { get; }
public static int NestingLevel { get; set; } = 0;
public bool IsComplex() {
return Nt == CodeNodeType.File ||
Nt == CodeNodeType.FuncDef ||
Nt == CodeNodeType.MainFuncDef ||
Nt == CodeNodeType.Block ||
Nt == CodeNodeType.If ||
Nt == CodeNodeType.While;
}
public abstract StringBuilder AssemblyCode();
public virtual void DeclareVar(string varname, string type) {
(Parents.Last() as CodeASTElement)?.DeclareVar(varname, type);
}
public virtual bool HasVar(string varname) {
return false;
}
public void AddCode(string code, int context) {
AddChild(new GCodeRepo(code), context);
}
protected CodeASTElement(int context, CodeNodeType type) : base(context) {
Nt = type;
}
public void EnterScope() { ++NestingLevel; }
public void LeaveScope() { --NestingLevel; }
public StringBuilder Nest(StringBuilder s) {
return new StringBuilder(new string('\t', NestingLevel) + s);
}
public string Nest(string s) {
return new string('\t', NestingLevel) + s;
}
}
public class GFile : CodeASTElement {
public HashSet<string> GlobalVarSymbolTable { get; set; } = new HashSet<string>();
// private HashSet<string> _functionsSymbolTable { get; set; } = new HashSet<string>();
public const int Directives = 0, ForwardDecls = 1, Globals = 2, FuncDefs = 3;
public override string[] ContextNames { get; } = { "Directives", "ForwardDecls", "Globals", "FuncDefs" };
public GFile() : base(context: 4, CodeNodeType.File) { }
public override void DeclareVar(string varname, string type) {
if (!GlobalVarSymbolTable.Contains(varname))
AddCode(type + " " + varname + " = 0", Globals);
GlobalVarSymbolTable.Add(varname);
}
public override bool HasVar(string varname) {
return GlobalVarSymbolTable.Contains(varname);
}
public override StringBuilder AssemblyCode() {
var result = new StringBuilder();
foreach (CodeASTElement child in GetChildren(Directives)) {
result.Append(child.AssemblyCode() + "\n");
}
if (ChildrenNumber(Directives) != 0)
result.Append('\n');
foreach (CodeASTElement child in GetChildren(ForwardDecls)) {
result.Append(child.AssemblyCode() + ";\n");
}
if (ChildrenNumber(ForwardDecls) != 0)
result.Append('\n');
foreach (CodeASTElement child in GetChildren(Globals)) {
result.Append(child.AssemblyCode() + ";\n");
}
if (ChildrenNumber(Globals) != 0)
result.Append('\n');
foreach (CodeASTElement child in GetChildren(FuncDefs)) {
result.Append(child.AssemblyCode());
result.Append('\n');
}
return result;
}
public override T Accept<T>(ASTBaseVisitor<T> visitor) {
if (visitor is CodeBaseVisitor<T> codeBaseVisitor) {
return codeBaseVisitor.VisitFile(this);
}
return default;
}
public override string GenerateNodeName() {
return $"File{base.GenerateNodeName()}";
}
}
public class GFuncDef : CodeASTElement {
private HashSet<string> _varSymbolTable = new HashSet<string>();
public const int FName = 0, Params = 1, Body = 2, Decls = 3;
public override string[] ContextNames { get; } = { "Name", "Params", "Body", "Decls"};
public GFuncDef() : base(context: 4, CodeNodeType.FuncDef) { }
public override void DeclareVar(string varname, string type) {
bool isUpstairs = false;
CodeASTElement p = this;
while (p.Parents.Count() != 0 && (p.Parents.Last() as CodeASTElement).IsComplex()) {
if ((p.Parents.Last() as CodeASTElement).HasVar(varname)) {
isUpstairs = true;
break;
}
p = p.Parents.Last() as CodeASTElement;
}
if (!isUpstairs) {
if (!_varSymbolTable.Contains(varname))
AddCode(type + " " + varname + " = 0", Decls);
_varSymbolTable.Add(varname);
}
}
public override bool HasVar(string varname) {
return _varSymbolTable.Contains(varname);
}
public override StringBuilder AssemblyCode() {
var result = new StringBuilder();
result.Append("float ");
result.Append((GetChild(FName, 0) as GCodeRepo).AssemblyCode());
result.Append("(");
var parameters = GetChildren(Params);
var argCount = parameters.Count();
for (int i = 0; i < argCount; ++i) {
result.Append($"float {(parameters.ElementAt(i) as GCodeRepo).AssemblyCode()}");
if (i != argCount - 1) {
result.Append(", ");
}
}
result.Append(") {\n");
EnterScope();
foreach (CodeASTElement child in GetChildren(Decls)) {
result.Append(Nest(child.AssemblyCode()) + ";\n");
}
foreach (CodeASTElement child in GetChildren(Body)) {
result.Append(Nest(child.AssemblyCode()) + ";\n");
}
LeaveScope();
result.Append("}\n");
return result;
}
public override T Accept<T>(ASTBaseVisitor<T> visitor) {
if (visitor is CodeBaseVisitor<T> codeBaseVisitor) {
return codeBaseVisitor.VisitFuncDef(this);
}
return default;
}
public override string GenerateNodeName() {
return $"FuncDef{base.GenerateNodeName()}";
}
}
public class GMainFuncDef : CodeASTElement {
public const int Body = 0, Decls = 1;
public override string[] ContextNames { get; } = { "Body", "Decls" };
public GMainFuncDef() : base(context: 2, CodeNodeType.MainFuncDef) { }
public override void DeclareVar(string varname, string type) {
var p = (Parents.Last() as GFile);
if (!p.GlobalVarSymbolTable.Contains(varname))
p.AddCode(type + " " + varname + " = 0", GFile.Globals);
p.GlobalVarSymbolTable.Add(varname);
}
public override bool HasVar(string varname) {
return (Parents.Last() as GFile).GlobalVarSymbolTable.Contains(varname);
}
public override StringBuilder AssemblyCode() {
var result = new StringBuilder();
result.Append("int main(void) {\n");
EnterScope();
foreach (CodeASTElement child in GetChildren(Decls)) {
result.Append(Nest(child.AssemblyCode()) + ";\n");
}
foreach (CodeASTElement child in GetChildren(Body)) {
result.Append(Nest(child.AssemblyCode()) + ";\n");
}
LeaveScope();
result.Append("}\n");
return result;
}
public override T Accept<T>(ASTBaseVisitor<T> visitor) {
if (visitor is CodeBaseVisitor<T> CodeBaseVisitor) {
return CodeBaseVisitor.VisitMainFuncDef(this);
}
return default;
}
public override string GenerateNodeName() {
return $"MainFuncDef{base.GenerateNodeName()}";
}
}
public class GWhile : CodeASTElement {
private HashSet<string> _varSymbolTable = new HashSet<string>();
public const int Cond = 0, Body = 1, Decls = 2;
public override string[] ContextNames { get; } = { "Cond", "Body", "Decls" };
public GWhile() : base(context: 3, CodeNodeType.While) { }
public override StringBuilder AssemblyCode() {
var result = new StringBuilder();
result.Append("while (");
result.Append((GetChild(Cond, 0) as CodeASTElement).AssemblyCode());
result.Append(") {\n");
EnterScope();
foreach (CodeASTElement child in GetChildren(Decls)) {
result.Append(Nest(child.AssemblyCode()) + ";\n");
}
foreach (CodeASTElement child in GetChildren(Body)) {
result.Append(Nest(child.AssemblyCode()) + ";\n");
}
LeaveScope();
result.Append(Nest("}"));
return result;
}
public override T Accept<T>(ASTBaseVisitor<T> visitor) {
if (visitor is CodeBaseVisitor<T> CodeBaseVisitor) {
return CodeBaseVisitor.VisitWhile(this);
}
return default;
}
public override string GenerateNodeName() {
return $"While{base.GenerateNodeName()}";
}
}
public class GIf : CodeASTElement {
public const int Cond = 0, BodyTrue = 1, BodyFalse = 2, BodyTrueDecls = 3;
public override string[] ContextNames { get; } = { "Cond", "BodyTrue", "BodyFalse" };
public GIf() : base(context: 3, CodeNodeType.If) { }
public override StringBuilder AssemblyCode() {
var result = new StringBuilder("if (");
result.Append((GetChild(GIf.Cond, 0) as CodeASTElement).AssemblyCode());
result.Append(") {\n");
EnterScope();
foreach (CodeASTElement child in GetChildren(GIf.BodyTrue))
result.Append(Nest(child.AssemblyCode()) + ";\n");
LeaveScope();
result.Append(Nest("}"));
if (GetChildren(CIf.BodyFalse).Count() != 0) {
result.Append(" else {\n");
EnterScope();
foreach (CodeASTElement child in GetChildren(GIf.BodyFalse))
result.Append(Nest(child.AssemblyCode()) + ";\n");
LeaveScope();
result.Append(Nest("}"));
}
return result;
}
public override T Accept<T>(ASTBaseVisitor<T> visitor) {
if (visitor is CodeBaseVisitor<T> CodeBaseVisitor) {
return CodeBaseVisitor.VisitIf(this);
}
return default;
}
public override string GenerateNodeName() {
return $"If{base.GenerateNodeName()}";
}
}
public class GBlock : CodeASTElement {
private HashSet<string> _varSymbolTable = new HashSet<string>();
public const int Code = 0, Decls = 1;
public override string[] ContextNames { get; } = { "Code", "Decls" };
public GBlock() : base(context: 2, CodeNodeType.Block) { }
public override void DeclareVar(string varname, string type) {
bool isUpstairs = false;
CodeASTElement p = this;
while (p.Parents.Count() != 0 && (p.Parents.Last() as CodeASTElement).IsComplex()) {
if ((p.Parents.Last() as CodeASTElement).HasVar(varname)) {
isUpstairs = true;
break;
}
p = p.Parents.Last() as CodeASTElement;
}
if (!isUpstairs) {
if (!_varSymbolTable.Contains(varname))
AddCode(type + " " + varname + " = 0", Decls);
_varSymbolTable.Add(varname);
}
}
public override bool HasVar(string varname) {
return _varSymbolTable.Contains(varname);
}
public override StringBuilder AssemblyCode() {
var result = new StringBuilder();
result.Append("{\n");
EnterScope();
foreach (CodeASTElement child in GetChildren(Decls))
result.Append(Nest(child.AssemblyCode()) + ";\n");
foreach (CodeASTElement child in GetChildren(Code))
result.Append(Nest(child.AssemblyCode()) + ";\n");
LeaveScope();
result.Append(Nest("}"));
return result;
}
public override T Accept<T>(ASTBaseVisitor<T> visitor) {
if (visitor is CodeBaseVisitor<T> CodeBaseVisitor) {
return CodeBaseVisitor.VisitBlock(this);
}
return default;
}
public override string GenerateNodeName() {
return $"Block{base.GenerateNodeName()}";
}
}
public class GCodeRepo : CodeASTElement {
public const int Code = 0;
public override string[] ContextNames { get; } = { "Code" };
private StringBuilder code;
public GCodeRepo() : base(context: 1, CodeNodeType.CodeRepo) {
code = new StringBuilder();
}
public GCodeRepo(string code) : base(context: 1, CodeNodeType.CodeRepo) {
this.code = new StringBuilder();
this.code.Append(code);
}
public override StringBuilder AssemblyCode() {
var result = new StringBuilder();
result.Append(code);
foreach (CodeASTElement child in GetChildren(0))
result.Append(child.AssemblyCode());
return result;
}
public override T Accept<T>(ASTBaseVisitor<T> visitor) {
if (visitor is CodeBaseVisitor<T> codeBaseVisitor) {
return codeBaseVisitor.VisitCodeRepo(this);
}
return default;
}
public override string GenerateNodeName() {
return $"CodeRepo{base.GenerateNodeName()}";
}
public void AddCode(string code) {
AddChild(new GCodeRepo(code), 0);
}
public static GCodeRepo operator+(GCodeRepo b, GCodeRepo c) {
var repo = new GCodeRepo();
repo.Append(b.code.ToString());
repo.Append(c.code.ToString());
return repo;
}
public static implicit operator GCodeRepo(string s) {
var repo = new GCodeRepo(s);
return repo;
}
public string ToString(int startIndex, int length) {
return code.ToString(startIndex, length);
}
public GCodeRepo Append(string value) {
code.Append(value);
return this;
}
}
}