-
Notifications
You must be signed in to change notification settings - Fork 0
/
AST.cs
500 lines (471 loc) · 18.3 KB
/
AST.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
using System.Collections.Generic;
using VinewoodCC.Semantic;
using System.Text.Json.Serialization;
using System.Text.Json;
namespace VinewoodCC
{
namespace AST
{
[JsonDerivedType(typeof(ASTCompilationUnit), "Program")]
[JsonDerivedType(typeof(ASTFunctionDefine), "FunctionDefine")]
[JsonDerivedType(typeof(ASTDeclaration), "Declaration")]
[JsonDerivedType(typeof(ASTToken), "Token")]
[JsonDerivedType(typeof(ASTTypename), "Typename")]
[JsonDerivedType(typeof(ASTParamsDeclarator), "ParamsDeclarator")]
[JsonDerivedType(typeof(ASTInitList), "InitList")]
[JsonDerivedType(typeof(ASTIdentifier), "Identifier")]
[JsonDerivedType(typeof(ASTArrayAccess), "ArrayAccess")]
[JsonDerivedType(typeof(ASTBinaryExpression), "BinaryExpression")]
[JsonDerivedType(typeof(ASTFunctionCall), "FunctionCall")]
[JsonDerivedType(typeof(ASTPostfixExpression), "PostfixExpression")]
[JsonDerivedType(typeof(ASTUnaryExpression), "UnaryExpression")]
[JsonDerivedType(typeof(ASTCharConstant), "CharConstant")]
[JsonDerivedType(typeof(ASTFloatConstant), "FloatConstant")]
[JsonDerivedType(typeof(ASTStringConstant), "StringConstant")]
[JsonDerivedType(typeof(ASTIntegerConstant), "IntegerConstant")]
[JsonDerivedType(typeof(ASTBreakStatement), "BreakStatement")]
[JsonDerivedType(typeof(ASTCompoundStatement), "CompoundStatement")]
[JsonDerivedType(typeof(ASTContinueStatement), "ContinueStatement")]
[JsonDerivedType(typeof(ASTExpressionStatement), "ExpressionStatement")]
[JsonDerivedType(typeof(ASTIterationDeclaredStatement), "IterationDeclaredStatement")]
[JsonDerivedType(typeof(ASTIterationStatement), "IterationStatement")]
[JsonDerivedType(typeof(ASTReturnStatement), "ReturnStatement")]
[JsonDerivedType(typeof(ASTSelectionStatement), "SelectionStatement")]
[JsonDerivedType(typeof(ASTArrayDeclarator), "ArrayDeclarator")]
[JsonDerivedType(typeof(ASTVariableDeclarator), "VariableDeclarator")]
[JsonDerivedType(typeof(ASTFunctionDeclarator), "FunctionDeclarator")]
public partial class ASTNode
{
public string Type { get; set; }
public ASTNode(string type)
{
Type = type;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(ASTNode))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}
public partial class ASTCompilationUnit : ASTNode
{
public List<ASTNode> Items { get; set; }
[JsonIgnore]
public Dictionary<string, STItem> GlobalSymbolTable { get; set; }
public ASTCompilationUnit() : base("Program")
{
Items = new List<ASTNode>();
}
public ASTCompilationUnit(List<ASTNode> items) : base("Program")
{
Items = items;
}
}
[JsonDerivedType(typeof(ASTIdentifier), "Identifier")]
[JsonDerivedType(typeof(ASTArrayAccess), "ArrayAccess")]
[JsonDerivedType(typeof(ASTBinaryExpression), "BinaryExpression")]
[JsonDerivedType(typeof(ASTFunctionCall), "FunctionCall")]
[JsonDerivedType(typeof(ASTPostfixExpression), "PostfixExpression")]
[JsonDerivedType(typeof(ASTUnaryExpression), "UnaryExpression")]
[JsonDerivedType(typeof(ASTCharConstant), "CharConstant")]
[JsonDerivedType(typeof(ASTFloatConstant), "FloatConstant")]
[JsonDerivedType(typeof(ASTStringConstant), "StringConstant")]
[JsonDerivedType(typeof(ASTIntegerConstant), "IntegerConstant")]
public abstract partial class ASTExpression : ASTNode
{
public ASTExpression(string type) : base(type)
{
}
}
public abstract partial class ASTConstant : ASTExpression
{
public ASTConstant(string type) : base(type)
{
}
}
[JsonDerivedType(typeof(ASTBreakStatement), "BreakStatement")]
[JsonDerivedType(typeof(ASTCompoundStatement), "CompoundStatement")]
[JsonDerivedType(typeof(ASTContinueStatement), "ContinueStatement")]
[JsonDerivedType(typeof(ASTExpressionStatement), "ExpressionStatement")]
[JsonDerivedType(typeof(ASTIterationDeclaredStatement), "IterationDeclaredStatement")]
[JsonDerivedType(typeof(ASTIterationStatement), "IterationStatement")]
[JsonDerivedType(typeof(ASTReturnStatement), "ReturnStatement")]
[JsonDerivedType(typeof(ASTSelectionStatement), "SelectionStatement")]
public abstract partial class ASTStatement : ASTNode
{
public ASTStatement(string type) : base(type)
{
}
}
public partial class ASTFunctionDefine : ASTNode
{
public List<ASTToken> Specifiers { get; set; }
public ASTDeclarator Declarator { get; set; }
public ASTCompoundStatement Body { get; set; }
[JsonIgnore]
private Dictionary<string, STItem> LocalSymbolTable { get; set; }
public ASTFunctionDefine() : base("FunctionDefine")
{
Specifiers = new List<ASTToken>();
}
public ASTFunctionDefine(List<ASTToken> specList, ASTDeclarator declarator, ASTCompoundStatement bodyStatement) : base("FunctionDefine")
{
Specifiers = specList;
Declarator = declarator;
Body = bodyStatement;
}
}
public partial class ASTDeclaration : ASTNode
{
public List<ASTToken> Specifiers { get; set; }
public List<ASTInitList> InitLists { get; set; }
public ASTDeclaration() : base("Declaration")
{
Specifiers = new List<ASTToken>();
InitLists = new List<ASTInitList>();
}
public ASTDeclaration(List<ASTToken> specList, List<ASTInitList> initList) : base("Declaration")
{
Specifiers = specList;
InitLists = initList;
}
}
public partial class ASTToken : ASTNode
{
public string Value { get; set; }
public int TokenID { get; set; }
public ASTToken() : base("Token")
{
}
public ASTToken(string value, int tid) : base("Token")
{
Value = value;
TokenID = tid;
}
}
public partial class ASTTypename : ASTNode
{
public List<ASTToken> Specfiers { get; set; }
public ASTDeclarator Declarator { get; set; }
public ASTTypename() : base("Typename")
{
Specfiers = new List<ASTToken>();
}
public ASTTypename(List<ASTToken> specList, ASTDeclarator declarator) : base("Typename")
{
Specfiers = specList;
Declarator = declarator;
}
}
[JsonDerivedType(typeof(ASTArrayDeclarator), "ArrayDeclarator")]
[JsonDerivedType(typeof(ASTVariableDeclarator), "VariableDeclarator")]
[JsonDerivedType(typeof(ASTFunctionDeclarator), "FunctionDeclarator")]
abstract public partial class ASTDeclarator : ASTNode
{
public ASTDeclarator(string type) : base(type)
{
}
}
public partial class ASTParamsDeclarator : ASTNode
{
public List<ASTToken> Specfiers { get; set; }
public ASTDeclarator Declarator { get; set; }
public ASTParamsDeclarator() : base("ParamsDeclarator")
{
Specfiers = new List<ASTToken>();
}
public ASTParamsDeclarator(List<ASTToken> specList, ASTDeclarator declarator) : base("ParamsDeclarator")
{
Specfiers = specList;
Declarator = declarator;
}
}
public partial class ASTInitList : ASTNode
{
public ASTDeclarator Declarator { get; set; }
public List<ASTExpression> Expressions { get; set; }
public ASTInitList() : base("InitList")
{
}
public ASTInitList(ASTDeclarator d, List<ASTExpression> e) : base("InitList")
{
Declarator = d;
Expressions = e;
}
}
public partial class ASTIdentifier : ASTExpression
{
public string Value { get; set; }
public int TokenID { get; set; }
public ASTIdentifier() : base("Identifier")
{
}
public ASTIdentifier(string value, int tid) : base("Identifier")
{
Value = value;
TokenID = tid;
}
}
public partial class ASTArrayAccess : ASTExpression
{
public ASTExpression ArrayName { get; set; }
public List<ASTExpression> Elements { get; set; }
public ASTArrayAccess() : base("ArrayAccess")
{
Elements = new List<ASTExpression>();
}
public ASTArrayAccess(ASTExpression arrayname, List<ASTExpression> elements) : base("ArrayAccess")
{
ArrayName = arrayname;
Elements = elements;
}
}
public partial class ASTBinaryExpression : ASTExpression
{
public ASTToken Operator { get; set; }
public ASTExpression Expr1 { get; set; }
public ASTExpression Expr2 { get; set; }
public ASTBinaryExpression() : base("BinaryExpression")
{
}
public ASTBinaryExpression(ASTToken op, ASTExpression e1, ASTExpression e2) : base("BinaryExpression")
{
Operator = op;
Expr1 = e1;
Expr2 = e2;
}
}
public partial class ASTCharConstant : ASTConstant
{
public string Value { get; set; }
public int TokenID { get; set; }
public ASTCharConstant() : base("CharConstant")
{
}
public ASTCharConstant(string value, int tid) : base("CharConstant")
{
Value = value;
TokenID = tid;
}
}
public partial class ASTFloatConstant : ASTConstant
{
public double Value { get; set; }
public int TokenID { get; set; }
public ASTFloatConstant() : base("FloatConstant")
{
}
public ASTFloatConstant(double value, int tid) : base("FloatConstant")
{
Value = value;
TokenID = tid;
}
}
public partial class ASTFunctionCall : ASTExpression
{
public ASTExpression FunctionName { get; set; }
public List<ASTNode> ArgList { get; set; }
public ASTFunctionCall() : base("FunctionCall")
{
}
public ASTFunctionCall(ASTExpression name, List<ASTNode> args) : base("FunctionCall")
{
FunctionName = name;
ArgList = args;
}
}
public partial class ASTIntegerConstant : ASTConstant
{
public int Value { get; set; }
public int TokenID { get; set; }
public ASTIntegerConstant() : base("IntegerConstant")
{
}
public ASTIntegerConstant(int value, int tid) : base("IntegerConstant")
{
Value = value;
TokenID = tid;
}
}
public partial class ASTPostfixExpression : ASTExpression
{
public ASTExpression Expression { get; set; }
public ASTToken Operator { get; set; }
public ASTPostfixExpression() : base("PostfixExpression")
{
}
public ASTPostfixExpression(ASTExpression expr, ASTToken op) : base("PostfixExpression")
{
Expression = expr;
Operator = op;
}
}
public partial class ASTStringConstant : ASTConstant
{
public string Value { get; set; }
public int TokenID { get; set; }
public ASTStringConstant() : base("StringConstant")
{
}
public ASTStringConstant(string value, int tid) : base("StringConstant")
{
Value = value;
TokenID = tid;
}
}
public partial class ASTUnaryExpression : ASTExpression
{
public ASTToken Operator { get; set; }
public ASTExpression Expression { get; set; }
public ASTUnaryExpression() : base("UnaryExpression")
{
}
public ASTUnaryExpression(ASTToken op, ASTExpression expr) : base("UnaryExpression")
{
Expression = expr;
Operator = op;
}
}
public partial class ASTBreakStatement : ASTStatement
{
public ASTBreakStatement() : base("BreakStatement")
{
}
}
public partial class ASTCompoundStatement : ASTStatement
{
public List<ASTNode> BlockItems { get; set; }
public ASTCompoundStatement() : base("CompoundStatement")
{
BlockItems = new List<ASTNode>();
}
public ASTCompoundStatement(List<ASTNode> items) : base("CompoundStatement")
{
BlockItems = items;
}
}
public partial class ASTContinueStatement : ASTStatement
{
public ASTContinueStatement() : base("ContinueStatement")
{
}
}
public partial class ASTExpressionStatement : ASTStatement
{
public List<ASTNode> Expressions { get; set; }
public ASTExpressionStatement() : base("ExpressionStatement")
{
}
public ASTExpressionStatement(List<ASTNode> exprs) : base("ExpressionStatement")
{
Expressions = exprs;
}
}
public partial class ASTIterationDeclaredStatement : ASTStatement
{
public ASTDeclaration Initilize { get; set; }
public List<ASTNode> Condition { get; set; }
public List<ASTNode> Step { get; set; }
public ASTStatement Stat { get; set; }
public ASTIterationDeclaredStatement() : base("IterationDeclaredStatement")
{
}
public ASTIterationDeclaredStatement(ASTDeclaration init,
List<ASTNode> cond,
List<ASTNode> step,
ASTStatement stat) : base("IterationDeclaredStatement")
{
Initilize = init;
Condition = cond;
Step = step;
Stat = stat;
}
}
public partial class ASTIterationStatement : ASTStatement
{
public List<ASTNode> Initilize { get; set; }
public List<ASTNode> Condition { get; set; }
public List<ASTNode> Step { get; set; }
public ASTStatement Stat { get; set; }
public ASTIterationStatement() : base("IterationStatement")
{
}
public ASTIterationStatement(List<ASTNode> init,
List<ASTNode> cond,
List<ASTNode> step,
ASTStatement stat) : base("IterationStatement")
{
Initilize = init;
Condition = cond;
Step = step;
Stat = stat;
}
}
public partial class ASTReturnStatement : ASTStatement
{
public List<ASTNode> Expression { get; set; }
public ASTReturnStatement() : base("ReturnStatement")
{
}
public ASTReturnStatement(List<ASTNode> expr) : base("ReturnStatement")
{
Expression = expr;
}
}
public partial class ASTSelectionStatement : ASTStatement
{
public List<ASTNode> Condition { get; set; }
public ASTStatement Then { get; set; }
public ASTStatement Otherwise { get; set; }
public ASTSelectionStatement() : base("SelectionStatement")
{
}
public ASTSelectionStatement(List<ASTNode> cond,
ASTStatement then,
ASTStatement otherwise) : base("SelectionStatement")
{
Condition = cond;
Then = then;
Otherwise = otherwise;
}
}
public partial class ASTArrayDeclarator : ASTDeclarator
{
public ASTDeclarator Declarator { get; set; }
public ASTExpression Expression { get; set; }
public ASTArrayDeclarator() : base("ArrayDeclarator")
{
}
public ASTArrayDeclarator(ASTDeclarator declarator, ASTExpression expressions) : base("ArrayDeclarator")
{
Declarator = declarator;
Expression = expressions;
}
}
public partial class ASTVariableDeclarator : ASTDeclarator
{
public ASTIdentifier Identifier { get; set; }
public ASTVariableDeclarator() : base("VariableDeclarator")
{
}
public ASTVariableDeclarator(ASTIdentifier declarator) : base("VariableDeclarator")
{
Identifier = declarator;
}
}
public partial class ASTFunctionDeclarator : ASTDeclarator
{
public ASTDeclarator Declarator { get; set; }
public List<ASTParamsDeclarator> Parameters { get; set; }
public ASTFunctionDeclarator() : base("FunctionDeclarator")
{
}
public ASTFunctionDeclarator(ASTDeclarator declarator,
List<ASTParamsDeclarator> paramsDecl) : base("FunctionDeclarator")
{
Declarator = declarator;
Parameters = paramsDecl;
}
}
}
}