-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnative_fun.go
More file actions
209 lines (180 loc) · 5.32 KB
/
Copy pathnative_fun.go
File metadata and controls
209 lines (180 loc) · 5.32 KB
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
package feel
import (
"fmt"
"sync"
)
type ArgTypeError struct {
index int
expect string
}
func (self ArgTypeError) Error() string {
return fmt.Sprintf("type error at %d, expect %s", self.index, self.expect)
}
type ArgSizeError struct {
has int
expect int
}
func (self ArgSizeError) Error() string {
return fmt.Sprintf("argument size error, has %d, expect %d", self.has, self.expect)
}
// NativeFunDef native function
type NativeFunDef func(args map[string]interface{}) (interface{}, error)
type NativeFun struct {
fn NativeFunDef
requiredArgNames []string
optionalArgNames []string
varArgName string
help string
}
func NewNativeFunc(fn NativeFunDef) *NativeFun {
return &NativeFun{fn: fn}
}
func (nativeFun *NativeFun) Required(argNames ...string) *NativeFun {
nativeFun.requiredArgNames = append(nativeFun.requiredArgNames, argNames...)
return nativeFun
}
func (nativeFun *NativeFun) Optional(argNames ...string) *NativeFun {
nativeFun.optionalArgNames = append(nativeFun.optionalArgNames, argNames...)
return nativeFun
}
func (nativeFun *NativeFun) Vararg(argName string) *NativeFun {
nativeFun.varArgName = argName
return nativeFun
}
func (nativeFun *NativeFun) Help(help string) *NativeFun {
nativeFun.help = help
return nativeFun
}
func (nativeFun NativeFun) ArgNameAt(at int) (string, bool) {
if at >= 0 && at < len(nativeFun.requiredArgNames) {
return nativeFun.requiredArgNames[at], true
} else if at >= len(nativeFun.requiredArgNames) && at < len(nativeFun.requiredArgNames)+len(nativeFun.optionalArgNames) {
return nativeFun.optionalArgNames[at-len(nativeFun.requiredArgNames)], true
}
return "", false
}
func (nativeFun *NativeFun) Call(intp *Interpreter, args map[string]interface{}) (interface{}, error) {
v, err := nativeFun.fn(args)
if err != nil {
return nil, err
}
return normalizeValue(v), nil
}
type MacroDef func(intp *Interpreter, args map[string]Node, varArgs []Node) (interface{}, error)
type Macro struct {
fn MacroDef
requiredArgNames []string
optionalArgNames []string
varArgName string
help string
}
func NewMacro(fn MacroDef) *Macro {
return &Macro{fn: fn}
}
func (self *Macro) Required(argNames ...string) *Macro {
self.requiredArgNames = append(self.requiredArgNames, argNames...)
return self
}
func (self *Macro) Optional(argNames ...string) *Macro {
self.optionalArgNames = append(self.optionalArgNames, argNames...)
return self
}
func (self *Macro) Vararg(argName string) *Macro {
self.varArgName = argName
return self
}
func (self *Macro) Help(help string) *Macro {
self.help = help
return self
}
type Prelude struct {
vars map[string]interface{}
}
var loadOnce sync.Once
var inst *Prelude
func GetPrelude() *Prelude {
loadOnce.Do(func() {
inst = &Prelude{vars: make(map[string]interface{})}
inst.Load()
})
return inst
}
func (self *Prelude) Load() {
self.Bind("bind", NewMacro(func(intp *Interpreter, args map[string]Node, varArgs []Node) (any, error) {
name, err := args["name"].Eval(intp)
strName, ok := name.(string)
if !ok {
return nil, NewErrTypeMismatch("string")
}
v, err := args["value"].Eval(intp)
if err != nil {
return nil, err
}
intp.Bind(strName, v)
return v, nil
}).Required("name", "value").Help("bind value to name in current top scope"))
self.Bind("set", NewMacro(func(intp *Interpreter, args map[string]Node, varArgs []Node) (any, error) {
name, err := args["name"].Eval(intp)
strName, ok := name.(string)
if !ok {
return nil, NewErrTypeMismatch("string")
}
v, err := args["value"].Eval(intp)
if err != nil {
return nil, err
}
if intp.Set(strName, v) {
return v, nil
} else {
intp.Bind(strName, v)
return v, nil
}
}).Required("name", "value").Help("bind value to name in resolved scope, if not found, it's bind to current top scope(the same as 'bind')"))
self.Bind("block", NewMacro(func(intp *Interpreter, args map[string]Node, exprlist []Node) (interface{}, error) {
var lastValue interface{}
var err error
for _, expr := range exprlist {
lastValue, err = expr.Eval(intp)
if err != nil {
return nil, err
}
}
return lastValue, nil
}).Vararg("express list").Help("quote a sequence of expresses and return the last result"))
self.Bind("help", NewMacro(func(intp *Interpreter, args map[string]Node, exprlist []Node) (interface{}, error) {
v, err := args["value"].Eval(intp)
if err != nil {
return nil, err
}
switch vv := v.(type) {
case *NativeFun:
return vv.help, nil
case *Macro:
return vv.help, nil
default:
return typeName(vv), nil
}
}).Required("value").Help("the help information of a value"))
self.Bind("typeof", NewMacro(func(intp *Interpreter, args map[string]Node, exprlist []Node) (interface{}, error) {
v, err := args["value"].Eval(intp)
if err != nil {
return nil, err
}
return typeName(v), nil
}).Required("value").Help("the type of a value"))
installDatetimeFunctions(self)
installBuiltinFunctions(self)
installContextFunctions(self)
installRangeFunctions(self)
}
func (self *Prelude) Bind(name string, value interface{}) *Prelude {
if _, ok := self.vars[name]; ok {
panic(fmt.Sprintf("bind(), name '%s' already bound", name))
}
self.vars[name] = normalizeValue(value)
return self
}
func (self *Prelude) Resolve(name string) (interface{}, bool) {
v, ok := self.vars[name]
return v, ok
}