-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuiltin_functions.go
255 lines (221 loc) · 6.38 KB
/
builtin_functions.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package dicescript
import (
"errors"
"math"
"strconv"
)
func funcCeil(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
if params[0].TypeId == VMTypeInt {
return params[0]
}
v, ok := params[0].ReadFloat()
if ok {
return NewIntVal(IntType(math.Ceil(v)))
} else {
ctx.Error = errors.New("(ceil)类型错误: 只能是数字类型")
}
return nil
}
func funcRound(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
if params[0].TypeId == VMTypeInt {
return params[0]
}
v, ok := params[0].ReadFloat()
if ok {
return NewIntVal(IntType(math.Round(v)))
} else {
ctx.Error = errors.New("(round)类型错误: 只能是数字类型")
}
return nil
}
func funcFloor(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
if params[0].TypeId == VMTypeInt {
return params[0]
}
v, ok := params[0].ReadFloat()
if ok {
return NewIntVal(IntType(math.Floor(v)))
} else {
ctx.Error = errors.New("(floor)类型错误: 只能是数字类型")
}
return nil
}
func funcAbs(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
v := params[0]
switch v.TypeId {
case VMTypeInt:
val := v.MustReadInt()
if val < 0 {
return NewIntVal(-val)
}
return v
case VMTypeFloat:
val := v.MustReadFloat()
if val < 0 {
return NewFloatVal(-val)
}
return v
}
ctx.Error = errors.New("(abs)类型错误: 参数必须为int或float")
return nil
}
func funcBool(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
v := params[0]
if v.AsBool() {
return NewIntVal(1)
}
return NewIntVal(0)
}
func funcInt(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
switch params[0].TypeId {
case VMTypeInt:
return params[0]
case VMTypeFloat:
v, _ := params[0].ReadFloat()
return NewIntVal(IntType(v))
case VMTypeString:
s, _ := params[0].ReadString()
val, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return NewIntVal(IntType(val))
} else {
ctx.Error = errors.New("(int)值错误: 无法进行 int() 转换: " + s)
}
default:
ctx.Error = errors.New("(int)类型错误: 只能是数字类型")
}
return nil
}
func funcFloat(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
switch params[0].TypeId {
case VMTypeInt:
v, _ := params[0].ReadInt()
return NewFloatVal(float64(v))
case VMTypeFloat:
return params[0]
case VMTypeString:
s, _ := params[0].ReadString()
val, err := strconv.ParseFloat(s, 64)
if err == nil {
return NewFloatVal(val)
} else {
ctx.Error = errors.New("(float)值错误: 无法进行 float() 转换: " + s)
}
default:
ctx.Error = errors.New("(float)类型错误: 只能是数字类型")
}
return nil
}
func funcStr(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
return NewStrVal(params[0].ToString())
}
func funcRepr(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
return NewStrVal(params[0].ToRepr())
}
func funcTypeId(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
return NewIntVal(IntType(params[0].TypeId))
}
func funcLoadBase(ctx *Context, this *VMValue, params []*VMValue, isRaw bool) *VMValue {
v := params[0]
if v.TypeId != VMTypeString {
ctx.Error = errors.New("(load)类型错误: 参数类型必须为str")
return nil
}
name := v.Value.(string)
val := ctx.LoadName(name, true, true)
if ctx.Error != nil {
return nil
}
// computed 回调
doCompute := func(val *VMValue) *VMValue {
if !isRaw {
if val.TypeId == VMTypeComputedValue {
val = val.ComputedExecute(ctx, nil)
if ctx.Error != nil {
return nil
}
}
}
return val
}
if ctx.Config.HookFuncValueLoadOverwrite != nil {
val = ctx.Config.HookFuncValueLoadOverwrite(ctx, name, val, doCompute, &BufferSpan{})
} else {
val = doCompute(val)
}
if ctx.Error != nil {
return nil
}
return val
}
func funcLoad(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
return funcLoadBase(ctx, this, params, false)
}
func funcLoadRaw(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
return funcLoadBase(ctx, this, params, true)
}
func funcStore(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
name := params[0]
if name.TypeId != VMTypeString {
ctx.Error = errors.New("(store)类型错误: 参数1类型必须为str")
return nil
}
val := params[1]
ctx.StoreName(name.Value.(string), val, true)
return val
}
func funcDir(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
typeId := params[0].TypeId
var arr []*VMValue
if v, ok := builtinProto[typeId]; ok {
v.Range(func(key string, value *VMValue) bool {
arr = append(arr, NewStrVal(key))
return true
})
}
if typeId == VMTypeNativeObject {
v := params[0]
d, _ := v.ReadNativeObjectData()
if d.DirFunc != nil {
arr = append(arr, d.DirFunc(ctx)...)
}
}
return NewArrayValRaw(arr)
}
//
// func funcHelp(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
// // 函数名,参数,说明
// return NewStrVal(params[0].ToString())
// }
var nnf = NewNativeFunctionVal
type ndf = NativeFunctionData
var builtinValues = map[string]*VMValue{
"ceil": nnf(&ndf{"ceil", []string{"value"}, nil, nil, funcCeil}),
"floor": nnf(&ndf{"floor", []string{"value"}, nil, nil, funcFloor}),
"round": nnf(&ndf{"round", []string{"value"}, nil, nil, funcRound}),
"abs": nnf(&ndf{"abs", []string{"value"}, nil, nil, funcAbs}),
"int": nnf(&ndf{"int", []string{"value"}, nil, nil, funcInt}),
"float": nnf(&ndf{"float", []string{"value"}, nil, nil, funcFloat}),
"str": nnf(&ndf{"str", []string{"value"}, nil, nil, funcStr}),
"bool": nnf(&ndf{"bool", []string{"value"}, nil, nil, funcBool}),
"repr": nnf(&ndf{"repr", []string{"value"}, nil, nil, funcRepr}),
"load": nnf(&ndf{"load", []string{"value"}, nil, nil, nil}),
"loadRaw": nnf(&ndf{"loadRaw", []string{"value"}, nil, nil, nil}),
"store": nnf(&ndf{"store", []string{"name", "value"}, nil, nil, nil}),
// TODO: roll()
// 要不要进行权限隔绝?
"dir": nnf(&ndf{"dir", []string{"value"}, nil, nil, funcDir}),
// "help": nnf(&ndf{"help", []string{"value"}, nil, nil, funcHelp}),
"typeId": nnf(&ndf{"typeId", []string{"value"}, nil, nil, funcTypeId}),
}
func _init() bool {
// 因循环引用问题无法在上面声明
nfd, _ := builtinValues["load"].ReadNativeFunctionData()
nfd.NativeFunc = funcLoad
nfd, _ = builtinValues["loadRaw"].ReadNativeFunctionData()
nfd.NativeFunc = funcLoadRaw
nfd, _ = builtinValues["store"].ReadNativeFunctionData()
nfd.NativeFunc = funcStore
return false
}
var _ = _init()