-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.go
598 lines (567 loc) · 14.7 KB
/
macro.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package vesper
import (
"fmt"
)
// Macro a structure to hold Macro values
type Macro struct {
Name *Object
Expander *Object // a function of one argument
}
// NewMacro - create a new Macro
func NewMacro(name *Object, expander *Object) *Macro {
return &Macro{name, expander}
}
func (mac *Macro) String() string {
return fmt.Sprintf("(macro %v %v)", mac.Name, mac.Expander)
}
// Macroexpand - return the expansion of all macros in the object and return the result
func (vm *VM) Macroexpand(expr *Object) (*Object, error) {
return vm.macroexpandObject(expr)
}
func (vm *VM) macroexpandObject(expr *Object) (*Object, error) {
if IsList(expr) {
if expr != EmptyList {
return vm.macroexpandList(expr)
}
}
return expr, nil
}
func (vm *VM) macroexpandList(expr *Object) (*Object, error) {
if expr == EmptyList {
return expr, nil
}
lst := expr
fn := Car(lst)
head := fn
if IsSymbol(fn) {
result, err := vm.expandPrimitive(fn, lst)
if err != nil {
return nil, err
}
if result != nil {
return result, nil
}
head = fn
} else if IsList(fn) {
expanded, err := vm.macroexpandList(fn)
if err != nil {
return nil, err
}
head = expanded
}
tail, err := vm.expandSequence(Cdr(expr))
if err != nil {
return nil, err
}
return Cons(head, tail), nil
}
func (vm *VM) expand(mac *Macro, expr *Object) (*Object, error) {
expander := mac.Expander
if expander.Type == FunctionType {
if expander.code != nil {
if expander.code.argc == 1 {
expanded, err := vm.execCompileTime(expander.code, expr)
if err == nil {
if IsList(expanded) {
return vm.macroexpandObject(expanded)
}
return expanded, err
}
return nil, err
}
} else if expander.primitive != nil {
args := []*Object{expr}
expanded, err := expander.primitive.fun(args)
if err == nil {
return vm.macroexpandObject(expanded)
}
return nil, err
}
}
return nil, Error(MacroErrorKey, "Bad macro expander function: ", expander)
}
func (vm *VM) expandSequence(seq *Object) (*Object, error) {
var result []*Object
if seq == nil {
return EmptyList, Error(InternalErrorKey, "expandSequence: nil seq")
}
for seq != EmptyList {
item := Car(seq)
if IsList(item) {
expanded, err := vm.macroexpandList(item)
if err != nil {
return nil, err
}
result = append(result, expanded)
} else {
result = append(result, item)
}
seq = Cdr(seq)
}
lst := ListFromValues(result)
if seq != EmptyList {
tmp := Cons(seq, EmptyList)
return Concat(lst, tmp)
}
return lst, nil
}
func (vm *VM) expandIf(expr *Object) (*Object, error) {
i := ListLength(expr)
if i == 4 {
tmp, err := vm.expandSequence(Cdr(expr))
if err != nil {
return nil, err
}
return Cons(Car(expr), tmp), nil
} else if i == 3 {
tmp := List(Cadr(expr), Caddr(expr), Null)
tmp, err := vm.expandSequence(tmp)
if err != nil {
return nil, err
}
return Cons(Car(expr), tmp), nil
} else {
return nil, Error(SyntaxErrorKey, expr)
}
}
func expandUndef(expr *Object) (*Object, error) {
if ListLength(expr) != 2 || !IsSymbol(Cadr(expr)) {
return nil, Error(SyntaxErrorKey, expr)
}
return expr, nil
}
// (defn f (x) (+ 1 x))
// ->
// (def f (fn (x) (+ 1 x)))
func (vm *VM) expandDefn(expr *Object) (*Object, error) {
exprLen := ListLength(expr)
if exprLen >= 4 {
name := Cadr(expr)
if IsSymbol(name) {
args := Caddr(expr)
body, err := vm.expandSequence(Cdddr(expr))
if err != nil {
return nil, err
}
tmp, err := vm.expandFn(Cons(vm.Intern("fn"), Cons(args, body)))
if err != nil {
return nil, err
}
return List(vm.Intern("def"), name, tmp), nil
}
}
return nil, Error(SyntaxErrorKey, expr)
}
func (vm *VM) expandDefmacro(expr *Object) (*Object, error) {
exprLen := ListLength(expr)
if exprLen >= 4 {
name := Cadr(expr)
if IsSymbol(name) {
args := Caddr(expr)
body, err := vm.expandSequence(Cdddr(expr))
if err != nil {
return nil, err
}
//(fn (expr) (apply xxx
tmp, err := vm.expandFn(Cons(vm.Intern("fn"), Cons(args, body))) //this is the expander with special args\
if err != nil {
return nil, err
}
sym := vm.Intern("expr")
tmp, err = vm.expandFn(List(vm.Intern("fn"), List(sym), List(vm.Intern("apply"), tmp, List(vm.Intern("cdr"), sym))))
if err != nil {
return nil, err
}
return List(vm.Intern("defmacro"), name, tmp), nil
}
}
return nil, Error(SyntaxErrorKey, expr)
}
//(defmacro (defmacro expr)
// `(defmacro ~(cadr expr) (fn (expr) (apply (fn ~(caddr expr) ~@(cdddr expr)) (cdr expr)))))
func (vm *VM) expandDef(expr *Object) (*Object, error) {
exprLen := ListLength(expr)
if exprLen != 3 {
return nil, Error(SyntaxErrorKey, expr)
}
name := Cadr(expr)
if !IsSymbol(name) {
return nil, Error(SyntaxErrorKey, expr)
}
if exprLen > 3 {
return nil, Error(SyntaxErrorKey, expr)
}
body := Caddr(expr)
if !IsList(body) {
return expr, nil
}
val, err := vm.macroexpandList(body)
if err != nil {
return nil, err
}
return List(Car(expr), name, val), nil
}
func (vm *VM) expandFn(expr *Object) (*Object, error) {
exprLen := ListLength(expr)
if exprLen < 3 {
return nil, Error(SyntaxErrorKey, expr)
}
body, err := vm.expandSequence(Cddr(expr))
if err != nil {
return nil, err
}
bodyLen := ListLength(body)
if bodyLen > 0 {
tmp := body
if IsList(tmp) && Caar(tmp) == vm.Intern("def") || Caar(tmp) == vm.Intern("defmacro") {
bindings := EmptyList
for Caar(tmp) == vm.Intern("def") || Caar(tmp) == vm.Intern("defmacro") {
if Caar(tmp) == vm.Intern("defmacro") {
return nil, Error(MacroErrorKey, "macros can only be defined at top level")
}
def, err := vm.expandDef(Car(tmp))
if err != nil {
return nil, err
}
bindings = Cons(Cdr(def), bindings)
tmp = Cdr(tmp)
}
bindings = Reverse(bindings)
tmp = Cons(vm.Intern("letrec"), Cons(bindings, tmp)) //scheme specifies letrec*
tmp2, err := vm.macroexpandList(tmp)
return List(Car(expr), Cadr(expr), tmp2), err
}
}
args := Cadr(expr)
return Cons(Car(expr), Cons(args, body)), nil
}
func (vm *VM) expandSetBang(expr *Object) (*Object, error) {
exprLen := ListLength(expr)
if exprLen != 3 {
return nil, Error(SyntaxErrorKey, expr)
}
var val = Caddr(expr)
if IsList(val) {
v, err := vm.macroexpandList(val)
if err != nil {
return nil, err
}
val = v
}
return List(Car(expr), Cadr(expr), val), nil
}
func (vm *VM) expandPrimitive(fn *Object, expr *Object) (*Object, error) {
switch fn {
case vm.Intern("quote"):
return expr, nil
case vm.Intern("do"):
return vm.expandSequence(expr)
case vm.Intern("if"):
return vm.expandIf(expr)
case vm.Intern("def"):
return vm.expandDef(expr)
case vm.Intern("undef"):
return expandUndef(expr)
case vm.Intern("defn"):
return vm.expandDefn(expr)
case vm.Intern("defmacro"):
return vm.expandDefmacro(expr)
case vm.Intern("fn"):
return vm.expandFn(expr)
case vm.Intern("set!"):
return vm.expandSetBang(expr)
case vm.Intern("lap"):
return expr, nil
case vm.Intern("use"):
return expr, nil
default:
macro := vm.GetMacro(fn)
if macro != nil {
tmp, err := vm.expand(macro, expr)
return tmp, err
}
return nil, nil
}
}
func (vm *VM) crackLetrecBindings(bindings *Object, tail *Object) (*Object, *Object, bool) {
var names []*Object
inits := EmptyList
for bindings != EmptyList {
if IsList(bindings) {
tmp := Car(bindings)
if IsArray(tmp) {
tmp, _ = ToList(tmp)
}
if IsList(tmp) {
name := Car(tmp)
if IsSymbol(name) {
names = append(names, name)
} else {
return nil, nil, false
}
if IsList(Cdr(tmp)) {
inits = Cons(Cons(vm.Intern("set!"), tmp), inits)
} else {
return nil, nil, false
}
} else {
return nil, nil, false
}
} else {
return nil, nil, false
}
bindings = Cdr(bindings)
}
inits = Reverse(inits)
head := inits
for inits.cdr != EmptyList {
inits = inits.cdr
}
inits.cdr = tail
return ListFromValues(names), head, true
}
func (vm *VM) expandLetrec(expr *Object) (*Object, error) {
body := Cddr(expr)
if body == EmptyList {
return nil, Error(SyntaxErrorKey, expr)
}
bindings := Cadr(expr)
if IsArray(bindings) {
bindings, _ = ToList(bindings)
}
if !IsList(bindings) {
return nil, Error(SyntaxErrorKey, expr)
}
names, body, ok := vm.crackLetrecBindings(bindings, body)
if !ok {
return nil, Error(SyntaxErrorKey, expr)
}
code, err := vm.macroexpandList(Cons(vm.Intern("fn"), Cons(names, body)))
if err != nil {
return nil, err
}
values := MakeList(ListLength(names), Null)
return Cons(code, values), nil
}
func (vm *VM) crackLetBindings(bindings *Object) (*Object, *Object, bool) {
var names []*Object
var values []*Object
for bindings != EmptyList {
tmp := Car(bindings)
if IsArray(tmp) {
tmp, _ = ToList(tmp)
}
if IsList(tmp) {
name := Car(tmp)
if IsSymbol(name) {
names = append(names, name)
tmp2 := Cdr(tmp)
if tmp2 != EmptyList {
val, err := vm.macroexpandObject(Car(tmp2))
if err == nil {
values = append(values, val)
bindings = Cdr(bindings)
continue
}
}
}
}
return nil, nil, false
}
return ListFromValues(names), ListFromValues(values), true
}
func (vm *VM) expandLet(expr *Object) (*Object, error) {
// (let () expr ...) -> (do expr ...)
// (let ((x 1) (y 2)) expr ...) -> ((fn (x y) expr ...) 1 2)
// (let label ((x 1) (y 2)) expr ...) -> (fn (label) expr
if IsSymbol(Cadr(expr)) {
return vm.expandNamedLet(expr)
}
bindings := Cadr(expr)
if IsArray(bindings) {
bindings, _ = ToList(bindings)
}
if !IsList(bindings) {
return nil, Error(SyntaxErrorKey, expr)
}
names, values, ok := vm.crackLetBindings(bindings)
if !ok {
return nil, Error(SyntaxErrorKey, expr)
}
body := Cddr(expr)
if body == EmptyList {
return nil, Error(SyntaxErrorKey, expr)
}
code, err := vm.macroexpandList(Cons(vm.Intern("fn"), Cons(names, body)))
if err != nil {
return nil, err
}
return Cons(code, values), nil
}
func (vm *VM) expandNamedLet(expr *Object) (*Object, error) {
name := Cadr(expr)
bindings := Caddr(expr)
if IsArray(bindings) {
bindings, _ = ToList(bindings)
}
if !IsList(bindings) {
return nil, Error(SyntaxErrorKey, expr)
}
names, values, ok := vm.crackLetBindings(bindings)
if !ok {
return nil, Error(SyntaxErrorKey, expr)
}
body := Cdddr(expr)
tmp := List(vm.Intern("letrec"), List(List(name, Cons(vm.Intern("fn"), Cons(names, body)))), Cons(name, values))
return vm.macroexpandList(tmp)
}
func (vm *VM) nextCondClause(expr *Object, clauses *Object, count int) (*Object, error) {
var result *Object
var err error
tmpsym := vm.Intern("__tmp__")
ifsym := vm.Intern("if")
elsesym := vm.Intern("else")
letsym := vm.Intern("let")
dosym := vm.Intern("do")
clause0 := Car(clauses)
next := Cdr(clauses)
clause1 := Car(next)
if count == 2 {
if !IsList(clause1) {
return nil, Error(SyntaxErrorKey, expr)
}
if elsesym == Car(clause1) {
if Cadr(clause0) == vm.Intern("=>") {
if ListLength(clause0) != 3 {
return nil, Error(SyntaxErrorKey, expr)
}
result = List(letsym, List(List(tmpsym, Car(clause0))), List(ifsym, tmpsym, List(Caddr(clause0), tmpsym), Cons(dosym, Cdr(clause1))))
} else {
result = List(ifsym, Car(clause0), Cons(dosym, Cdr(clause0)), Cons(dosym, Cdr(clause1)))
}
} else {
if Cadr(clause1) == vm.Intern("=>") {
if ListLength(clause1) != 3 {
return nil, Error(SyntaxErrorKey, expr)
}
result = List(letsym, List(List(tmpsym, Car(clause1))), List(ifsym, tmpsym, List(Caddr(clause1), tmpsym), clause1))
} else {
result = List(ifsym, Car(clause1), Cons(dosym, Cdr(clause1)))
}
if Cadr(clause0) == vm.Intern("=>") {
if ListLength(clause0) != 3 {
return nil, Error(SyntaxErrorKey, expr)
}
result = List(letsym, List(List(tmpsym, Car(clause0))), List(ifsym, tmpsym, List(Caddr(clause0), tmpsym), result))
} else {
result = List(ifsym, Car(clause0), Cons(dosym, Cdr(clause0)), result)
}
}
} else {
result, err = vm.nextCondClause(expr, next, count-1)
if err != nil {
return nil, err
}
if Cadr(clause0) == vm.Intern("=>") {
if ListLength(clause0) != 3 {
return nil, Error(SyntaxErrorKey, expr)
}
result = List(letsym, List(List(tmpsym, Car(clause0))), List(ifsym, tmpsym, List(Caddr(clause0), tmpsym), result))
} else {
result = List(ifsym, Car(clause0), Cons(dosym, Cdr(clause0)), result)
}
}
return vm.macroexpandObject(result)
}
func (vm *VM) expandCond(expr *Object) (*Object, error) {
i := ListLength(expr)
if i < 2 {
return nil, Error(SyntaxErrorKey, expr)
} else if i == 2 {
tmp := Cadr(expr)
if Car(tmp) == vm.Intern("else") {
tmp = Cons(vm.Intern("do"), Cdr(tmp))
} else {
expr = Cons(vm.Intern("do"), Cdr(tmp))
tmp = List(vm.Intern("if"), Car(tmp), expr)
}
return vm.macroexpandObject(tmp)
} else {
return vm.nextCondClause(expr, Cdr(expr), i-1)
}
}
func (vm *VM) expandQuasiquote(expr *Object) (*Object, error) {
if ListLength(expr) != 2 {
return nil, Error(SyntaxErrorKey, expr)
}
return vm.expandQQ(Cadr(expr))
}
func (vm *VM) expandQQ(expr *Object) (*Object, error) {
switch expr.Type {
case ListType:
if expr == EmptyList {
return expr, nil
}
if expr.cdr != EmptyList {
if expr.car == UnquoteSymbol {
if expr.cdr.cdr != EmptyList {
return nil, Error(SyntaxErrorKey, expr)
}
return vm.macroexpandObject(expr.cdr.car)
} else if expr.car == UnquoteSplicingSymbol {
return nil, Error(MacroErrorKey, "unquote-splicing can only occur in the context of a list ")
}
}
tmp, err := vm.expandQQList(expr)
if err != nil {
return nil, err
}
return vm.macroexpandObject(tmp)
case SymbolType:
return List(vm.Intern("quote"), expr), nil
default: // all other objects evaluate to themselves
return expr, nil
}
}
func (vm *VM) expandQQList(lst *Object) (*Object, error) {
var tmp *Object
var err error
result := List(vm.Intern("concat"))
tail := result
for lst != EmptyList {
item := Car(lst)
if IsList(item) && item != EmptyList {
if Car(item) == QuasiquoteSymbol {
return nil, Error(MacroErrorKey, "nested quasiquote not supported")
}
if Car(item) == UnquoteSymbol && ListLength(item) == 2 {
tmp, err = vm.macroexpandObject(Cadr(item))
tmp = List(vm.Intern("list"), tmp)
if err != nil {
return nil, err
}
tail.cdr = List(tmp)
tail = tail.cdr
} else if Car(item) == UnquoteSplicingSymbol && ListLength(item) == 2 {
tmp, err = vm.macroexpandObject(Cadr(item))
if err != nil {
return nil, err
}
tail.cdr = List(tmp)
tail = tail.cdr
} else {
tmp, err = vm.expandQQList(item)
if err != nil {
return nil, err
}
tail.cdr = List(List(vm.Intern("list"), tmp))
tail = tail.cdr
}
} else {
tail.cdr = List(List(vm.Intern("quote"), List(item)))
tail = tail.cdr
}
lst = Cdr(lst)
}
return result, nil
}