-
Notifications
You must be signed in to change notification settings - Fork 0
/
std.go
613 lines (518 loc) · 12.9 KB
/
std.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package std
import (
"fmt"
"math"
"github.com/DeedleFake/wdte"
"github.com/DeedleFake/wdte/wdteutil"
)
// Plus is a WDTE function with the following signatures:
//
// + a ...
// (+ a) ...
//
// Returns the sum of a and the rest of its arguments.
func Plus(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(Plus), args...)
}
frame = frame.Sub("+")
var sum wdte.Number
for _, arg := range args {
arg = arg.Call(frame)
if _, ok := arg.(error); ok {
return arg
}
sum += arg.(wdte.Number)
}
return sum
}
// Minus is a WDTE with the following signatures:
//
// - a b
// (- b) a
//
// Returns a minus b.
func Minus(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(Minus), args...)
}
frame = frame.Sub("-")
a1 := args[0].Call(frame)
if _, ok := a1.(error); ok {
return a1
}
a2 := args[1].Call(frame)
if _, ok := a2.(error); ok {
return a2
}
return a1.(wdte.Number) - a2.(wdte.Number)
}
// Times is a WDTE function with the following signatures:
//
// * a ...
// (* a) ...
//
// Returns the product of a and its other arguments.
func Times(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(Times), args...)
}
frame = frame.Sub("*")
p := wdte.Number(1)
for _, arg := range args {
arg = arg.Call(frame)
if _, ok := arg.(error); ok {
return arg
}
p *= arg.(wdte.Number)
}
return p
}
// Div is a WDTE function with the following signatures:
//
// / a b
// (/ b) a
//
// Returns a divided by b.
func Div(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(Div), args...)
}
frame = frame.Sub("/")
a1 := args[0].Call(frame)
if _, ok := a1.(error); ok {
return a1
}
a2 := args[1].Call(frame)
if _, ok := a2.(error); ok {
return a2
}
return a1.(wdte.Number) / a2.(wdte.Number)
}
// Mod is a WDTE function with the following signatures:
//
// % a b
// (% b) a
//
// Returns a mod b.
func Mod(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(Mod), args...)
}
frame = frame.Sub("%")
a1 := args[0].Call(frame)
if _, ok := a1.(error); ok {
return a1
}
a2 := args[1].Call(frame)
if _, ok := a2.(error); ok {
return a2
}
return wdte.Number(math.Mod(
float64(a1.(wdte.Number)),
float64(a2.(wdte.Number)),
))
}
// Equals is a WDTE function with the following signatures:
//
// == a b
// (== b) a
//
// Returns true if a equals b. If a implements wdte.Comparer, the
// equality check is done using that implementation. If a does not but
// b does, b's implementation is used. If neither does, a direct Go
// equality check is used.
func Equals(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(Equals), args...)
}
a1 := args[0].Call(frame)
if _, ok := a1.(error); ok {
return a1
}
a2 := args[1].Call(frame)
if _, ok := a2.(error); ok {
return a2
}
if cmp, ok := a1.(wdte.Comparer); ok {
c, _ := cmp.Compare(a2)
return wdte.Bool(c == 0)
}
if cmp, ok := a2.(wdte.Comparer); ok {
c, _ := cmp.Compare(a1)
return wdte.Bool(c == 0)
}
return wdte.Bool(a1 == a2)
}
// Less is a WDTE function with the following signatures:
//
// < a b
// (< b) a
//
// Returns true if a is less than b. Comparison rules are the same as
// those used for Equals, with the exception that the argument used
// must not only implement wdte.Comparer but that that implementation
// must support ordering.
func Less(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(Less), args...)
}
a1 := args[0].Call(frame)
if _, ok := a1.(error); ok {
return a1
}
a2 := args[1].Call(frame)
if _, ok := a2.(error); ok {
return a2
}
if cmp, ok := a1.(wdte.Comparer); ok {
c, ord := cmp.Compare(a2)
if ord {
return wdte.Bool(c < 0)
}
}
if cmp, ok := a2.(wdte.Comparer); ok {
c, ord := cmp.Compare(a1)
if ord {
return wdte.Bool(c > 0)
}
}
return wdte.Error{
Err: fmt.Errorf("Unable to compare %v and %v", a1, a2),
Frame: frame,
}
}
// Greater is a WDTE function with the following signatures:
//
// > a b
// (> b) a
//
// Returns true if a is greater than b. Comparison rules are the same
// as those used for Equals, with the exception that the argument used
// must not only implement wdte.Comparer but that that implementation
// must support ordering.
func Greater(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(Greater), args...)
}
a1 := args[0].Call(frame)
if _, ok := a1.(error); ok {
return a1
}
a2 := args[1].Call(frame)
if _, ok := a2.(error); ok {
return a2
}
if cmp, ok := a1.(wdte.Comparer); ok {
c, ord := cmp.Compare(a2)
if ord {
return wdte.Bool(c > 0)
}
}
if cmp, ok := a2.(wdte.Comparer); ok {
c, ord := cmp.Compare(a1)
if ord {
return wdte.Bool(c < 0)
}
}
return wdte.Error{
Err: fmt.Errorf("Unable to compare %v and %v", a1, a2),
Frame: frame,
}
}
// LessEqual is a WDTE function with the following signatures:
//
// <= a b
// (<= b) a
//
// Returns true if a is less than or equal to b. Comparison rules are
// the same as those used for Equals, with the exception that the
// argument used must not only implement wdte.Comparer but that that
// implementation must support ordering.
func LessEqual(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(LessEqual), args...)
}
a1 := args[0].Call(frame)
if _, ok := a1.(error); ok {
return a1
}
a2 := args[1].Call(frame)
if _, ok := a2.(error); ok {
return a2
}
if cmp, ok := a1.(wdte.Comparer); ok {
c, ord := cmp.Compare(a2)
if ord {
return wdte.Bool(c <= 0)
}
}
if cmp, ok := a2.(wdte.Comparer); ok {
c, ord := cmp.Compare(a1)
if ord {
return wdte.Bool(c >= 0)
}
}
return wdte.Error{
Err: fmt.Errorf("Unable to compare %v and %v", a1, a2),
Frame: frame,
}
}
// GreaterEqual is a WDTE function with the following signatures:
//
// >= a b
// (>= b) a
//
// Returns true if a is greater than or equal to b. Comparison rules
// are the same as those used for Equals, with the exception that the
// argument used must not only implement wdte.Comparer but that that
// implementation must support ordering.
func GreaterEqual(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(GreaterEqual), args...)
}
a1 := args[0].Call(frame)
if _, ok := a1.(error); ok {
return a1
}
a2 := args[1].Call(frame)
if _, ok := a2.(error); ok {
return a2
}
if cmp, ok := a1.(wdte.Comparer); ok {
c, ord := cmp.Compare(a2)
if ord {
return wdte.Bool(c >= 0)
}
}
if cmp, ok := a2.(wdte.Comparer); ok {
c, ord := cmp.Compare(a1)
if ord {
return wdte.Bool(c <= 0)
}
}
return wdte.Error{
Err: fmt.Errorf("Unable to compare %v and %v", a1, a2),
Frame: frame,
}
}
const (
// True is a WDTE function with the following signature:
//
// true
//
// As you can probably guess, it returns a boolean true.
True wdte.Bool = true
// False is a WDTE function with the following signature:
//
// false
//
// Returns a boolean false. This is rarely necessary as most
// built-in functionality considers any value other than a boolean
// true to be false, but it's provided for completeness.
False wdte.Bool = false
)
// And is a WDTE function with the following signature:
//
// && ...
//
// Returns true if all of its arguments are true.
func And(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("&&")
switch len(args) {
case 0:
return wdte.GoFunc(And)
}
for _, arg := range args {
arg = arg.Call(frame)
if arg != wdte.Bool(true) {
return wdte.Bool(false)
}
}
return wdte.Bool(true)
}
// Or is a WDTE function with the following signature:
//
// || ...
//
// Returns true if any of its arguments are true.
func Or(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("||")
switch len(args) {
case 0:
return wdte.GoFunc(Or)
}
for _, arg := range args {
arg = arg.Call(frame)
if arg == wdte.Bool(true) {
return wdte.Bool(true)
}
}
return wdte.Bool(false)
}
// Not is a WDTE function with the following signature:
//
// ! a
//
// Returns true if a is not true or false if a is not true.
func Not(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("!")
switch len(args) {
case 0:
return wdte.GoFunc(Not)
}
return wdte.Bool(args[0].Call(frame) != wdte.Bool(true))
}
// Len is a WDTE function with the following signature:
//
// len a
//
// Returns the length of a if a implements wdte.Lenner, or false if it
// doesn't.
func Len(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("len")
switch len(args) {
case 0:
return wdte.GoFunc(Len)
}
if lenner, ok := args[0].Call(frame).(wdte.Lenner); ok {
return wdte.Number(lenner.Len())
}
return wdte.Bool(false)
}
// At is a WDTE function with the following signatures:
//
// at a i
// (at i) a
//
// Returns the ith index of a. a is assumed to implement wdte.Atter.
func At(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) <= 1 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(At), args...)
}
frame = frame.Sub("at")
at := args[0].Call(frame).(wdte.Atter)
i := args[1].Call(frame)
ret, err := at.At(i)
if err != nil {
return &wdte.Error{
Frame: frame,
Err: err,
}
}
return ret
}
// Set is a WDTE function with the following signatures:
//
// set con key val
// (set val) con key
// (set key val) con
//
// Set uses con's implementation of Setter to produce a new value from
// con with a key-val mapping applied to it. For example,
//
// set [1; 2; 3] 1 5
//
// returns a new Array containing [1; 5; 3].
func Set(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("set")
if len(args) < 3 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(Set), args...)
}
s := args[0].Call(frame).(wdte.Setter)
k := args[1].Call(frame)
v := args[2]
r, err := s.Set(k, v)
if err != nil {
return &wdte.Error{
Frame: frame,
Err: err,
}
}
return r
}
// Collect is a WDTE function with the following signature:
//
// collect compound
//
// Collect takes a compound as its argument and returns the scope
// collected from executing that compound. The argument must be a
// compound literal or the function will fail. Assigning a compound to
// an ID and then passing that ID will not work.
func Collect(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) == 0 {
return wdte.GoFunc(Collect)
}
frame = frame.Sub("collect")
sf := args[0].(*wdte.ScopedFunc)
s, _ := sf.Func.(wdte.Compound).Collect(frame.WithScope(sf.Scope))
return s
}
// Known is a WDTE function with the following signature:
//
// known scope
//
// Returns an array containing known identifiers in the given scope
// sorted alphabetically.
func Known(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) == 0 {
return wdte.GoFunc(Known)
}
frame = frame.Sub("known")
s := args[0].Call(frame).(*wdte.Scope)
k := s.Known()
ret := make(wdte.Array, 0, len(k))
for _, id := range k {
ret = append(ret, wdte.String(id))
}
return ret
}
// Reflect is a WDTE function with the following signature:
//
// reflect v type
// (reflect type) v
//
// It provides a simple wrapper around wdte.Reflect, checking
// underlying type compatability.
func Reflect(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("reflect")
if len(args) < 2 {
return wdteutil.SaveArgsReverse(wdte.GoFunc(Reflect), args...)
}
v := args[0].Call(frame)
t := args[1].Call(frame).(wdte.String)
return wdte.Bool(wdte.Reflect(v, string(t)))
}
// Scope is a scope containing the functions in this package.
//
// This scope is primarily useful for bootstrapping an environment for
// running scripts in. To use it, simply pass a frame containing it or
// a subscope of it to a function call. In many cases, a client can
// simply call F to obtain such a frame.
var Scope = wdte.S().Map(map[wdte.ID]wdte.Func{
"+": wdte.GoFunc(Plus),
"-": wdte.GoFunc(Minus),
"*": wdte.GoFunc(Times),
"/": wdte.GoFunc(Div),
"%": wdte.GoFunc(Mod),
"==": wdte.GoFunc(Equals),
"<": wdte.GoFunc(Less),
">": wdte.GoFunc(Greater),
"<=": wdte.GoFunc(LessEqual),
">=": wdte.GoFunc(GreaterEqual),
"true": True,
"false": False,
"&&": wdte.GoFunc(And),
"||": wdte.GoFunc(Or),
"!": wdte.GoFunc(Not),
"len": wdte.GoFunc(Len),
"at": wdte.GoFunc(At),
"collect": wdte.GoFunc(Collect),
"known": wdte.GoFunc(Known),
"set": wdte.GoFunc(Set),
"reflect": wdte.GoFunc(Reflect),
})
// F returns a top-level frame that has S as its scope.
func F() wdte.Frame {
return wdte.F().WithScope(Scope)
}