-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAST.cs
More file actions
146 lines (101 loc) · 3.67 KB
/
AST.cs
File metadata and controls
146 lines (101 loc) · 3.67 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static PasCompiler.Token.E;
namespace PasCompiler {
// Base abstract syntax tree node class that all the classes that represent
// nodes of different language constructs inherit from.
abstract class ASTNode {
}
abstract class ExpressionNode : ASTNode {
}
// A constant value. Does not have children
class ConstantNode<T> : ExpressionNode {
// Value of the token. May be a number or string
public T Val { get; set; }
public ConstantNode(T value)
=> Val = value;
}
class UnaryNode : ExpressionNode {
public UnaryNode(Token operation, ExpressionNode expr) {
Op = operation;
Expr = expr;
}
public Token Op { get; set; }
public ExpressionNode Expr { get; set; }
}
// A binary node is an operator and two child subexpressions. This is the building block of an expression tree.
class BinaryNode : ExpressionNode {
// Binary operator that represents the root of an expression
public Token Op { get; set; }
// Left subexpression subtree
public ExpressionNode Left { get; set; }
// Right subexpression subtree
public ExpressionNode Right { get; set; }
public BinaryNode(Token operation, ExpressionNode l, ExpressionNode r) {
Op = operation;
Left = l;
Right = r;
}
}
class IdentNode : ExpressionNode {
// Identifier name
public string Name { get; set; }
public IdentNode(string name)
=> Name = name;
}
class VarDecl : ASTNode {
public IdentNode Id { get; set; }
public string Type { get; set; }
public VarDecl(IdentNode id, string type) {
Id = id;
Type = type;
}
}
abstract class StatementNode : ASTNode {
}
// Assignment of a right hand side value to a left hand side identifier. Value maybe constants, arrays, pointers, expressions etc.
class AssignmentNode : StatementNode {
// Left hand side identifier
public IdentNode Id { get; set; }
// Right hand side value
public ExpressionNode Expr { get; set; }
public AssignmentNode(IdentNode id, ExpressionNode expr) {
Id = id;
Expr = expr;
}
}
class ConditionalStatementNode : StatementNode {
public ExpressionNode Condition { get; set; }
public StatementNode IfBody { get; set; }
public StatementNode ElseBody { get; set; }
public ConditionalStatementNode(ExpressionNode condition, StatementNode ifBody, StatementNode elseBody) {
Condition = condition;
IfBody = ifBody;
ElseBody = elseBody;
}
}
class CompoundStatementNode : StatementNode {
public List<StatementNode> StatementList { get; set; }
public CompoundStatementNode(List<StatementNode> statementList) {
StatementList = statementList;
}
}
class BlockNode : ASTNode {
public List<VarDecl> VarDeclarations { get; set; }
public CompoundStatementNode StatementList { get; set; }
public BlockNode(List<VarDecl> varDeclarations, CompoundStatementNode statementList) {
VarDeclarations = varDeclarations;
StatementList = statementList;
}
}
// The root node of an entire program.
class ProgramNode : ASTNode {
public BlockNode Block { get; set; }
public ProgramNode(BlockNode block) {
Block = block;
}
}
}