-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_function.go
74 lines (68 loc) · 1.65 KB
/
exec_function.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ivyshims
import (
"fmt"
)
// Function represents a unary or binary user-defined operator.
type Function struct {
IsBinary bool
Name string
Left string
Right string
Body []Expr
Locals []string
Globals []string
}
func (fn *Function) String() string {
left := ""
if fn.IsBinary {
left = fn.Left + " "
}
s := fmt.Sprintf("op %s%s %s =", left, fn.Name, fn.Right)
if len(fn.Body) == 1 {
return s + " " + fn.Body[0].ProgString()
}
for _, stmt := range fn.Body {
s += "\n\t" + stmt.ProgString()
}
return s
}
func (fn *Function) EvalUnary(context Context, right Value) Value {
if fn.Body == nil {
Errorf("unary %q undefined", fn.Name)
}
// It's known to be an exec.Context.
c := context.(*ExecContext)
if uint(len(c.frameSizes)) >= c.config.MaxStack() {
Errorf("stack overflow calling %q", fn.Name)
}
c.push(fn)
defer c.pop()
c.AssignLocal(1, right)
v := EvalFunctionBody(c, fn.Name, fn.Body)
if v == nil {
Errorf("no value returned by %q", fn.Name)
}
return v
}
func (fn *Function) EvalBinary(context Context, left, right Value) Value {
if fn.Body == nil {
Errorf("binary %q undefined", fn.Name)
}
// It's known to be an exec.Context.
c := context.(*ExecContext)
if uint(len(c.frameSizes)) >= c.config.MaxStack() {
Errorf("stack overflow calling %q", fn.Name)
}
c.push(fn)
defer c.pop()
c.AssignLocal(1, left)
c.AssignLocal(2, right)
v := EvalFunctionBody(c, fn.Name, fn.Body)
if v == nil {
Errorf("no value returned by %q", fn.Name)
}
return v
}