This repository has been archived by the owner on May 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
func.go
341 lines (273 loc) · 6.63 KB
/
func.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
package sabre
import (
"fmt"
"reflect"
"strings"
)
// MacroExpand expands the macro invocation form.
func MacroExpand(scope Scope, form Value) (Value, bool, error) {
list, ok := form.(*List)
if !ok || list.Size() == 0 {
return form, false, nil
}
symbol, ok := list.First().(Symbol)
if !ok {
return form, false, nil
}
target, err := symbol.resolveValue(scope)
if err != nil || !isMacro(target) {
return form, false, nil
}
mfn := target.(MultiFn)
v, err := mfn.Expand(scope, list.Values[1:])
return v, true, err
}
// MultiFn represents a multi-arity function or macro definition.
type MultiFn struct {
Name string
IsMacro bool
Methods []Fn
}
// Eval returns the multiFn definition itself.
func (multiFn MultiFn) Eval(_ Scope) (Value, error) { return multiFn, nil }
func (multiFn MultiFn) String() string {
var sb strings.Builder
for _, fn := range multiFn.Methods {
sb.WriteString("[" + strings.Trim(fn.String(), "()") + "] ")
}
s := multiFn.Name + " " + strings.TrimSpace(sb.String())
return "(" + strings.TrimSpace(s) + ")"
}
// Invoke dispatches the call to a method based on number of arguments.
func (multiFn MultiFn) Invoke(scope Scope, args ...Value) (Value, error) {
if multiFn.IsMacro {
form, err := multiFn.Expand(scope, args)
if err != nil {
return nil, err
}
return form.Eval(scope)
}
fn, err := multiFn.selectMethod(args)
if err != nil {
return nil, err
}
argVals, err := evalValueList(scope, args)
if err != nil {
return nil, err
}
result, err := fn.Invoke(scope, argVals...)
if !isRecur(result) {
return result, err
}
for isRecur(result) {
args = result.(*List).Values[1:]
result, err = fn.Invoke(scope, args...)
}
return result, err
}
func isRecur(value Value) bool {
list, ok := value.(*List)
if !ok {
return false
}
sym, ok := list.First().(Symbol)
if !ok {
return false
}
if sym.Value != "recur" {
return false
}
return true
}
// Expand executes the macro body and returns the result of the expansion.
func (multiFn MultiFn) Expand(scope Scope, args []Value) (Value, error) {
fn, err := multiFn.selectMethod(args)
if err != nil {
return nil, err
}
if !multiFn.IsMacro {
return &fn, nil
}
return fn.Invoke(scope, args...)
}
// Compare returns true if 'v' is also a MultiFn and all methods are
// equivalent.
func (multiFn MultiFn) Compare(v Value) bool {
other, ok := v.(MultiFn)
if !ok {
return false
}
sameHeader := (multiFn.Name == other.Name) &&
(multiFn.IsMacro == other.IsMacro) &&
(len(multiFn.Methods) == len(other.Methods))
if !sameHeader {
return false
}
for i, fn1 := range multiFn.Methods {
fn2 := other.Methods[i]
if !fn1.Compare(&fn2) {
return false
}
}
return true
}
func (multiFn MultiFn) selectMethod(args []Value) (Fn, error) {
for _, fn := range multiFn.Methods {
if fn.matchArity(args) {
return fn, nil
}
}
return Fn{}, fmt.Errorf("wrong number of args (%d) to '%s'",
len(args), multiFn.Name)
}
func (multiFn *MultiFn) validate() error {
variadicAt := -1
variadicArity := 0
for idx, method := range multiFn.Methods {
if method.Variadic {
if variadicAt >= 0 {
return fmt.Errorf("can't have multiple variadic overloads")
}
variadicAt = idx
variadicArity = len(method.Args)
}
}
fixedArities := map[int]struct{}{}
for idx, method := range multiFn.Methods {
if method.Variadic {
continue
}
arity := method.minArity()
if variadicAt >= 0 && idx != variadicAt && arity >= variadicArity {
return fmt.Errorf("can't have fixed arity overload with more params than variadic")
}
if _, exists := fixedArities[arity]; exists {
return fmt.Errorf("ambiguous arities defined for '%s'", multiFn.Name)
}
fixedArities[arity] = struct{}{}
}
return nil
}
// Fn represents a function or macro definition.
type Fn struct {
Args []string
Variadic bool
Body Value
Func func(scope Scope, args []Value) (Value, error)
}
// Eval returns the function itself.
func (fn *Fn) Eval(_ Scope) (Value, error) { return fn, nil }
func (fn Fn) String() string {
var sb strings.Builder
for i, arg := range fn.Args {
if i == len(fn.Args)-1 && fn.Variadic {
sb.WriteString(" & " + arg)
} else {
sb.WriteString(arg + " ")
}
}
return "(" + strings.TrimSpace(sb.String()) + ")"
}
// Invoke executes the function with given arguments.
func (fn *Fn) Invoke(scope Scope, args ...Value) (Value, error) {
if fn.Func != nil {
return fn.Func(scope, args)
}
fnScope := NewScope(scope)
for idx := range fn.Args {
var argVal Value
if idx == len(fn.Args)-1 && fn.Variadic {
argVal = &List{
Values: args[idx:],
}
} else {
argVal = args[idx]
}
_ = fnScope.Bind(fn.Args[idx], argVal)
}
if fn.Body == nil {
return Nil{}, nil
}
return Eval(fnScope, fn.Body)
}
// Compare returns true if 'other' is also a function and has the same
// signature and body.
func (fn *Fn) Compare(v Value) bool {
other, ok := v.(*Fn)
if !ok || other == nil {
return false
}
if !reflect.DeepEqual(fn.Args, other.Args) {
return false
}
bothVariadic := (fn.Variadic == other.Variadic)
noFunc := (fn.Func == nil && other.Func == nil)
return bothVariadic && noFunc && Compare(fn.Body, other.Body)
}
func (fn Fn) minArity() int {
if len(fn.Args) > 0 && fn.Variadic {
return len(fn.Args) - 1
}
return len(fn.Args)
}
func (fn Fn) matchArity(args []Value) bool {
argc := len(args)
if fn.Variadic {
return argc >= len(fn.Args)-1
}
return argc == len(fn.Args)
}
func (fn *Fn) parseArgSpec(spec Value) error {
vec, isVector := spec.(Vector)
if !isVector {
return fmt.Errorf("argument spec must be a vector of symbols, not '%s'",
reflect.TypeOf(spec))
}
argNames, err := toArgNames(vec.Values)
if err != nil {
return err
}
fn.Variadic, err = checkVariadic(argNames)
if err != nil {
return err
}
if fn.Variadic {
argc := len(argNames)
fn.Args = append(argNames[:argc-2], argNames[argc-1])
} else {
fn.Args = argNames
}
return nil
}
func checkVariadic(args []string) (bool, error) {
for i, arg := range args {
if arg != "&" {
continue
}
if i > len(args)-2 {
return false, fmt.Errorf("expecting one more symbol after '&'")
} else if i < len(args)-2 {
return false, fmt.Errorf("expecting only one symbol after '&'")
}
return true, nil
}
return false, nil
}
func toArgNames(vals []Value) ([]string, error) {
var names []string
for i, v := range vals {
sym, isSymbol := v.(Symbol)
if !isSymbol {
return nil, fmt.Errorf(
"expecting symbol at '%d', not '%s'",
i, reflect.TypeOf(v),
)
}
names = append(names, sym.Value)
}
return names, nil
}
func isMacro(target Value) bool {
multiFn, ok := target.(MultiFn)
return ok && multiFn.IsMacro
}