-
Notifications
You must be signed in to change notification settings - Fork 4
/
ast.go
232 lines (184 loc) · 5.37 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
package ast
import (
"bytes"
"magpie/token"
"unicode/utf8"
)
type Node interface {
Pos() token.Position // position of first character belonging to the node
End() token.Position // position of first character immediately after the node
TokenLiteral() string
String() string
}
type Statement interface {
Node
statementNode()
}
type Expression interface {
Node
expressionNode()
}
type Program struct {
Statements []Statement
}
func (p *Program) Pos() token.Position {
if len(p.Statements) > 0 {
return p.Statements[0].Pos()
}
return token.Position{}
}
func (p *Program) End() token.Position {
aLen := len(p.Statements)
if aLen > 0 {
return p.Statements[aLen-1].End()
}
return token.Position{}
}
func (p *Program) TokenLiteral() string {
if len(p.Statements) > 0 {
return p.Statements[0].TokenLiteral()
}
return ""
}
func (p *Program) String() string {
var out bytes.Buffer
for _, s := range p.Statements {
out.WriteString(s.String())
}
return out.String()
}
//let <identifier> = <expression>
type LetStatement struct {
Token token.Token
Name *Identifier
Value Expression
}
func (ls *LetStatement) Pos() token.Position {
return ls.Token.Pos
}
func (ls *LetStatement) End() token.Position {
return ls.Value.End()
}
func (ls *LetStatement) statementNode() {}
func (ls *LetStatement) TokenLiteral() string { return ls.Token.Literal }
func (ls *LetStatement) String() string {
var out bytes.Buffer
out.WriteString(ls.TokenLiteral() + " ")
out.WriteString(ls.Name.String())
out.WriteString(" = ")
if ls.Value != nil {
out.WriteString(ls.Value.String())
}
out.WriteString(";")
return out.String()
}
type ExpressionStatement struct {
Token token.Token
Expression Expression
}
func (es *ExpressionStatement) Pos() token.Position {
return es.Token.Pos
}
func (es *ExpressionStatement) End() token.Position {
return es.Expression.End()
}
func (es *ExpressionStatement) statementNode() {}
func (es *ExpressionStatement) TokenLiteral() string { return es.Token.Literal }
func (es *ExpressionStatement) String() string {
if es.Expression != nil {
return es.Expression.String()
}
return ""
}
// 1 + 2 * 3
type InfixExpression struct {
Token token.Token
Operator string
Right Expression
Left Expression
}
func (ie *InfixExpression) Pos() token.Position { return ie.Token.Pos }
func (ie *InfixExpression) End() token.Position { return ie.Right.End() }
func (ie *InfixExpression) expressionNode() {}
func (ie *InfixExpression) TokenLiteral() string { return ie.Token.Literal }
func (ie *InfixExpression) String() string {
var out bytes.Buffer
out.WriteString("(")
out.WriteString(ie.Left.String())
out.WriteString(" " + ie.Operator + " ")
out.WriteString(ie.Right.String())
out.WriteString(")")
return out.String()
}
// -2, -3
type PrefixExpression struct {
Token token.Token
Operator string
Right Expression
}
func (pe *PrefixExpression) Pos() token.Position { return pe.Token.Pos }
func (pe *PrefixExpression) End() token.Position { return pe.Right.End() }
func (pe *PrefixExpression) expressionNode() {}
func (pe *PrefixExpression) TokenLiteral() string { return pe.Token.Literal }
func (pe *PrefixExpression) String() string {
var out bytes.Buffer
out.WriteString("(")
out.WriteString(pe.Operator)
out.WriteString(pe.Right.String())
out.WriteString(")")
return out.String()
}
type NumberLiteral struct {
Token token.Token
Value float64
}
func (nl *NumberLiteral) Pos() token.Position { return nl.Token.Pos }
func (nl *NumberLiteral) End() token.Position {
length := utf8.RuneCountInString(nl.Token.Literal)
pos := nl.Token.Pos
return token.Position{Filename: pos.Filename, Line: pos.Line, Col: pos.Col + length}
}
func (nl *NumberLiteral) expressionNode() {}
func (nl *NumberLiteral) TokenLiteral() string { return nl.Token.Literal }
func (nl *NumberLiteral) String() string { return nl.Token.Literal }
type Identifier struct {
Token token.Token
Value string
}
func (i *Identifier) Pos() token.Position { return i.Token.Pos }
func (i *Identifier) End() token.Position {
length := utf8.RuneCountInString(i.Value)
return token.Position{Filename: i.Token.Pos.Filename, Line: i.Token.Pos.Line, Col: i.Token.Pos.Col + length}
}
func (i *Identifier) expressionNode() {}
func (i *Identifier) TokenLiteral() string { return i.Token.Literal }
func (i *Identifier) String() string { return i.Value }
type NilLiteral struct {
Token token.Token
}
func (n *NilLiteral) Pos() token.Position {
return n.Token.Pos
}
func (n *NilLiteral) End() token.Position {
length := len(n.Token.Literal)
pos := n.Token.Pos
return token.Position{Filename: pos.Filename, Line: pos.Line, Col: pos.Col + length}
}
func (n *NilLiteral) expressionNode() {}
func (n *NilLiteral) TokenLiteral() string { return n.Token.Literal }
func (n *NilLiteral) String() string { return n.Token.Literal }
type BooleanLiteral struct {
Token token.Token
Value bool
}
func (b *BooleanLiteral) Pos() token.Position {
return b.Token.Pos
}
func (b *BooleanLiteral) End() token.Position {
length := utf8.RuneCountInString(b.Token.Literal)
pos := b.Token.Pos
return token.Position{Filename: pos.Filename, Line: pos.Line, Col: pos.Col + length}
}
func (b *BooleanLiteral) expressionNode() {}
func (b *BooleanLiteral) TokenLiteral() string { return b.Token.Literal }
func (b *BooleanLiteral) String() string { return b.Token.Literal }