/
default.go
403 lines (368 loc) · 11.5 KB
/
default.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package function
import (
"math"
"strings"
"github.com/5anthosh/chili/evaluator/datatype"
"github.com/shopspring/decimal"
)
var one = decimal.NewFromInt(1)
var three = decimal.NewFromInt(3)
var two = decimal.NewFromInt(2)
//E Euler constant
var E, _ = decimal.NewFromString("2.71828182845904523536028747135266249775724709369995957496696763")
//PI constant
var PI, _ = decimal.NewFromString("3.14159265358979323846264338327950288419716939937510582097494459")
func absImpl(args []interface{}) (interface{}, error) {
arg := args[0]
return arg.(decimal.Decimal).Abs(), nil
}
func cbrtImpl(args []interface{}) (interface{}, error) {
arg := args[0].(decimal.Decimal)
result, _ := root(arg.BigFloat(), 3).Float64()
return decimal.NewFromFloat(result), nil
}
func ceilImpl(args []interface{}) (interface{}, error) {
arg := args[0]
return arg.(decimal.Decimal).Ceil(), nil
}
func floorImpl(args []interface{}) (interface{}, error) {
arg := args[0]
return arg.(decimal.Decimal).Floor(), nil
}
func expImpl(args []interface{}) (interface{}, error) {
arg := args[0]
return E.Pow(arg.(decimal.Decimal)), nil
}
// TODO implement natural log using decimal.Decimal
func lnImpl(args []interface{}) (interface{}, error) {
arg := args[0].(decimal.Decimal)
v, _ := arg.Float64()
return decimal.NewFromFloat(math.Log(v)), nil
}
func log10Impl(args []interface{}) (interface{}, error) {
arg := args[0].(decimal.Decimal)
v, _ := arg.Float64()
return decimal.NewFromFloat(math.Log10(v)), nil
}
func log2Impl(args []interface{}) (interface{}, error) {
arg := args[0].(decimal.Decimal)
v, _ := arg.Float64()
return decimal.NewFromFloat(math.Log2(v)), nil
}
func maxImpl(args []interface{}) (interface{}, error) {
max := args[0].(decimal.Decimal)
for i := 1; i < len(args); i++ {
v := args[i].(decimal.Decimal)
if v.GreaterThan(max) {
max = v
}
}
return max, nil
}
func minImpl(args []interface{}) (interface{}, error) {
min := args[0].(decimal.Decimal)
for i := 1; i < len(args); i++ {
v := args[i].(decimal.Decimal)
if v.LessThan(min) {
min = v
}
}
return min, nil
}
func powImpl(args []interface{}) (interface{}, error) {
base := args[0].(decimal.Decimal)
power := args[1].(decimal.Decimal)
return base.Pow(power), nil
}
func roundImpl(args []interface{}) (interface{}, error) {
arg := args[0].(decimal.Decimal)
v, _ := arg.Float64()
return decimal.NewFromFloat(math.Round(v)), nil
}
func signImpl(args []interface{}) (interface{}, error) {
arg := args[0].(decimal.Decimal)
return decimal.NewFromInt(int64(arg.Sign())), nil
}
func sqrtImpl(args []interface{}) (interface{}, error) {
arg := args[0].(decimal.Decimal)
result, _ := root(arg.BigFloat(), 2).Float64()
return decimal.NewFromFloat(result), nil
}
func toNumberImpl(args []interface{}) (interface{}, error) {
arg := args[0].(string)
return decimal.NewFromString(arg)
}
/**************************String function************************/
func concatImpl(args []interface{}) (interface{}, error) {
var builder strings.Builder
for _, arg := range args {
_, err := builder.WriteString(arg.(string))
if err != nil {
return "", nil
}
}
return builder.String(), nil
}
func containsImpl(args []interface{}) (interface{}, error) {
str1 := args[0].(string)
str2 := args[1].(string)
return strings.Contains(str1, str2), nil
}
func joinImpl(args []interface{}) (interface{}, error) {
delimiter := args[0].(string)
var builder strings.Builder
_, err := builder.WriteString(args[1].(string))
if err != nil {
return nil, err
}
for i := 2; i < len(args); i++ {
_, err = builder.WriteString(delimiter)
if err != nil {
return nil, err
}
_, err = builder.WriteString(args[i].(string))
if err != nil {
return nil, err
}
}
return builder.String(), nil
}
func lengthImpl(args []interface{}) (interface{}, error) {
return len(args[0].(string)), nil
}
func replaceImpl(args []interface{}) (interface{}, error) {
str1 := args[0].(string)
str2 := args[1].(string)
str3 := args[2].(string)
oc := args[3].(decimal.Decimal)
return strings.Replace(str1, str2, str3, int(oc.IntPart())), nil
}
func replaceAllImpl(args []interface{}) (interface{}, error) {
str1 := args[0].(string)
str2 := args[1].(string)
str3 := args[2].(string)
return strings.ReplaceAll(str1, str2, str3), nil
}
func sliceImpl(args []interface{}) (interface{}, error) {
str := args[0].(string)
start := args[1].(decimal.Decimal).IntPart()
stop := args[2].(decimal.Decimal).IntPart()
if start < 0 {
start = 0
}
if stop > int64(len(str)) {
stop = int64(len(str))
}
return str[start:stop], nil
}
// Functions
var (
AbsFunction = Function{
Name: "abs",
Arity: 1,
FunctionImpl: absImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the absolute value of a number.\n Returns a number.",
}
CbrtFunction = Function{
Name: "cbrt",
Arity: 1,
FunctionImpl: cbrtImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the cube root of a number.\n Returns a number.",
}
CeilFunction = Function{
Name: "ceil",
Arity: 1,
FunctionImpl: ceilImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the smallest integer greater than or equal to a number.\n Returns a number.",
}
ExpFunction = Function{
Name: "exp",
Arity: 1,
FunctionImpl: expImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns E^x, where x is the argument, and E is Euler's constant (2.718…), the base of the natural logarithm.\n Returns a number.",
}
FloorFunction = Function{
Name: "floor",
Arity: 1,
FunctionImpl: floorImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the largest integer less than or equal to a number.\n Returns a number.",
}
LnFunction = Function{
Name: "ln",
Arity: 1,
FunctionImpl: lnImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the natural logarithm of a number.\n Returns a number.",
}
Log10Function = Function{
Name: "log10",
Arity: 1,
FunctionImpl: log10Impl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the base 10 logarithm of a number.\n Returns a number.",
}
Log2Function = Function{
Name: "log2",
Arity: 1,
FunctionImpl: log2Impl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the base 2 logarithm of a number.\n Returns a number.",
}
MaxFunction = Function{
Name: "max",
Arity: -1,
MinArity: 1,
MaxArity: MaximumNumberOfParamsLimit,
FunctionImpl: maxImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the largest number from a list, where numbers are separated by commas.\n Returns a number",
}
MinFunction = Function{
Name: "min",
Arity: -1,
MinArity: 1,
MaxArity: MaximumNumberOfParamsLimit,
FunctionImpl: minImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the smallest number from a list, where numbers are separated by commas.\n Returns a number.",
}
PowFunction = Function{
Name: "pow",
Arity: 2,
FunctionImpl: powImpl,
ParamsType: []uint{datatype.NumberType, datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns base to the exponent power.\n Returns a number.",
}
RoundFunction = Function{
Name: "round",
Arity: 1,
FunctionImpl: roundImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Rounds a number to the nearest integer.\n Returns a number.",
}
SignFunction = Function{
Name: "sign",
Arity: 1,
FunctionImpl: signImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the sign of a number, indicating whether it's positive (1), negative (-1) or zero (0).\n Returns a number.",
}
SqrtFunction = Function{
Name: "sqrt",
Arity: 1,
FunctionImpl: sqrtImpl,
ParamsType: []uint{datatype.NumberType},
ReturnType: datatype.NumberType,
Documentation: "Returns the square root of a number.\n Returns a number.",
}
TonumberFunction = Function{
Name: "toNumber",
Arity: 1,
FunctionImpl: toNumberImpl,
ParamsType: []uint{datatype.StringType},
ReturnType: datatype.NumberType,
Documentation: "Converts a text string to a number.\n Returns a number.",
}
ConcantFunction = Function{
Name: "concat",
Arity: -1,
MinArity: 1,
MaxArity: MaximumNumberOfParamsLimit,
FunctionImpl: concatImpl,
ParamsType: []uint{datatype.StringType},
ReturnType: datatype.StringType,
Documentation: "Concatenates (combines) text strings.\n Returns a text string.",
}
ContainsFunction = Function{
Name: "contains",
Arity: 2,
FunctionImpl: containsImpl,
ParamsType: []uint{datatype.StringType, datatype.StringType},
ReturnType: datatype.BooleanType,
Documentation: "Tests whether a text string contains another text string.\n Returns a boolean.",
}
JoinFunction = Function{
Name: "join",
Arity: -1,
MinArity: 2,
MaxArity: MaximumNumberOfParamsLimit,
FunctionImpl: joinImpl,
ParamsType: []uint{datatype.StringType},
ReturnType: datatype.StringType,
Documentation: "Combines text strings, with a specified delimiter.\n Returns a text string.",
}
LengthFunction = Function{
Name: "length",
Arity: 1,
FunctionImpl: lengthImpl,
ParamsType: []uint{datatype.StringType},
ReturnType: datatype.NumberType,
Documentation: "Returns the number of characters in a text string.\n Returns a number.",
}
ReplaceFunction = Function{
Name: "replace",
Arity: 4,
FunctionImpl: replaceImpl,
ParamsType: []uint{datatype.StringType, datatype.StringType, datatype.StringType, datatype.NumberType},
ReturnType: datatype.StringType,
Documentation: "Replaces the n match within a text string with a specified new text string.\n Returns a text string.",
}
ReplaceAllFunction = Function{
Name: "replaceAll",
Arity: 3,
FunctionImpl: replaceAllImpl,
ParamsType: []uint{datatype.StringType, datatype.StringType, datatype.StringType},
ReturnType: datatype.StringType,
Documentation: "Replaces all matches within a text string with a specified new text string.\n Returns a text string.",
}
SliceFunction = Function{
Name: "slice",
Arity: 3,
FunctionImpl: sliceImpl,
ParamsType: []uint{datatype.StringType, datatype.NumberType, datatype.NumberType},
ReturnType: datatype.StringType,
Documentation: "Extracts a substring from a text string, given a specified starting point and end point.\n Returns a text string",
}
DefaultFunctions = []Function{
AbsFunction,
CbrtFunction,
CeilFunction,
ExpFunction,
FloorFunction,
LnFunction,
Log2Function,
Log10Function,
MaxFunction,
MinFunction,
PowFunction,
RoundFunction,
SignFunction,
SqrtFunction,
TonumberFunction,
ConcantFunction,
ContainsFunction,
JoinFunction,
LengthFunction,
ReplaceFunction,
ReplaceAllFunction,
SliceFunction,
}
)