-
Notifications
You must be signed in to change notification settings - Fork 5
/
function.go
211 lines (172 loc) · 4.13 KB
/
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
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
package exec
import (
"errors"
"fmt"
"os"
)
var (
// ErrReturn is used for `panic(ErrReturn)`
ErrReturn = errors.New("return")
)
// -----------------------------------------------------------------------------
// Exit
func doExit(ret interface{}) {
code := 0
if ret != nil {
code = ret.(int)
}
os.Exit(code)
}
var (
// Exit is the instruction that executes `exit(<code>)`.
Exit = Call(doExit)
)
// -----------------------------------------------------------------------------
// Return
type iReturn int
func (arity iReturn) Exec(stk *Stack, ctx *Context) {
if arity == 0 {
ctx.ret = nil
} else if arity == 1 {
ctx.ret, _ = stk.Pop()
} else {
ctx.ret = stk.PopNArgs(int(arity))
}
if ctx.parent != nil {
panic(ErrReturn) // 利用 panic 来实现 return (正常退出)
}
ctx.ExecDefers()
if ctx.ret == nil {
os.Exit(0)
}
if v, ok := ctx.ret.(int); ok {
os.Exit(v)
}
panic("must return `int` for main function")
}
// Return returns an instruction that means `return <expr1>, ..., <exprN>`.
//
func Return(arity int) Instr {
return iReturn(arity)
}
// -----------------------------------------------------------------------------
// Defer
type iDefer struct {
start int
end int
}
func (p *iDefer) Exec(stk *Stack, ctx *Context) {
ctx.defers = &theDefer{
next: ctx.defers,
start: p.start,
end: p.end,
}
}
// Defer returns an instruction that executes `defer <expr>`.
//
func Defer(start, end int) Instr {
return &iDefer{start, end}
}
// -----------------------------------------------------------------------------
// Recover
type iRecover int
func (p iRecover) Exec(stk *Stack, ctx *Context) {
if parent := ctx.parent; parent != nil {
e := parent.Recov
parent.Recov = nil
stk.Push(e)
} else {
stk.Push(nil)
}
}
var (
// Recover is the instruction that executes `recover()`.
Recover Instr = iRecover(0)
)
// -----------------------------------------------------------------------------
// NewFunction
// A Function represents a function object.
//
type Function struct {
Cls *Class
Parent *Context
start int
end int
symtbl map[string]int
Args []string
Variadic bool
}
// NewFunction creates a new function object.
//
func NewFunction(cls *Class, start, end int, symtbl map[string]int, args []string, variadic bool) *Function {
return &Function{cls, nil, start, end, symtbl, args, variadic}
}
// Call calls this function with default context.
//
func (p *Function) Call(stk *Stack, args ...interface{}) (ret interface{}) {
parent := p.Parent
ctx := &Context{
parent: parent,
Stack: stk,
Code: parent.Code,
modmgr: parent.modmgr,
base: stk.BaseFrame(),
}
ctx.initVars(p.symtbl)
return p.ExtCall(ctx, args...)
}
// ExtCall calls this function with a specified context.
//
func (p *Function) ExtCall(ctx *Context, args ...interface{}) (ret interface{}) {
n := len(p.Args)
if p.Variadic {
if len(args) < n-1 {
panic(fmt.Sprintf("function requires >= %d arguments, but we got %d", n-1, len(args)))
}
} else {
if len(args) != n {
panic(fmt.Sprintf("function requires %d arguments, but we got %d", n, len(args)))
}
}
if p.start == p.end {
return nil
}
stk := ctx.Stack
vars := ctx.Vars()
if p.Variadic {
for i := 0; i < n-1; i++ {
vars[i] = args[i]
}
vars[n-1] = args[n-1:]
} else {
for i, arg := range args {
vars[i] = arg
}
}
defer func() {
if e := recover(); e != ErrReturn { // 正常 return 导致,见 (*iReturn).Exec 函数
ctx.Recov = e
}
ret = ctx.ret
ctx.ExecDefers()
stk.SetFrame(ctx.base)
if ctx.Recov != nil {
panic(ctx.Recov)
}
}()
ctx.Code.Exec(p.start, p.end, stk, ctx)
return
}
// -----------------------------------------------------------------------------
type iFunc Function
func (p *iFunc) Exec(stk *Stack, ctx *Context) {
p.Parent = ctx
stk.Push((*Function)(p))
}
// Func returns an instruction that create a function object.
//
func Func(cls *Class, start, end int, symtbl map[string]int, args []string, variadic bool) Instr {
f := NewFunction(cls, start, end, symtbl, args, variadic)
return (*iFunc)(f)
}
// -----------------------------------------------------------------------------