1 |
package amber |
2 |
|
3 |
import ( |
4 |
"encoding/json" |
5 |
"fmt" |
6 |
"html/template" |
7 |
"reflect" |
8 |
) |
9 |
|
10 |
var FuncMap = template.FuncMap{ |
11 |
"__amber_add": runtime_add, |
12 |
"__amber_sub": runtime_sub, |
13 |
"__amber_mul": runtime_mul, |
14 |
"__amber_quo": runtime_quo, |
15 |
"__amber_rem": runtime_rem, |
16 |
"__amber_minus": runtime_minus, |
17 |
"__amber_plus": runtime_plus, |
18 |
"__amber_eql": runtime_eql, |
19 |
"__amber_gtr": runtime_gtr, |
20 |
"__amber_lss": runtime_lss, |
21 |
|
22 |
"json": runtime_json, |
23 |
"unescaped": runtime_unescaped, |
24 |
} |
25 |
|
26 |
func runtime_add(x, y interface{}) interface{} { |
27 |
vx, vy := reflect.ValueOf(x), reflect.ValueOf(y) |
28 |
switch vx.Kind() { |
29 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
30 |
{ |
31 |
switch vy.Kind() { |
32 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
33 |
return vx.Int() + vy.Int() |
34 |
case reflect.Float32, reflect.Float64: |
35 |
return float64(vx.Int()) + vy.Float() |
36 |
case reflect.String: |
37 |
return fmt.Sprintf("%d%s", vx.Int(), vy.String()) |
38 |
} |
39 |
} |
40 |
case reflect.Float32, reflect.Float64: |
41 |
{ |
42 |
switch vy.Kind() { |
43 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
44 |
return vx.Float() + float64(vy.Int()) |
45 |
case reflect.Float32, reflect.Float64: |
46 |
return vx.Float() + vy.Float() |
47 |
case reflect.String: |
48 |
return fmt.Sprintf("%f%s", vx.Float(), vy.String()) |
49 |
} |
50 |
} |
51 |
case reflect.String: |
52 |
{ |
53 |
switch vy.Kind() { |
54 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
55 |
return fmt.Sprintf("%s%d", vx.String(), vy.Int()) |
56 |
case reflect.Float32, reflect.Float64: |
57 |
return fmt.Sprintf("%s%f", vx.String(), vy.Float()) |
58 |
case reflect.String: |
59 |
return fmt.Sprintf("%s%s", vx.String(), vy.String()) |
60 |
} |
61 |
} |
62 |
} |
63 |
|
64 |
return "<nil>" |
65 |
} |
66 |
|
67 |
func runtime_sub(x, y interface{}) interface{} { |
68 |
vx, vy := reflect.ValueOf(x), reflect.ValueOf(y) |
69 |
switch vx.Kind() { |
70 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
71 |
{ |
72 |
switch vy.Kind() { |
73 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
74 |
return vx.Int() - vy.Int() |
75 |
case reflect.Float32, reflect.Float64: |
76 |
return float64(vx.Int()) - vy.Float() |
77 |
} |
78 |
} |
79 |
case reflect.Float32, reflect.Float64: |
80 |
{ |
81 |
switch vy.Kind() { |
82 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
83 |
return vx.Float() - float64(vy.Int()) |
84 |
case reflect.Float32, reflect.Float64: |
85 |
return vx.Float() - vy.Float() |
86 |
} |
87 |
} |
88 |
} |
89 |
|
90 |
return "<nil>" |
91 |
} |
92 |
|
93 |
func runtime_mul(x, y interface{}) interface{} { |
94 |
vx, vy := reflect.ValueOf(x), reflect.ValueOf(y) |
95 |
switch vx.Kind() { |
96 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
97 |
{ |
98 |
switch vy.Kind() { |
99 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
100 |
return vx.Int() * vy.Int() |
101 |
case reflect.Float32, reflect.Float64: |
102 |
return float64(vx.Int()) * vy.Float() |
103 |
} |
104 |
} |
105 |
case reflect.Float32, reflect.Float64: |
106 |
{ |
107 |
switch vy.Kind() { |
108 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
109 |
return vx.Float() * float64(vy.Int()) |
110 |
case reflect.Float32, reflect.Float64: |
111 |
return vx.Float() * vy.Float() |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
return "<nil>" |
117 |
} |
118 |
|
119 |
func runtime_quo(x, y interface{}) interface{} { |
120 |
vx, vy := reflect.ValueOf(x), reflect.ValueOf(y) |
121 |
switch vx.Kind() { |
122 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
123 |
{ |
124 |
switch vy.Kind() { |
125 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
126 |
return vx.Int() / vy.Int() |
127 |
case reflect.Float32, reflect.Float64: |
128 |
return float64(vx.Int()) / vy.Float() |
129 |
} |
130 |
} |
131 |
case reflect.Float32, reflect.Float64: |
132 |
{ |
133 |
switch vy.Kind() { |
134 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
135 |
return vx.Float() / float64(vy.Int()) |
136 |
case reflect.Float32, reflect.Float64: |
137 |
return vx.Float() / vy.Float() |
138 |
} |
139 |
} |
140 |
} |
141 |
|
142 |
return "<nil>" |
143 |
} |
144 |
|
145 |
func runtime_rem(x, y interface{}) interface{} { |
146 |
vx, vy := reflect.ValueOf(x), reflect.ValueOf(y) |
147 |
switch vx.Kind() { |
148 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
149 |
{ |
150 |
switch vy.Kind() { |
151 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
152 |
return vx.Int() % vy.Int() |
153 |
} |
154 |
} |
155 |
} |
156 |
|
157 |
return "<nil>" |
158 |
} |
159 |
|
160 |
func runtime_minus(x interface{}) interface{} { |
161 |
vx := reflect.ValueOf(x) |
162 |
switch vx.Kind() { |
163 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
164 |
return -vx.Int() |
165 |
case reflect.Float32, reflect.Float64: |
166 |
return -vx.Float() |
167 |
} |
168 |
|
169 |
return "<nil>" |
170 |
} |
171 |
|
172 |
func runtime_plus(x interface{}) interface{} { |
173 |
vx := reflect.ValueOf(x) |
174 |
switch vx.Kind() { |
175 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
176 |
return +vx.Int() |
177 |
case reflect.Float32, reflect.Float64: |
178 |
return +vx.Float() |
179 |
} |
180 |
|
181 |
return "<nil>" |
182 |
} |
183 |
|
184 |
func runtime_eql(x, y interface{}) bool { |
185 |
vx, vy := reflect.ValueOf(x), reflect.ValueOf(y) |
186 |
switch vx.Kind() { |
187 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
188 |
{ |
189 |
switch vy.Kind() { |
190 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
191 |
return vx.Int() == vy.Int() |
192 |
case reflect.Float32, reflect.Float64: |
193 |
return float64(vx.Int()) == vy.Float() |
194 |
case reflect.String: |
195 |
return fmt.Sprintf("%d", vx.Int()) == vy.String() |
196 |
} |
197 |
} |
198 |
case reflect.Float32, reflect.Float64: |
199 |
{ |
200 |
switch vy.Kind() { |
201 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
202 |
return vx.Float() == float64(vy.Int()) |
203 |
case reflect.Float32, reflect.Float64: |
204 |
return vx.Float() == vy.Float() |
205 |
case reflect.String: |
206 |
return fmt.Sprintf("%f", vx.Float()) == vy.String() |
207 |
} |
208 |
} |
209 |
case reflect.String: |
210 |
{ |
211 |
switch vy.Kind() { |
212 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
213 |
return vx.String() == fmt.Sprintf("%d", vy.Int()) |
214 |
case reflect.Float32, reflect.Float64: |
215 |
return vx.String() == fmt.Sprintf("%f", vy.Float()) |
216 |
case reflect.String: |
217 |
return vx.String() == fmt.Sprintf("%s", vy.String()) |
218 |
} |
219 |
} |
220 |
case reflect.Bool: |
221 |
{ |
222 |
switch vy.Kind() { |
223 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
224 |
return vx.Bool() && vy.Int() != 0 |
225 |
case reflect.Bool: |
226 |
return vx.Bool() == vy.Bool() |
227 |
} |
228 |
} |
229 |
} |
230 |
|
231 |
return false |
232 |
} |
233 |
|
234 |
func runtime_lss(x, y interface{}) bool { |
235 |
vx, vy := reflect.ValueOf(x), reflect.ValueOf(y) |
236 |
switch vx.Kind() { |
237 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
238 |
{ |
239 |
switch vy.Kind() { |
240 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
241 |
return vx.Int() < vy.Int() |
242 |
case reflect.Float32, reflect.Float64: |
243 |
return float64(vx.Int()) < vy.Float() |
244 |
case reflect.String: |
245 |
return fmt.Sprintf("%d", vx.Int()) < vy.String() |
246 |
} |
247 |
} |
248 |
case reflect.Float32, reflect.Float64: |
249 |
{ |
250 |
switch vy.Kind() { |
251 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
252 |
return vx.Float() < float64(vy.Int()) |
253 |
case reflect.Float32, reflect.Float64: |
254 |
return vx.Float() < vy.Float() |
255 |
case reflect.String: |
256 |
return fmt.Sprintf("%f", vx.Float()) < vy.String() |
257 |
} |
258 |
} |
259 |
case reflect.String: |
260 |
{ |
261 |
switch vy.Kind() { |
262 |
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int16, reflect.Int8: |
263 |
return vx.String() < fmt.Sprintf("%d", vy.Int()) |
264 |
case reflect.Float32, reflect.Float64: |
265 |
return vx.String() < fmt.Sprintf("%f", vy.Float()) |
266 |
case reflect.String: |
267 |
return vx.String() < vy.String() |
268 |
} |
269 |
} |
270 |
} |
271 |
|
272 |
return false |
273 |
} |
274 |
|
275 |
func runtime_gtr(x, y interface{}) bool { |
276 |
return !runtime_lss(x, y) && !runtime_eql(x, y) |
277 |
} |
278 |
|
279 |
func runtime_json(x interface{}) (res string, err error) { |
280 |
bres, err := json.Marshal(x) |
281 |
res = string(bres) |
282 |
return |
283 |
} |
284 |
|
285 |
func runtime_unescaped(x string) interface{} { |
286 |
return template.HTML(x) |
287 |
} |