-
Notifications
You must be signed in to change notification settings - Fork 6
/
ast.go
169 lines (138 loc) · 2.23 KB
/
ast.go
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
package dsl
var count int
var blockNb int
type Node interface {
NodeId() int
Front() Node
Next() Node
AppendChild(Node)
}
type BasicNode struct {
BasicNodeId int
currChild int
children []Node
}
func CreateBasicNode(id int) BasicNode {
b := BasicNode{BasicNodeId: count}
b.children = make([]Node, 0)
return b
}
func (b *BasicNode) AppendChild(n Node) {
b.children = append(b.children, n)
}
func (b *BasicNode) NodeId() int {
return b.BasicNodeId
}
func (b *BasicNode) Front() Node {
b.currChild = 0
if len(b.children) < 1 {
return nil
}
return b.children[b.currChild]
}
func (b *BasicNode) Next() Node {
b.currChild = b.currChild + 1
if b.currChild >= len(b.children) {
return nil
}
return b.children[b.currChild]
}
type ProgramNode struct {
BasicNode
}
type StatementNode struct {
BasicNode
}
type AssignNode struct {
BasicNode
}
type TokenNode struct {
BasicNode
Token string
}
type OpNode struct {
BasicNode
Operator string
}
type WhileNode struct {
BasicNode
}
type PrintNode struct {
BasicNode
}
func newProgramNode(n Node) Node {
b := CreateBasicNode(count)
b.AppendChild(n)
e := &ProgramNode{
BasicNode: b,
}
count++
return e
}
func newStatementNode(l Node, r Node) Node {
b := CreateBasicNode(count)
if r != nil {
b.AppendChild(l)
b.AppendChild(r)
e := &StatementNode{
BasicNode: b,
}
count++
return e
}
b.AppendChild(l)
e := &StatementNode{
BasicNode: b,
}
count++
return e
}
func newAssignNode(l Node, r Node) Node {
b := CreateBasicNode(count)
b.AppendChild(l)
b.AppendChild(r)
e := &AssignNode{
BasicNode: b,
}
count++
return e
}
func newTokenNode(str string) Node {
b := CreateBasicNode(count)
e := &TokenNode{
BasicNode: b,
Token: str,
}
count++
return e
}
func newOpNode(str string, l Node, r Node) Node {
b := CreateBasicNode(count)
b.AppendChild(l)
b.AppendChild(r)
e := &OpNode{
BasicNode: b,
Operator: str,
}
count++
return e
}
func newWhileNode(l Node, r Node) Node {
b := CreateBasicNode(count)
b.AppendChild(l)
b.AppendChild(r)
e := &WhileNode{
BasicNode: b,
}
count++
return e
}
func newPrintNode(l Node) Node {
b := CreateBasicNode(count)
b.AppendChild(l)
e := &PrintNode{
BasicNode: b,
}
count++
return e
}