-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.go
606 lines (570 loc) · 14.8 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
599
600
601
602
603
604
605
606
/*
Copyright 2015 Lee Boynton
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ell
import (
"fmt"
. "github.com/boynton/ell/data"
)
var MacroErrorKey = Intern("macro-error:")
type macro struct {
name Value
expander *Function //a function of one argument
}
// Macro - create a new Macro
func NewMacro(name Value, expander *Function) *macro {
return ¯o{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 Macroexpand(expr Value) (Value, error) {
return macroexpandObject(expr)
}
func macroexpandObject(expr Value) (Value, error) {
if lst, ok := expr.(*List); ok {
if lst != EmptyList {
return macroexpandList(lst)
}
}
return expr, nil
}
func macroexpandList(expr *List) (Value, error) {
if expr == nil {
panic("whoops")
}
if expr == EmptyList {
return expr, nil
}
lst := expr
fn := Car(lst)
head := fn
if IsSymbol(fn) {
result, err := expandPrimitive(fn, lst)
if err != nil {
return nil, err
}
if result != nil {
return result, nil
}
head = fn
} else if lst, ok := fn.(*List); ok {
//panic("non-primitive macro")
expanded, err := macroexpandList(lst)
if err != nil {
return nil, err
}
head = expanded
}
tail, err := expandSequence(Cdr(expr))
if err != nil {
return nil, err
}
return Cons(head, tail), nil
}
func (mac *macro) expand(expr Value) (Value, error) {
if mac.expander.code != nil {
if mac.expander.code.argc == 1 {
expanded, err := execCompileTime(mac.expander.code, expr)
if err == nil {
if IsList(expanded) {
return macroexpandObject(expanded)
}
return expanded, err
}
return nil, err
}
} else if mac.expander.primitive != nil {
args := []Value{expr}
expanded, err := mac.expander.primitive.fun(args)
if err == nil {
return macroexpandObject(expanded)
}
return nil, err
}
return nil, NewError(MacroErrorKey, "Bad macro expander function: ", mac.expander)
}
func expandSequence(seq Value) (*List, error) {
var result []Value
if seq == nil {
panic("Whoops: should be (), not nil!")
}
for seq != EmptyList {
item := Car(seq)
if lst, ok := item.(*List); ok {
expanded, err := macroexpandList(lst)
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 expandIf(expr Value) (Value, error) {
i := ListLength(expr)
if i == 4 {
tmp, err := expandSequence(Cdr(expr))
if err != nil {
return nil, err
}
return Cons(Car(expr), tmp), nil
} else if i == 3 {
tmp := NewList(Cadr(expr), Caddr(expr), Null)
tmp, err := expandSequence(tmp)
if err != nil {
return nil, err
}
return Cons(Car(expr), tmp), nil
} else {
return nil, NewError(SyntaxErrorKey, expr)
}
}
func expandUndef(expr Value) (Value, error) {
if ListLength(expr) != 2 || !IsSymbol(Cadr(expr)) {
return nil, NewError(SyntaxErrorKey, expr)
}
return expr, nil
}
// (defn f (x) (+ 1 x))
//
// ->
//
// (def f (fn (x) (+ 1 x)))
func expandDefn(expr Value) (Value, error) {
exprLen := ListLength(expr)
if exprLen >= 4 {
name := Cadr(expr)
if IsSymbol(name) {
args := Caddr(expr)
body, err := expandSequence(Cdddr(expr))
if err != nil {
return nil, err
}
tmp, err := expandFn(Cons(Intern("fn"), Cons(args, body)))
if err != nil {
return nil, err
}
return NewList(Intern("def"), name, tmp), nil
}
}
return nil, NewError(SyntaxErrorKey, expr)
}
func expandDefmacro(expr Value) (Value, error) {
exprLen := ListLength(expr)
if exprLen >= 4 {
name := Cadr(expr)
if IsSymbol(name) {
args := Caddr(expr)
body, err := expandSequence(Cdddr(expr))
if err != nil {
return nil, err
}
//(fn (expr) (apply xxx
tmp, err := expandFn(Cons(Intern("fn"), Cons(args, body))) //this is the expander with special args\
if err != nil {
return nil, err
}
sym := Intern("expr")
tmp, err = expandFn(NewList(Intern("fn"), NewList(sym), NewList(Intern("apply"), tmp, NewList(Intern("cdr"), sym))))
if err != nil {
return nil, err
}
return NewList(Intern("defmacro"), name, tmp), nil
}
}
return nil, NewError(SyntaxErrorKey, expr)
}
//(defmacro (defmacro expr)
// `(defmacro ~(cadr expr) (fn (expr) (apply (fn ~(caddr expr) ~@(cdddr expr)) (cdr expr)))))
func expandDef(expr Value) (Value, error) {
exprLen := ListLength(expr)
if exprLen != 3 {
return nil, NewError(SyntaxErrorKey, expr)
}
name := Cadr(expr)
if !IsSymbol(name) {
return nil, NewError(SyntaxErrorKey, expr)
}
if exprLen > 3 {
return nil, NewError(SyntaxErrorKey, expr)
}
body := Caddr(expr)
if lst, ok := body.(*List); ok {
val, err := macroexpandList(lst)
if err != nil {
return nil, err
}
return NewList(Car(expr), name, val), nil
}
return expr, nil
}
func expandFn(expr Value) (Value, error) {
exprLen := ListLength(expr)
if exprLen < 3 {
return nil, NewError(SyntaxErrorKey, expr)
}
body, err := expandSequence(Cddr(expr))
if err != nil {
return nil, err
}
bodyLen := ListLength(body)
if bodyLen > 0 {
tmp := body
if IsList(tmp) && Caar(tmp) == Intern("def") || Caar(tmp) == Intern("defmacro") {
bindings := EmptyList
for Caar(tmp) == Intern("def") || Caar(tmp) == Intern("defmacro") {
if Caar(tmp) == Intern("defmacro") {
return nil, NewError(MacroErrorKey, "macros can only be defined at top level")
}
def, err := expandDef(Car(tmp))
if err != nil {
return nil, err
}
bindings = Cons(Cdr(def), bindings)
tmp = Cdr(tmp)
}
bindings = Reverse(bindings)
tmp = Cons(Intern("letrec"), Cons(bindings, tmp)) //scheme specifies letrec*
tmp2, err := macroexpandList(tmp)
return NewList(Car(expr), Cadr(expr), tmp2), err
}
}
args := Cadr(expr)
return Cons(Car(expr), Cons(args, body)), nil
}
func expandSetBang(expr Value) (Value, error) {
exprLen := ListLength(expr)
if exprLen != 3 {
return nil, NewError(SyntaxErrorKey, expr)
}
var val = Caddr(expr)
if lst, ok := val.(*List); ok {
v, err := macroexpandList(lst)
if err != nil {
return nil, err
}
val = v
}
return NewList(Car(expr), Cadr(expr), val), nil
}
func expandPrimitive(fn Value, expr Value) (Value, error) {
switch fn {
case Intern("quote"):
return expr, nil
case Intern("do"):
return expandSequence(expr)
case Intern("if"):
return expandIf(expr)
case Intern("def"):
return expandDef(expr)
case Intern("undef"):
return expandUndef(expr)
case Intern("defn"):
return expandDefn(expr)
case Intern("defmacro"):
return expandDefmacro(expr)
case Intern("fn"):
return expandFn(expr)
case Intern("set!"):
return expandSetBang(expr)
case Intern("lap"):
return expr, nil
case Intern("use"):
return expr, nil
default:
macro := GetMacro(fn)
if macro != nil {
tmp, err := macro.expand(expr)
return tmp, err
}
return nil, nil
}
}
func crackLetrecBindings(bindings Value, tail *List) (*List, *List, bool) {
var names []Value
inits := EmptyList
for bindings != EmptyList {
if IsList(bindings) {
tmp := Car(bindings)
if lst, ok := tmp.(*List); ok {
name := lst.Car
if IsSymbol(name) {
names = append(names, name)
} else {
return nil, nil, false
}
if IsList(lst.Cdr) {
inits = Cons(Cons(Intern("set!"), lst), 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 expandLetrec(expr Value) (Value, error) {
// (letrec () expr ...) -> (do expr ...)
// (letrec ((x 1) (y 2)) expr ...) -> ((fn (x y) (set! x 1) (set! y 2) expr ...) nil nil)
body := Cddr(expr)
if body == EmptyList {
return nil, NewError(SyntaxErrorKey, expr)
}
bindings := Cadr(expr)
if !IsList(bindings) {
return nil, NewError(SyntaxErrorKey, expr)
}
names, body, ok := crackLetrecBindings(bindings, body)
if !ok {
return nil, NewError(SyntaxErrorKey, expr)
}
code, err := macroexpandList(Cons(Intern("fn"), Cons(names, body)))
if err != nil {
return nil, err
}
values := MakeList(ListLength(names), Null)
return Cons(code, values), nil
}
func crackLetBindings(bindings Value) (*List, *List, bool) {
var names []Value
var values []Value
for bindings != EmptyList {
tmp := Car(bindings)
if IsList(tmp) {
name := Car(tmp)
if IsSymbol(name) {
names = append(names, name)
tmp2 := Cdr(tmp)
if tmp2 != EmptyList {
val, err := 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 expandLet(expr Value) (Value, 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 ell_expand_named_let(argv, argc)
return expandNamedLet(expr)
}
bindings := Cadr(expr)
if !IsList(bindings) {
return nil, NewError(SyntaxErrorKey, expr)
}
names, values, ok := crackLetBindings(bindings)
if !ok {
return nil, NewError(SyntaxErrorKey, expr)
}
body := Cddr(expr)
if body == EmptyList {
return nil, NewError(SyntaxErrorKey, expr)
}
code, err := macroexpandList(Cons(Intern("fn"), Cons(names, body)))
if err != nil {
return nil, err
}
return Cons(code, values), nil
}
func expandNamedLet(expr Value) (Value, error) {
name := Cadr(expr)
bindings := Caddr(expr)
if !IsList(bindings) {
return nil, NewError(SyntaxErrorKey, expr)
}
names, values, ok := crackLetBindings(bindings)
if !ok {
return nil, NewError(SyntaxErrorKey, expr)
}
body := Cdddr(expr)
tmp := NewList(Intern("letrec"), NewList(NewList(name, Cons(Intern("fn"), Cons(names, body)))), Cons(name, values))
return macroexpandList(tmp)
}
func nextCondClause(expr Value, clauses Value, count int) (Value, error) {
var result Value
var err error
tmpsym := Intern("__tmp__")
ifsym := Intern("if")
elsesym := Intern("else")
letsym := Intern("let")
dosym := Intern("do")
clause0 := Car(clauses)
next := Cdr(clauses)
clause1 := Car(next)
if count == 2 {
if !IsList(clause1) {
return nil, NewError(SyntaxErrorKey, expr)
}
if elsesym == Car(clause1) {
if Cadr(clause0) == Intern("=>") {
if ListLength(clause0) != 3 {
return nil, NewError(SyntaxErrorKey, expr)
}
result = NewList(letsym, NewList(NewList(tmpsym, Car(clause0))), NewList(ifsym, tmpsym, NewList(Caddr(clause0), tmpsym), Cons(dosym, Cdr(clause1))))
} else {
result = NewList(ifsym, Car(clause0), Cons(dosym, Cdr(clause0)), Cons(dosym, Cdr(clause1)))
}
} else {
if Cadr(clause1) == Intern("=>") {
if ListLength(clause1) != 3 {
return nil, NewError(SyntaxErrorKey, expr)
}
result = NewList(letsym, NewList(NewList(tmpsym, Car(clause1))), NewList(ifsym, tmpsym, NewList(Caddr(clause1), tmpsym), clause1))
} else {
result = NewList(ifsym, Car(clause1), Cons(dosym, Cdr(clause1)))
}
if Cadr(clause0) == Intern("=>") {
if ListLength(clause0) != 3 {
return nil, NewError(SyntaxErrorKey, expr)
}
result = NewList(letsym, NewList(NewList(tmpsym, Car(clause0))), NewList(ifsym, tmpsym, NewList(Caddr(clause0), tmpsym), result))
} else {
result = NewList(ifsym, Car(clause0), Cons(dosym, Cdr(clause0)), result)
}
}
} else {
result, err = nextCondClause(expr, next, count-1)
if err != nil {
return nil, err
}
if Cadr(clause0) == Intern("=>") {
if ListLength(clause0) != 3 {
return nil, NewError(SyntaxErrorKey, expr)
}
result = NewList(letsym, NewList(NewList(tmpsym, Car(clause0))), NewList(ifsym, tmpsym, NewList(Caddr(clause0), tmpsym), result))
} else {
result = NewList(ifsym, Car(clause0), Cons(dosym, Cdr(clause0)), result)
}
}
return macroexpandObject(result)
}
func expandCond(expr Value) (Value, error) {
i := ListLength(expr)
if i < 2 {
return nil, NewError(SyntaxErrorKey, expr)
} else if i == 2 {
tmp := Cadr(expr)
if Car(tmp) == Intern("else") {
tmp = Cons(Intern("do"), Cdr(tmp))
} else {
expr = Cons(Intern("do"), Cdr(tmp))
tmp = NewList(Intern("if"), Car(tmp), expr)
}
return macroexpandObject(tmp)
} else {
return nextCondClause(expr, Cdr(expr), i-1)
}
}
func expandQuasiquote(expr Value) (Value, error) {
if ListLength(expr) != 2 {
return nil, NewError(SyntaxErrorKey, expr)
}
return expandQQ(Cadr(expr))
}
func expandQQ(expr Value) (Value, error) {
switch p := expr.(type) {
case *List:
if p == EmptyList {
return expr, nil
}
if p.Cdr != EmptyList {
if p.Car == UnquoteSymbol {
if p.Cdr.Cdr != EmptyList {
return nil, NewError(SyntaxErrorKey, expr)
}
return macroexpandObject(p.Cdr.Car)
} else if p.Car == UnquoteSymbolSplicing {
return nil, NewError(MacroErrorKey, "unquote-splicing can only occur in the context of a list ")
}
}
tmp, err := expandQQList(p)
if err != nil {
return nil, err
}
return macroexpandObject(tmp)
case *Symbol:
return NewList(Intern("quote"), expr), nil
default: //all other objects evaluate to themselves
return expr, nil
}
}
func expandQQList(lst *List) (*List, error) {
var tmp Value
var err error
result := NewList(Intern("concat"))
tail := result
for lst != EmptyList {
if item, ok := Car(lst).(*List); ok && item != EmptyList {
if item.Car == QuasiquoteSymbol {
return nil, NewError(MacroErrorKey, "nested quasiquote not supported")
}
if item.Car == UnquoteSymbol && item.Length() == 2 {
tmp, err = macroexpandObject(Cadr(item))
tmp = NewList(Intern("list"), tmp)
if err != nil {
return nil, err
}
tail.Cdr = NewList(tmp)
tail = tail.Cdr
} else if item.Car == UnquoteSymbolSplicing && item.Length() == 2 {
tmp, err = macroexpandObject(Cadr(item))
if err != nil {
return nil, err
}
tail.Cdr = NewList(tmp)
tail = tail.Cdr
} else {
tmp, err = expandQQList(item)
if err != nil {
return nil, err
}
tail.Cdr = NewList(NewList(Intern("list"), tmp))
tail = tail.Cdr
}
} else {
tail.Cdr = NewList(NewList(Intern("quote"), NewList(Car(lst))))
tail = tail.Cdr
}
lst = lst.Cdr
}
return result, nil
}