-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.go
166 lines (161 loc) · 3.78 KB
/
base.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
package exprel
import (
"bytes"
"math"
"math/rand"
"strings"
)
var baseSource = SourceMap{
// Etc.
"CHOOSE": func(c *Call) (interface{}, error) {
number := c.Number(0)
rest := c.Values[1:]
index := int(number)
if index < 0 || index >= len(rest) {
panic(&RuntimeError{Message: "CHOOSE index out of range"})
}
return rest[index], nil
},
"TYPE": func(c *Call) (interface{}, error) {
if len(c.Values) < 1 {
panic(&RuntimeError{Message: "TYPE requires one argument"})
}
switch c.Values[0].(type) {
case float64:
return float64(1), nil
case string:
return float64(2), nil
case bool:
return float64(4), nil
default:
panic("never reached")
}
},
// Math
"ABS": func(c *Call) (interface{}, error) {
number := c.Number(0)
return math.Abs(number), nil
},
"EXP": func(c *Call) (interface{}, error) {
number := c.Number(0)
return math.Exp(number), nil
},
"LN": func(c *Call) (interface{}, error) {
number := c.Number(0)
return math.Log(number), nil
},
"LOG10": func(c *Call) (interface{}, error) {
number := c.Number(0)
return math.Log10(number), nil
},
"PI": func(c *Call) (interface{}, error) {
return float64(math.Pi), nil
},
"RAND": func(c *Call) (interface{}, error) {
return rand.Float64(), nil
},
"SIGN": func(c *Call) (interface{}, error) {
number := c.Number(0)
if number < 0 {
return float64(-1), nil
}
if number > 0 {
return float64(1), nil
}
return float64(0), nil
},
// Strings
"CHAR": func(c *Call) (interface{}, error) {
var r []rune
for _, v := range c.Values {
code, ok := v.(float64)
if !ok {
panic(&RuntimeError{Message: "CHAR argument must be number"})
}
r = append(r, rune(code))
}
return string(r), nil
},
"JOIN": func(c *Call) (interface{}, error) {
sep := c.String(0)
var buff bytes.Buffer
for i, v := range c.Values[1:] {
str, ok := v.(string)
if !ok {
panic(&RuntimeError{Message: "JOIN arguments must be string"})
}
if i > 0 {
buff.WriteString(sep)
}
buff.WriteString(str)
}
return buff.String(), nil
},
"LEFT": func(c *Call) (interface{}, error) {
str := c.String(0)
count := int(c.OptNumber(1, 1))
if count > len(str) {
return str, nil
}
return str[:count], nil
},
"LEN": func(c *Call) (interface{}, error) {
str := c.String(0)
return float64(len(str)), nil
},
"LOWER": func(c *Call) (interface{}, error) {
str := c.String(0)
return strings.ToLower(str), nil
},
"MID": func(c *Call) (interface{}, error) {
str := c.String(0)
start := int(c.Number(1)) - 1
length := int(c.OptNumber(2, 1))
if len(str) <= start || start < 0 {
return "", nil
}
if len(str) <= start+length {
return str[start:], nil
}
return str[start : start+length], nil
},
"REPT": func(c *Call) (interface{}, error) {
str := c.String(0)
count := c.Number(1)
if count < 0 {
panic(&RuntimeError{Message: "REPT argument must be positive"})
}
return strings.Repeat(str, int(count)), nil
},
"RIGHT": func(c *Call) (interface{}, error) {
str := c.String(0)
count := int(c.OptNumber(1, 1))
if count > len(str) {
return str, nil
}
return str[len(str)-count:], nil
},
"SEARCH": func(c *Call) (interface{}, error) {
needle := c.String(0)
haystack := c.String(1)
start := int(c.OptNumber(2, 1)) - 1
if len(haystack) <= start || start < 0 {
return float64(-1), nil
}
ret := strings.Index(haystack[start:], needle)
if ret == -1 {
return float64(-1), nil
}
return float64(ret + start + 1), nil
},
"TRIM": func(c *Call) (interface{}, error) {
str := c.String(0)
return strings.TrimSpace(str), nil
},
"UPPER": func(c *Call) (interface{}, error) {
str := c.String(0)
return strings.ToUpper(str), nil
},
}
// Base contains the base functions, as described in the package documentation.
var Base Source = baseSource