-
Notifications
You must be signed in to change notification settings - Fork 0
/
specials.go
397 lines (321 loc) · 9.36 KB
/
specials.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
// specials.go - Implementation of the special forms.
package eval
import (
"fmt"
"github.com/skx/yal/env"
"github.com/skx/yal/primitive"
)
// evalSpecialForm is invoked to execute one of our special forms.
//
// This is done to centralize the code, and also ensure that eval doesn't
// get too dense.
//
// The return value from this function is "XX, BOOL". If the boolean result
// is true this function handled the call, otherwise it did not.
//
// This is required because special forms take precedence over other calls.
func (ev *Eval) evalSpecialForm(name string, args []primitive.Primitive, e *env.Environment, expandMacro bool) (primitive.Primitive, bool) {
switch name {
case "alias":
// We need at least one pair.
if len(args) < 2 {
return primitive.ArityError(), true
}
if len(args)%2 != 0 {
return primitive.Error(fmt.Sprintf("(alias ..) must have an even length of arguments, got %v", args)), true
}
for i := 0; i < len(args); i += 2 {
// The key/val pair we're working with
new := args[i]
orig := args[i+1]
old, ok := e.Get(orig.ToString())
if ok {
e.Set(new.ToString(), old)
ev.aliases[new.ToString()] = orig.ToString()
}
}
return primitive.Nil{}, true
case "define", "def!":
if len(args) < 2 {
return primitive.ArityError(), true
}
symb, ok := args[0].(primitive.Symbol)
if ok {
val := ev.eval(args[1], e, expandMacro)
e.Set(string(symb), val)
return primitive.Nil{}, true
}
return primitive.Error(fmt.Sprintf("Expected a symbol, got %v", args[0])), true
case "defmacro!":
if len(args) < 2 {
return primitive.ArityError(), true
}
// name of macro
symb, ok := args[0].(primitive.Symbol)
if !ok {
return primitive.Error(fmt.Sprintf("Expected a symbol, got %v", args[0])), true
}
// macro body
val := ev.eval(args[1], e, expandMacro)
mac, ok2 := val.(*primitive.Procedure)
if !ok2 {
return primitive.Error(fmt.Sprintf("expected a function body for (defmacro..), got %v", val)), true
}
// this is now a macro
mac.Macro = true
e.Set(string(symb), mac)
return primitive.Nil{}, true
case "do":
var ret primitive.Primitive
for _, x := range args {
ret = ev.eval(x, e, expandMacro)
}
return ret, true
case "eval":
if len(args) != 1 {
return primitive.ArityError(), true
}
switch val := args[0].(type) {
// Evaluate
case primitive.List:
// Evaluate the list
res := ev.eval(args[0], e, expandMacro)
// Create a new evaluator with
// the result as a string
tmp := New(res.ToString())
// Ensure that we have a suitable
// child-environment.
nEnv := env.NewEnvironment(e)
// Now evaluate it.
return tmp.Evaluate(nEnv), true
// symbol solely so we can do env. lookup
case primitive.Symbol:
str, ok := e.Get(val.ToString())
if ok {
tmp := New(str.(primitive.Primitive).ToString())
nEnv := env.NewEnvironment(e)
return tmp.Evaluate(nEnv), true
}
return primitive.Nil{}, true
// string eval
case primitive.String:
tmp := New(string(val))
nEnv := env.NewEnvironment(e)
return tmp.Evaluate(nEnv), true
default:
return primitive.Error(fmt.Sprintf("unexpected type for eval %V.", args[0])), true
}
case "if":
if len(args) < 2 {
return primitive.ArityError(), true
}
test := ev.eval(args[0], e, expandMacro)
// If we got an error inside the `if` then we return it
er, eok := test.(primitive.Error)
if eok {
return er, true
}
// if the test was false then we return
// the else-section
if b, ok := test.(primitive.Bool); (ok && !bool(b)) || primitive.IsNil(test) {
if len(args) < 3 {
return primitive.Nil{}, true
}
return ev.eval(args[2], e, expandMacro), true
}
// otherwise we handle the true-section.
return ev.eval(args[1], e, expandMacro), true
case "lambda", "fn*":
// ensure we have arguments
if len(args) != 2 && len(args) != 3 {
return primitive.ArityError(), true
}
// ensure that our arguments are a list
argMarkers, ok := args[0].(primitive.List)
if !ok {
return primitive.Error(fmt.Sprintf("expected a list for arguments, got %v", args[0])), true
}
// Collect arguments
arguments := []primitive.Symbol{}
for _, x := range argMarkers {
xs, ok := x.(primitive.Symbol)
if !ok {
return primitive.Error(fmt.Sprintf("expected a symbol for an argument, got %v", x)), true
}
arguments = append(arguments, xs)
}
body := args[1]
help := ""
// If there's an optional help string ..
if len(args) == 3 {
help = args[1].ToString()
body = args[2]
}
// This is a procedure, which will default
// to not being a macro.
//
// To make it a macro it should be set with
// "(defmacro!..)"
return &primitive.Procedure{
Args: arguments,
Body: body,
Env: e,
Help: help,
Macro: false,
}, true
case "let*":
// We need to have at least one argument.
//
// Later we'll test for more. Because we need a multiple of two.
if len(args) < 1 {
return primitive.ArityError(), true
}
newEnv := env.NewEnvironment(e)
bindingsList, ok := args[0].(primitive.List)
if !ok {
return primitive.Error(fmt.Sprintf("argument is not a list, got %v", args[0])), true
}
// Length of binding must be %2
if len(bindingsList)%2 != 0 {
return primitive.Error(fmt.Sprintf("list for (len*) must have even length, got %v", bindingsList)), true
}
for i := 0; i < len(bindingsList); i += 2 {
// The key/val pair we're working with
key := bindingsList[i]
val := bindingsList[i+1]
// evaluate the value - use the new environment.
eVal := ev.eval(val, newEnv, expandMacro)
// The thing to set
eKey, ok := key.(primitive.Symbol)
if !ok {
return primitive.Error(fmt.Sprintf("binding name is not a symbol, got %v", key)), true
}
// Finally set the parameter
newEnv.Set(string(eKey), eVal)
}
// Now we've populated the new
// environment with the pairs we received
// in the setup phase we can execute
// the body.
var ret primitive.Primitive
for _, x := range args[1:] {
ret = ev.eval(x, newEnv, expandMacro)
}
return ret, true
case "macroexpand":
if len(args) != 1 {
return primitive.ArityError(), true
}
return ev.macroExpand(args[0], e), true
case "quasiquote":
if len(args) != 1 {
return primitive.ArityError(), true
}
return ev.eval(ev.quasiquote(args[0]), e, expandMacro), true
case "quote":
if len(args) != 1 {
return primitive.ArityError(), true
}
return args[0], true
case "read":
if len(args) != 1 {
return primitive.ArityError(), true
}
arg := args[0].ToString()
// Create a new evaluator with the list
tmp := New(arg)
// Read an expression with it.
//
// Note here we just _read_ the expression,
// we don't evaluate it.
//
// So we don't need an environment, etc.
//
out, err := tmp.readExpression(e)
if err != nil {
return primitive.Error(fmt.Sprintf("failed to read %s:%s", arg, err.Error())), true
}
// Return it.
return out, true
case "set!":
if len(args) < 2 {
return primitive.ArityError(), true
}
// Get the symbol we're gonna set
sym, ok := args[0].(primitive.Symbol)
if !ok {
return primitive.Error(fmt.Sprintf("tried to set a non-symbol %v", args[0])), true
}
// Get the value.
val := ev.eval(args[1], e, expandMacro)
// Now set, either locally or in the parent scope.
if len(args) == 3 {
e.SetOuter(string(sym), val)
} else {
e.Set(string(sym), val)
}
return primitive.Nil{}, true
case "struct":
if len(args) <= 1 {
return primitive.ArityError(), true
}
// name of structure
name := args[0].ToString()
// the fields it contains
fields := []string{}
// convert the fields to strings
for _, field := range args[1:] {
f := field.ToString()
ev.accessors[name+"."+f] = f
fields = append(fields, f)
}
// save the structure as a known-thing
ev.structs[name] = fields
return primitive.Nil{}, true
case "symbol":
if len(args) != 1 {
return primitive.ArityError(), true
}
return ev.atom(args[0].ToString()), true
case "try":
if len(args) < 2 {
return primitive.ArityError(), true
}
// first expression is what to execute: a list
expr := args[0]
// Cast the argument to a list
expLst, ok1 := expr.(primitive.List)
if !ok1 {
return primitive.Error(fmt.Sprintf("expected a list for argument, got %v", args[0])), true
}
// second expression is the catch: a list
blk := args[1]
blkLst, ok2 := blk.(primitive.List)
if !ok2 {
return primitive.Error(fmt.Sprintf("expected a list for argument, got %v", args[1])), true
}
if len(blkLst) != 3 {
return primitive.Error(fmt.Sprintf("list should have three elements, got %v", blkLst)), true
}
if !ev.startsWith(blkLst, "catch") {
return primitive.Error(fmt.Sprintf("catch list should begin with 'catch', got %v", blkLst)), true
}
// Evaluate the expression
out := ev.eval(expLst, e, expandMacro)
// Evaluating the expression didn't return an error.
//
// Nothing to catch, all OK
_, ok3 := out.(primitive.Error)
if !ok3 {
return out, true
}
// The catch statement is blkLst[0] - we tested for that already
// The variable to bind is blkLst[1]
// The form to execute with that is blkLst[2]
tmpEnv := env.NewEnvironment(e)
tmpEnv.Set(blkLst[1].ToString(), primitive.String(out.ToString()))
return ev.eval(blkLst[2], tmpEnv, expandMacro), true
}
// The input was not handled as a special.
return primitive.Nil{}, false
}