-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
721 lines (569 loc) · 14.2 KB
/
value.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
package gal
import (
"fmt"
"math/big"
"strings"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type Stringer interface {
AsString() String // name is not String so to not clash with fmt.Stringer interface
}
type Numberer interface {
Number() Number
}
type Booler interface {
Bool() Bool
}
type Evaler interface {
Eval() Value
}
func ToValue(value any) Value {
v, _ := toValue(value)
return v
}
func toValue(value any) (Value, bool) {
v, err := goAnyToGalType(value)
if err != nil {
return NewUndefinedWithReasonf("value type %T - %s", value, err.Error()), false
}
return v, true
}
func ToNumber(val Value) Number {
return val.(Numberer).Number() // may panic
}
func ToString(val Value) String {
return val.AsString()
}
func ToBool(val Value) Bool {
return val.(Booler).Bool() // may panic
}
// MultiValue is a container of zero or more Value's.
// For the time being, it is only usable and useful with functions.
// Functions can accept a MultiValue, and also return a MultiValue.
// This allows a function to effectively return multiple values as a MultiValue.
// TODO: we could add a syntax to instantiate a MultiValue within an expression.
// ... perhaps along the lines of [[v1 v2 ...]] or simply a built-in function such as
// ... MultiValue(...) - nothing stops the user from creating their own for now :-)
//
// TODO: implement other methods such as Add, LessThan, etc (if meaningful)
type MultiValue struct {
Undefined
values []Value
}
func NewMultiValue(values ...Value) MultiValue {
return MultiValue{values: values}
}
func (MultiValue) kind() entryKind {
return valueEntryKind
}
// Equal satisfies the external Equaler interface such as in testify assertions and the cmp package
// Note that the current implementation defines equality as values matching and in order they appear.
func (m MultiValue) Equal(other MultiValue) bool {
if m.Size() != other.Size() {
return false
}
for i := range m.values {
// TODO: add test to confirm this is correct!
if m.values[i].NotEqualTo(other.values[i]) == False {
return false
}
}
return true
}
func (m MultiValue) String() string {
var vals []string
for _, val := range m.values {
vals = append(vals, val.String())
}
return strings.Join(vals, `,`)
}
func (m MultiValue) AsString() String {
return NewString(m.String())
}
func (m MultiValue) Get(i int) Value {
if i > len(m.values) {
return NewUndefinedWithReasonf("out of bounds: trying to get arg #%d on MultiValue that has %d arguments", i, len(m.values))
}
return m.values[i]
}
func (m MultiValue) Size() int {
return len(m.values)
}
type String struct {
Undefined
value string
}
func NewString(s string) String {
return String{value: s}
}
func (String) kind() entryKind {
return valueEntryKind
}
// Equal satisfies the external Equaler interface such as in testify assertions and the cmp package
func (s String) Equal(other String) bool {
return s.value == other.value
}
func (s String) LessThan(other Value) Bool {
if v, ok := other.(Stringer); ok {
return NewBool(s.value < v.AsString().value)
}
return False
}
func (s String) LessThanOrEqual(other Value) Bool {
if v, ok := other.(Stringer); ok {
return NewBool(s.value <= v.AsString().value)
}
return False
}
func (s String) EqualTo(other Value) Bool {
if v, ok := other.(Stringer); ok {
return NewBool(s.value == v.AsString().value) // beware to compare what's comparable: do NOT use s.value == v.String() because String() may decorate the value (see String and MultiValue for example)
}
return False
}
func (s String) NotEqualTo(other Value) Bool {
return s.EqualTo(other).Not()
}
func (s String) GreaterThan(other Value) Bool {
if v, ok := other.(Stringer); ok {
return NewBool(s.value > v.AsString().value)
}
return False
}
func (s String) GreaterThanOrEqual(other Value) Bool {
if v, ok := other.(Stringer); ok {
return NewBool(s.value >= v.AsString().value)
}
return False
}
func (s String) Add(other Value) Value {
if v, ok := other.(Stringer); ok {
return String{value: s.value + v.AsString().value}
}
return NewUndefinedWithReasonf("cannot Add non-string to a string")
}
func (s String) Multiply(other Value) Value {
if v, ok := other.(Numberer); ok {
return String{
value: strings.Repeat(s.value, int(v.Number().value.IntPart())),
}
}
return NewUndefinedWithReasonf("NaN: %s", other.String())
}
func (s String) LShift(Value) Value {
return Undefined{} // TODO: could eject characters on the left-wise
}
func (s String) RShift(Value) Value {
return Undefined{} // TODO: could eject characters on the right-wise
}
func (s String) String() string {
return `"` + s.value + `"`
}
func (s String) RawString() string {
return s.value
}
func (s String) AsString() String {
return s
}
func (s String) Number() Number {
n, err := NewNumberFromString(s.value) // beware that `.String()` may decorate the value!!
if err != nil {
panic(err) // TODO :-/
}
return n
}
func (s String) Eval() Value {
tree, err := NewTreeBuilder().FromExpr(s.value)
if err != nil {
return s
}
return tree.Eval()
}
type Number struct {
Undefined
value decimal.Decimal
}
func NewNumber(i int64, exp int32) Number {
d := decimal.New(i, exp)
return Number{value: d}
}
func NewNumberFromInt(i int64) Number {
d := decimal.NewFromInt(i)
return Number{value: d}
}
func NewNumberFromFloat(f float64) Number {
d := decimal.NewFromFloat(f)
return Number{value: d}
}
func NewNumberFromString(s string) (Number, error) {
d, err := decimal.NewFromString(s)
if err != nil {
return Number{}, errors.WithStack(err)
}
return Number{value: d}, nil
}
func (Number) kind() entryKind {
return valueEntryKind
}
// Equal satisfies the external Equaler interface such as in testify assertions and the cmp package
func (n Number) Equal(other Number) bool {
return n.value.Equal(other.value)
}
func (n Number) Add(other Value) Value {
if v, ok := other.(Numberer); ok {
return Number{value: n.value.Add(v.Number().value)}
}
return NewUndefinedWithReasonf("NaN: %s", other.String())
}
func (n Number) Sub(other Value) Value {
if v, ok := other.(Numberer); ok {
return Number{
value: n.value.Sub(v.Number().value),
}
}
return NewUndefinedWithReasonf("NaN: %s", other.String())
}
func (n Number) Multiply(other Value) Value {
if v, ok := other.(Numberer); ok {
return Number{
value: n.value.Mul(v.Number().value),
}
}
return NewUndefinedWithReasonf("NaN: %s", other.String())
}
func (n Number) Divide(other Value) Value {
if v, ok := other.(Numberer); ok {
return Number{
value: n.value.Div(v.Number().value),
}
}
return NewUndefinedWithReasonf("NaN: %s", other.String())
}
func (n Number) PowerOf(other Value) Value {
if v, ok := other.(Numberer); ok {
return Number{
value: n.value.Pow(v.Number().value),
}
}
return NewUndefinedWithReasonf("NaN: %s", other.String())
}
func (n Number) Mod(other Value) Value {
if v, ok := other.(Numberer); ok {
return Number{
value: n.value.Mod(v.Number().value),
}
}
return NewUndefinedWithReasonf("NaN: %s", other.String())
}
func (n Number) IntPart() Value {
return Number{
value: n.value.Truncate(0),
}
}
func (n Number) LShift(other Value) Value {
if v, ok := other.(Numberer); ok {
if v.Number().value.IsNegative() {
return NewUndefinedWithReasonf("invalid negative bitwise shift")
}
if !v.Number().value.IsInteger() {
return NewUndefinedWithReasonf("invalid non-integer bitwise shift")
}
return Number{
value: n.value.Mul(decimal.NewFromInt(2).Pow(v.Number().value)).Floor(),
}
}
return NewUndefinedWithReasonf("NaN: %s", other.String())
}
func (n Number) RShift(other Value) Value {
if v, ok := other.(Numberer); ok {
if v.Number().value.IsNegative() {
return NewUndefinedWithReasonf("invalid negative bitwise shift")
}
if !v.Number().value.IsInteger() {
return NewUndefinedWithReasonf("invalid non-integer bitwise shift")
}
return Number{
value: n.value.Div(decimal.NewFromInt(2).Pow(v.Number().value)).Floor(),
}
}
return NewUndefinedWithReasonf("NaN: %s", other.String())
}
func (n Number) Neg() Number {
return Number{
value: n.value.Neg(),
}
}
func (n Number) Sin() Number {
return Number{
value: n.value.Sin(),
}
}
func (n Number) Cos() Number {
return Number{
value: n.value.Cos(),
}
}
func (n Number) Sqrt() Value {
n, err := NewNumberFromString(
new(big.Float).Sqrt(n.value.BigFloat()).String(),
)
if err != nil {
return NewUndefinedWithReasonf("Sqrt:" + err.Error())
}
return n
}
func (n Number) Tan() Number {
return Number{
value: n.value.Tan(),
}
}
func (n Number) Ln(precision int32) Value {
res, err := n.value.Ln(precision)
if err != nil {
return NewUndefinedWithReasonf("Ln:" + err.Error())
}
return Number{
value: res,
}
}
func (n Number) Log(precision int32) Value {
res, err := n.value.Ln(precision + 1)
if err != nil {
return NewUndefinedWithReasonf("Log:" + err.Error())
}
res10, err := decimal.New(10, 0).Ln(precision + 1)
if err != nil {
return NewUndefinedWithReasonf("Log:" + err.Error())
}
return Number{
value: res.Div(res10).Truncate(precision),
}
}
func (n Number) Floor() Number {
return Number{
value: n.value.Floor(),
}
}
func (n Number) Trunc(precision int32) Number {
return Number{
value: n.value.Truncate(precision),
}
}
func (n Number) Factorial() Value {
if !n.value.IsInteger() || n.value.IsNegative() {
return NewUndefinedWithReasonf("Factorial: requires a positive integer, cannot accept %s", n.String())
}
res := decimal.NewFromInt(1)
one := decimal.NewFromInt(1)
i := decimal.NewFromInt(2)
for i.LessThanOrEqual(n.value) {
res = res.Mul(i)
i = i.Add(one)
}
return Number{
value: res,
}
}
func (n Number) LessThan(other Value) Bool {
if v, ok := other.(Numberer); ok {
return NewBool(n.value.LessThan(v.Number().value))
}
return False
}
func (n Number) LessThanOrEqual(other Value) Bool {
if v, ok := other.(Numberer); ok {
return NewBool(n.value.LessThanOrEqual(v.Number().value))
}
return False
}
func (n Number) EqualTo(other Value) Bool {
if v, ok := other.(Numberer); ok {
return NewBool(n.value.Equal(v.Number().value))
}
return False
}
func (n Number) NotEqualTo(other Value) Bool {
return n.EqualTo(other).Not()
}
func (n Number) GreaterThan(other Value) Bool {
if v, ok := other.(Numberer); ok {
return NewBool(n.value.GreaterThan(v.Number().value))
}
return False
}
func (n Number) GreaterThanOrEqual(other Value) Bool {
if v, ok := other.(Numberer); ok {
return NewBool(n.value.GreaterThanOrEqual(v.Number().value))
}
return False
}
func (n Number) String() string {
return n.value.String()
}
func (n Number) Bool() Bool {
if n.value.IsZero() {
return False
}
return True
}
func (n Number) AsString() String {
return NewString(n.String())
}
func (n Number) Number() Number {
return n
}
func (n Number) Float64() float64 {
return n.value.InexactFloat64()
}
func (n Number) Int64() int64 {
return n.value.IntPart()
}
type Bool struct {
Undefined
value bool
}
func NewBool(b bool) Bool {
return Bool{value: b}
}
// TODO: another option would be to return a Value and hence allow Undefined when neither True nor False is provided.
func NewBoolFromString(s string) (Bool, error) {
switch s {
case "True":
return True, nil
case "False":
return False, nil
default:
return False, errors.Errorf("'%s' cannot be converted to a Bool", s)
}
}
func (Bool) kind() entryKind {
return valueEntryKind
}
// Equal satisfies the external Equaler interface such as in testify assertions and the cmp package
func (b Bool) Equal(other Bool) bool {
return b.value == other.value
}
func (b Bool) EqualTo(other Value) Bool {
if v, ok := other.(Booler); ok {
return NewBool(b.value == v.Bool().value)
}
return False
}
func (b Bool) NotEqualTo(other Value) Bool {
return b.EqualTo(other).Not()
}
func (b Bool) Not() Bool {
return NewBool(!b.value)
}
func (b Bool) And(other Value) Bool {
if v, ok := other.(Booler); ok {
return NewBool(b.value && v.Bool().value)
}
return False // TODO: should Bool be a Maybe?
}
func (b Bool) Or(other Value) Bool {
if v, ok := other.(Booler); ok {
return NewBool(b.value || v.Bool().value)
}
return False // TODO: should Bool be a Maybe?
}
func (b Bool) Bool() Bool {
return b
}
func (b Bool) String() string {
if b.value {
return "True"
}
return "False"
}
func (b Bool) Number() Number {
if b.value {
return NewNumberFromInt(1)
}
return NewNumberFromInt(0)
}
func (b Bool) AsString() String {
return NewString(b.String())
}
var (
False = NewBool(false)
True = NewBool(true)
)
type Undefined struct {
reason string // optional
}
func NewUndefined() Undefined {
return Undefined{}
}
func NewUndefinedWithReasonf(format string, a ...interface{}) Undefined {
return Undefined{
reason: fmt.Sprintf(format, a...),
}
}
func (Undefined) kind() entryKind {
return unknownEntryKind
}
// Equal satisfies the external Equaler interface such as in testify assertions and the cmp package
func (u Undefined) Equal(other Undefined) bool {
return u.reason == other.reason
}
func (u Undefined) EqualTo(other Value) Bool {
return False
}
func (u Undefined) NotEqualTo(other Value) Bool {
return True
}
func (u Undefined) GreaterThan(other Value) Bool {
return False
}
func (u Undefined) GreaterThanOrEqual(other Value) Bool {
return False
}
func (u Undefined) LessThan(other Value) Bool {
return False
}
func (u Undefined) LessThanOrEqual(other Value) Bool {
return False
}
func (Undefined) Add(Value) Value {
return Undefined{}
}
func (Undefined) Sub(Value) Value {
return Undefined{}
}
func (Undefined) Multiply(Value) Value {
return Undefined{}
}
func (Undefined) Divide(Value) Value {
return Undefined{}
}
func (Undefined) PowerOf(Value) Value {
return Undefined{}
}
func (Undefined) Mod(Value) Value {
return Undefined{}
}
func (Undefined) LShift(Value) Value {
return Undefined{}
}
func (Undefined) RShift(Value) Value {
return Undefined{}
}
func (Undefined) And(other Value) Bool {
// perhaps this should be a panic... Or else Bool should be a Maybe?
return False
}
func (Undefined) Or(other Value) Bool {
// perhaps this should be a panic... Or else Bool should be a Maybe?
return False
}
func (u Undefined) String() string {
if u.reason == "" {
return "undefined"
}
return "undefined: " + u.reason
}
func (u Undefined) AsString() String {
return NewString(u.String())
}