-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.go
376 lines (315 loc) · 6.8 KB
/
nodes.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package deslang
import (
"fmt"
"io"
"strconv"
)
type litKind uint
const (
nilLit litKind = iota
floatLit
stringLit
boolLit
)
var types = map[litKind]string{
nilLit: "nil",
floatLit: "float",
stringLit: "string",
boolLit: "boolean",
}
func toFloat(s string) float64 {
// TODO: Handle err when converting string to int
f, _ := strconv.ParseFloat(s, 64)
return f
}
func fromFloat(f float64) string {
return strconv.FormatFloat(f, 'E', -1, 64)
}
func isTruthy(lit BasicLit) bool {
switch lit.Kind {
case nilLit:
return false
case floatLit:
return lit.Value != "0"
case stringLit:
return len(lit.Value) > 0
case boolLit:
return lit.Value == "true"
default:
return false
}
}
// ----------------------------------------------------------------------------
// Expressions
type (
Expr interface {
Interpret(*Environment) (BasicLit, error)
}
Unary struct {
Right Expr
Op Token
}
Binary struct {
Left, Right Expr
Op Token
}
Assign struct {
Name Token
Value Expr
}
// (X)
Grouping struct {
X Expr
}
Variable struct {
Name Token
}
// and, or
Logical struct {
Left, Right Expr
Op Token
}
// Primitive value. It's stored as a string and the Kind field is used to
// figure out typecasting. It would probably be faster to split this into a
// few structs based on type but this way is simpler.
BasicLit struct {
Value string
Kind litKind
}
)
// ----------------------------------------------------------------------------
// Interpretation methods
func (expr Unary) Interpret(env *Environment) (BasicLit, error) {
var result BasicLit
right, err := expr.Right.Interpret(env)
if err != nil {
return result, err
}
switch expr.Op.Type {
case _bang:
result.Kind = boolLit
if right.Kind == boolLit && right.Value == "true" {
result.Value = "false"
} else {
result.Value = "true"
}
case _minus:
result.Kind = floatLit
result.Value = fromFloat(-toFloat(right.Value))
}
return result, nil
}
func (expr Binary) Interpret(env *Environment) (BasicLit, error) {
var result BasicLit
left, err := expr.Left.Interpret(env)
if err != nil {
return result, err
}
right, err := expr.Right.Interpret(env)
if err != nil {
return result, err
}
// Type check
if left.Kind != right.Kind {
err := fmt.Errorf(
"Invalid operation. Mismatched types %s and %s",
types[left.Kind],
types[right.Kind],
)
return result, err
}
switch expr.Op.Type {
case _plus:
if left.Kind == stringLit && right.Kind == stringLit {
result.Value = left.Value + right.Value
result.Kind = stringLit
} else {
result.Value = fromFloat(toFloat(left.Value) + toFloat(right.Value))
result.Kind = floatLit
}
case _minus:
result.Value = fromFloat(toFloat(left.Value) - toFloat(right.Value))
result.Kind = floatLit
case _slash:
result.Value = fromFloat(toFloat(left.Value) / toFloat(right.Value))
result.Kind = floatLit
case _star:
result.Value = fromFloat(toFloat(left.Value) * toFloat(right.Value))
result.Kind = floatLit
case _greater:
result.Kind = boolLit
if toFloat(left.Value) > toFloat(right.Value) {
result.Value = "true"
} else {
result.Value = "false"
}
case _greater_equal:
result.Kind = boolLit
if toFloat(left.Value) >= toFloat(right.Value) {
result.Value = "true"
} else {
result.Value = "false"
}
case _less:
result.Kind = boolLit
if toFloat(left.Value) < toFloat(right.Value) {
result.Value = "true"
} else {
result.Value = "false"
}
case _less_equal:
result.Kind = boolLit
if toFloat(left.Value) <= toFloat(right.Value) {
result.Value = "true"
} else {
result.Value = "false"
}
case _bang_equal:
result.Kind = boolLit
if left.Value == right.Value {
result.Value = "false"
} else {
result.Value = "true"
}
case _equal_equal:
result.Kind = boolLit
if left.Value == right.Value {
result.Value = "true"
} else {
result.Value = "false"
}
default:
result = BasicLit{Value: "", Kind: nilLit}
}
return result, nil
}
func (expr Assign) Interpret(env *Environment) (BasicLit, error) {
var result BasicLit
result, err := expr.Value.Interpret(env)
if err != nil {
return result, err
}
env.Assign(expr.Name, result)
return result, nil
}
func (expr Grouping) Interpret(env *Environment) (BasicLit, error) {
return expr.X.Interpret(env)
}
func (expr BasicLit) Interpret(env *Environment) (BasicLit, error) {
return expr, nil
}
func (expr Variable) Interpret(env *Environment) (BasicLit, error) {
return env.Get(expr.Name)
}
func (expr Logical) Interpret(env *Environment) (BasicLit, error) {
left, err := expr.Left.Interpret(env)
if err != nil {
return BasicLit{}, nil
}
right, err := expr.Right.Interpret(env)
if err != nil {
return BasicLit{}, nil
}
if expr.Op.Type == _or {
if isTruthy(left) {
return left, nil
}
} else if !isTruthy(left) {
return left, nil
}
return right, nil
}
// ----------------------------------------------------------------------------
// Statements
type (
Stmt interface {
Execute(io.Writer, *Environment) error
}
// Empty Stmt
NilStmt struct{}
ExprStmt struct {
Expr Expr
}
PrintStmt struct {
Expr Expr
}
VarStmt struct {
Name Token
Expr Expr
}
AssignStmt struct {
Name Token
Expr Expr
}
BlockStmt struct {
Stmts []Stmt
}
IfStmt struct {
Cond Expr
Then Stmt
Else Stmt
}
)
// ----------------------------------------------------------------------------
// Executor methods
func (stmt ExprStmt) Execute(_ io.Writer, env *Environment) error {
stmt.Expr.Interpret(env)
return nil
}
func (stmt PrintStmt) Execute(w io.Writer, env *Environment) error {
lit, err := stmt.Expr.Interpret(env)
if err != nil {
return err
}
fmt.Fprintln(w, lit.Value)
return nil
}
func (stmt VarStmt) Execute(_ io.Writer, env *Environment) error {
lit, err := stmt.Expr.Interpret(env)
if err != nil {
return err
}
env.Define(string(stmt.Name.Lexeme), lit)
return nil
}
func (stmt AssignStmt) Execute(_ io.Writer, env *Environment) error {
lit, err := stmt.Expr.Interpret(env)
if err != nil {
return err
}
env.Assign(stmt.Name, lit)
return nil
}
func (stmt BlockStmt) Execute(w io.Writer, env *Environment) error {
local := NewEnvironment(false)
local.Enclosing = env
return executeBlock(stmt.Stmts, w, local)
}
func (NilStmt) Execute(w io.Writer, env *Environment) error {
return nil
}
func (stmt IfStmt) Execute(w io.Writer, env *Environment) error {
condLit, err := stmt.Cond.Interpret(env)
if err != nil {
return err
}
if isTruthy(condLit) {
return stmt.Then.Execute(w, env)
} else {
switch stmt.Else.(type) {
case NilStmt:
default:
return stmt.Else.Execute(w, env)
}
}
return nil
}
func executeBlock(stmts []Stmt, w io.Writer, env *Environment) error {
for _, s := range stmts {
err := s.Execute(w, env)
if err != nil {
return err
}
}
return nil
}