-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
503 lines (438 loc) · 10.7 KB
/
env.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package core
import (
"bytes"
"encoding/json"
"fmt"
"math"
"sort"
"strconv"
"strings"
)
func (c *Context) RequireArgLen(fnName string, args []Value, count int) *RuntimeError {
if len(args) < count {
return &RuntimeError{
Reason: fmt.Sprintf("%s requires %d arguments, got %d", fnName, count, len(args)),
}
}
return nil
}
type stackEntry struct {
name string
position
}
func (e stackEntry) String() string {
if e.name != "" {
return fmt.Sprintf(" in fn %s %s", e.name, e.position)
}
return fmt.Sprintf(" in anonymous fn %s", e.position)
}
type RuntimeError struct {
Reason string
position
stackTrace []stackEntry
}
func (e *RuntimeError) Error() string {
trace := make([]string, len(e.stackTrace))
for i, entry := range e.stackTrace {
trace[i] = entry.String()
}
return fmt.Sprintf("Runtime error %s: %s\n%s", e.position, e.Reason, strings.Join(trace, "\n"))
}
type Context struct {
// directory containing the root file of this context, used for loading
// other modules with relative paths / URLs
RootPath string
Builtins []BuiltinFnValue
Modules []module
ImportMap map[string]Value
}
func NewContext(rootPath string) Context {
return Context{
RootPath: rootPath,
Builtins: []BuiltinFnValue{},
Modules: []module{},
ImportMap: make(map[string]Value),
}
}
type builtinFn func([]Value) (Value, *RuntimeError)
type BuiltinFnValue struct {
Name string
Fn builtinFn
}
func (v BuiltinFnValue) String() string {
return fmt.Sprintf("%s := () -> { <native fn> }", v.Name)
}
func (v BuiltinFnValue) Eq(u Value) bool {
if w, ok := u.(BuiltinFnValue); ok {
return v.Name == w.Name
}
return false
}
func (v BuiltinFnValue) Truthy() bool {
return true
}
func (c *Context) LoadFunc(name string, fn builtinFn) {
c.Builtins = append(c.Builtins, BuiltinFnValue{
Name: name,
Fn: fn,
})
}
func (c *Context) LoadBuiltins() {
// c.LoadModules()
c.LoadFunc("int", c.builtinInt)
c.LoadFunc("float", c.builtinFloat)
c.LoadFunc("string", c.builtinString)
c.LoadFunc("type", c.builtinType)
c.LoadFunc("len", c.builtinLen)
c.LoadFunc("print", c.builtinPrint)
c.LoadFunc("import", c.builtinImport)
}
func (c *Context) builtinType(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("type", args, 1); err != nil {
return nil, err
}
switch args[0].(type) {
case NullValue:
value := StringValue("null")
return &value, nil
case IntValue:
value := StringValue("int")
return &value, nil
case FloatValue:
value := StringValue("float")
return &value, nil
case BoolValue:
value := StringValue("bool")
return &value, nil
case *StringValue:
value := StringValue("string")
return &value, nil
case ListValue:
value := StringValue("list")
return &value, nil
case ObjectValue:
value := StringValue("object")
return &value, nil
case FunctionValue, BuiltinFnValue:
value := StringValue("function")
return &value, nil
}
panic("Unreachable: unknown runtime value")
}
func (c *Context) builtinInt(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("int", args, 1); err != nil {
return nil, err
}
switch arg := args[0].(type) {
case IntValue:
return arg, nil
case FloatValue:
return IntValue(math.Floor(float64(arg))), nil
case *StringValue:
n, err := strconv.ParseInt(string(*arg), 10, 64)
if err != nil {
return null, nil
}
return IntValue(n), nil
default:
return null, nil
}
}
func (c *Context) builtinFloat(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("float", args, 1); err != nil {
return nil, err
}
switch arg := args[0].(type) {
case IntValue:
return FloatValue(arg), nil
case FloatValue:
return arg, nil
case *StringValue:
f, err := strconv.ParseFloat(string(*arg), 64)
if err != nil {
return null, nil
}
return FloatValue(f), nil
default:
return null, nil
}
}
func (c *Context) builtinString(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("string", args, 1); err != nil {
return nil, err
}
switch arg := args[0].(type) {
case IntValue:
value := StringValue(strconv.FormatInt(int64(arg), 10))
return &value, nil
case FloatValue:
value := StringValue(strconv.FormatFloat(float64(arg), 'f', -1, 64))
return &value, nil
case *StringValue:
return arg, nil
default:
return null, nil
}
}
func (c *Context) builtinLen(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("len", args, 1); err != nil {
return nil, err
}
switch arg := args[0].(type) {
case *StringValue:
return IntValue(len(*arg)), nil
case ListValue:
return IntValue(len(arg)), nil
case ObjectValue:
return IntValue(len(arg)), nil
default:
return nil, &RuntimeError{
Reason: fmt.Sprintf("%s does not support a len() call", arg),
}
}
}
func (c *Context) builtinPrint(args []Value) (Value, *RuntimeError) {
for _, arg := range args {
switch arg := arg.(type) {
case *StringValue:
fmt.Print(string(*arg))
default:
fmt.Print(arg)
}
}
fmt.Println()
return null, nil
}
func (c *Context) builtinImport(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("import", args, 1); err != nil {
return nil, err
}
arg, ok := args[0].(*StringValue)
if !ok {
return nil, &RuntimeError{
Reason: fmt.Sprintf("import requires a string, got %s", args[0]),
}
}
name := string(*arg)
if value, ok := c.ImportMap[name]; ok {
return value, nil
}
for _, module := range c.Modules {
if module.name == string(*arg) {
value := make(ObjectValue, len(module.items))
for k, v := range module.items {
str := StringValue(k)
value[str.HashKey()] = ObjectPair{&str, v}
}
c.ImportMap[name] = value
return value, nil
}
}
return nil, &RuntimeError{
Reason: fmt.Sprintf("module %s not found", name),
}
}
type module struct {
name string
items map[string]Value
}
func (c *Context) LoadModule(name string, items map[string]Value) {
c.Modules = append(c.Modules, module{
name: name,
items: items,
})
}
func (c *Context) LoadModules() {
// c.LoadModule("math", map[string]Value{
// "pi": FloatValue(math.Pi),
// "ceil": BuiltinFnValue{
// "ceil",
// c.mathCeil,
// },
// "floor": BuiltinFnValue{
// "floor",
// c.mathFloor,
// },
// "sin": BuiltinFnValue{
// "sin",
// c.mathSin,
// },
// })
// c.LoadModule("json", map[string]Value{
// "parse": BuiltinFnValue{
// "parse",
// c.jsonParse,
// },
// "serialize": BuiltinFnValue{
// "serialize",
// c.jsonSerialize,
// },
// })
}
func (c *Context) mathCeil(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("math.ceil", args, 1); err != nil {
return nil, err
}
switch arg := args[0].(type) {
case IntValue:
return arg, nil
case FloatValue:
return IntValue(math.Ceil(float64(arg))), nil
default:
return nil, &RuntimeError{
Reason: fmt.Sprintf("math.ceil does not support type %s", arg),
}
}
}
func (c *Context) mathFloor(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("math.floor", args, 1); err != nil {
return nil, err
}
switch arg := args[0].(type) {
case IntValue:
return arg, nil
case FloatValue:
return IntValue(math.Floor(float64(arg))), nil
default:
return nil, &RuntimeError{
Reason: fmt.Sprintf("math.floor does not support type %s", arg),
}
}
}
func (c *Context) mathSin(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("math.sin", args, 1); err != nil {
return nil, err
}
switch arg := args[0].(type) {
case IntValue:
return FloatValue(math.Sin(float64(arg))), nil
case FloatValue:
return FloatValue(math.Sin(float64(arg))), nil
default:
return nil, &RuntimeError{
Reason: fmt.Sprintf("math.sin does not support type %s", arg),
}
}
}
func (c *Context) jsonParse(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("json.parse", args, 1); err != nil {
return nil, err
}
arg, ok := args[0].(*StringValue)
if !ok {
return nil, &RuntimeError{
Reason: fmt.Sprintf("json.parse requires a string, got %s", args[0]),
}
}
var data interface{}
if err := json.Unmarshal([]byte(*arg), &data); err != nil {
return nil, &RuntimeError{
Reason: fmt.Sprintf("json.parse: %s", err),
}
}
return c.convertJSON(data), nil
}
func (c *Context) jsonSerialize(args []Value) (Value, *RuntimeError) {
if err := c.RequireArgLen("json.serialize", args, 1); err != nil {
return nil, err
}
arg := args[0]
builder := bytes.Buffer{}
err := c.serializeValue(arg, &builder)
if err != nil {
return nil, &RuntimeError{
Reason: err.Error(),
}
}
value := StringValue(builder.Bytes())
return &value, nil
}
func (c *Context) serializeValue(value Value, builder *bytes.Buffer) *RuntimeError {
switch value := value.(type) {
case NullValue:
builder.WriteString("null")
case IntValue:
builder.WriteString(strconv.FormatInt(int64(value), 10))
case FloatValue:
builder.WriteString(strconv.FormatFloat(float64(value), 'f', -1, 64))
case BoolValue:
if value {
builder.WriteString("true")
} else {
builder.WriteString("false")
}
case *StringValue:
builder.WriteString(strconv.Quote(string(*value)))
case ListValue:
builder.WriteString("[")
for i, item := range value {
if i > 0 {
builder.WriteString(",")
}
if err := c.serializeValue(item, builder); err != nil {
return err
}
}
builder.WriteString("]")
case ObjectValue:
builder.WriteString("{")
i := 0
entries := make([]ObjectPair, 0, len(value))
for _, pair := range value {
entries = append(entries, pair)
}
sort.SliceStable(entries, func(i, j int) bool {
return entries[i].key.String() < entries[j].key.String()
})
for _, pair := range entries {
if i > 0 {
builder.WriteString(",")
}
str, ok := pair.key.(*StringValue)
if !ok {
return &RuntimeError{
Reason: fmt.Sprintf("json.serialize: unsupported type %s", pair.key),
}
}
builder.WriteString(strconv.Quote(string(*str)))
builder.WriteString(":")
if err := c.serializeValue(pair.value, builder); err != nil {
return err
}
i++
}
builder.WriteString("}")
default:
return &RuntimeError{
Reason: fmt.Sprintf("json.serialize: unsupported type %s", value),
}
}
return nil
}
func (c *Context) convertJSON(data interface{}) Value {
switch data := data.(type) {
case bool:
return BoolValue(data)
case float64:
return FloatValue(data)
case string:
value := StringValue(data)
return &value
case []interface{}:
value := make(ListValue, len(data))
for i, item := range data {
value[i] = c.convertJSON(item)
}
return value
case map[string]interface{}:
value := make(ObjectValue, len(data))
for k, v := range data {
str := StringValue(k)
value[str.HashKey()] = ObjectPair{&str, c.convertJSON(v)}
}
return value
case nil:
return null
}
panic("json.parse: unexpected type")
}