-
Notifications
You must be signed in to change notification settings - Fork 10
/
ProgramTree.cs
302 lines (241 loc) · 7.27 KB
/
ProgramTree.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
using System.Collections.Generic;
using SimpleLang.Visitors;
namespace ProgramTree
{
public enum OpType
{
OR,
AND,
EQUAL,
NOTEQUAL,
GREATER,
LESS,
EQGREATER,
EQLESS,
PLUS,
MINUS,
MULT,
DIV,
NOT,
UNMINUS
};
public abstract class Node
{
public Node Parent { get; set; }
public List<ExprNode> ExprChildren { get; set; } = new List<ExprNode>();
public List<StatementNode> StatChildren { get; set; } = new List<StatementNode>();
public abstract void Visit(Visitor v);
}
public abstract class ExprNode : Node
{
}
public class IdNode : ExprNode
{
public string Name { get; set; }
public IdNode(string name) => Name = name;
public override void Visit(Visitor v) => v.VisitIdNode(this);
}
public class IntNumNode : ExprNode
{
public int Num { get; set; }
public IntNumNode(int num) => Num = num;
public override void Visit(Visitor v) => v.VisitIntNumNode(this);
}
public class BoolValNode : ExprNode
{
public bool Val { get; set; }
public BoolValNode(bool val) => Val = val;
public override void Visit(Visitor v) => v.VisitBoolValNode(this);
}
public class BinOpNode : ExprNode
{
public ExprNode Left
{
get => ExprChildren[0];
set => ExprChildren[0] = value;
}
public ExprNode Right
{
get => ExprChildren[1];
set => ExprChildren[1] = value;
}
public OpType Op { get; set; }
public BinOpNode(ExprNode left, ExprNode right, OpType op)
{
Op = op;
ExprChildren.Add(left);
ExprChildren.Add(right);
}
public override void Visit(Visitor v) => v.VisitBinOpNode(this);
}
public class UnOpNode : ExprNode
{
public ExprNode Expr
{
get => ExprChildren[0];
set => ExprChildren[0] = value;
}
public OpType Op { get; set; }
public UnOpNode(ExprNode expr, OpType op)
{
Op = op;
ExprChildren.Add(expr);
}
public override void Visit(Visitor v) => v.VisitUnOpNode(this);
}
public abstract class StatementNode : Node
{
}
public class AssignNode : StatementNode
{
public IdNode Id { get; set; }
public ExprNode Expr
{
get => ExprChildren[0];
set => ExprChildren[0] = value;
}
public AssignNode(IdNode id, ExprNode expr)
{
Id = id;
ExprChildren.Add(expr);
}
public override void Visit(Visitor v) => v.VisitAssignNode(this);
}
public class WhileNode : StatementNode
{
public ExprNode Expr
{
get => ExprChildren[0];
set => ExprChildren[0] = value;
}
public StatementNode Stat
{
get => StatChildren[0];
set => StatChildren[0] = value;
}
public WhileNode(ExprNode expr, StatementNode stat)
{
ExprChildren.Add(expr);
StatChildren.Add(stat);
}
public override void Visit(Visitor v) => v.VisitWhileNode(this);
}
public class ForNode : StatementNode
{
public IdNode Id { get; set; }
public ExprNode From
{
get => ExprChildren[0];
set => ExprChildren[0] = value;
}
public ExprNode To
{
get => ExprChildren[1];
set => ExprChildren[1] = value;
}
public StatementNode Stat
{
get => StatChildren[0];
set => StatChildren[0] = value;
}
public ForNode(IdNode id, ExprNode from, ExprNode to, StatementNode stat)
{
Id = id;
ExprChildren.Add(from);
ExprChildren.Add(to);
StatChildren.Add(stat);
}
public override void Visit(Visitor v) => v.VisitForNode(this);
}
public class StListNode : StatementNode
{
public StListNode(StatementNode stat) => Add(stat);
public void Add(StatementNode stat) => StatChildren.Add(stat);
public override void Visit(Visitor v) => v.VisitStListNode(this);
}
public class IfElseNode : StatementNode
{
public ExprNode Expr
{
get => ExprChildren[0];
set => ExprChildren[0] = value;
}
public StatementNode TrueStat
{
get => StatChildren[0];
set => StatChildren[0] = value;
}
public StatementNode FalseStat
{
get => StatChildren[1];
set => StatChildren[1] = value;
}
public IfElseNode(ExprNode expr, StatementNode trueSt, StatementNode falseSt = null)
{
ExprChildren.Add(expr);
StatChildren.Add(trueSt);
StatChildren.Add(falseSt);
}
public override void Visit(Visitor v) => v.VisitIfElseNode(this);
}
public class PrintNode : StatementNode
{
public ExprListNode ExprList { get; set; }
public PrintNode(ExprListNode list)
{
ExprList = list;
ExprChildren = ExprList.ExprChildren;
}
public override void Visit(Visitor v) => v.VisitPrintNode(this);
}
public class InputNode : StatementNode
{
public IdNode Ident { get; set; }
public InputNode(IdNode ident) => Ident = ident;
public override void Visit(Visitor v) => v.VisitInputNode(this);
}
public class ExprListNode : ExprNode
{
public ExprListNode(ExprNode expr) => Add(expr);
public void Add(ExprNode expr) => ExprChildren.Add(expr);
public override void Visit(Visitor v) => v.VisitExprListNode(this);
}
public class VarListNode : StatementNode
{
public List<IdNode> vars = new List<IdNode>();
public VarListNode(IdNode id) => Add(id);
public void Add(IdNode id) => vars.Add(id);
public override void Visit(Visitor v) => v.VisitVarListNode(this);
}
public class EmptyNode : StatementNode
{
public override void Visit(Visitor v) => v.VisitEmptyNode(this);
}
public class GotoNode : StatementNode
{
public IntNumNode Label { get; set; }
public GotoNode(int num) => Label = new IntNumNode(num);
public override void Visit(Visitor v) => v.VisitGotoNode(this);
}
public class LabelStatementNode : StatementNode
{
public IntNumNode Label { get; set; }
public StatementNode Stat { get => StatChildren[0]; set => StatChildren[0] = value; }
public LabelStatementNode(int num, StatementNode stat)
{
Label = new IntNumNode(num);
StatChildren.Add(stat);
}
public override void Visit(Visitor v) => v.VisitLabelstatementNode(this);
}
public class BlockNode : StatementNode
{
public StListNode List { get; set; }
public BlockNode(StListNode st)
{
List = st;
StatChildren = List.StatChildren;
}
public override void Visit(Visitor v) => v.VisitBlockNode(this);
}
}