-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
198 lines (153 loc) · 3.18 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
package simple_template
import (
"fmt"
"strings"
)
type Position struct {
Source string
Line int
Column int
}
type Token struct {
Type rune
Str string
Pos Position
}
// TODO print better readible type
func (self *Token) String() string {
return fmt.Sprintf("<%s,%s>", TokenName(int(self.Type)), self.Str)
}
func (self *Token) Line() int {
return self.Pos.Line
}
type Expr interface {
SetLine(line int)
Line() int
String() string
}
type BaseExpr struct {
Pos Position
}
func (b *BaseExpr) String() string {
return "<base, nil>"
}
func (e *BaseExpr) SetLine(line int) {
e.Pos.Line = line
}
func (e *BaseExpr) Line() int {
return e.Pos.Line
}
type StringExpr struct {
BaseExpr
Value string
}
func (b *StringExpr) String() string {
return fmt.Sprintf("<strexpr,%s>", b.Value)
}
type NumberExpr struct {
BaseExpr
Value float64
}
func (e *NumberExpr) String() string {
return fmt.Sprintf("<numexpr,%v>", e.Value)
}
type TrueExpr struct {
BaseExpr
}
func (e *TrueExpr) String() string {
return "<true expr>"
}
type FalseExpr struct {
BaseExpr
}
func (e *FalseExpr) String() string {
return "<false expr>"
}
type IdentExpr struct {
BaseExpr
Value string
}
func (e *IdentExpr) String() string {
return fmt.Sprintf("<identxpr,%s>", e.Value)
}
type LogicalExpr struct {
BaseExpr
Lhs Expr
Operator string
Rhs Expr
}
func (e *LogicalExpr) String() string {
return fmt.Sprintf("<%s,%s,%s>", e.Lhs.String(), e.Operator, e.Rhs.String())
}
type ArithmeticOpExpr struct {
BaseExpr
Lhs Expr
Operator string
Rhs Expr
}
func (e *ArithmeticOpExpr) String() string {
return fmt.Sprintf("<%s,%s,%s>", e.Lhs.String(), e.Operator, e.Rhs.String())
}
type FuncCallExpr struct {
BaseExpr
Func *IdentExpr
Args []Expr
}
func (e *FuncCallExpr) String() string {
args := make([]string, 0, len(e.Args))
for _, a := range e.Args {
args = append(args, a.String())
}
return fmt.Sprintf("<%s(%s)>", e.Func.String(), strings.Join(args, ","))
}
type AttrGetExpr struct {
BaseExpr
Object Expr
Key Expr
}
func (e *AttrGetExpr) String() string {
return fmt.Sprintf("<%s[%s]>", e.Object.String(), e.Key.String())
}
type IfExpr struct {
BaseExpr
Cond Expr
Then Expr
Else Expr
}
func (e *IfExpr) String() string {
if e.Else == nil {
return fmt.Sprintf("<if(%s)then{%s}>", e.Cond.String(), e.Then.String())
} else {
return fmt.Sprintf("<if(%s)then{%s}else{%s}>", e.Cond.String(), e.Then.String(), e.Else.String())
}
}
type ArrayConstructExpr struct {
BaseExpr
Members []Expr
}
func (e *ArrayConstructExpr) String() string {
memStrs := make([]string, 0, len(e.Members))
for _, m := range e.Members {
memStrs = append(memStrs, m.String())
}
return fmt.Sprintf("<array(%s)>", strings.Join(memStrs, ","))
}
type MapItemExpr struct {
BaseExpr
Key Expr
Value Expr
}
func (e *MapItemExpr) String() string {
return fmt.Sprintf("%s=>%s", e.Key.String(), e.Value.String())
}
type MapConstructExpr struct {
BaseExpr
Members []Expr
}
func (e *MapConstructExpr) String() string {
memStrs := make([]string, 0, len(e.Members))
for _, m := range e.Members {
memStrs = append(memStrs, m.String())
}
return fmt.Sprintf("<map(%s)>", strings.Join(memStrs, ","))
}