-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.go
More file actions
executable file
·1204 lines (986 loc) · 29 KB
/
Copy pathexpression.go
File metadata and controls
executable file
·1204 lines (986 loc) · 29 KB
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package glerp
import (
"fmt"
"strconv"
"strings"
)
// Expr is any scheme value or expression that can be evaluated.
type Expr interface {
Eval(env *Environment) (Expr, error)
Token() Token
String() string
}
// joinExprs formats a slice of expressions into a single string with the
// given separator, used by ListExpr, VectorExpr, and ValuesExpr.
func joinExprs(exprs []Expr, sep string) string {
parts := make([]string, len(exprs))
for i, e := range exprs {
parts[i] = e.String()
}
return strings.Join(parts, sep)
}
// isFalse reports whether e is the Scheme false value (#f).
// In Scheme, only #f is falsy; everything else (including 0, "", and '()) is truthy.
func isFalse(e Expr) bool {
b, ok := e.(*BoolExpr)
return ok && !b.val
}
// NumberExpr is a numeric literal.
type NumberExpr struct {
tok Token
val float64
}
func (e *NumberExpr) Eval(_ *Environment) (Expr, error) { return e, nil }
// Token returns the source token for this expression.
func (e *NumberExpr) Token() Token { return e.tok }
// Value returns the underlying float64 value.
func (e *NumberExpr) Value() float64 { return e.val }
func (e *NumberExpr) String() string {
if e.val == float64(int64(e.val)) {
return strconv.FormatInt(int64(e.val), 10)
}
return strconv.FormatFloat(e.val, 'f', -1, 64)
}
// StringExpr is a string literal.
type StringExpr struct {
tok Token
val string
}
func (e *StringExpr) Eval(_ *Environment) (Expr, error) { return e, nil }
// Token returns the source token for this expression.
func (e *StringExpr) Token() Token { return e.tok }
// Value returns the raw string contents, without surrounding quotes.
func (e *StringExpr) Value() string { return e.val }
// String returns the quoted representation, e.g. "hello".
func (e *StringExpr) String() string { return fmt.Sprintf("%q", e.val) }
// BoolExpr is a boolean value (#t or #f).
type BoolExpr struct {
tok Token
val bool
}
func (e *BoolExpr) Eval(_ *Environment) (Expr, error) { return e, nil }
// Token returns the source token for this expression.
func (e *BoolExpr) Token() Token { return e.tok }
// Value returns the underlying boolean. Only #f is false; everything else is truthy.
func (e *BoolExpr) Value() bool { return e.val }
func (e *BoolExpr) String() string {
if e.val {
return "#t"
}
return "#f"
}
// SymbolExpr is a symbol that resolves to a value via environment lookup.
type SymbolExpr struct {
tok Token
val string
}
func (e *SymbolExpr) Eval(env *Environment) (Expr, error) {
return env.Find(e.val)
}
// Token returns the source token for this expression.
func (e *SymbolExpr) Token() Token { return e.tok }
// String returns the symbol name.
func (e *SymbolExpr) String() string { return e.val }
// Pair is a traditional Scheme cons cell with a car and a cdr.
// Lists in glerp are represented as a chain of Pair objects, ending in a
// null ListExpr (the empty list).
type Pair struct {
tok Token
car Expr
cdr Expr
}
func (p *Pair) Eval(env *Environment) (Expr, error) {
proc, err := EvalFull(p.car, env)
if err != nil {
return nil, fmt.Errorf("in procedure position: %w", err)
}
if f, ok := proc.(*FormExpr); ok {
var slice []Expr
if pair, ok := p.cdr.(*Pair); ok {
var err error
slice, err = pair.ToSlice()
if err != nil {
return nil, err
}
}
return f.fn(slice, env)
}
if transformer, ok := proc.(*TransformerExpr); ok {
result, err := trampoline(apply(transformer.proc, []Expr{p}))
if err != nil {
return nil, err
}
return EvalFull(result, env)
}
if transformer, ok := proc.(*SyntaxRulesExpr); ok {
expanded, err := transformer.expand(p)
if err != nil {
return nil, err
}
return EvalFull(expanded, env)
}
var args []Expr
curr := p.cdr
for {
switch l := curr.(type) {
case *Pair:
val, err := EvalFull(l.car, env)
if err != nil {
return nil, err
}
args = append(args, val)
curr = l.cdr
case *ListExpr:
if len(l.elements) == 0 {
goto apply
}
return nil, fmt.Errorf("dotted list in procedure application")
default:
return nil, fmt.Errorf("dotted list in procedure application")
}
}
apply:
return apply(proc, args)
}
func (p *Pair) Token() Token { return p.tok }
func (p *Pair) String() string {
var sb strings.Builder
sb.WriteString("(")
curr := p
for {
sb.WriteString(curr.car.String())
switch next := curr.cdr.(type) {
case *Pair:
sb.WriteString(" ")
curr = next
case *ListExpr:
if len(next.elements) == 0 {
sb.WriteString(")")
return sb.String()
}
// Should not happen with proper list structure but handle anyway.
sb.WriteString(" . ")
sb.WriteString(next.String())
sb.WriteString(")")
return sb.String()
default:
sb.WriteString(" . ")
sb.WriteString(next.String())
sb.WriteString(")")
return sb.String()
}
}
}
func (p *Pair) Car() Expr { return p.car }
func (p *Pair) Cdr() Expr { return p.cdr }
func (p *Pair) SetCar(e Expr) { p.car = e }
func (p *Pair) SetCdr(e Expr) { p.cdr = e }
// ToSlice converts a proper list (chain of pairs ending in '()) to a slice.
// Returns an error if the list is improper (dotted).
func (p *Pair) ToSlice() ([]Expr, error) {
var res []Expr
var curr Expr = p
for {
switch l := curr.(type) {
case *Pair:
res = append(res, l.car)
curr = l.cdr
case *ListExpr:
if l == Null() {
return res, nil
}
return nil, fmt.Errorf("dotted list cannot be converted to slice")
default:
return nil, fmt.Errorf("dotted list cannot be converted to slice")
}
}
}
var nullSingleton = &ListExpr{}
// Null returns the empty list singleton.
func Null() *ListExpr {
return nullSingleton
}
// ListExpr is a parenthesized s-expression. Evaluation dispatches on the head:
// special forms are handled directly; otherwise it is a procedure application.
//
// In glerp, ListExpr now primarily serves as a bridge for special forms and
// as the representation for the empty list. Chains of pairs are converted to
// ListExpr during evaluation to reuse the slice-based dispatch logic.
type ListExpr struct {
tok Token
elements []Expr
}
// Token returns the source token for this expression.
func (e *ListExpr) Token() Token { return e.tok }
// Elements returns the expressions contained in this list.
func (e *ListExpr) Elements() []Expr { return e.elements }
func (e *ListExpr) String() string {
if len(e.elements) == 0 {
return "()"
}
return "(" + joinExprs(e.elements, " ") + ")"
}
func (e *ListExpr) Eval(env *Environment) (Expr, error) {
if len(e.elements) == 0 {
return e, nil
}
head := e.elements[0]
tail := e.elements[1:]
proc, err := EvalFull(head, env)
if err != nil {
return nil, fmt.Errorf("in procedure position: %w", err)
}
if f, ok := proc.(*FormExpr); ok {
return f.fn(tail, env)
}
if transformer, ok := proc.(*TransformerExpr); ok {
result, err := trampoline(apply(transformer.proc, []Expr{e}))
if err != nil {
return nil, err
}
return EvalFull(result, env)
}
if transformer, ok := proc.(*SyntaxRulesExpr); ok {
expanded, err := transformer.expand(e)
if err != nil {
return nil, err
}
return EvalFull(expanded, env)
}
args := make([]Expr, len(tail))
for i, arg := range tail {
args[i], err = EvalFull(arg, env)
if err != nil {
return nil, err
}
}
return apply(proc, args)
}
// VectorExpr is a fixed-length, mutable array of Scheme values.
// Vectors are self-evaluating (like numbers and strings). They are written
// as #(elem ...) and support O(1) access by index via vector-ref.
type VectorExpr struct {
tok Token
elements []Expr
}
func (e *VectorExpr) Eval(_ *Environment) (Expr, error) { return e, nil }
// Token returns the source token for this expression.
func (e *VectorExpr) Token() Token { return e.tok }
// Elements returns the expressions contained in this vector.
func (e *VectorExpr) Elements() []Expr { return e.elements }
// Length returns the number of elements in this vector.
func (e *VectorExpr) Length() int { return len(e.elements) }
func (e *VectorExpr) String() string {
return "#(" + joinExprs(e.elements, " ") + ")"
}
// HashTableExpr is a mutable hash table mapping Scheme values to Scheme values.
// Keys are compared by their String() representation. Written as {k1 v1 k2 v2 ...}.
type HashTableExpr struct {
tok Token
pairs []Expr // unevaluated AST pairs (nil after eval)
data map[string]Expr // String(key) -> value
keys map[string]Expr // String(key) -> evaluated key
order []string // insertion-order of String(key)
}
func (e *HashTableExpr) Eval(env *Environment) (Expr, error) {
if e.data != nil {
return e, nil
}
ht := newHashTable(e.tok)
for i := 0; i < len(e.pairs); i += 2 {
k, err := EvalFull(e.pairs[i], env)
if err != nil {
return nil, err
}
v, err := EvalFull(e.pairs[i+1], env)
if err != nil {
return nil, err
}
ht.Set(k, v)
}
return ht, nil
}
func (e *HashTableExpr) Token() Token { return e.tok }
func (e *HashTableExpr) String() string {
if e.data == nil {
return "{" + joinExprs(e.pairs, " ") + "}"
}
if len(e.order) == 0 {
return "{}"
}
parts := make([]string, 0, len(e.order)*2)
for _, sk := range e.order {
if k, ok := e.keys[sk]; ok {
parts = append(parts, k.String(), e.data[sk].String())
}
}
return "{" + strings.Join(parts, " ") + "}"
}
func newHashTable(tok Token) *HashTableExpr {
return &HashTableExpr{
tok: tok,
data: make(map[string]Expr),
keys: make(map[string]Expr),
}
}
func (e *HashTableExpr) Get(key Expr) (Expr, bool) {
v, ok := e.data[e.key(key)]
return v, ok
}
func (e *HashTableExpr) Set(key, val Expr) {
sk := e.key(key)
if _, exists := e.data[sk]; !exists {
e.order = append(e.order, sk)
}
e.data[sk] = val
e.keys[sk] = key
}
func (e *HashTableExpr) Delete(key Expr) {
sk := e.key(key)
delete(e.data, sk)
delete(e.keys, sk)
for i, k := range e.order {
if k == sk {
e.order = append(e.order[:i], e.order[i+1:]...)
break
}
}
}
func (e *HashTableExpr) key(key Expr) string {
return fmt.Sprintf("%T:%s", key, key.String())
}
func (e *HashTableExpr) Size() int { return len(e.data) }
// LambdaExpr is a user-defined procedure (closure).
type LambdaExpr struct {
tok Token
params []string
rest string // non-empty when the lambda accepts variadic trailing args
body []Expr
env *Environment
}
func (e *LambdaExpr) Eval(_ *Environment) (Expr, error) { return e, nil }
// Token returns the source token for this expression.
func (e *LambdaExpr) Token() Token { return e.tok }
// String returns a summary representation showing the parameter list.
func (e *LambdaExpr) String() string {
return "#<procedure>"
}
// FormExpr is a Go-implemented special form. Unlike BuiltinExpr, its arguments
// are passed unevaluated, giving the implementation full control over
// evaluation semantics (identical to built-in forms like define and if).
// Register one via Environment.RegisterForm.
type FormExpr struct {
name string
fn func(args []Expr, env *Environment) (Expr, error)
}
func (e *FormExpr) Eval(_ *Environment) (Expr, error) { return e, nil }
// Token returns an empty token; FormExpr values have no source position.
func (e *FormExpr) Token() Token { return Token{} }
// String returns a display name identifying this as a special form.
func (e *FormExpr) String() string { return fmt.Sprintf("#<form:%s>", e.name) }
// ValuesExpr holds multiple return values produced by (values ...).
// It may only appear where multiple values are explicitly consumed, such as
// define-values. Using a ValuesExpr in a single-value position is an error.
type ValuesExpr struct {
vals []Expr
}
func (e *ValuesExpr) Eval(_ *Environment) (Expr, error) { return e, nil }
// Token returns an empty token; ValuesExpr values have no source position.
func (e *ValuesExpr) Token() Token { return Token{} }
// String returns a readable representation of all contained values.
func (e *ValuesExpr) String() string {
return "(values " + joinExprs(e.vals, " ") + ")"
}
// Values returns the individual expressions wrapped by this object.
func (e *ValuesExpr) Values() []Expr { return e.vals }
// VoidExpr is the unspecified return value produced by side-effecting forms
// such as display, newline, define, set!, and import. It is distinct from #f
// so the REPL and callers can suppress printing it.
type VoidExpr struct{}
func (e *VoidExpr) Eval(_ *Environment) (Expr, error) { return e, nil }
// Token returns an empty token; VoidExpr has no source position.
func (e *VoidExpr) Token() Token { return Token{} }
// String returns an empty string; void is intentionally invisible.
func (e *VoidExpr) String() string { return "" }
// BuiltinExpr is a Go-implemented procedure.
type BuiltinExpr struct {
name string
fn func(args []Expr) (Expr, error)
}
func (e *BuiltinExpr) Eval(_ *Environment) (Expr, error) { return e, nil }
// Token returns an empty token; BuiltinExpr values have no source position.
func (e *BuiltinExpr) Token() Token { return Token{} }
// String returns a display name identifying this as a built-in procedure.
func (e *BuiltinExpr) String() string { return fmt.Sprintf("#<builtin:%s>", e.name) }
// apply calls a procedure (lambda or builtin) with already-evaluated arguments.
func apply(proc Expr, args []Expr) (Expr, error) {
switch p := proc.(type) {
case *BuiltinExpr:
return p.fn(args)
case *LambdaExpr:
if p.rest == "" && len(args) != len(p.params) {
return nil, fmt.Errorf("%s: expected %d args, got %d", p.String(), len(p.params), len(args))
}
if p.rest != "" && len(args) < len(p.params) {
return nil, fmt.Errorf("%s: expected at least %d args, got %d", p.String(), len(p.params), len(args))
}
child := p.env.Extend()
for i, param := range p.params {
child.Bind(param, args[i])
}
if p.rest != "" {
restList, _ := builtinList(args[len(p.params):])
child.Bind(p.rest, restList)
}
return evalBody(p.body, child)
default:
return nil, fmt.Errorf("%s is not a procedure", proc.String())
}
}
func evalDefine(args []Expr, env *Environment) (Expr, error) {
if len(args) < 2 {
return nil, fmt.Errorf("define: too few arguments")
}
switch target := args[0].(type) {
case *SymbolExpr:
// (define name value)
if len(args) != 2 {
return nil, fmt.Errorf("define: variable form expects exactly 1 value")
}
val, err := EvalFull(args[1], env)
if err != nil {
return nil, err
}
env.Bind(target.val, val)
return Void(), nil
case *Pair:
// (define (name params...) body...) is sugar for (define name (lambda (params...) body...))
nameSym, ok := target.car.(*SymbolExpr)
if !ok {
return nil, fmt.Errorf("define: function name must be a symbol")
}
lambda, err := makeLambda(target.cdr, args[1:], env)
if err != nil {
return nil, err
}
env.Bind(nameSym.val, lambda)
return Void(), nil
default:
return nil, fmt.Errorf("define: target must be a symbol or pair, got %T", args[0])
}
}
func evalLambda(args []Expr, env *Environment) (Expr, error) {
if len(args) < 2 {
return nil, fmt.Errorf("lambda: requires parameter list and body")
}
return makeLambda(args[0], args[1:], env)
}
func makeLambda(paramsExpr Expr, body []Expr, env *Environment) (*LambdaExpr, error) {
var params []string
var rest string
curr := paramsExpr
for {
switch p := curr.(type) {
case *Pair:
sym, ok := p.car.(*SymbolExpr)
if !ok {
return nil, fmt.Errorf("lambda: parameter must be a symbol, got %T", p.car)
}
params = append(params, sym.val)
curr = p.cdr
case *SymbolExpr:
rest = p.val
goto done
case *ListExpr:
if len(p.elements) == 0 {
goto done
}
return nil, fmt.Errorf("lambda: invalid parameter list")
default:
return nil, fmt.Errorf("lambda: invalid parameter list")
}
}
done:
return &LambdaExpr{params: params, rest: rest, body: body, env: env}, nil
}
func evalIf(args []Expr, env *Environment) (Expr, error) {
if len(args) < 2 || len(args) > 3 {
return nil, fmt.Errorf("if: expected 2 or 3 arguments, got %d", len(args))
}
cond, err := EvalFull(args[0], env)
if err != nil {
return nil, err
}
if isFalse(cond) {
if len(args) == 3 {
return &TailCall{expr: args[2], env: env}, nil
}
return Void(), nil
}
return &TailCall{expr: args[1], env: env}, nil
}
// evalLetBindings is the shared core of let and let*. In let (sequential=false),
// all binding values are evaluated in the outer env before any are bound. In
// let* (sequential=true), each binding is evaluated in the growing child env.
func evalLetBindings(name string, args []Expr, env *Environment, sequential bool) (Expr, error) {
if len(args) < 2 {
return nil, fmt.Errorf("%s: requires bindings and body", name)
}
bindingSlice, err := toSlice(name, args[0])
if err != nil {
return nil, err
}
child := env.Extend()
for _, b := range bindingSlice {
pairSlice, err := toSlice(name, b)
if err != nil || len(pairSlice) != 2 {
return nil, fmt.Errorf("%s: each binding must be (name value)", name)
}
sym, ok := pairSlice[0].(*SymbolExpr)
if !ok {
return nil, fmt.Errorf("%s: binding name must be a symbol", name)
}
evalEnv := env
if sequential {
evalEnv = child
}
val, err := EvalFull(pairSlice[1], evalEnv)
if err != nil {
return nil, err
}
child.Bind(sym.val, val)
}
return evalBody(args[1:], child)
}
func evalLet(args []Expr, env *Environment) (Expr, error) {
return evalLetBindings("let", args, env, false)
}
func evalLetStar(args []Expr, env *Environment) (Expr, error) {
return evalLetBindings("let*", args, env, true)
}
func evalQuote(args []Expr, _ *Environment) (Expr, error) {
if len(args) != 1 {
return nil, fmt.Errorf("quote: expected 1 argument, got %d", len(args))
}
return args[0], nil
}
func evalQuasiquote(args []Expr, env *Environment) (Expr, error) {
if len(args) != 1 {
return nil, fmt.Errorf("quasiquote: expected 1 argument, got %d", len(args))
}
return expandQQ(args[0], 0, env)
}
// isTagged reports whether expr is a list whose first element is a symbol with
// the given name, returning the remaining elements if so.
func isTagged(expr Expr, name string) ([]Expr, bool) {
elements, err := toSlice(name, expr)
if err != nil || len(elements) == 0 {
return nil, false
}
sym, ok := elements[0].(*SymbolExpr)
if !ok || sym.val != name {
return nil, false
}
return elements[1:], true
}
// expandQQ recursively expands a quasiquote template at the given nesting
// depth. depth 0 means we are in the innermost quasiquote and unquote
// expressions are evaluated immediately.
func expandQQ(expr Expr, depth int, env *Environment) (Expr, error) {
if inner, ok := isTagged(expr, "unquote"); ok {
if len(inner) != 1 {
return nil, fmt.Errorf("unquote: expected 1 argument, got %d", len(inner))
}
if depth == 0 {
return EvalFull(inner[0], env)
}
expanded, err := expandQQ(inner[0], depth-1, env)
if err != nil {
return nil, err
}
sym := &SymbolExpr{tok: Token{Kind: Symbol, Value: "unquote"}, val: "unquote"}
return &ListExpr{tok: expr.Token(), elements: []Expr{sym, expanded}}, nil
}
if inner, ok := isTagged(expr, "quasiquote"); ok {
if len(inner) != 1 {
return nil, fmt.Errorf("quasiquote: expected 1 argument, got %d", len(inner))
}
expanded, err := expandQQ(inner[0], depth+1, env)
if err != nil {
return nil, err
}
sym := &SymbolExpr{tok: Token{Kind: Symbol, Value: "quasiquote"}, val: "quasiquote"}
return &ListExpr{tok: expr.Token(), elements: []Expr{sym, expanded}}, nil
}
// Hash tables: expand key/value elements.
if ht, ok := expr.(*HashTableExpr); ok && ht.pairs != nil {
var result []Expr
for _, el := range ht.pairs {
expanded, err := expandQQ(el, depth, env)
if err != nil {
return nil, err
}
result = append(result, expanded)
}
return &HashTableExpr{tok: ht.tok, pairs: result}, nil
}
// Vectors: expand elements the same way as lists, but produce a VectorExpr.
if vec, ok := expr.(*VectorExpr); ok {
var result []Expr
for _, el := range vec.elements {
expanded, err := expandQQ(el, depth, env)
if err != nil {
return nil, err
}
result = append(result, expanded)
}
return &VectorExpr{tok: vec.tok, elements: result}, nil
}
elements, err := toSlice("quasiquote", expr)
if err != nil {
// Improper list (dotted)
if p, ok := expr.(*Pair); ok {
car, err := expandQQ(p.car, depth, env)
if err != nil {
return nil, err
}
cdr, err := expandQQ(p.cdr, depth, env)
if err != nil {
return nil, err
}
return &Pair{tok: p.tok, car: car, cdr: cdr}, nil
}
return expr, nil
}
var result []Expr
for _, el := range elements {
if spliceArgs, ok := isTagged(el, "unquote-splicing"); ok {
if len(spliceArgs) != 1 {
return nil, fmt.Errorf(
"unquote-splicing: expected 1 argument, got %d",
len(spliceArgs),
)
}
if depth == 0 {
val, err := EvalFull(spliceArgs[0], env)
if err != nil {
return nil, err
}
spliceSlice, err := toSlice("unquote-splicing", val)
if err != nil {
return nil, fmt.Errorf(
"unquote-splicing: expected a list, got %s",
val.String(),
)
}
result = append(result, spliceSlice...)
continue
}
expanded, err := expandQQ(spliceArgs[0], depth-1, env)
if err != nil {
return nil, err
}
sym := &SymbolExpr{
tok: Token{Kind: Symbol, Value: "unquote-splicing"},
val: "unquote-splicing",
}
unquoteList, _ := builtinList([]Expr{sym, expanded})
result = append(result, unquoteList)
continue
}
expanded, err := expandQQ(el, depth, env)
if err != nil {
return nil, err
}
result = append(result, expanded)
}
return builtinList(result)
}
func evalSetBang(args []Expr, env *Environment) (Expr, error) {
if len(args) != 2 {
return nil, fmt.Errorf("set!: expected 2 arguments, got %d", len(args))
}
sym, ok := args[0].(*SymbolExpr)
if !ok {
return nil, fmt.Errorf("set!: target must be a symbol")
}
val, err := EvalFull(args[1], env)
if err != nil {
return nil, err
}
if err := env.Set(sym.val, val); err != nil {
return nil, err
}
return Void(), nil
}
func evalBody(exprs []Expr, env *Environment) (Expr, error) {
if len(exprs) == 0 {
return Void(), nil
}
for _, expr := range exprs[:len(exprs)-1] {
if _, err := EvalFull(expr, env); err != nil {
return nil, err
}
}
return &TailCall{expr: exprs[len(exprs)-1], env: env}, nil
}
// evalDefineValues implements (define-values (name ...) expr).
// expr must evaluate to a ValuesExpr whose arity matches the name list.
// As a special case, a single-name list accepts any non-values result.
func evalDefineValues(args []Expr, env *Environment) (Expr, error) {
if len(args) != 2 {
return nil, fmt.Errorf(
"define-values: expected name list and expression, got %d args",
len(args),
)
}
nameSlice, err := toSlice("define-values", args[0])
if err != nil {
return nil, err
}
syms := make([]string, len(nameSlice))
for i, el := range nameSlice {
sym, ok := el.(*SymbolExpr)
if !ok {
return nil, fmt.Errorf("define-values: names must be symbols, got %s", el.String())
}
syms[i] = sym.val
}
result, err := EvalFull(args[1], env)
if err != nil {
return nil, err
}
if mv, ok := result.(*ValuesExpr); ok {
if len(mv.vals) != len(syms) {
return nil, fmt.Errorf(
"define-values: expected %d values, got %d",
len(syms),
len(mv.vals),
)
}
for i, name := range syms {
env.Bind(name, mv.vals[i])
}
return Void(), nil
}
// Single (non-values) result: only valid with exactly one name.
if len(syms) != 1 {
return nil, fmt.Errorf("define-values: expected %d values, got 1", len(syms))
}
env.Bind(syms[0], result)
return Void(), nil
}
// eqv reports whether two expressions are equivalent in the sense of Scheme's
// eqv?: identical booleans, equal numbers, equal strings, or identical symbols.
// For vectors, eqv? tests identity (pointer equality), not structural equality.
func eqv(a, b Expr) bool {
switch x := a.(type) {
case *NumberExpr:
y, ok := b.(*NumberExpr)
return ok && x.val == y.val
case *StringExpr:
y, ok := b.(*StringExpr)
return ok && x.val == y.val
case *BoolExpr:
y, ok := b.(*BoolExpr)
return ok && x.val == y.val
case *SymbolExpr:
y, ok := b.(*SymbolExpr)
return ok && x.val == y.val
case *Pair:
return a == b
case *ListExpr:
return a == b
case *VectorExpr:
return a == b
case *HashTableExpr:
return a == b
}
return false
}
// evalCase implements (case <key> ((<datum> ...) <body> ...) ... [(else <body> ...)]).
// The key is evaluated once; each clause's datum list is compared against it
// using eqv. The body of the first matching clause is evaluated and returned.
// An else clause matches unconditionally. Returns void if no clause matches.
func evalCase(args []Expr, env *Environment) (Expr, error) {
if len(args) < 2 {
return nil, fmt.Errorf("case: requires a key expression and at least one clause")
}
key, err := EvalFull(args[0], env)
if err != nil {
return nil, err
}
for _, arg := range args[1:] {
clauseSlice, err := toSlice("case", arg)
if err != nil || len(clauseSlice) < 2 {
return nil, fmt.Errorf("case: invalid clause %s", arg.String())
}