/
exec.go
368 lines (294 loc) · 7.46 KB
/
exec.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
package main
import (
"fmt"
"io"
"reflect"
"runtime"
)
type lambdaValue struct {
args []string
body *CallNode
}
func (v *lambdaValue) String() string {
return fmt.Sprintf("<lambda value with arity %d>", len(v.args))
}
var (
errorType = reflect.TypeOf((*error)(nil)).Elem()
zero reflect.Value
lambdaType = reflect.TypeOf((*lambdaValue)(nil))
)
func Exec(output io.Writer, tree *ListNode, funcs map[string]interface{}) (err error) {
// Convert the functions to reflect values
f := map[string]reflect.Value{}
for name, fn := range funcs {
f[name] = reflect.ValueOf(fn)
}
// Build the environment
s := &state{
vars: make(variables),
funcs: f,
output: output,
t: tree,
}
// Hook up the recover
defer s.recover(&err)
// Start the execution
for _, n := range s.t.Nodes {
s.print(s.walkNode(n))
}
return nil
}
// ========================================================
type variables map[string]reflect.Value
type functions map[string]reflect.Value
type state struct {
funcs functions
vars variables
output io.Writer
t *ListNode
outer *state
}
func (s *state) recover(errp *error) {
if e := recover(); e != nil {
*errp = fmt.Errorf("%s", e)
if _, ok := e.(runtime.Error); ok {
panic(e)
}
}
}
func (s *state) errorf(format string, args ...interface{}) {
panic(fmt.Sprintf(format, args...))
}
func (s *state) print(v reflect.Value) {
// Don't print the zero value
if v == zero {
return
}
// Strings are printed as they come, without newlines
if v.Kind() == reflect.String {
fmt.Fprint(s.output, v.Interface())
return
}
// The rest of values are printed with a newline
// (in prevention of an object printing)
fmt.Fprintln(s.output, v.Interface())
}
func (s *state) walkNode(n Node) reflect.Value {
switch n := n.(type) {
case *CallNode:
return s.walkCall(n)
case *DefineNode:
return s.walkDefine(n)
case *SetNode:
return s.walkSet(n)
case *IfNode:
return s.walkIf(n)
case *BeginNode:
return s.walkBegin(n)
case *VarNode:
return s.walkVar(n)
case *BoolNode:
return s.walkBool(n)
case *NumberNode:
return s.walkNumber(n)
case *StringNode:
return s.walkString(n)
case *LambdaNode:
return s.walkLambda(n)
}
s.errorf("cannot walk the node: %s", n)
panic("not reached")
}
func (s *state) walkCall(n *CallNode) reflect.Value {
// Get the func in the index
f, ok := s.evalFunction(n.Name)
if !ok {
f, ok = s.vars[n.Name]
if !ok || f.Type() != lambdaType {
s.errorf("function not defined: %s", n.Name)
}
// User-defined funcs are called in a different way
return s.walkUserCall(f, n)
}
// Analyze its type
t := f.Type()
numArgs := t.NumIn()
// Check if the number of args it's correct
if t.IsVariadic() {
numArgs -= 1
if len(n.Args) < numArgs {
s.errorf("wrong number of args for %s: want at least %d, got %d", n.Name,
numArgs, len(n.Args))
}
} else if len(n.Args) != numArgs {
s.errorf("wrong number of args for %s: want %d, got %d", n.Name, numArgs, len(n.Args))
}
// Check if the function return it's correct
s.checkFuncReturn(n.Name, t)
// Prepare the arguments array
nargs := numArgs
if t.IsVariadic() {
nargs += len(n.Args) - nargs
}
args := make([]reflect.Value, nargs)
// Add the fixed arguments
i := 0
for ; i < numArgs; i++ {
args[i] = s.evalArg(t.In(i), n.Args[i])
}
// Add the variadic arguments
if t.IsVariadic() {
argType := t.In(numArgs).Elem()
for ; i < len(n.Args); i++ {
args[i] = s.evalArg(argType, n.Args[i])
}
}
// Exec the call
res := f.Call(args)
// Check if the func has and returned an error
if len(res) == 2 && !res[1].IsNil() {
s.errorf("error calling %s: %s", n.Name, res[1].Interface().(error))
}
if t.NumOut() == 0 {
return zero
}
return res[0]
}
func (s *state) checkFuncReturn(name string, t reflect.Type) {
// Check the number of return values
if t.NumOut() == 0 || t.NumOut() == 1 || (t.NumOut() == 2 && t.Out(1) == errorType) {
return
}
s.errorf("can't handle multiple returns from function %s", name)
}
func (s *state) evalFunction(name string) (reflect.Value, bool) {
for {
if s == nil {
break
}
if s.funcs != nil {
f, ok := s.funcs[name]
if ok {
return f, true
}
}
s = s.outer
}
return zero, false
}
// Return the correct value for an argument based on its needed argument.
// It gives the fixed list of types that a Go function can receive from the
// interpreter.
func (s *state) evalArg(t reflect.Type, n Node) reflect.Value {
param := s.walkNode(n)
// Extract the runtime types
tparam := param.Type().Kind()
expected := t.Kind()
// As a special case, unsigned number should be converted from the
// signed ones
switch expected {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if tparam == reflect.Int {
param = reflect.New(t).Elem()
param.SetUint(n.(*NumberNode).Uint64)
}
}
// Signal a type mismatching between the formal and current parameter
// interface{} it's an exception, because accepts all kind of types
if expected != reflect.Interface && expected != tparam {
s.errorf("incorrect argument type, expected %s, got %s", expected, tparam)
}
return param
}
func (s *state) walkUserCall(v reflect.Value, n *CallNode) reflect.Value {
// Check if the variable value it's really a lambda function,
// and not any other kind of value
if v.Type() != lambdaType {
s.errorf("the variable %s is not a function, cannot be called", n.Name)
}
f := v.Interface().(*lambdaValue)
// Check the arity of the func
if len(f.args) != len(n.Args) {
s.errorf("call doesn't use the correct arity: expected %d, got %s",
len(f.args), len(n.Args))
}
// Create the new sub-environment
env := &state{
vars: make(variables),
output: s.output,
outer: s,
}
// Evaluate the arguments
for i, node := range n.Args {
env.vars[f.args[i]] = s.walkNode(node)
}
return env.walkCall(f.body)
}
func (s *state) walkDefine(n *DefineNode) reflect.Value {
name := n.Variable.Name
if _, ok := s.vars[name]; ok {
s.errorf("variable already defined: %s", name)
}
s.vars[name] = s.walkNode(n.Value)
return s.vars[name]
}
func (s *state) walkSet(n *SetNode) reflect.Value {
name := n.Variable.Name
if _, ok := s.vars[name]; !ok {
s.errorf("variable not defined: %s", name)
}
s.vars[name] = s.walkNode(n.Value)
return s.vars[name]
}
func (s *state) walkIf(n *IfNode) reflect.Value {
test := s.walkNode(n.Test)
if test.Kind() == reflect.Bool {
if test.Bool() {
return s.walkNode(n.Conseq)
} else {
return s.walkNode(n.Alt)
}
}
s.errorf("if condition is not a boolean")
panic("not reached")
}
func (s *state) walkBegin(n *BeginNode) (v reflect.Value) {
for _, node := range n.Nodes {
v = s.walkNode(node)
}
return
}
func (s *state) walkNumber(c *NumberNode) reflect.Value {
switch {
case c.IsInt:
n := int(c.Int64)
if int64(n) != c.Int64 {
s.errorf("%s overflows int", c.Text)
}
return reflect.ValueOf(n)
}
panic("not reached")
}
func (s *state) walkVar(n *VarNode) reflect.Value {
value, ok := s.vars[n.Name]
if !ok {
s.errorf("variable not defined: %s", n.Name)
}
return value
}
func (s *state) walkBool(n *BoolNode) reflect.Value {
return reflect.ValueOf(n.Value)
}
func (s *state) walkString(n *StringNode) reflect.Value {
return reflect.ValueOf(n.Text)
}
func (s *state) walkLambda(n *LambdaNode) reflect.Value {
c := &lambdaValue{
args: make([]string, len(n.Args)),
body: n.Body.(*CallNode),
}
for i, arg := range n.Args {
c.args[i] = arg.(*VarNode).Name
}
return reflect.ValueOf(c)
}