-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathruntime.go
148 lines (126 loc) · 2.76 KB
/
runtime.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
package gates
import (
"context"
"github.com/lujjjh/gates/syntax"
)
type ToNativeOption int
const (
SkipCircularReference ToNativeOption = 1 << iota
)
func checkToNativeOption(desiredOption ToNativeOption, options int) bool {
return options&int(desiredOption) == int(desiredOption)
}
func convertToNativeOption2BinaryOptions(options []ToNativeOption) int {
ops := 0
for _, op := range options {
ops |= int(op)
}
return ops
}
func Compile(x string) (program *Program, err error) {
defer func() {
if x := recover(); x != nil {
program = nil
switch x1 := x.(type) {
case *CompilerSyntaxError:
err = x1
default:
panic(x)
}
}
}()
e, err := syntax.ParseExpr(x)
if err != nil {
return nil, err
}
compiler := &compiler{
program: &Program{
src: syntax.NewFileSet().AddFile("", -1, len(x)),
},
}
compiler.compile(e)
return compiler.program, nil
}
type Runtime struct {
vm *vm
global *Global
}
func New() *Runtime {
r := &Runtime{}
r.init()
return r
}
func (r *Runtime) init() {
r.vm = &vm{r: r}
r.vm.init()
r.global = NewGlobal()
r.global.initBuiltInFunctions()
r.global.Set("strings", packageStrings())
}
func (r *Runtime) Global() *Global {
return r.global
}
func (r *Runtime) SetCyclesLimit(max int) {
r.vm.cyclesLimit = max
}
func (r *Runtime) RunProgram(ctx context.Context, program *Program) (Value, error) {
r.vm.program = program
r.vm.pc = 0
r.vm.ctx = ctx
if err := r.vm.run(); err != nil {
return nil, err
}
return r.vm.stack.Pop(), nil
}
func (r *Runtime) RunString(s string) (Value, error) {
program, err := Compile(s)
if err != nil {
return nil, err
}
return r.RunProgram(context.Background(), program)
}
func (r *Runtime) Call(f Function, args ...Value) Value {
switch f := f.(type) {
case *nativeFunction:
return f.fun(&functionCall{vm: r.vm, args: args})
case *literalFunction:
vm := r.vm
for i := range args {
vm.stack.Push(args[i])
}
vm.stack.Push(Int(len(args)))
pc := vm.pc
vm.pc = -1
vm.pushCtx()
vm.bp = vm.stack.sp
for i := 0; i < f.stackSize; i++ {
vm.stack.Push(Null)
}
vm.stash = f.stash
vm.program = f.program
vm.pc = 0
if err := vm.run(); err != nil {
panic(err)
}
vm.halt = false
vm.pc = pc
return vm.stack.Pop()
}
return Null
}
func (r *Runtime) ToValue(i interface{}) Value {
return ToValue(i)
}
func (r *Runtime) Context() context.Context { return r.vm.ctx }
type toNativer interface {
toNative(seen map[interface{}]interface{}, options int) interface{}
}
func toNative(seen map[interface{}]interface{}, v Value, options int) (result interface{}) {
if seen == nil {
seen = make(map[interface{}]interface{})
}
if toNativer, haveToNativer := v.(toNativer); haveToNativer {
return toNativer.toNative(seen, options)
}
return v.ToNative()
}