-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds.go
199 lines (175 loc) · 4.51 KB
/
cmds.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
package gmnlisp
import (
"context"
"os"
)
func cmdQuote(_ context.Context, _ *World, n Node) (Node, error) {
var argv [1]Node
if err := ListToArray(n, argv[:]); err != nil {
return nil, err
}
return argv[0], nil
}
func backQuote(ctx context.Context, w *World, n Node) (Node, error) {
if cons, ok := n.(*Cons); ok {
if cons.Car == commaSymbol {
if cdrCons, ok := cons.Cdr.(*Cons); ok {
newCar, err := cdrCons.Car.Eval(ctx, w)
if err != nil {
return nil, err
}
newCdr, err := backQuote(ctx, w, cdrCons.Cdr)
if err != nil {
return nil, err
}
return &Cons{Car: newCar, Cdr: newCdr}, nil
}
return cons.Cdr.Eval(ctx, w)
}
newCar, err := backQuote(ctx, w, cons.Car)
if err != nil {
return nil, err
}
newCdr, err := backQuote(ctx, w, cons.Cdr)
if err != nil {
return nil, err
}
return &Cons{Car: newCar, Cdr: newCdr}, nil
}
return n, nil
}
func cmdBackQuote(ctx context.Context, w *World, n Node) (Node, error) {
value, _, err := Shift(n)
if err != nil {
return nil, err
}
return backQuote(ctx, w, value)
}
func funAtom(_ context.Context, _ *World, argv []Node) (Node, error) {
if _, ok := argv[0].(*Cons); ok {
return Null, nil
}
return True, nil
}
func equalSub(ctx context.Context, w *World, list Node, eq func(Node, Node) bool) (Node, error) {
first, rest, err := w.ShiftAndEvalCar(ctx, list)
if err != nil {
return nil, err
}
for IsSome(rest) {
var next Node
next, rest, err = w.ShiftAndEvalCar(ctx, rest)
if err != nil {
return nil, err
}
if !eq(first, next) {
return Null, nil
}
}
return True, nil
}
func cmdEq(ctx context.Context, w *World, list Node) (Node, error) {
return equalSub(ctx, w, list, func(left, right Node) bool {
return left == right
})
}
func cmdEql(ctx context.Context, w *World, list Node) (Node, error) {
return equalSub(ctx, w, list, func(left, right Node) bool {
return left.Equals(right, STRICT)
})
}
func cmdEqual(ctx context.Context, w *World, list Node) (Node, error) {
return equalSub(ctx, w, list, func(left, right Node) bool {
return left.Equals(right, EQUAL)
})
}
func funNot(_ context.Context, w *World, argv []Node) (Node, error) {
if IsNone(argv[0]) {
return True, nil
}
return Null, nil
}
func funLoad(ctx context.Context, w *World, argv []Node) (Node, error) {
fname, ok := argv[0].(String)
if !ok {
return nil, ErrExpectedString
}
script, err := os.ReadFile(fname.String())
if err != nil {
return nil, err
}
return w.InterpretBytes(ctx, script)
}
func funNotEqual(_ context.Context, _ *World, argv []Node) (Node, error) {
if argv[0].Equals(argv[1], EQUALP) {
return Null, nil
}
return True, nil
}
func xxxxP(args []Node, f1 func(Integer) bool, f2 func(Float) bool) (Node, error) {
if value, ok := args[0].(Integer); ok {
if f1(value) {
return True, nil
}
} else if value, ok := args[0].(Float); ok {
if f2(value) {
return True, nil
}
}
return Null, nil
}
func funZerop(_ context.Context, _ *World, args []Node) (Node, error) {
return xxxxP(args,
func(value Integer) bool { return value == 0 },
func(value Float) bool { return value == 0 })
}
func funNumberp(_ context.Context, _ *World, args []Node) (Node, error) {
return xxxxP(args,
func(Integer) bool { return true },
func(Float) bool { return true })
}
func funPlusp(_ context.Context, _ *World, args []Node) (Node, error) {
return xxxxP(args,
func(value Integer) bool { return value > 0 },
func(value Float) bool { return value > 0 })
}
func funMinusp(_ context.Context, _ *World, args []Node) (Node, error) {
return xxxxP(args,
func(value Integer) bool { return value < 0 },
func(value Float) bool { return value < 0 })
}
func funOddp(_ context.Context, _ *World, args []Node) (Node, error) {
if value, ok := args[0].(Integer); ok && value%2 == 1 {
return True, nil
}
return Null, nil
}
func funEvenp(_ context.Context, _ *World, args []Node) (Node, error) {
if value, ok := args[0].(Integer); ok && value%2 == 0 {
return True, nil
}
return Null, nil
}
func funNullp(_ context.Context, _ *World, args []Node) (Node, error) {
if IsNone(args[0]) {
return True, nil
}
return Null, nil
}
func funAnyTypep[T Node](_ context.Context, _ *World, args []Node) (Node, error) {
if _, ok := args[0].(T); ok {
return True, nil
}
return Null, nil
}
func funParseNumber(_ context.Context, _ *World, args []Node) (Node, error) {
s, ok := args[0].(String)
if !ok {
return nil, ErrExpectedString
}
val, ok, err := tryParseAsNumber(s.String())
if !ok {
return nil, ErrCanNotParseNumber
}
return val, err
}