-
Notifications
You must be signed in to change notification settings - Fork 20
/
expressions.go
815 lines (692 loc) · 19.8 KB
/
expressions.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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
package datalog
import (
"errors"
"fmt"
"math/big"
"regexp"
"strings"
)
// maxStackSize defines the maximum number of elements that can be stored on the stack.
// Trying to store more than maxStackSize elements returns an error.
const maxStackSize = 1000
var (
ErrExprDivByZero = errors.New("datalog: Div by zero")
ErrInt64Overflow = errors.New("datalog: expression overflowed int64")
)
type Expression []Op
func (e *Expression) Evaluate(values map[Variable]*Term, symbols *SymbolTable) (Term, error) {
s := &stack{}
for _, op := range *e {
switch op.Type() {
case OpTypeValue:
id := op.(Value).ID
switch id.Type() {
case TermTypeVariable:
idptr, ok := values[id.(Variable)]
if !ok {
return nil, fmt.Errorf("datalog: expressions: unknown variable %d", id.(Variable))
}
id = *idptr
}
s.Push(id)
case OpTypeUnary:
v, err := s.Pop()
if err != nil {
return nil, fmt.Errorf("datalog: expressions: failed to pop unary value: %w", err)
}
res, err := op.(UnaryOp).Eval(v, symbols)
if err != nil {
return nil, fmt.Errorf("datalog: expressions: unary eval failed: %w", err)
}
s.Push(res)
case OpTypeBinary:
right, err := s.Pop()
if err != nil {
return nil, fmt.Errorf("datalog: expressions: failed to pop binary right value: %w", err)
}
left, err := s.Pop()
if err != nil {
return nil, fmt.Errorf("datalog: expressions: failed to pop binary left value: %w", err)
}
res, err := op.(BinaryOp).Eval(left, right, symbols)
if err != nil {
return nil, fmt.Errorf("datalog: expressions: binary eval failed: %w", err)
}
s.Push(res)
default:
return nil, fmt.Errorf("datalog: expressions: unsupported Op: %v", op.Type())
}
}
// after processing all operations, there must be a single value left in the stack
if len(*s) != 1 {
return nil, fmt.Errorf("datalog: expressions: invalid resulting stack: %#v", *s)
}
return s.Pop()
}
func (e *Expression) Print(symbols *SymbolTable) string {
s := &stringstack{}
for _, op := range *e {
switch op.Type() {
case OpTypeValue:
id := op.(Value).ID
switch id.Type() {
case TermTypeString:
s.Push(fmt.Sprintf("\"%s\"", symbols.Str(id.(String))))
case TermTypeVariable:
s.Push(fmt.Sprintf("$%s", symbols.Var(id.(Variable))))
default:
s.Push(id.String())
}
case OpTypeUnary:
v, err := s.Pop()
if err != nil {
return "<invalid expression: unary operation failed to pop value>"
}
res := op.(UnaryOp).Print(v)
if err != nil {
return "<invalid expression: binary operation failed to pop right value>"
}
s.Push(res)
case OpTypeBinary:
right, err := s.Pop()
if err != nil {
return "<invalid expression: binary operation failed to pop right value>"
}
left, err := s.Pop()
if err != nil {
return "<invalid expression: binary operation failed to pop left value>"
}
res := op.(BinaryOp).Print(left, right)
s.Push(res)
default:
return fmt.Sprintf("<invalid expression: unsupported op type %v>", op.Type())
}
}
if len(*s) == 1 {
v, err := s.Pop()
if err != nil {
return "<invalid expression: failed to pop result value>"
}
return v
}
return "<invalid expression: invalid resulting stack>"
}
type OpType byte
const (
OpTypeValue OpType = iota
OpTypeUnary
OpTypeBinary
)
type Op interface {
Type() OpType
}
type Value struct {
ID Term
}
func (v Value) Type() OpType {
return OpTypeValue
}
type UnaryOp struct {
UnaryOpFunc
}
func (UnaryOp) Type() OpType {
return OpTypeUnary
}
func (op UnaryOp) Print(value string) string {
var out string
switch op.UnaryOpFunc.Type() {
case UnaryNegate:
out = fmt.Sprintf("!%s", value)
case UnaryParens:
out = fmt.Sprintf("(%s)", value)
default:
out = fmt.Sprintf("unknown(%s)", value)
}
return out
}
type UnaryOpFunc interface {
Type() UnaryOpType
Eval(value Term, symbols *SymbolTable) (Term, error)
}
type UnaryOpType byte
const (
UnaryNegate UnaryOpType = iota
UnaryParens
UnaryLength
)
// Negate returns the negation of a value.
// It only accepts a Bool value.
type Negate struct{}
func (Negate) Type() UnaryOpType {
return UnaryNegate
}
func (Negate) Eval(value Term, symbols *SymbolTable) (Term, error) {
var out Term
switch value.Type() {
case TermTypeBool:
out = !value.(Bool)
default:
return nil, fmt.Errorf("datalog: unexpected Negate value type: %d", value.Type())
}
return out, nil
}
// Parens allows expression priority and grouping (like parenthesis in math operations)
// it is a no-op, but is used to print back the expressions properly, putting their value
// inside parenthesis.
type Parens struct{}
func (Parens) Type() UnaryOpType {
return UnaryParens
}
func (Parens) Eval(value Term, symbols *SymbolTable) (Term, error) {
return value, nil
}
// Length returns the length of a value.
// It accepts String, Bytes and Set
type Length struct{}
func (Length) Type() UnaryOpType {
return UnaryLength
}
func (Length) Eval(value Term, symbols *SymbolTable) (Term, error) {
var out Term
switch value.Type() {
case TermTypeString:
str := symbols.Str(value.(String))
out = Integer(len(str))
case TermTypeBytes:
out = Integer(len(value.(Bytes)))
case TermTypeSet:
out = Integer(len(value.(Set)))
default:
return nil, fmt.Errorf("datalog: unexpected Negate value type: %d", value.Type())
}
return out, nil
}
type BinaryOp struct {
BinaryOpFunc
}
func (BinaryOp) Type() OpType {
return OpTypeBinary
}
func (op BinaryOp) Print(left, right string) string {
var out string
switch op.BinaryOpFunc.Type() {
case BinaryLessThan:
out = fmt.Sprintf("%s < %s", left, right)
case BinaryLessOrEqual:
out = fmt.Sprintf("%s <= %s", left, right)
case BinaryGreaterThan:
out = fmt.Sprintf("%s > %s", left, right)
case BinaryGreaterOrEqual:
out = fmt.Sprintf("%s >= %s", left, right)
case BinaryEqual:
out = fmt.Sprintf("%s == %s", left, right)
case BinaryContains:
out = fmt.Sprintf("%s.contains(%s)", left, right)
case BinaryPrefix:
out = fmt.Sprintf("%s.starts_with(%s)", left, right)
case BinarySuffix:
out = fmt.Sprintf("%s.ends_with(%s)", left, right)
case BinaryRegex:
out = fmt.Sprintf("%s.matches(%s)", left, right)
case BinaryAdd:
out = fmt.Sprintf("%s + %s", left, right)
case BinarySub:
out = fmt.Sprintf("%s - %s", left, right)
case BinaryMul:
out = fmt.Sprintf("%s * %s", left, right)
case BinaryDiv:
out = fmt.Sprintf("%s / %s", left, right)
case BinaryAnd:
out = fmt.Sprintf("%s && %s", left, right)
case BinaryOr:
out = fmt.Sprintf("%s || %s", left, right)
case BinaryIntersection:
out = fmt.Sprintf("%s.intersection(%s)", left, right)
case BinaryUnion:
out = fmt.Sprintf("%s.union(%s)", left, right)
default:
out = fmt.Sprintf("unknown(%s, %s)", left, right)
}
return out
}
type BinaryOpFunc interface {
Type() BinaryOpType
Eval(left, right Term, symbols *SymbolTable) (Term, error)
}
type BinaryOpType byte
const (
BinaryLessThan BinaryOpType = iota
BinaryLessOrEqual
BinaryGreaterThan
BinaryGreaterOrEqual
BinaryEqual
BinaryContains
BinaryPrefix
BinarySuffix
BinaryRegex
BinaryAdd
BinarySub
BinaryMul
BinaryDiv
BinaryAnd
BinaryOr
BinaryIntersection
BinaryUnion
)
// LessThan returns true when left is less than right.
// It requires left and right to have the same concrete type
// and only accepts Integer.
type LessThan struct{}
func (LessThan) Type() BinaryOpType {
return BinaryLessThan
}
func (LessThan) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
if g, w := left.Type(), right.Type(); g != w {
return nil, fmt.Errorf("datalog: LessThan type mismatch: %d != %d", g, w)
}
var out Term
switch left.Type() {
case TermTypeInteger:
out = Bool(left.(Integer) < right.(Integer))
case TermTypeDate:
out = Bool(left.(Date) < right.(Date))
default:
return nil, fmt.Errorf("datalog: unexpected LessThan value type: %d", left.Type())
}
return out, nil
}
// LessOrEqual returns true when left is less or equal than right.
// It requires left and right to have the same concrete type
// and only accepts Integer and Date.
type LessOrEqual struct{}
func (LessOrEqual) Type() BinaryOpType {
return BinaryLessOrEqual
}
func (LessOrEqual) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
if g, w := left.Type(), right.Type(); g != w {
return nil, fmt.Errorf("datalog: LessOrEqual type mismatch: %d != %d", g, w)
}
var out Term
switch left.Type() {
case TermTypeInteger:
out = Bool(left.(Integer) <= right.(Integer))
case TermTypeDate:
out = Bool(left.(Date) <= right.(Date))
default:
return nil, fmt.Errorf("datalog: unexpected LessOrEqual value type: %d", left.Type())
}
return out, nil
}
// GreaterThan returns true when left is greater than right.
// It requires left and right to have the same concrete type
// and only accepts Integer.
type GreaterThan struct{}
func (GreaterThan) Type() BinaryOpType {
return BinaryGreaterThan
}
func (GreaterThan) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
if g, w := left.Type(), right.Type(); g != w {
return nil, fmt.Errorf("datalog: GreaterThan type mismatch: %d != %d", g, w)
}
var out Term
switch left.Type() {
case TermTypeInteger:
out = Bool(left.(Integer) > right.(Integer))
case TermTypeDate:
out = Bool(left.(Date) > right.(Date))
default:
return nil, fmt.Errorf("datalog: unexpected GreaterThan value type: %d", left.Type())
}
return out, nil
}
// GreaterOrEqual returns true when left is greater than right.
// It requires left and right to have the same concrete type
// and only accepts Integer and Date.
type GreaterOrEqual struct{}
func (GreaterOrEqual) Type() BinaryOpType {
return BinaryGreaterOrEqual
}
func (GreaterOrEqual) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
if g, w := left.Type(), right.Type(); g != w {
return nil, fmt.Errorf("datalog: GreaterOrEqual type mismatch: %d != %d", g, w)
}
var out Term
switch left.Type() {
case TermTypeInteger:
out = Bool(left.(Integer) >= right.(Integer))
case TermTypeDate:
out = Bool(left.(Date) >= right.(Date))
default:
return nil, fmt.Errorf("datalog: unexpected GreaterOrEqual value type: %d", left.Type())
}
return out, nil
}
// Equal returns true when left and right are equal.
// It requires left and right to have the same concrete type
// and only accepts Integer, Bytes or String.
type Equal struct{}
func (Equal) Type() BinaryOpType {
return BinaryEqual
}
func (Equal) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
if g, w := left.Type(), right.Type(); g != w {
return nil, fmt.Errorf("datalog: Equal type mismatch: %d != %d", g, w)
}
switch left.Type() {
case TermTypeInteger:
case TermTypeBytes:
case TermTypeString:
case TermTypeDate:
case TermTypeBool:
case TermTypeSet:
default:
return nil, fmt.Errorf("datalog: unexpected Equal value type: %d", left.Type())
}
return Bool(left.Equal(right)), nil
}
// Contains returns true when the right value exists in the left Set.
// The right value must be an Integer, Bytes, String or Symbol.
// The left value must be a Set, containing elements of right type.
type Contains struct{}
func (Contains) Type() BinaryOpType {
return BinaryContains
}
func (Contains) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
sleft, ok := left.(String)
if ok {
sright, ok := right.(String)
if !ok {
return nil, fmt.Errorf("datalog: Contains requires right value to be a String, got %T", right)
}
return Bool(strings.Contains(symbols.Str(sleft), symbols.Str(sright))), nil
}
switch right.Type() {
case TermTypeInteger:
case TermTypeBytes:
case TermTypeString:
case TermTypeDate:
case TermTypeBool:
case TermTypeSet:
default:
return nil, fmt.Errorf("datalog: unexpected Contains right value type: %d", right.Type())
}
set, ok := left.(Set)
if !ok {
return nil, errors.New("datalog: Contains left value must be a Set")
}
rhsset, ok := right.(Set)
if ok {
for _, rhselt := range rhsset {
rhsinlhs := false
for _, lhselt := range set {
if lhselt.Equal(rhselt) {
rhsinlhs = true
}
}
if !rhsinlhs {
return Bool(false), nil
}
}
return Bool(true), nil
}
for _, elt := range set {
if right.Equal(elt) {
return Bool(true), nil
}
}
return Bool(false), nil
}
// Intersection returns the intersection of two sets
type Intersection struct{}
func (Intersection) Type() BinaryOpType {
return BinaryIntersection
}
func (Intersection) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
set, ok := left.(Set)
if !ok {
return nil, errors.New("datalog: Intersection left value must be a Set")
}
set2, ok := right.(Set)
if !ok {
return nil, errors.New("datalog: Intersection rightt value must be a Set")
}
return set.Intersect(set2), nil
}
// Intersection returns the intersection of two sets
type Union struct{}
func (Union) Type() BinaryOpType {
return BinaryUnion
}
func (Union) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
set, ok := left.(Set)
if !ok {
return nil, errors.New("datalog: Union left value must be a Set")
}
set2, ok := right.(Set)
if !ok {
return nil, errors.New("datalog: Union rightt value must be a Set")
}
return set.Union(set2), nil
}
// Prefix returns true when the left string starts with the right string.
// left and right must be String.
type Prefix struct{}
func (Prefix) Type() BinaryOpType {
return BinaryPrefix
}
func (Prefix) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
sleft, ok := left.(String)
if !ok {
return nil, fmt.Errorf("datalog: Prefix requires left value to be a String, got %T", left)
}
sright, ok := right.(String)
if !ok {
return nil, fmt.Errorf("datalog: Prefix requires right value to be a String, got %T", right)
}
return Bool(strings.HasPrefix(symbols.Str(sleft), symbols.Str(sright))), nil
}
// Suffix returns true when the left string ends with the right string.
// left and right must be String.
type Suffix struct{}
func (Suffix) Type() BinaryOpType {
return BinarySuffix
}
func (Suffix) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
sleft, ok := left.(String)
if !ok {
return nil, fmt.Errorf("datalog: Suffix requires left value to be a String, got %T", left)
}
sright, ok := right.(String)
if !ok {
return nil, fmt.Errorf("datalog: Suffix requires right value to be a String, got %T", right)
}
return Bool(strings.HasSuffix(symbols.Str(sleft), symbols.Str(sright))), nil
}
// Regex returns true when the right string is a regexp and left matches against it.
// left and right must be String.
type Regex struct{}
func (Regex) Type() BinaryOpType {
return BinaryRegex
}
func (Regex) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
sleft, ok := left.(String)
if !ok {
return nil, fmt.Errorf("datalog: Regex requires left value to be a String, got %T", left)
}
sright, ok := right.(String)
if !ok {
return nil, fmt.Errorf("datalog: Regex requires right value to be a String, got %T", right)
}
re, err := regexp.Compile(symbols.Str(sright))
if err != nil {
return nil, fmt.Errorf("datalog: invalid regex: %q: %v", right, err)
}
return Bool(re.Match([]byte(symbols.Str(sleft)))), nil
}
// Add performs the addition of left + right and returns the result.
// It requires left and right to be Integer.
type Add struct{}
func (Add) Type() BinaryOpType {
return BinaryAdd
}
func (Add) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
sleft, ok := left.(String)
if ok {
sright, ok := right.(String)
if !ok {
return nil, fmt.Errorf("datalog: Add requires right value to be a String, got %T", right)
}
s := symbols.Insert(symbols.Str(sleft) + symbols.Str(sright))
return s, nil
}
ileft, ok := left.(Integer)
if !ok {
return nil, fmt.Errorf("datalog: Add requires left value to be an Integer, got %T", left)
}
iright, ok := right.(Integer)
if !ok {
return nil, fmt.Errorf("datalog: Add requires right value to be an Integer, got %T", right)
}
bleft := big.NewInt(int64(ileft))
bright := big.NewInt(int64(iright))
res := big.NewInt(0)
res.Add(bleft, bright)
if !res.IsInt64() {
return nil, ErrInt64Overflow
}
return Integer(res.Int64()), nil
}
// Sub performs the substraction of left - right and returns the result.
// It requires left and right to be Integer.
type Sub struct{}
func (Sub) Type() BinaryOpType {
return BinarySub
}
func (Sub) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
ileft, ok := left.(Integer)
if !ok {
return nil, fmt.Errorf("datalog: Sub requires left value to be an Integer, got %T", left)
}
iright, ok := right.(Integer)
if !ok {
return nil, fmt.Errorf("datalog: Sub requires right value to be an Integer, got %T", right)
}
bleft := big.NewInt(int64(ileft))
bright := big.NewInt(int64(iright))
res := big.NewInt(0)
res.Sub(bleft, bright)
if !res.IsInt64() {
return nil, ErrInt64Overflow
}
return Integer(res.Int64()), nil
}
// Mul performs the multiplication of left * right and returns the result.
// It requires left and right to be Integer.
type Mul struct{}
func (Mul) Type() BinaryOpType {
return BinaryMul
}
func (Mul) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
ileft, ok := left.(Integer)
if !ok {
return nil, fmt.Errorf("datalog: Mul requires left value to be an Integer, got %T", left)
}
iright, ok := right.(Integer)
if !ok {
return nil, fmt.Errorf("datalog: Mul requires right value to be an Integer, got %T", right)
}
bleft := big.NewInt(int64(ileft))
bright := big.NewInt(int64(iright))
res := big.NewInt(0)
res.Mul(bleft, bright)
if !res.IsInt64() {
return nil, ErrInt64Overflow
}
return Integer(res.Int64()), nil
}
// Div performs the division of left / right and returns the result.
// It requires left and right to be Integer.
type Div struct{}
func (Div) Type() BinaryOpType {
return BinaryDiv
}
func (Div) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
ileft, ok := left.(Integer)
if !ok {
return nil, fmt.Errorf("datalog: Div requires left value to be an Integer, got %T", left)
}
iright, ok := right.(Integer)
if !ok {
return nil, fmt.Errorf("datalog: Div requires right value to be an Integer, got %T", right)
}
if iright == 0 {
return nil, ErrExprDivByZero
}
return Integer(ileft / iright), nil
}
// And performs a logical AND between left and right and returns a Bool.
// It requires left and right to be Bool.
type And struct{}
func (And) Type() BinaryOpType {
return BinaryAnd
}
func (And) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
bleft, ok := left.(Bool)
if !ok {
return nil, fmt.Errorf("datalog: And requires left value to be a Bool, got %T", left)
}
bright, ok := right.(Bool)
if !ok {
return nil, fmt.Errorf("datalog: And requires right value to be a Bool, got %T", right)
}
return Bool(bleft && bright), nil
}
// Or performs a logical OR between left and right and returns a Bool.
// It requires left and right to be Bool.
type Or struct{}
func (Or) Type() BinaryOpType {
return BinaryOr
}
func (Or) Eval(left Term, right Term, symbols *SymbolTable) (Term, error) {
bleft, ok := left.(Bool)
if !ok {
return nil, fmt.Errorf("datalog: Or requires left value to be a Bool, got %T", left)
}
bright, ok := right.(Bool)
if !ok {
return nil, fmt.Errorf("datalog: Or requires right value to be a Bool, got %T", right)
}
return Bool(bleft || bright), nil
}
type stack []Term
func (s *stack) Push(v Term) error {
if len(*s) >= maxStackSize {
return errors.New("stack overflow")
}
*s = append(*s, v)
return nil
}
func (s *stack) Pop() (Term, error) {
if len(*s) == 0 {
return nil, errors.New("cannot pop from empty stack")
}
e := (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
return e, nil
}
type stringstack []string
func (s *stringstack) Push(v string) error {
if len(*s) >= maxStackSize {
return errors.New("stack overflow")
}
*s = append(*s, v)
return nil
}
func (s *stringstack) Pop() (string, error) {
if len(*s) == 0 {
return "", errors.New("cannot pop from empty stack")
}
e := (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
return e, nil
}