-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTGenerator.cs
More file actions
127 lines (105 loc) · 4.7 KB
/
Copy pathASTGenerator.cs
File metadata and controls
127 lines (105 loc) · 4.7 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Antlr4.Runtime.Tree;
namespace TeachingMaterial {
public class ASTGenerator : SimpleCalcParserBaseVisitor<int> {
private CCompileUnit m_root;
Stack<ValueTuple<SimpleCalcASTElement,int>> m_parents =
new Stack<(SimpleCalcASTElement, int)>();
public CCompileUnit M_Root {
get => m_root;
}
public override int VisitExprAssignment(SimpleCalcParser.ExprAssignmentContext context) {
CAssignment newNode = new CAssignment();
ValueTuple<SimpleCalcASTElement,int> parent= m_parents.Peek();
parent.Item1.AddChild(newNode,parent.Item2);
m_parents.Push((newNode,CAssignment.CT_LEFT));
Visit(context.VARIABLE());
m_parents.Pop();
m_parents.Push((newNode, CAssignment.CT_RIGHT));
Visit(context.expr());
m_parents.Pop();
return 0;
}
public override int VisitExprMulDiv(SimpleCalcParser.ExprMulDivContext context) {
switch (context.op.Type) {
case SimpleCalcLexer.MULT:
CMultiplication newNode = new CMultiplication();
ValueTuple<SimpleCalcASTElement, int> parent = m_parents.Peek();
parent.Item1.AddChild(newNode, parent.Item2);
m_parents.Push((newNode, CMultiplication.CT_LEFT));
Visit(context.expr(0));
m_parents.Pop();
m_parents.Push((newNode, CMultiplication.CT_RIGHT));
Visit(context.expr(1));
m_parents.Pop();
break;
case SimpleCalcLexer.DIV:
CDivision newNode1 = new CDivision();
ValueTuple<SimpleCalcASTElement, int> parent1 = m_parents.Peek();
parent1.Item1.AddChild(newNode1, parent1.Item2);
m_parents.Push((newNode1, CDivision.CT_LEFT));
Visit(context.expr(0));
m_parents.Pop();
m_parents.Push((newNode1, CDivision.CT_RIGHT));
Visit(context.expr(1));
m_parents.Pop();
break;
}
return 0;
}
public override int VisitExprAddSub(SimpleCalcParser.ExprAddSubContext context) {
switch (context.op.Type) {
case SimpleCalcLexer.PLUS:
CAddition newNode = new CAddition();
ValueTuple<SimpleCalcASTElement, int> parent = m_parents.Peek();
parent.Item1.AddChild(newNode, parent.Item2);
m_parents.Push((newNode, CAddition.CT_LEFT));
Visit(context.expr(0));
m_parents.Pop();
m_parents.Push((newNode, CAddition.CT_RIGHT));
Visit(context.expr(1));
m_parents.Pop();
break;
case SimpleCalcLexer.MINUS:
CSubtraction newNode1 = new CSubtraction();
ValueTuple<SimpleCalcASTElement, int> parent1 = m_parents.Peek();
parent1.Item1.AddChild(newNode1, parent1.Item2);
m_parents.Push((newNode1, CSubtraction.CT_LEFT));
Visit(context.expr(0));
m_parents.Pop();
m_parents.Push((newNode1, CSubtraction.CT_RIGHT));
Visit(context.expr(1));
m_parents.Pop();
break;
}
return 0;
}
public override int VisitCompileUnit(SimpleCalcParser.CompileUnitContext context) {
CCompileUnit newNode = new CCompileUnit();
m_root = newNode;
m_parents.Push((newNode,CCompileUnit.CT_BODY));
base.VisitCompileUnit(context);
m_parents.Pop();
return 0;
}
public override int VisitTerminal(ITerminalNode node) {
switch (node.Symbol.Type) {
case SimpleCalcLexer.NUMBER:
CNUMBER newNumber= new CNUMBER();
ValueTuple<SimpleCalcASTElement, int> parent = m_parents.Peek();
parent.Item1.AddChild(newNumber, parent.Item2);
break;
case SimpleCalcLexer.VARIABLE:
CVARIABLE newVar = new CVARIABLE();
ValueTuple<SimpleCalcASTElement, int> parent1 = m_parents.Peek();
parent1.Item1.AddChild(newVar, parent1.Item2);
break;
}
return 0;
}
}
}