-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.cs
453 lines (347 loc) · 17.7 KB
/
Node.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
using System;
using System.Collections.Generic;
namespace Language {
public class Node {
public string Filename { get; set; }
public int Line { get; set; }
public int Position { get; set; }
public Node (string filename, int line, int position) {
Filename = filename;
Line = line;
Position = position;
}
public virtual string toSyntaxGraph () {
return string.Format("[{0}]", this.GetType().Name);
}
public virtual DataType staticTypeCheck() {
return DataType.NoneType;
}
public virtual void interpret() { }
}
public class IntegerNode : Node {
public string Number { get; set; }
public IntegerNode (string number, string filename, int line, int position) : base(filename, line, position) {
Number = number;
}
public override string toSyntaxGraph () {
return string.Format("[{0} {1}]", this.GetType().Name, Number);
}
public override DataType staticTypeCheck() {
return DataType.IntegerType;
}
}
public class FloatNode : Node {
public string Number { get; set; }
public FloatNode (string number, string filename, int line, int position) : base(filename, line, position) {
Number = number;
}
public override string toSyntaxGraph () {
return string.Format("[{0} {1}]", this.GetType().Name, Number);
}
public override DataType staticTypeCheck() {
return DataType.FloatType;
}
}
public class StringNode : Node {
public string Characters { get; set; }
public StringNode (string characters, string filename, int line, int position) : base(filename, line, position) {
Characters = characters;
}
public override string toSyntaxGraph () {
return string.Format("[{0} \"{1}\"]", this.GetType().Name, Characters);
}
public override DataType staticTypeCheck() {
return DataType.StringType;
}
}
public class IdentifierNode : Node {
public string Identifier { get; set; }
public SymbolTable Scope { get; set; }
public IdentifierNode (string identifier, SymbolTable scope, string filename, int line, int position) : base(filename, line, position) {
Identifier = identifier;
Scope = scope;
}
public override string toSyntaxGraph () {
return string.Format("[{0} {1}]", this.GetType().Name, Identifier);
}
public override DataType staticTypeCheck() {
DataType symbolType = DataType.NoneType;
try {
symbolType = Scope.Symbols[Identifier];
} catch (KeyNotFoundException) {
Log.Error(String.Format("Variable {0} used before defined", Identifier), Filename, Line, Position);
}
return symbolType;
}
}
public class FunctionCallNode : IdentifierNode {
public List<Node> Parameters { get; set; }
public FunctionCallNode (string identifier, List<Node> parameters, SymbolTable scope, string filename, int line, int position) : base(identifier, scope, filename, line, position) {
Parameters = parameters;
}
public override string toSyntaxGraph () {
string parameters = "";
foreach (Node parameter in Parameters) {
parameters += parameter.toSyntaxGraph();
}
return string.Format("[{0}() [PARAMS {1}]]", Identifier, parameters);
}
public override DataType staticTypeCheck () {
DataType type = base.staticTypeCheck();
// check if callable
Console.WriteLine(type.ToString());
if (type != DataType.CallableType)
Log.Error(String.Format("{0} is not callable", Identifier), Filename, Line, Position);
CallableType callable = Scope.Callables[Identifier];
// check parameter count matches defined parameter count
if (Parameters.Count != callable.ParameterList.Count)
Log.Error("Parameter count mismatch", Filename, Line, Position);
// check parameter types match
for (int index = 0; index < Parameters.Count; index++) {
DataType parameterType = Parameters[index].staticTypeCheck();
DataType definedType = callable.ParameterList[index].staticTypeCheck();
if (parameterType != callable.ParameterList[index].staticTypeCheck())
Log.Error(String.Format("Parameter type mismatch {0}->{1}", definedType.ToString(), parameterType.ToString()), Filename, Line, Position);
}
return callable.ReturnType;
}
}
public class BinaryOperatorNode : Node {
public Node LeftOperand { get; set; }
public Node RightOperand { get; set; }
public BinaryOperatorNode (Node leftOperand, Node rightOperand, string filename, int line, int position) : base(filename, line, position) {
LeftOperand = leftOperand;
RightOperand = rightOperand;
}
public override string toSyntaxGraph () {
return string.Format("[{0} {1} {2}]", this.GetType().Name, LeftOperand.toSyntaxGraph(), RightOperand.toSyntaxGraph());
}
public override DataType staticTypeCheck() {
DataType leftType = LeftOperand.staticTypeCheck();
DataType rightType = RightOperand.staticTypeCheck();
if (leftType == DataType.StringType || rightType == DataType.StringType) {
Log.Error(String.Format("Type mismatch - {0}->{1}", leftType.ToString(), rightType.ToString()), Filename, Line, Position);
}
if (leftType == DataType.FloatType || rightType == DataType.FloatType) {
if (leftType == DataType.IntegerType) {
Log.Warning("Implicit IntType->FloatType cast", Filename, Line, Position);
TypeCastNode castNode = new TypeCastNode(DataType.FloatType, LeftOperand, LeftOperand.Filename, LeftOperand.Line, LeftOperand.Position);
LeftOperand = castNode;
}
if (rightType == DataType.IntegerType) {
Log.Warning("Implicit IntType->FloatType cast", Filename, Line, Position);
TypeCastNode castNode = new TypeCastNode(DataType.FloatType, RightOperand, RightOperand.Filename, RightOperand.Line, RightOperand.Position);
RightOperand = castNode;
}
return DataType.FloatType;
}
return DataType.IntegerType;
}
}
public class ExponentialNode : BinaryOperatorNode {
public ExponentialNode(Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
}
public class MultiplyNode : BinaryOperatorNode {
public MultiplyNode (Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
}
public class DivideNode : BinaryOperatorNode {
public DivideNode (Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
}
public class AdditionNode : BinaryOperatorNode {
public AdditionNode (Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
}
public class SubtractionNode : BinaryOperatorNode {
public SubtractionNode(Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
}
public class ComparisonNode : BinaryOperatorNode {
public ComparisonNode(Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
public override DataType staticTypeCheck() {
base.staticTypeCheck();
return DataType.BooleanType;
}
}
public class GreaterThanNode : ComparisonNode {
public GreaterThanNode (Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
}
public class GreaterThanOrEqualNode : ComparisonNode {
public GreaterThanOrEqualNode (Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
}
public class LessThanNode : ComparisonNode {
public LessThanNode (Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
}
public class LessThanOrEqualNode : ComparisonNode {
public LessThanOrEqualNode (Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
}
public class EqualNode : ComparisonNode {
public EqualNode (Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
public override DataType staticTypeCheck() {
DataType leftType = LeftOperand.staticTypeCheck();
DataType rightType = RightOperand.staticTypeCheck();
if (leftType == DataType.StringType && rightType == DataType.StringType) {
return DataType.StringType;
}
else if (leftType == DataType.FloatType || rightType == DataType.FloatType) {
return DataType.FloatType;
}
else if (leftType == DataType.IntegerType && rightType == DataType.IntegerType) {
return DataType.IntegerType;
}
Log.Error("Type mismatch", Filename, Line, Position);
return DataType.NoneType;
}
}
public class NotEqualNode : EqualNode {
public NotEqualNode(Node leftOperand, Node rightOperand, string filename, int line, int position) : base(leftOperand, rightOperand, filename, line, position) { }
}
public class PrefixOperatorNode : Node {
public Node Target { get; set; }
public PrefixOperatorNode(Node target, string filename, int line, int position) : base(filename, line, position) { }
}
public class TypeCastNode : PrefixOperatorNode {
public DataType TargetDataType { get; set; }
public TypeCastNode(DataType targetDataType, Node target, string filename, int line, int position) : base(target, filename, line, position) {
TargetDataType = targetDataType;
Target = target;
}
public override string toSyntaxGraph() {
return String.Format("[CAST({0}) {1}]", TargetDataType.ToString(), Target.toSyntaxGraph());
}
public override DataType staticTypeCheck() {
DataType targetType = Target.staticTypeCheck();
if (targetType == DataType.StringType)
Log.Error(String.Format("Unable to cast from StringType to {0}", TargetDataType.ToString()), Filename, Line, Position);
return TargetDataType;
}
}
public class StatementNode : Node {
public StatementNode (string filename, int line, int position) : base(filename, line, position) { }
}
public class StatementListNode : Node {
public List<Node> Statements { get; set; }
public StatementListNode (List<Node> statements, string filename, int line, int position) : base(filename, line, position) {
Statements = statements;
}
public override string toSyntaxGraph () {
string output = "";
foreach (Node statement in Statements) {
output += statement.toSyntaxGraph() + " ";
}
//output += "]";
return output;
}
public override DataType staticTypeCheck() {
foreach(Node statement in Statements) {
statement.staticTypeCheck();
}
return DataType.NoneType;
}
}
public class IfStatementNode : StatementNode {
public Node Predicate { get; set; }
public Node TrueBranch { get; set; }
public Node FalseBranch { get; set; }
public IfStatementNode (Node predicate, Node trueBranch, Node falseBranch, string filename, int line, int position) : base(filename, line, position) {
Predicate = predicate;
TrueBranch = trueBranch;
FalseBranch = falseBranch;
}
public override string toSyntaxGraph () {
return String.Format("[IF [CONDITION {0}] [THEN {1}] [ELSE {2}]]", Predicate.toSyntaxGraph(), TrueBranch.toSyntaxGraph(), FalseBranch?.toSyntaxGraph() ?? "NULL");
}
public override DataType staticTypeCheck() {
if (Predicate.staticTypeCheck() != DataType.BooleanType)
Log.Error("Type mismatch - predicate must evaluate to boolean", Filename, Line, Position);
TrueBranch.staticTypeCheck();
FalseBranch?.staticTypeCheck();
return DataType.NoneType;
}
}
public class WhileStatementNode : StatementNode {
public Node Predicate { get; set; }
public Node Repeat { get; set; }
public WhileStatementNode (Node predicate, Node repeat, string filename, int line, int position) : base(filename, line, position) {
Predicate = predicate;
Repeat = repeat;
}
public override string toSyntaxGraph () {
return String.Format("[WHILE [CONDITION {0}] [DO {1}]]", Predicate.toSyntaxGraph(), Repeat.toSyntaxGraph());
}
public override DataType staticTypeCheck() {
if (Predicate.staticTypeCheck() != DataType.BooleanType)
Log.Error("Type mismatch - predicate must evaluate to boolean", Filename, Line, Position);
Repeat.staticTypeCheck();
return DataType.NoneType;
}
}
public class PrintStatementNode : StatementNode {
public Node Expression { get; set; }
public PrintStatementNode (Node expression, string filename, int line, int position) : base(filename, line, position) {
Expression = expression;
}
public override string toSyntaxGraph () {
return String.Format("[PRINT {0}]", Expression.toSyntaxGraph());
}
public override DataType staticTypeCheck() {
Expression.staticTypeCheck();
return DataType.NoneType;
}
}
public class IdentifierDefinitionNode : StatementNode {
public DataType Type { get; set; }
public IdentifierNode Symbol { get; set; }
public Node Value { get; set; }
public IdentifierDefinitionNode(DataType type, Node symbol, Node value, string filename, int line, int position) : base(filename, line, position) {
Type = type;
Symbol = (IdentifierNode)symbol;
Value = value;
if (!SymbolTable.CurrentScope.addSymbol(Symbol.Identifier, Type))
Log.Error("Identifier already exists in this scope", Filename, Line, Position);
}
public override string toSyntaxGraph () {
return String.Format("[VAR [= {0} {1}]]", Symbol.toSyntaxGraph(), Value?.toSyntaxGraph() ?? "NULL");
}
public override DataType staticTypeCheck() {
if ((Value?.staticTypeCheck() ?? Type) != Type)
Log.Error("Type mismatch - rvalue not equal to lvalue", Filename, Line, Position);
return Type;
}
}
public class FunctionDefinitionNode : IdentifierDefinitionNode {
public CallableType CallableType { get; set; }
public FunctionDefinitionNode (CallableType callableType, Node symbol, string filename, int line, int position) : base(DataType.CallableType, symbol, null, filename, line, position) {
CallableType = callableType;
}
public override string toSyntaxGraph () {
string parameters = "";
foreach (Node parameter in CallableType.ParameterList) {
parameters += parameter.toSyntaxGraph();
}
return String.Format("[{0}(..) [PARAMS {1}] [EXEC {2}] [RET {3}]]", Symbol.Identifier, parameters, CallableType.FunctionBody.toSyntaxGraph(), CallableType.ReturnType.ToString());
}
}
public class AssignmentNode : StatementNode {
public IdentifierNode Symbol { get; set; }
public Node Value { get; set; }
public AssignmentNode(Node identifier, Node value, string filename, int line, int position) : base(filename, line, position) {
Symbol = (IdentifierNode)identifier;
Value = value;
}
public override string toSyntaxGraph() {
return String.Format("[ASSIGN [= {0} {1}]]", Symbol.toSyntaxGraph(), Value.toSyntaxGraph());
}
public override DataType staticTypeCheck() {
DataType type = DataType.NoneType;
try {
type = Symbol.Scope.Symbols[Symbol.Identifier];
} catch (KeyNotFoundException) {
Log.Error("Variable referenced before assignment", Filename, Line, Position);
}
if (type != Value.staticTypeCheck())
Log.Error("Assignment type mismatch", Filename, Line, Position);
return DataType.NoneType;
}
}
public class ProgramNode : StatementListNode {
public ProgramNode(List<Node> statements, string filename) : base(statements, filename, 1, 1) { }
}
}