-
Notifications
You must be signed in to change notification settings - Fork 67
/
eval.go
829 lines (762 loc) · 19.4 KB
/
eval.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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
package expr
import (
"bytes"
"errors"
"fmt"
"math"
"regexp"
"github.com/brimdata/zed"
"github.com/brimdata/zed/pkg/field"
"github.com/brimdata/zed/runtime/expr/coerce"
"github.com/brimdata/zed/zcode"
"github.com/brimdata/zed/zson"
)
type Evaluator interface {
Eval(Context, *zed.Value) *zed.Value
}
type Function interface {
Call(zed.Allocator, []zed.Value) *zed.Value
}
type Not struct {
zctx *zed.Context
expr Evaluator
}
var _ Evaluator = (*Not)(nil)
func NewLogicalNot(zctx *zed.Context, e Evaluator) *Not {
return &Not{zctx, e}
}
func (n *Not) Eval(ectx Context, this *zed.Value) *zed.Value {
val, ok := EvalBool(n.zctx, ectx, this, n.expr)
if !ok {
return val
}
if val.Bool() {
return zed.False
}
return zed.True
}
type And struct {
zctx *zed.Context
lhs Evaluator
rhs Evaluator
}
func NewLogicalAnd(zctx *zed.Context, lhs, rhs Evaluator) *And {
return &And{zctx, lhs, rhs}
}
type Or struct {
zctx *zed.Context
lhs Evaluator
rhs Evaluator
}
func NewLogicalOr(zctx *zed.Context, lhs, rhs Evaluator) *Or {
return &Or{zctx, lhs, rhs}
}
// EvalBool evaluates e with this and if the result is a Zed bool, returns the
// result and true. Otherwise, a Zed error (inclusive of missing) and false
// are returned.
func EvalBool(zctx *zed.Context, ectx Context, this *zed.Value, e Evaluator) (*zed.Value, bool) {
val := e.Eval(ectx, this)
if zed.TypeUnder(val.Type) == zed.TypeBool {
return val, true
}
if val.IsError() {
return val, false
}
return ectx.CopyValue(*zctx.WrapError("not type bool", val)), false
}
func (a *And) Eval(ectx Context, this *zed.Value) *zed.Value {
lhs, ok := EvalBool(a.zctx, ectx, this, a.lhs)
if !ok {
return lhs
}
if !lhs.Bool() {
return zed.False
}
rhs, ok := EvalBool(a.zctx, ectx, this, a.rhs)
if !ok {
return rhs
}
if !rhs.Bool() {
return zed.False
}
return zed.True
}
func (o *Or) Eval(ectx Context, this *zed.Value) *zed.Value {
lhs, ok := EvalBool(o.zctx, ectx, this, o.lhs)
if ok && lhs.Bool() {
return zed.True
}
if lhs.IsError() && !lhs.IsMissing() {
return lhs
}
rhs, ok := EvalBool(o.zctx, ectx, this, o.rhs)
if ok {
if rhs.Bool() {
return zed.True
}
return zed.False
}
return rhs
}
type In struct {
zctx *zed.Context
elem Evaluator
container Evaluator
vals coerce.Pair
}
func NewIn(zctx *zed.Context, elem, container Evaluator) *In {
return &In{
zctx: zctx,
elem: elem,
container: container,
}
}
func (i *In) Eval(ectx Context, this *zed.Value) *zed.Value {
elem := i.elem.Eval(ectx, this)
if elem.IsError() {
return elem
}
container := i.container.Eval(ectx, this)
if container.IsError() {
return container
}
tmpVal := ectx.NewValue(nil, nil)
err := container.Walk(func(typ zed.Type, body zcode.Bytes) error {
*tmpVal = *zed.NewValue(typ, body)
if _, err := i.vals.Coerce(elem, tmpVal); err != nil {
if err != coerce.IncompatibleTypes {
return err
}
} else if i.vals.Equal() {
return errMatch
}
return nil
})
switch err {
case errMatch:
return zed.True
case nil:
return zed.False
default:
return i.zctx.NewError(err)
}
}
type Equal struct {
numeric
equality bool
}
func NewCompareEquality(zctx *zed.Context, lhs, rhs Evaluator, operator string) (*Equal, error) {
e := &Equal{numeric: newNumeric(zctx, lhs, rhs)} //XXX
switch operator {
case "==":
e.equality = true
case "!=":
default:
return nil, fmt.Errorf("unknown equality operator: %s", operator)
}
return e, nil
}
func (e *Equal) Eval(ectx Context, this *zed.Value) *zed.Value {
_, zerr, err := e.numeric.eval(ectx, this)
if zerr != nil {
return zerr
}
if err != nil {
if errors.Is(err, coerce.IncompatibleTypes) || errors.Is(err, coerce.Overflow) {
// If the types are incompatible or there was overflow,
// then, then we know the values can't be equal.
if e.equality {
return zed.False
}
return zed.True
}
return e.zctx.NewError(err)
}
result := e.vals.Equal()
if !e.equality {
result = !result
}
if result {
return zed.True
}
return zed.False
}
type RegexpMatch struct {
re *regexp.Regexp
expr Evaluator
}
func NewRegexpMatch(re *regexp.Regexp, e Evaluator) *RegexpMatch {
return &RegexpMatch{re, e}
}
func (r *RegexpMatch) Eval(ectx Context, this *zed.Value) *zed.Value {
val := r.expr.Eval(ectx, this)
if val.Type.ID() == zed.IDString && r.re.Match(val.Bytes()) {
return zed.True
}
return zed.False
}
type numeric struct {
zctx *zed.Context
lhs Evaluator
rhs Evaluator
vals coerce.Pair
}
func newNumeric(zctx *zed.Context, lhs, rhs Evaluator) numeric {
return numeric{
zctx: zctx,
lhs: lhs,
rhs: rhs,
}
}
func enumify(ectx Context, val *zed.Value) *zed.Value {
// automatically convert an enum to its index value when coercing
if _, ok := val.Type.(*zed.TypeEnum); ok {
return ectx.NewValue(zed.TypeUint64, val.Bytes())
}
return val
}
func (n *numeric) eval(ectx Context, this *zed.Value) (int, *zed.Value, error) {
lhs := n.lhs.Eval(ectx, this)
if lhs.IsError() {
return 0, lhs, nil
}
lhs = enumify(ectx, lhs)
rhs := n.rhs.Eval(ectx, this)
if rhs.IsError() {
return 0, rhs, nil
}
rhs = enumify(ectx, rhs)
id, err := n.vals.Coerce(lhs, rhs)
return id, nil, err
}
func (n *numeric) floats() (float64, float64) {
return zed.DecodeFloat(n.vals.A), zed.DecodeFloat(n.vals.B)
}
func (n *numeric) ints() (int64, int64) {
return zed.DecodeInt(n.vals.A), zed.DecodeInt(n.vals.B)
}
func (n *numeric) uints() (uint64, uint64) {
return zed.DecodeUint(n.vals.A), zed.DecodeUint(n.vals.B)
}
type Compare struct {
zctx *zed.Context
numeric
convert func(int) bool
}
func NewCompareRelative(zctx *zed.Context, lhs, rhs Evaluator, operator string) (*Compare, error) {
c := &Compare{zctx: zctx, numeric: newNumeric(zctx, lhs, rhs)}
switch operator {
case "<":
c.convert = func(v int) bool { return v < 0 }
case "<=":
c.convert = func(v int) bool { return v <= 0 }
case ">":
c.convert = func(v int) bool { return v > 0 }
case ">=":
c.convert = func(v int) bool { return v >= 0 }
default:
return nil, fmt.Errorf("unknown comparison operator: %s", operator)
}
return c, nil
}
func (c *Compare) result(result int) *zed.Value {
if c.convert(result) {
return zed.True
}
return zed.False
}
func (c *Compare) Eval(ectx Context, this *zed.Value) *zed.Value {
lhs := c.lhs.Eval(ectx, this)
if lhs.IsError() {
return lhs
}
rhs := c.rhs.Eval(ectx, this)
if rhs.IsError() {
return rhs
}
id, err := c.vals.Coerce(lhs, rhs)
if err != nil {
// If coercion fails due to overflow, then we know there is a
// mixed signed and unsigned situation and either the unsigned
// value couldn't be converted to an int64 because it was too big,
// or the signed value couldn't be converted to a uint64 because
// it was negative. In either case, the unsigned value is bigger
// than the signed value.
if err == coerce.Overflow {
result := 1
if zed.IsSigned(lhs.Type.ID()) {
result = -1
}
return c.result(result)
}
return zed.False
}
var result int
if !c.vals.Equal() {
switch {
case c.vals.A == nil || c.vals.B == nil:
return zed.False
case zed.IsFloat(id):
v1, v2 := c.floats()
if v1 < v2 {
result = -1
} else {
result = 1
}
case zed.IsSigned(id):
v1, v2 := c.ints()
if v1 < v2 {
result = -1
} else {
result = 1
}
case zed.IsNumber(id):
v1, v2 := c.uints()
if v1 < v2 {
result = -1
} else {
result = 1
}
case id == zed.IDString:
if zed.DecodeString(c.vals.A) < zed.DecodeString(c.vals.B) {
result = -1
} else {
result = 1
}
default:
return ectx.CopyValue(*c.zctx.NewErrorf("bad comparison type ID: %d", id))
}
}
if c.convert(result) {
return zed.True
}
return zed.False
}
type Add struct {
zctx *zed.Context
operands numeric
}
type Subtract struct {
zctx *zed.Context
operands numeric
}
type Multiply struct {
zctx *zed.Context
operands numeric
}
type Divide struct {
zctx *zed.Context
operands numeric
}
type Modulo struct {
zctx *zed.Context
operands numeric
}
var DivideByZero = errors.New("divide by zero")
// NewArithmetic compiles an expression of the form "expr1 op expr2"
// for the arithmetic operators +, -, *, /
func NewArithmetic(zctx *zed.Context, lhs, rhs Evaluator, op string) (Evaluator, error) {
n := newNumeric(zctx, lhs, rhs)
switch op {
case "+":
return &Add{zctx: zctx, operands: n}, nil
case "-":
return &Subtract{zctx: zctx, operands: n}, nil
case "*":
return &Multiply{zctx: zctx, operands: n}, nil
case "/":
return &Divide{zctx: zctx, operands: n}, nil
case "%":
return &Modulo{zctx: zctx, operands: n}, nil
}
return nil, fmt.Errorf("unknown arithmetic operator: %s", op)
}
func (a *Add) Eval(ectx Context, this *zed.Value) *zed.Value {
id, zerr, err := a.operands.eval(ectx, this)
if err != nil {
return a.zctx.NewError(err)
}
if zerr != nil {
return zerr
}
typ, err := zed.LookupPrimitiveByID(id)
if err != nil {
return a.zctx.NewError(err)
}
switch {
case zed.IsFloat(id):
v1, v2 := a.operands.floats()
return ectx.CopyValue(*zed.NewFloat(typ, v1+v2))
case zed.IsSigned(id):
v1, v2 := a.operands.ints()
return ectx.CopyValue(*zed.NewInt(typ, v1+v2))
case zed.IsNumber(id):
v1, v2 := a.operands.uints()
return ectx.CopyValue(*zed.NewUint(typ, v1+v2))
case id == zed.IDString:
v1, v2 := zed.DecodeString(a.operands.vals.A), zed.DecodeString(a.operands.vals.B)
// XXX GC
return ectx.NewValue(typ, zed.EncodeString(v1+v2))
}
return ectx.CopyValue(*a.zctx.NewErrorf("type %s incompatible with '+' operator", zson.FormatType(typ)))
}
func (s *Subtract) Eval(ectx Context, this *zed.Value) *zed.Value {
id, zerr, err := s.operands.eval(ectx, this)
if err != nil {
return s.zctx.NewError(err)
}
if zerr != nil {
return zerr
}
typ, err := zed.LookupPrimitiveByID(id)
if err != nil {
return s.zctx.NewError(err)
}
switch {
case zed.IsFloat(id):
v1, v2 := s.operands.floats()
return ectx.CopyValue(*zed.NewFloat(typ, v1-v2))
case zed.IsSigned(id):
v1, v2 := s.operands.ints()
if id == zed.IDTime {
// Return the difference of two times as a duration.
typ = zed.TypeDuration
}
return ectx.CopyValue(*zed.NewInt(typ, v1-v2))
case zed.IsNumber(id):
v1, v2 := s.operands.uints()
return ectx.CopyValue(*zed.NewUint(typ, v1-v2))
}
return ectx.CopyValue(*s.zctx.NewErrorf("type %s incompatible with '-' operator", zson.FormatType(typ)))
}
func (m *Multiply) Eval(ectx Context, this *zed.Value) *zed.Value {
id, zerr, err := m.operands.eval(ectx, this)
if err != nil {
return m.zctx.NewError(err)
}
if zerr != nil {
return zerr
}
typ, err := zed.LookupPrimitiveByID(id)
if err != nil {
return m.zctx.NewError(err)
}
switch {
case zed.IsFloat(id):
v1, v2 := m.operands.floats()
return ectx.CopyValue(*zed.NewFloat(typ, v1*v2))
case zed.IsSigned(id):
v1, v2 := m.operands.ints()
return ectx.CopyValue(*zed.NewInt(typ, v1*v2))
case zed.IsNumber(id):
v1, v2 := m.operands.uints()
return ectx.CopyValue(*zed.NewUint(typ, v1*v2))
}
return ectx.CopyValue(*m.zctx.NewErrorf("type %s incompatible with '*' operator", zson.FormatType(typ)))
}
func (d *Divide) Eval(ectx Context, this *zed.Value) *zed.Value {
id, zerr, err := d.operands.eval(ectx, this)
if err != nil {
return d.zctx.NewError(err)
}
if zerr != nil {
return zerr
}
typ, err := zed.LookupPrimitiveByID(id)
if err != nil {
return d.zctx.NewError(err)
}
switch {
case zed.IsFloat(id):
v1, v2 := d.operands.floats()
if v2 == 0 {
return d.zctx.NewError(DivideByZero)
}
return ectx.CopyValue(*zed.NewFloat(typ, v1/v2))
case zed.IsSigned(id):
v1, v2 := d.operands.ints()
if v2 == 0 {
return d.zctx.NewError(DivideByZero)
}
return ectx.CopyValue(*zed.NewInt(typ, v1/v2))
case zed.IsNumber(id):
v1, v2 := d.operands.uints()
if v2 == 0 {
return d.zctx.NewError(DivideByZero)
}
return ectx.CopyValue(*zed.NewUint(typ, v1/v2))
}
return ectx.CopyValue(*d.zctx.NewErrorf("type %s incompatible with '/' operator", zson.FormatType(typ)))
}
func (m *Modulo) Eval(ectx Context, this *zed.Value) *zed.Value {
id, zerr, err := m.operands.eval(ectx, this)
if err != nil {
return m.zctx.NewError(err)
}
if zerr != nil {
return zerr
}
typ, err := zed.LookupPrimitiveByID(id)
if err != nil {
return m.zctx.NewError(err)
}
if zed.IsFloat(id) || !zed.IsNumber(id) {
return ectx.CopyValue(*m.zctx.NewErrorf("type %s incompatible with '%%' operator", zson.FormatType(typ)))
}
if zed.IsSigned(id) {
x, y := m.operands.ints()
if y == 0 {
return m.zctx.NewError(DivideByZero)
}
return ectx.CopyValue(*zed.NewInt(typ, x%y))
}
x, y := m.operands.uints()
if y == 0 {
return m.zctx.NewError(DivideByZero)
}
return ectx.CopyValue(*zed.NewUint(typ, x%y))
}
type UnaryMinus struct {
zctx *zed.Context
expr Evaluator
}
func NewUnaryMinus(zctx *zed.Context, e Evaluator) *UnaryMinus {
return &UnaryMinus{
zctx: zctx,
expr: e,
}
}
func (u *UnaryMinus) Eval(ectx Context, this *zed.Value) *zed.Value {
val := u.expr.Eval(ectx, this)
typ := val.Type
if val.IsNull() && zed.IsNumber(typ.ID()) {
return val
}
switch typ.ID() {
case zed.IDFloat16, zed.IDFloat32, zed.IDFloat64:
return ectx.CopyValue(*zed.NewFloat(typ, -val.Float()))
case zed.IDInt8:
v := val.Int()
if v == math.MinInt8 {
return ectx.CopyValue(*u.zctx.WrapError("unary '-' underflow", val))
}
return ectx.CopyValue(*zed.NewInt8(int8(-v)))
case zed.IDInt16:
v := val.Int()
if v == math.MinInt16 {
return ectx.CopyValue(*u.zctx.WrapError("unary '-' underflow", val))
}
return ectx.CopyValue(*zed.NewInt16(int16(-v)))
case zed.IDInt32:
v := val.Int()
if v == math.MinInt32 {
return ectx.CopyValue(*u.zctx.WrapError("unary '-' underflow", val))
}
return ectx.CopyValue(*zed.NewInt32(int32(-v)))
case zed.IDInt64:
v := val.Int()
if v == math.MinInt64 {
return ectx.CopyValue(*u.zctx.WrapError("unary '-' underflow", val))
}
return ectx.CopyValue(*zed.NewInt64(-v))
case zed.IDUint8:
v := val.Uint()
if v > math.MaxInt8 {
return ectx.CopyValue(*u.zctx.WrapError("unary '-' overflow", val))
}
return ectx.CopyValue(*zed.NewInt8(int8(-v)))
case zed.IDUint16:
v := val.Uint()
if v > math.MaxInt16 {
return ectx.CopyValue(*u.zctx.WrapError("unary '-' overflow", val))
}
return ectx.CopyValue(*zed.NewInt16(int16(-v)))
case zed.IDUint32:
v := val.Uint()
if v > math.MaxInt32 {
return ectx.CopyValue(*u.zctx.WrapError("unary '-' overflow", val))
}
return ectx.CopyValue(*zed.NewInt32(int32(-v)))
case zed.IDUint64:
v := val.Uint()
if v > math.MaxInt64 {
return ectx.CopyValue(*u.zctx.WrapError("unary '-' overflow", val))
}
return ectx.CopyValue(*zed.NewInt64(int64(-v)))
}
return u.zctx.WrapError("type incompatible with unary '-' operator", val)
}
func getNthFromContainer(container zcode.Bytes, idx int) zcode.Bytes {
if idx < 0 {
var length int
for it := container.Iter(); !it.Done(); it.Next() {
length++
}
idx = length + idx
if idx < 0 || idx >= length {
return nil
}
}
for i, it := 0, container.Iter(); !it.Done(); i++ {
zv := it.Next()
if i == idx {
return zv
}
}
return nil
}
func lookupKey(mapBytes, target zcode.Bytes) (zcode.Bytes, bool) {
for it := mapBytes.Iter(); !it.Done(); {
key := it.Next()
val := it.Next()
if bytes.Equal(key, target) {
return val, true
}
}
return nil, false
}
// Index represents an index operator "container[index]" where container is
// either an array (with index type integer) or a record (with index type string).
type Index struct {
zctx *zed.Context
container Evaluator
index Evaluator
}
func NewIndexExpr(zctx *zed.Context, container, index Evaluator) Evaluator {
return &Index{zctx, container, index}
}
func (i *Index) Eval(ectx Context, this *zed.Value) *zed.Value {
container := i.container.Eval(ectx, this)
index := i.index.Eval(ectx, this)
switch typ := zed.TypeUnder(container.Type).(type) {
case *zed.TypeArray, *zed.TypeSet:
return indexVector(i.zctx, ectx, zed.InnerType(typ), container.Bytes(), index)
case *zed.TypeRecord:
return indexRecord(i.zctx, ectx, typ, container.Bytes(), index)
case *zed.TypeMap:
return indexMap(i.zctx, ectx, typ, container.Bytes(), index)
default:
return i.zctx.Missing()
}
}
func indexVector(zctx *zed.Context, ectx Context, inner zed.Type, vector zcode.Bytes, index *zed.Value) *zed.Value {
id := index.Type.ID()
if !zed.IsInteger(id) {
return ectx.CopyValue(*zctx.WrapError("array index is not an integer", index))
}
var idx int
if zed.IsSigned(id) {
idx = int(index.Int())
} else {
idx = int(index.Uint())
}
zv := getNthFromContainer(vector, idx)
if zv == nil {
return zctx.Missing()
}
return deunion(ectx, inner, zv)
}
func indexRecord(zctx *zed.Context, ectx Context, typ *zed.TypeRecord, record zcode.Bytes, index *zed.Value) *zed.Value {
id := index.Type.ID()
if id != zed.IDString {
return ectx.CopyValue(*zctx.WrapError("record index is not a string", index))
}
field := zed.DecodeString(index.Bytes())
val := ectx.NewValue(typ, record).Deref(field)
if val == nil {
return zctx.Missing()
}
return ectx.CopyValue(*val)
}
func indexMap(zctx *zed.Context, ectx Context, typ *zed.TypeMap, mapBytes zcode.Bytes, key *zed.Value) *zed.Value {
if key.IsMissing() {
return zctx.Missing()
}
if key.Type != typ.KeyType {
if union, ok := zed.TypeUnder(typ.KeyType).(*zed.TypeUnion); ok {
if tag := union.TagOf(key.Type); tag >= 0 {
var b zcode.Builder
zed.BuildUnion(&b, union.TagOf(key.Type), key.Bytes())
if valBytes, ok := lookupKey(mapBytes, b.Bytes().Body()); ok {
return deunion(ectx, typ.ValType, valBytes)
}
}
}
return zctx.Missing()
}
if valBytes, ok := lookupKey(mapBytes, key.Bytes()); ok {
return deunion(ectx, typ.ValType, valBytes)
}
return zctx.Missing()
}
func deunion(ectx Context, typ zed.Type, b zcode.Bytes) *zed.Value {
if union, ok := typ.(*zed.TypeUnion); ok {
typ, b = union.Untag(b)
}
return ectx.NewValue(typ, b)
}
type Conditional struct {
zctx *zed.Context
predicate Evaluator
thenExpr Evaluator
elseExpr Evaluator
}
func NewConditional(zctx *zed.Context, predicate, thenExpr, elseExpr Evaluator) *Conditional {
return &Conditional{
zctx: zctx,
predicate: predicate,
thenExpr: thenExpr,
elseExpr: elseExpr,
}
}
func (c *Conditional) Eval(ectx Context, this *zed.Value) *zed.Value {
val := c.predicate.Eval(ectx, this)
if val.Type.ID() != zed.IDBool {
val := *c.zctx.WrapError("?-operator: bool predicate required", val)
return &val
}
if val.Bool() {
return c.thenExpr.Eval(ectx, this)
}
return c.elseExpr.Eval(ectx, this)
}
type Call struct {
zctx *zed.Context
fn Function
exprs []Evaluator
args []zed.Value
}
func NewCall(zctx *zed.Context, fn Function, exprs []Evaluator) *Call {
return &Call{
zctx: zctx,
fn: fn,
exprs: exprs,
args: make([]zed.Value, len(exprs)),
}
}
func (c *Call) Eval(ectx Context, this *zed.Value) *zed.Value {
for k, e := range c.exprs {
c.args[k] = *e.Eval(ectx, this)
}
return c.fn.Call(ectx, c.args)
}
type Assignment struct {
LHS *Lval
RHS Evaluator
}
func NewAssignments(zctx *zed.Context, dsts field.List, srcs field.List) ([]*Lval, []Evaluator) {
if len(srcs) != len(dsts) {
panic("NewAssignments: argument mismatch")
}
var resolvers []Evaluator
var lvals []*Lval
for k, dst := range dsts {
elems := make([]LvalElem, 0, len(dst))
for _, d := range dst {
elems = append(elems, &StaticLvalElem{Name: d})
}
lvals = append(lvals, NewLval(elems))
resolvers = append(resolvers, NewDottedExpr(zctx, srcs[k]))
}
return lvals, resolvers
}