-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
251 lines (196 loc) · 4.53 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
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
package script
type Ast interface{}
type BinOp struct {
Left Ast
Token *Token
Op *Token
Right Ast
}
func NewBinOp(left Ast, op *Token, right Ast) *BinOp {
return &BinOp{Left: left, Token: op, Op: op, Right: right}
}
type Number struct {
Token *Token
Value float64
}
func NewNumber(token *Token) *Number {
ret := &Number{Token: token}
if v, ok := token.Value.(int); ok {
ret.Value = float64(v)
} else {
ret.Value = token.Value.(float64)
}
return ret
}
type String struct {
Token *Token
Value string
}
func NewString(token *Token) *String {
return &String{Token: token, Value: token.Value.(string)}
}
type Boolean struct {
Token *Token
Value bool
}
func NewBoolean(token *Token) *Boolean {
return &Boolean{Token: token, Value: token.Value.(bool)}
}
type List struct {
Token *Token
Value []Ast
}
func NewList(token *Token) *List {
return &List{Token: token, Value: token.Value.([]Ast)}
}
type Dict struct {
Token *Token
Value map[string]Ast
}
func NewDict(token *Token) *Dict {
return &Dict{Token: token, Value: token.Value.(map[string]Ast)}
}
type Message struct {
Token *Token
Value interface{}
}
func NewMessage(token *Token) *Message {
return &Message{Token: token, Value: token.Value.(int)}
}
type UnaryOp struct {
Op *Token
Expr Ast
}
func NewUnaryOp(op *Token, expr Ast) *UnaryOp {
return &UnaryOp{Op: op, Expr: expr}
}
type Compound struct {
Children []Ast
}
func NewCompound() *Compound {
return &Compound{}
}
type Assign struct {
Left Ast
Op *Token
Right Ast
}
func NewAssign(left Ast, op *Token, right Ast) *Assign {
return &Assign{Left: left, Op: op, Right: right}
}
type Var struct {
Token *Token
Value interface{}
}
func NewVar(token *Token) *Var {
return &Var{Token: token, Value: token.Value}
}
type NoOp struct{}
func NewNoOp() *NoOp {
return &NoOp{}
}
type Program struct {
Name string
Block Ast
Packages []Ast
}
func NewProgram(name string, packages []Ast, block Ast) *Program {
return &Program{Name: name, Packages: packages, Block: block}
}
type Package struct {
Name string
}
func NewPackage(name string) *Package {
return &Package{Name: name}
}
type Block struct {
Declarations [][]Ast
CompoundStatement Ast
}
func NewBlock(declarations [][]Ast, compoundStatement Ast) *Block {
return &Block{Declarations: declarations, CompoundStatement: compoundStatement}
}
type VarDecl struct {
VarNode Ast
TypeNode Ast
}
func NewVarDecl(varNode Ast, typeNode Ast) *VarDecl {
return &VarDecl{VarNode: varNode, TypeNode: typeNode}
}
type Type struct {
Token *Token
Value interface{}
}
func NewType(token *Token) *Type {
return &Type{Token: token, Value: token.Value}
}
type Param struct {
VarNode Ast
TypeNode Ast
}
func NewParam(varNode Ast, typeNode Ast) *Param {
return &Param{VarNode: varNode, TypeNode: typeNode}
}
type FunctionDecl struct {
PackageName string
FuncName string
FormalParams []Ast
BlockNode Ast
ReturnType Ast
}
func NewFunctionDecl(funcName string, formalParams []Ast, blockNode Ast, returnType Ast) *FunctionDecl {
return &FunctionDecl{PackageName: "", FuncName: funcName, FormalParams: formalParams, BlockNode: blockNode, ReturnType: returnType}
}
type FunctionCall struct {
PackageName string
FuncName string
ActualParams []Ast
Token *Token
FuncSymbol Symbol
}
func NewFunctionCall(packageName string, funcName string, actualParams []Ast, token *Token) *FunctionCall {
return &FunctionCall{PackageName: packageName, FuncName: funcName, ActualParams: actualParams, Token: token}
}
type If struct {
Condition Ast
ThenBranch []Ast
ElseBranch []Ast
}
func NewIf(condition Ast, thenBranch []Ast, elseBranch []Ast) *If {
return &If{Condition: condition, ThenBranch: thenBranch, ElseBranch: elseBranch}
}
type While struct {
Condition Ast
DoBranch []Ast
}
func NewWhile(condition Ast, doBranch []Ast) *While {
return &While{Condition: condition, DoBranch: doBranch}
}
type Logical struct {
Left Ast
Op *Token
Right Ast
}
func NewLogical(left Ast, op *Token, right Ast) *Logical {
return &Logical{Left: left, Op: op, Right: right}
}
type Print struct {
Statement Ast
}
func NewPrint(statement Ast) *Print {
return &Print{Statement: statement}
}
type Return struct {
Statement Ast
}
func NewReturn(statement Ast) *Return {
return &Return{Statement: statement}
}
type FunctionRef struct {
PackageName string
FuncName string
Token *Token
}
func NewFunctionRef(packageName string, funcName string, token *Token) *FunctionRef {
return &FunctionRef{PackageName: packageName, FuncName: funcName, Token: token}
}