This repository has been archived by the owner on May 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
specials.go
495 lines (408 loc) · 9.83 KB
/
specials.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
package sabre
import (
"errors"
"fmt"
"reflect"
"sort"
)
var (
// Def implements (def symbol value) form for defining bindings.
Def = SpecialForm{
Name: "def",
Parse: parseDef,
}
// Lambda defines an anonymous function and returns. Must have the form
// (fn* name? [arg*] expr*) or (fn* name? ([arg]* expr*)+)
Lambda = SpecialForm{
Name: "fn*",
Parse: fnParser(false),
}
// Macro defines an anonymous function and returns. Must have the form
// (macro* name? [arg*] expr*) or (fn* name? ([arg]* expr*)+)
Macro = SpecialForm{
Name: "macro*",
Parse: fnParser(true),
}
// Let implements the (let [binding*] expr*) form. expr are evaluated
// with given local bindings.
Let = SpecialForm{
Name: "let",
Parse: parseLet,
}
// Do special form evaluates args one by one and returns the result of
// the last expr.
Do = SpecialForm{
Name: "do",
Parse: parseDo,
}
// If implements if-conditional flow using (if test then else?) form.
If = SpecialForm{
Name: "if",
Parse: parseIf,
}
// SimpleQuote prevents a form from being evaluated.
SimpleQuote = SpecialForm{
Name: "quote",
Parse: parseSimpleQuote,
}
// SyntaxQuote recursively applies the quoting to the form.
SyntaxQuote = SpecialForm{
Name: "syntax-quote",
Parse: parseSyntaxQuote,
}
Recur = SpecialForm{
Name: "recur",
Parse: parseRecur,
}
)
func fnParser(isMacro bool) func(scope Scope, forms []Value) (*Fn, error) {
return func(scope Scope, forms []Value) (*Fn, error) {
if len(forms) < 1 {
return nil, fmt.Errorf("insufficient args (%d) for 'fn'", len(forms))
}
nextIndex := 0
def := MultiFn{
IsMacro: isMacro,
}
name, isName := forms[nextIndex].(Symbol)
if isName {
def.Name = name.String()
nextIndex++
}
return &Fn{
Func: func(_ Scope, args []Value) (Value, error) {
_, isList := forms[nextIndex].(*List)
if isList {
for _, arg := range forms[nextIndex:] {
spec, isList := arg.(*List)
if !isList {
return nil, fmt.Errorf("expected arg to be list, not %s",
reflect.TypeOf(arg))
}
fn, err := makeFn(scope, spec.Values)
if err != nil {
return nil, err
}
def.Methods = append(def.Methods, *fn)
}
} else {
fn, err := makeFn(scope, forms[nextIndex:])
if err != nil {
return nil, err
}
def.Methods = append(def.Methods, *fn)
}
return def, def.validate()
},
}, nil
}
}
func parseLet(scope Scope, args []Value) (*Fn, error) {
if len(args) < 1 {
return nil, fmt.Errorf("call requires at-least bindings argument")
}
vec, isVector := args[0].(Vector)
if !isVector {
return nil, fmt.Errorf(
"first argument to let must be bindings vector, not %v",
reflect.TypeOf(args[0]),
)
}
if len(vec.Values)%2 != 0 {
return nil, fmt.Errorf("bindings must contain event forms")
}
var bindings []binding
for i := 0; i < len(vec.Values); i += 2 {
sym, isSymbol := vec.Values[i].(Symbol)
if !isSymbol {
return nil, fmt.Errorf(
"item at %d must be symbol, not %s",
i, vec.Values[i],
)
}
bindings = append(bindings, binding{
Name: sym.Value,
Expr: vec.Values[i+1],
})
}
return &Fn{
Func: func(scope Scope, _ []Value) (Value, error) {
letScope := NewScope(scope)
for _, b := range bindings {
v, err := b.Expr.Eval(letScope)
if err != nil {
return nil, err
}
_ = letScope.Bind(b.Name, v)
}
return Module(args[1:]).Eval(letScope)
},
}, nil
}
func parseDo(scope Scope, args []Value) (*Fn, error) {
return &Fn{
Func: func(scope Scope, args []Value) (Value, error) {
if len(args) == 0 {
return Nil{}, nil
}
results, err := evalValueList(scope, args)
if err != nil {
return nil, err
}
return results[len(results)-1], err
},
}, nil
}
func parseDef(scope Scope, forms []Value) (*Fn, error) {
if err := verifyArgCount([]int{2}, forms); err != nil {
return nil, err
}
if err := analyze(scope, forms[1]); err != nil {
return nil, err
}
return &Fn{
Func: func(scope Scope, args []Value) (Value, error) {
sym, isSymbol := args[0].(Symbol)
if !isSymbol {
return nil, fmt.Errorf("first argument must be symbol, not '%v'",
reflect.TypeOf(args[0]))
}
v, err := args[1].Eval(scope)
if err != nil {
return nil, err
}
if err := rootScope(scope).Bind(sym.String(), v); err != nil {
return nil, err
}
return sym, nil
},
}, nil
}
func parseIf(scope Scope, args []Value) (*Fn, error) {
if err := verifyArgCount([]int{2, 3}, args); err != nil {
return nil, err
}
if err := analyzeSeq(scope, Values(args)); err != nil {
return nil, err
}
return &Fn{
Func: func(scope Scope, args []Value) (Value, error) {
test, err := args[0].Eval(scope)
if err != nil {
return nil, err
}
if !isTruthy(test) {
// handle 'else' flow.
if len(args) == 2 {
return Nil{}, nil
}
return args[2].Eval(scope)
}
// handle 'if true' flow.
return args[1].Eval(scope)
},
}, nil
}
func parseSimpleQuote(scope Scope, forms []Value) (*Fn, error) {
return &Fn{
Func: func(scope Scope, _ []Value) (Value, error) {
return forms[0], verifyArgCount([]int{1}, forms)
},
}, nil
}
func parseSyntaxQuote(scope Scope, forms []Value) (*Fn, error) {
if err := verifyArgCount([]int{1}, forms); err != nil {
return nil, err
}
if err := analyzeSeq(scope, Values(forms)); err != nil {
return nil, err
}
return &Fn{
Func: func(scope Scope, _ []Value) (Value, error) {
return recursiveQuote(scope, forms[0])
},
}, nil
}
func parseRecur(scope Scope, forms []Value) (*Fn, error) {
return &Fn{
Func: func(scope Scope, args []Value) (Value, error) {
symbol := Symbol{
Value: "recur",
}
results, err := evalValueList(scope, args)
if err != nil {
return nil, err
}
results = append([]Value{symbol}, results...)
return &List{Values: results}, nil
},
}, nil
}
// SpecialForm is a Value type for representing special forms that will be
// subjected to an intermediate Parsing stage before evaluation.
type SpecialForm struct {
Name string
Parse func(scope Scope, args []Value) (*Fn, error)
}
// Eval always returns error since it is not allowed to directly evaluate
// a special form.
func (sf SpecialForm) Eval(_ Scope) (Value, error) {
return nil, errors.New("can't take value of special form")
}
func (sf SpecialForm) String() string {
return fmt.Sprintf("SpecialForm{name=%s}", sf.Name)
}
func analyze(scope Scope, form Value) error {
switch f := form.(type) {
case Module:
for _, expr := range f {
if err := analyze(scope, expr); err != nil {
return err
}
}
case *List:
return f.parse(scope)
case String:
return nil
case Seq:
return analyzeSeq(scope, f)
}
return nil
}
func analyzeSeq(scope Scope, seq Seq) error {
for seq != nil {
f := seq.First()
if f == nil {
break
}
if err := analyze(scope, f); err != nil {
return err
}
seq = seq.Next()
}
return nil
}
func recursiveQuote(scope Scope, f Value) (Value, error) {
switch v := f.(type) {
case *List:
if isUnquote(v.Values) {
if err := verifyArgCount([]int{1}, v.Values[1:]); err != nil {
return nil, err
}
return v.Values[1].Eval(scope)
}
quoted, err := quoteSeq(scope, v.Values)
return &List{Values: quoted}, err
case Set:
quoted, err := quoteSeq(scope, v.Values)
return Set{Values: quoted}, err
case Vector:
quoted, err := quoteSeq(scope, v.Values)
return Vector{Values: quoted}, err
case String:
return f, nil
case Seq:
return quoteSeq(scope, v)
default:
return f, nil
}
}
func isUnquote(list []Value) bool {
if len(list) == 0 {
return false
}
sym, isSymbol := list[0].(Symbol)
if !isSymbol {
return false
}
return sym.Value == "unquote"
}
func quoteSeq(scope Scope, seq Seq) (Values, error) {
var quoted []Value
for seq != nil {
f := seq.First()
if f == nil {
break
}
q, err := recursiveQuote(scope, f)
if err != nil {
return nil, err
}
quoted = append(quoted, q)
seq = seq.Next()
}
return quoted, nil
}
func verifyArgCount(arities []int, args []Value) error {
actual := len(args)
sort.Ints(arities)
if len(arities) == 0 && actual != 0 {
return fmt.Errorf("call requires no arguments, got %d", actual)
}
L := len(arities)
switch {
case L == 1 && actual != arities[0]:
return fmt.Errorf("call requires exactly %d argument(s), got %d", arities[0], actual)
case L == 2:
c1, c2 := arities[0], arities[1]
if actual != c1 && actual != c2 {
return fmt.Errorf("call requires %d or %d argument(s), got %d", c1, c2, actual)
}
case L > 2:
return fmt.Errorf("wrong number of arguments (%d) passed", actual)
}
return nil
}
func rootScope(scope Scope) Scope {
if scope == nil {
return nil
}
p := scope
for temp := scope; temp != nil; temp = temp.Parent() {
p = temp
}
return p
}
func isTruthy(v Value) bool {
if v == (Nil{}) {
return false
}
if b, ok := v.(Bool); ok {
return bool(b)
}
return true
}
func makeFn(scope Scope, spec []Value) (*Fn, error) {
if len(spec) < 1 {
return nil, fmt.Errorf("insufficient args (%d) for 'fn'", len(spec))
}
body := Module(spec[1:])
if err := analyze(scope, body); err != nil {
return nil, err
}
fn := &Fn{Body: body}
if err := fn.parseArgSpec(spec[0]); err != nil {
return nil, err
}
return fn, nil
}
type binding struct {
Name string
Expr Value
}
func accessMember(target reflect.Value, member string) (reflect.Value, error) {
if member[0] >= 'a' && member[0] <= 'z' {
return reflect.Value{}, fmt.Errorf("cannot access private member")
}
if _, found := target.Type().MethodByName(member); found {
return target.MethodByName(member), nil
}
if target.Kind() == reflect.Ptr {
target = target.Elem()
}
if _, found := target.Type().FieldByName(member); found {
return target.FieldByName(member), nil
}
return reflect.Value{}, fmt.Errorf("value of type '%s' has no member named '%s'",
target.Type(), member)
}