-
Notifications
You must be signed in to change notification settings - Fork 3
/
specials.go
352 lines (293 loc) · 7.4 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
package slurp
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/spy16/slurp/builtin"
"github.com/spy16/slurp/core"
)
// ErrParseSpecial is returned when parsing a special form invocation
// fails due to malformed syntax.
var ErrParseSpecial = errors.New("invalid special form")
// parseDo parses the (do <expr>*) form and returns a DoExpr.
func parseDo(a core.Analyzer, env core.Env, args core.Seq) (core.Expr, error) {
var de builtin.DoExpr
err := core.ForEach(args, func(item core.Any) (bool, error) {
expr, err := a.Analyze(env, item)
if err != nil {
return true, err
}
de = append(de, expr)
return false, nil
})
if err != nil {
return nil, err
}
return de, nil
}
// parseIf parses the (if <test> <then> <else>?) form and returns IfExpr.
func parseIf(a core.Analyzer, env core.Env, args core.Seq) (core.Expr, error) {
count, err := args.Count()
if err != nil {
return nil, err
} else if count != 2 && count != 3 {
return nil, core.Error{
Cause: fmt.Errorf("%w: if", ErrParseSpecial),
Message: fmt.Sprintf("requires 2 or 3 arguments, got %d", count),
}
}
exprs := [3]core.Expr{}
for i := 0; i < count; i++ {
f, err := args.First()
if err != nil {
return nil, err
}
expr, err := a.Analyze(env, f)
if err != nil {
return nil, err
}
exprs[i] = expr
args, err = args.Next()
if err != nil {
return nil, err
}
}
return builtin.IfExpr{
Test: exprs[0],
Then: exprs[1],
Else: exprs[2],
}, nil
}
func parseQuote(a core.Analyzer, _ core.Env, args core.Seq) (core.Expr, error) {
if count, err := args.Count(); err != nil {
return nil, err
} else if count != 1 {
return nil, core.Error{
Cause: fmt.Errorf("%w: quote", ErrParseSpecial),
Message: fmt.Sprintf("requires exactly 1 argument, got %d", count),
}
}
first, err := args.First()
if err != nil {
return nil, err
}
return builtin.QuoteExpr{Form: first}, nil
}
func parseDef(a core.Analyzer, env core.Env, args core.Seq) (core.Expr, error) {
e := core.Error{Cause: fmt.Errorf("%w: def", ErrParseSpecial)}
if args == nil {
return nil, e.With("requires exactly 2 args, got 0")
}
if count, err := args.Count(); err != nil {
return nil, err
} else if count != 2 {
return nil, e.With(fmt.Sprintf(
"requires exactly 2 arguments, got %d", count))
}
first, err := args.First()
if err != nil {
return nil, err
}
sym, ok := first.(builtin.Symbol)
if !ok {
return nil, e.With(fmt.Sprintf(
"first arg must be symbol, not '%s'", reflect.TypeOf(first)))
}
rest, err := args.Next()
if err != nil {
return nil, err
}
second, err := rest.First()
if err != nil {
return nil, err
}
res, err := a.Analyze(env, second)
if err != nil {
return nil, err
}
return builtin.DefExpr{
Name: string(sym),
Value: res,
}, nil
}
func parseLet(a core.Analyzer, env core.Env, args core.Seq) (core.Expr, error) {
e := core.Error{Cause: fmt.Errorf("%w: def", ErrParseSpecial)}
if args == nil {
return nil, e.With("requires exactly 2 args, got 0")
}
cnt, err := args.Count()
if err != nil {
return nil, err
}
if cnt == 0 {
return nil, e.With("requires exactly 2 args, got 0")
}
var let builtin.LetExpr
// analyze bindings
bs, err := args.First()
if err != nil {
return nil, err
}
switch s := bs.(type) {
case core.Seq:
if err = parseLetBindings(a, env, s, &let); err != nil {
return nil, err
}
case core.Seqable:
seq, err := s.Seq()
if err != nil {
return nil, err
}
if err = parseLetBindings(a, env, seq, &let); err != nil {
return nil, err
}
default:
return nil, core.Error{
Cause: fmt.Errorf("%w: let", ErrParseSpecial),
Message: fmt.Sprintf("%s is not a sequence type", reflect.TypeOf(bs)),
}
}
// analyze expressions
if args, err = args.Next(); err != nil {
return nil, err
}
err = core.ForEach(args, func(item core.Any) (bool, error) {
expr, err := a.Analyze(env, item)
if err == nil {
let.Exprs = append(let.Exprs, expr)
}
return false, err
})
return let, err
}
func parseLetBindings(a core.Analyzer, env core.Env, seq core.Seq, le *builtin.LetExpr) error {
return core.ForEach(seq, func(item core.Any) (bool, error) {
// symbol?
if len(le.Names)%2 == 0 {
s, ok := item.(builtin.Symbol)
if !ok {
return false, core.Error{
Cause: fmt.Errorf("%w: let", ErrParseSpecial),
Message: fmt.Sprintf("expected symbol, got %s", reflect.TypeOf(item)),
}
}
le.Names = append(le.Names, string(s))
return false, nil
}
// it's a value
expr, err := a.Analyze(env, item)
if err == nil {
le.Values = append(le.Values, expr)
}
return false, err
})
}
func parseGo(a core.Analyzer, env core.Env, args core.Seq) (core.Expr, error) {
count, err := args.Count()
if err != nil {
return nil, err
}
v, err := args.First()
if err != nil {
return nil, err
}
if v == nil {
return nil, core.Error{
Cause: fmt.Errorf("%w: go", ErrParseSpecial),
Message: fmt.Sprintf("requires exactly 1 argument, got %d", count),
}
}
e, err := a.Analyze(env, v)
if err != nil {
return nil, err
}
return builtin.GoExpr{Form: e}, nil
}
// parseFn parses (fn name? doc? (<params>*) <body>*) special form and
// returns an Fn definition.
func parseFn(a core.Analyzer, env core.Env, argSeq core.Seq) (core.Expr, error) {
fn, err := parseFnDef(a, env, argSeq)
if err != nil {
return nil, err
}
return builtin.ConstExpr{Const: *fn}, nil
}
// parseMacro parses (macro name? doc? (<params>*) <body>*) special form and
// returns an Fn definition.
func parseMacro(a core.Analyzer, env core.Env, argSeq core.Seq) (core.Expr, error) {
fn, err := parseFnDef(a, env, argSeq)
if err != nil {
return nil, err
}
fn.Macro = true
return builtin.ConstExpr{Const: *fn}, nil
}
func parseFnDef(a core.Analyzer, env core.Env, argSeq core.Seq) (*builtin.Fn, error) {
if argSeq == nil {
return nil, errors.New("nil argument sequence")
}
fn := builtin.Fn{}
cnt, err := argSeq.Count()
if err != nil {
return nil, err
} else if cnt < 1 {
return nil, fmt.Errorf("%w: got %d, want at-least 1", core.ErrArity, cnt)
}
args, err := core.ToSlice(argSeq)
if err != nil {
return nil, err
}
i := 0
if sym, ok := args[i].(builtin.Symbol); ok {
fn.Name = strings.TrimSpace(sym.String())
i++
}
if str, ok := args[i].(builtin.String); ok {
fn.Doc = string(str)
i++
}
// TODO: add support for multi-arity parsing.
fnArgs, ok := args[i].(core.Seq)
if !ok {
return nil, fmt.Errorf(
"expecting a list of symbols, got '%s'", reflect.TypeOf(args[i]))
}
i++
f := builtin.Func{}
fnEnv := env.Child(fn.Name, nil)
argSet := map[string]struct{}{}
err = core.ForEach(fnArgs, func(item core.Any) (bool, error) {
sym, ok := item.(builtin.Symbol)
if !ok {
return true, fmt.Errorf(
"expecting parameter to be a symbol, got '%s'",
reflect.TypeOf(item))
}
if _, found := argSet[string(sym)]; found {
return true, fmt.Errorf("duplicate arg name '%s'", sym)
}
argSet[string(sym)] = struct{}{}
f.Params = append(f.Params, string(sym))
if err := fnEnv.Bind(string(sym), nil); err != nil {
return false, err
}
return false, nil
})
if err != nil {
return nil, err
}
// wrap body in (do <expr>*) and analyze.
bodyExprs, err := builtin.Cons(builtin.Symbol("do"), builtin.NewList(args[i:]...))
if err != nil {
return nil, err
}
body, err := a.Analyze(fnEnv, bodyExprs)
if err != nil {
return nil, err
}
f.Body = body
fn.Env = fnEnv
fn.Funcs = append(fn.Funcs, f)
return &fn, nil
}