-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTGenerator.cs
More file actions
106 lines (86 loc) · 4.81 KB
/
Copy pathASTGenerator.cs
File metadata and controls
106 lines (86 loc) · 4.81 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Antlr4.Runtime.Tree;
namespace ANTLR_Startup_Project {
public class ASTGenerator : firstBaseVisitor<int> {
private ASTComposite m_root;
public ASTComposite M_Root => m_root;
Stack<ASTComposite> m_parents = new Stack<ASTComposite>();
Stack<contextType> m_parentContext = new Stack<contextType>();
public override int VisitExpr_MULDIV(firstParser.Expr_MULDIVContext context) {
ASTComposite m_parent = m_parents.Peek();
if (context.op.Type == firstParser.MULT) {
CASTMultiplication newnode = new CASTMultiplication(context.GetText(),nodeType.NT_MULTIPLICATION, m_parents.Peek(), 2);
m_parent.AddChild(newnode,m_parentContext.Peek());
m_parents.Push(newnode);
this.VisitElementInContext(context.expr(0), m_parentContext, contextType.CT_MULTIPLICATION_LEFT);
this.VisitElementInContext(context.expr(1), m_parentContext, contextType.CT_MULTIPLICATION_RIGHT);
}
else if (context.op.Type == firstParser.DIV) {
CASTDivision newnode = new CASTDivision(context.GetText(),nodeType.NT_DIVISION, m_parents.Peek(), 2);
m_parent.AddChild(newnode, m_parentContext.Peek());
m_parents.Push(newnode);
this.VisitElementInContext(context.expr(0), m_parentContext, contextType.CT_DIVISION_LEFT);
this.VisitElementInContext(context.expr(1), m_parentContext, contextType.CT_DIVISION_RIGHT);
}
m_parents.Pop();
return 0;
}
public override int VisitExpr_PLUSMINUS(firstParser.Expr_PLUSMINUSContext context) {
ASTComposite m_parent = m_parents.Peek();
if (context.op.Type == firstParser.MINUS) {
CASTSubtraction newnode = new CASTSubtraction(context.GetText(), nodeType.NT_SUBSTRACTION, m_parents.Peek(), 2);
m_parent.AddChild(newnode, m_parentContext.Peek());
m_parents.Push(newnode);
this.VisitElementInContext(context.expr(0), m_parentContext, contextType.CT_SUBSTRACTION_LEFT);
this.VisitElementInContext(context.expr(1), m_parentContext, contextType.CT_SUBSTRACTION_RIGHT);
}
else if (context.op.Type == firstParser.PLUS) {
CASTAddition newnode = new CASTAddition(context.GetText(), nodeType.NT_ADDITION, m_parents.Peek(), 2);
m_parent.AddChild(newnode, m_parentContext.Peek());
m_parents.Push(newnode);
this.VisitElementInContext(context.expr(0), m_parentContext, contextType.CT_ADDITION_LEFT);
this.VisitElementInContext(context.expr(1), m_parentContext, contextType.CT_ADDITION_RIGHT);
}
m_parents.Pop();
return 0;
}
public override int VisitExpr_ASSIGNMENT(firstParser.Expr_ASSIGNMENTContext context) {
ASTComposite m_parent = m_parents.Peek();
CASTAssignment newnode = new CASTAssignment(context.GetText(), nodeType.NT_ASSIGNMENT, m_parents.Peek(), 2);
m_parent.AddChild(newnode, m_parentContext.Peek());
m_parents.Push(newnode);
this.VisitTerminalInContext(context,context.IDENTIFIER().Symbol, m_parentContext, contextType.CT_ASSIGNMENT_LEFT);
this.VisitElementInContext(context.expr(), m_parentContext, contextType.CT_ASSIGNMENT_RIGHT);
m_parents.Pop();
return 0;
}
public override int VisitCompileUnit(firstParser.CompileUnitContext context) {
CASTCompileUnit newnode = new CASTCompileUnit(context.GetText(), nodeType.NT_COMPILEUNIT, null, 1);
m_root = newnode;
m_parents.Push(newnode);
this.VisitElementsInContext(context.expr(), m_parentContext, contextType.CT_COMPILEUNIT_EXPRESSIONS);
m_parents.Pop();
return 0;
}
public override int VisitTerminal(ITerminalNode node) {
ASTComposite m_parent = m_parents.Peek();
switch (node.Symbol.Type){
case firstLexer.NUMBER:
CASTNUMBER newnode1 = new CASTNUMBER(node.Symbol.Text,nodeType.NT_NUMBER,m_parents.Peek(),0);
m_parent.AddChild(newnode1,m_parentContext.Peek());
break;
case firstLexer.IDENTIFIER:
CASTIDENTIFIER newnode2 = new CASTIDENTIFIER(node.Symbol.Text,nodeType.NT_IDENTIFIER, m_parents.Peek(), 0);
m_parent.AddChild(newnode2, m_parentContext.Peek());
break;
default:
break;
}
return base.VisitTerminal(node);
}
}
}