forked from xushiwei/qlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine2.go
231 lines (185 loc) · 4.4 KB
/
engine2.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
package qlang
import (
"errors"
"fmt"
"reflect"
"github.com/qiniu/text/tpl/interpreter"
"github.com/xushiwei/qlang/exec"
qcl "github.com/xushiwei/qlang/cl"
qlang "github.com/xushiwei/qlang/spec"
)
// Options represent interpreter options.
//
type Options interpreter.Options
var (
// InsertSemis is interpreter options that means to insert semis smartly.
InsertSemis = (*Options)(interpreter.InsertSemis)
)
// SetReadFile sets the `ReadFile` function.
//
func SetReadFile(fn func(file string) ([]byte, error)) {
qcl.ReadFile = fn
}
// SetFindEntry sets the `FindEntry` function.
//
func SetFindEntry(fn func(file string, libs []string) (string, error)) {
qcl.FindEntry = fn
}
// SetOnPop sets OnPop callback.
//
func SetOnPop(fn func(v interface{})) {
exec.OnPop = fn
}
// SetDumpCode sets dump code mode:
// "1" - dump code with rem instruction.
// "2" - dump code without rem instruction.
// else - don't dump code.
//
func SetDumpCode(dumpCode string) {
switch dumpCode {
case "true", "1":
qcl.DumpCode = 1
case "2":
qcl.DumpCode = 2
default:
qcl.DumpCode = 0
}
}
// Debug sets dump code mode to "1" for debug.
//
func Debug(fn func()) {
SetDumpCode("1")
defer SetDumpCode("0")
fn()
}
// -----------------------------------------------------------------------------
// A Qlang represents the qlang compiler and executor.
//
type Qlang struct {
*exec.Context
cl *qcl.Compiler
}
// New returns a new qlang instance.
//
func New() *Qlang {
cl := qcl.New()
stk := exec.NewStack()
ctx := exec.NewContextEx(cl.GlobalSymbols())
ctx.Stack = stk
ctx.Code = cl.Code()
return &Qlang{ctx, cl}
}
// SetLibs sets lib paths for searching modules.
//
func (p *Qlang) SetLibs(libs string) {
p.cl.SetLibs(libs)
}
// Cl compiles a source code.
//
func (p *Qlang) Cl(codeText []byte, fname string) (end int, err error) {
end = p.cl.Cl(codeText, fname)
p.cl.Done()
p.ResizeVars()
return
}
// SafeCl compiles a source code, without panic (will convert panic into an error).
//
func (p *Qlang) SafeCl(codeText []byte, fname string) (end int, err error) {
defer func() {
if e := recover(); e != nil {
switch v := e.(type) {
case string:
err = errors.New(v)
case error:
err = v
default:
panic(e)
}
}
}()
return p.Cl(codeText, fname)
}
// Exec compiles and executes a source code.
//
func (p *Qlang) Exec(codeText []byte, fname string) (err error) {
code := p.cl.Code()
start := code.Len()
end, err := p.Cl(codeText, fname)
if err != nil {
return
}
if qcl.DumpCode != 0 {
code.Dump(start)
}
p.ExecBlock(start, end, p.cl.GlobalSymbols())
return
}
// Eval compiles and executes a source code.
//
func (p *Qlang) Eval(expr string) (err error) {
return p.Exec([]byte(expr), "")
}
// SafeExec compiles and executes a source code, without panic (will convert panic into an error).
//
func (p *Qlang) SafeExec(code []byte, fname string) (err error) {
defer func() {
if e := recover(); e != nil {
switch v := e.(type) {
case string:
err = errors.New(v)
case error:
err = v
default:
panic(e)
}
}
}()
err = p.Exec(code, fname)
return
}
// SafeEval compiles and executes a source code, without panic (will convert panic into an error).
//
func (p *Qlang) SafeEval(expr string) (err error) {
return p.SafeExec([]byte(expr), "")
}
// InjectMethods injects some methods into a class.
// `pcls` can be a `*exec.Class` object or a `string` typed class name.
//
func (p *Qlang) InjectMethods(pcls interface{}, code []byte) (err error) {
var cls *exec.Class
switch v := pcls.(type) {
case *exec.Class:
cls = v
case string:
val, ok := p.GetVar(v)
if !ok {
return fmt.Errorf("class `%s` not exists", v)
}
if cls, ok = val.(*exec.Class); !ok {
return fmt.Errorf("var `%s` not a class", v)
}
default:
return fmt.Errorf("invalid cls argument type: %v", reflect.TypeOf(pcls))
}
err = p.cl.InjectMethods(cls, code)
p.ResizeVars()
return
}
// Import imports a module written in Go.
//
func Import(mod string, table map[string]interface{}) {
qlang.Import(mod, table)
}
// SetAutoCall is reserved for internal use.
//
func SetAutoCall(t reflect.Type) {
qlang.SetAutoCall(t)
}
// -----------------------------------------------------------------------------
// Exports is the export table of this module.
//
var Exports = map[string]interface{}{
"new": New,
"New": New,
}
// -----------------------------------------------------------------------------