-
Notifications
You must be signed in to change notification settings - Fork 0
/
progn.go
370 lines (330 loc) · 7.58 KB
/
progn.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
package gmnlisp
import (
"context"
"errors"
"fmt"
"io"
)
type ErrEarlyReturns struct {
Value Node
Name Symbol
}
func (e *ErrEarlyReturns) Error() string {
if e.Name == nulSymbol {
return "Unexpected (return)"
}
return fmt.Sprintf("Unexpected (return-from %s)", e.Name)
}
func funReturn(ctx context.Context, w *World, argv []Node) (Node, error) {
// from CommonLisp
return nil, &ErrEarlyReturns{Value: argv[0], Name: nulSymbol}
}
func cmdReturnFrom(ctx context.Context, w *World, n Node) (Node, error) {
// from CommonLisp
var argv [2]Node
if err := ListToArray(n, argv[:]); err != nil {
return nil, err
}
symbol, ok := argv[0].(Symbol)
if !ok {
return nil, ErrExpectedSymbol
}
value, err := argv[1].Eval(ctx, w)
if err != nil {
return nil, err
}
return nil, &ErrEarlyReturns{Value: value, Name: symbol}
}
func Progn(ctx context.Context, w *World, n Node) (value Node, err error) {
value = Null
for IsSome(n) {
value, n, err = w.ShiftAndEvalCar(ctx, n)
if err != nil {
return nil, err
}
}
return
}
func cmdProgn(ctx context.Context, w *World, c Node) (Node, error) {
return Progn(ctx, w, c)
}
func cmdBlock(ctx context.Context, w *World, node Node) (Node, error) {
// from CommonLisp
nameNode, statements, err := Shift(node)
if err != nil {
return nil, err
}
var nameSymbol Symbol
if IsSome(nameNode) {
var ok bool
nameSymbol, ok = nameNode.(Symbol)
if !ok {
return nil, ErrExpectedSymbol
}
} else {
nameSymbol = nulSymbol
}
var errEarlyReturns *ErrEarlyReturns
rv, err := Progn(ctx, w, statements)
if errors.As(err, &errEarlyReturns) && errEarlyReturns.Name == nameSymbol {
return errEarlyReturns.Value, nil
}
return rv, err
}
type ErrThrown struct {
Value Node
TagForm Node
}
func (e *ErrThrown) Error() string {
return fmt.Sprintf("Thrown tag-form %#v was not caught", e.TagForm)
}
func cmdCatch(ctx context.Context, w *World, node Node) (Node, error) {
// from ISLisp
tagForm, statements, err := w.ShiftAndEvalCar(ctx, node)
if err != nil {
return nil, err
}
var errThrown *ErrThrown
rv, err := Progn(ctx, w, statements)
if errors.As(err, &errThrown) && errThrown.TagForm.Equals(tagForm, EQUALP) {
return errThrown.Value, nil
}
return rv, err
}
func funThrow(ctx context.Context, w *World, list []Node) (Node, error) {
// from ISLisp
return nil, &ErrThrown{Value: list[1], TagForm: list[0]}
}
func cmdCond(ctx context.Context, w *World, list Node) (Node, error) {
return cmdCondWithTailRecOpt(ctx, w, list, -1)
}
func cmdCondWithTailRecOpt(ctx context.Context, w *World, list Node, currFunc Symbol) (Node, error) {
for IsSome(list) {
var condAndAct Node
var err error
condAndAct, list, err = Shift(list)
if err != nil {
return nil, err
}
cond, act, err := w.ShiftAndEvalCar(ctx, condAndAct)
if err != nil {
return nil, err
}
if IsSome(cond) {
return prognWithTailRecOpt(ctx, w, act, currFunc)
}
}
return Null, nil
}
func cmdCase(ctx context.Context, w *World, list Node) (Node, error) {
var swValue Node
var err error
swValue, list, err = w.ShiftAndEvalCar(ctx, list)
if err != nil {
return nil, err
}
for IsSome(list) {
var caseAndAct Node
var err error
caseAndAct, list, err = Shift(list)
if err != nil {
return nil, err
}
caseValue, act, err := Shift(caseAndAct)
if err != nil {
return nil, err
}
if cons, ok := caseValue.(*Cons); ok {
var list Node = cons
for IsSome(list) {
var _caseValue Node
_caseValue, list, err = w.ShiftAndEvalCar(ctx, list)
if err != nil {
return nil, err
}
if swValue.Equals(_caseValue, EQUALP) {
return Progn(ctx, w, act)
}
}
} else if caseValue.Equals(True, STRICT) {
return Progn(ctx, w, act)
}
}
return Null, nil
}
func cmdIf(ctx context.Context, w *World, params Node) (Node, error) {
return cmdIfWithTailRecOpt(ctx, w, params, -1)
}
func cmdIfWithTailRecOpt(ctx context.Context, w *World, params Node, tailOptSym Symbol) (Node, error) {
cond, params, err := w.ShiftAndEvalCar(ctx, params)
if err != nil {
return nil, err
}
thenClause, params, err := Shift(params)
if err != nil {
return nil, err
}
var elseClause Node = Null
if IsSome(params) {
elseClause, params, err = Shift(params)
if err != nil {
return nil, err
}
if IsSome(params) {
return nil, ErrTooManyArguments
}
}
if IsSome(cond) {
return evalWithTailRecOpt(ctx, w, thenClause, tailOptSym)
} else if IsSome(elseClause) {
return evalWithTailRecOpt(ctx, w, elseClause, tailOptSym)
} else {
return Null, nil
}
}
func cmdWhile(ctx context.Context, w *World, n Node) (Node, error) {
// from autolisp, ISLisp
cond, statements, err := Shift(n)
if err != nil {
return nil, err
}
var last Node = Null
for {
if err := checkContext(ctx); err != nil {
return nil, err
}
cont, err := cond.Eval(ctx, w)
if err != nil {
return nil, err
}
if IsNone(cont) {
return last, nil
}
last, err = Progn(ctx, w, statements)
if err != nil {
return nil, err
}
}
}
func cmdQuit(context.Context, *World, Node) (Node, error) {
return Null, ErrQuit
}
func cmdAbort(context.Context, *World, Node) (Node, error) {
return Null, ErrAbort
}
type ErrorNode struct {
Value error
}
func (e *ErrorNode) PrintTo(w io.Writer, m PrintMode) (int, error) {
return io.WriteString(w, e.Value.Error())
}
func (e *ErrorNode) Eval(context.Context, *World) (Node, error) {
return e, nil
}
func (e *ErrorNode) Equals(n Node, m EqlMode) bool {
f, ok := n.(*ErrorNode)
if !ok {
return false
}
return errors.Is(e.Value, f.Value) || errors.Is(f.Value, e.Value)
}
func cmdWithHandler(ctx context.Context, w *World, list Node) (Node, error) {
// ISLisp
handlerNode, list, err := w.ShiftAndEvalCar(ctx, list)
if err != nil {
return nil, err
}
handler, ok := handlerNode.(Callable)
if !ok {
return nil, ErrExpectedFunction
}
value, err := Progn(ctx, w, list)
if err == nil {
return value, nil
}
_, err2 := handler.Call(ctx, w, &Cons{Car: &ErrorNode{Value: err}, Cdr: Null})
if err2 != nil {
return nil, err2
}
return nil, err
}
func cmdUnwindProtect(ctx context.Context, w *World, list Node) (Node, error) {
var formErr error
_, list, formErr = w.ShiftAndEvalCar(ctx, list)
value, err := Progn(ctx, w, list)
if err != nil {
return nil, err
}
if formErr != nil {
return nil, formErr
}
return value, nil
}
type _TagBody struct {
tag Symbol
}
func (t *_TagBody) Error() string {
return fmt.Sprintf("tag: %s not found", t.tag)
}
func cmdGo(ctx context.Context, w *World, args Node) (Node, error) {
tag, _, err := Shift(args)
if err != nil {
return nil, err
}
symbol, ok := tag.(Symbol)
if !ok {
return nil, ErrExpectedSymbol
}
return Null, &_TagBody{tag: symbol}
}
func cmdTagBody(ctx context.Context, w *World, args Node) (Node, error) {
tag := map[Symbol]Node{}
mainloop:
for IsSome(args) {
var current Node
var err error
current, args, err = Shift(args)
if err != nil {
return nil, err
}
if symbol, ok := current.(Symbol); ok {
tag[symbol] = args
continue
}
_, err = current.Eval(ctx, w)
if err == nil {
continue
}
var t *_TagBody
if !errors.As(err, &t) {
return nil, err
}
if next, ok := tag[t.tag]; ok {
args = next
continue
}
for IsSome(args) {
current, args, err = Shift(args)
if err != nil {
return nil, err
}
symbol, ok := current.(Symbol)
if !ok {
continue
}
tag[symbol] = args
if symbol == t.tag {
continue mainloop
}
}
return nil, err
}
return Null, nil
}
func cmdIgnoreErrors(ctx context.Context, w *World, n Node) (Node, error) {
val, err := Progn(ctx, w, n)
if err != nil {
return Null, nil
}
return val, nil
}