-
Notifications
You must be signed in to change notification settings - Fork 19
/
prim_macros.go
126 lines (116 loc) · 3.44 KB
/
prim_macros.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
// Copyright 2014 SteelSeries ApS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file contains the macro primitive functions.
package golisp
import (
"fmt"
)
func RegisterMacroPrimitives() {
MakeSpecialForm("quote", "1", QuoteImpl)
MakeSpecialForm("quasiquote", "1", QuasiquoteImpl)
MakeSpecialForm("unquote", "1", UnquoteImpl)
MakeSpecialForm("unquote-splicing", "1", UnquoteSplicingImpl)
MakeSpecialForm("expand", ">=1", ExpandImpl)
}
func QuoteImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
if Car(args) == nil {
return EmptyCons(), nil
} else {
return Car(args), nil
}
}
func processQuasiquoted(sexpr *Data, level int, env *SymbolTableFrame) (result *Data, err error) {
if !ListP(sexpr) {
return Cons(sexpr, nil), nil
} else if SymbolP(Car(sexpr)) && StringValue(Car(sexpr)) == "quasiquote" {
processed, err := processQuasiquoted(Cadr(sexpr), level+1, env)
if err != nil {
return nil, err
}
return Cons(Cons(Intern("quasiquote"), processed), nil), nil
} else if SymbolP(Car(sexpr)) && StringValue(Car(sexpr)) == "unquote" {
if level == 1 {
processed, err := processQuasiquoted(Cadr(sexpr), level, env)
if err != nil {
return nil, err
}
r, err := Eval(Car(processed), env)
if err != nil {
return nil, err
}
return Cons(r, nil), nil
} else {
processed, err := processQuasiquoted(Cadr(sexpr), level-1, env)
if err != nil {
return nil, err
}
return Cons(Cons(Intern("unquote"), processed), nil), nil
}
} else if SymbolP(Car(sexpr)) && StringValue(Car(sexpr)) == "unquote-splicing" {
if level == 1 {
processed, err := processQuasiquoted(Cadr(sexpr), level, env)
if err != nil {
return nil, err
}
r, err := Eval(Car(processed), env)
if err != nil {
return nil, err
}
if NilP(r) {
return nil, nil
} else {
return r, nil
}
} else {
processed, err := processQuasiquoted(Cadr(sexpr), level-1, env)
if err != nil {
return nil, err
}
return Cons(Cons(Intern("unquote-splicing"), processed), nil), nil
}
} else {
parts := make([]*Data, 0, Length(Cdr(sexpr)))
for _, exp := range ToArray(sexpr) {
processed, err := processQuasiquoted(exp, level, env)
if err != nil {
return nil, err
}
if processed != nil {
parts = append(parts, processed)
}
}
flat, err := Flatten(ArrayToList(parts))
if err != nil {
return nil, err
}
return Cons(flat, nil), nil
}
}
func QuasiquoteImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
r, err := processQuasiquoted(Car(args), 1, env)
if err != nil {
return nil, err
}
return Car(r), nil
}
func UnquoteImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
err = ProcessError("unquote should not be used outside of a quasiquoted expression.", env)
return
}
func UnquoteSplicingImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
err = ProcessError("unquote-splicing should not be used outside of a quasiquoted expression.", env)
return
}
func ExpandImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
n, err := Eval(Car(args), env)
if err != nil {
return
}
if !MacroP(n) {
err = ProcessError(fmt.Sprintf("expand expected a macro, received %s", String(n)), env)
return
}
return MacroValue(n).Expand(Cdr(args), env)
}