modernc.org/ql@v1.4.7/expr.go (about) 1 // Copyright 2014 The ql Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found pIn the LICENSE file. 4 5 package ql // import "modernc.org/ql" 6 7 import ( 8 "bytes" 9 "fmt" 10 "math/big" 11 "regexp" 12 "strings" 13 "time" 14 ) 15 16 var ( 17 _ expression = (*binaryOperation)(nil) 18 _ expression = (*call)(nil) 19 _ expression = (*conversion)(nil) 20 _ expression = (*ident)(nil) 21 _ expression = (*indexOp)(nil) 22 _ expression = (*isNull)(nil) 23 _ expression = (*pIn)(nil) 24 _ expression = (*pLike)(nil) 25 _ expression = (*parameter)(nil) 26 _ expression = (*pexpr)(nil) 27 _ expression = (*slice)(nil) 28 _ expression = (*unaryOperation)(nil) 29 _ expression = value{} 30 ) 31 32 type expression interface { 33 clone(arg []interface{}, unqualify ...string) (expression, error) 34 eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) 35 isStatic() bool 36 String() string 37 } 38 39 func cloneExpressionList(arg []interface{}, list []expression, unqualify ...string) ([]expression, error) { 40 r := make([]expression, len(list)) 41 var err error 42 for i, v := range list { 43 if r[i], err = v.clone(arg, unqualify...); err != nil { 44 return nil, err 45 } 46 } 47 return r, nil 48 } 49 50 func isConstValue(v interface{}) interface{} { 51 switch x := v.(type) { 52 case value: 53 return x.val 54 case 55 idealComplex, 56 idealFloat, 57 idealInt, 58 idealRune, 59 idealUint: 60 return v 61 default: 62 return nil 63 } 64 } 65 66 func isColumnExpression(v expression) (bool, string) { 67 x, ok := v.(*ident) 68 if ok { 69 return true, x.s 70 } 71 72 c, ok := v.(*call) 73 if !ok || c.f != "id" || len(c.arg) != 0 { 74 return false, "" 75 } 76 77 return true, "id()" 78 } 79 80 func mentionedColumns0(e expression, q, nq bool, m map[string]struct{}) { 81 switch x := e.(type) { 82 case parameter, 83 value: 84 // nop 85 case *binaryOperation: 86 mentionedColumns0(x.l, q, nq, m) 87 mentionedColumns0(x.r, q, nq, m) 88 case *call: 89 if x.f != "id" { 90 for _, e := range x.arg { 91 mentionedColumns0(e, q, nq, m) 92 } 93 } 94 case *conversion: 95 mentionedColumns0(x.val, q, nq, m) 96 case *ident: 97 if q && x.isQualified() { 98 m[x.s] = struct{}{} 99 } 100 if nq && !x.isQualified() { 101 m[x.s] = struct{}{} 102 } 103 case *indexOp: 104 mentionedColumns0(x.expr, q, nq, m) 105 mentionedColumns0(x.x, q, nq, m) 106 case *isNull: 107 mentionedColumns0(x.expr, q, nq, m) 108 case *pexpr: 109 mentionedColumns0(x.expr, q, nq, m) 110 case *pIn: 111 mentionedColumns0(x.expr, q, nq, m) 112 for _, e := range x.list { 113 mentionedColumns0(e, q, nq, m) 114 } 115 case *pLike: 116 mentionedColumns0(x.expr, q, nq, m) 117 mentionedColumns0(x.pattern, q, nq, m) 118 case *slice: 119 mentionedColumns0(x.expr, q, nq, m) 120 if y := x.lo; y != nil { 121 mentionedColumns0(*y, q, nq, m) 122 } 123 if y := x.hi; y != nil { 124 mentionedColumns0(*y, q, nq, m) 125 } 126 case *unaryOperation: 127 mentionedColumns0(x.v, q, nq, m) 128 default: 129 panic("internal error 052") 130 } 131 } 132 133 func mentionedColumns(e expression) map[string]struct{} { 134 m := map[string]struct{}{} 135 mentionedColumns0(e, false, true, m) 136 return m 137 } 138 139 func staticExpr(e expression) (expression, error) { 140 if e.isStatic() { 141 v, err := e.eval(nil, nil) 142 if err != nil { 143 return nil, err 144 } 145 146 if v == nil { 147 return value{nil}, nil 148 } 149 150 return value{v}, nil 151 } 152 153 return e, nil 154 } 155 156 type ( 157 idealComplex complex128 158 idealFloat float64 159 idealInt int64 160 idealRune int32 161 idealUint uint64 162 ) 163 164 type pexpr struct { 165 expr expression 166 } 167 168 func (p *pexpr) clone(arg []interface{}, unqualify ...string) (expression, error) { 169 expr, err := p.expr.clone(arg, unqualify...) 170 if err != nil { 171 return nil, err 172 } 173 174 return &pexpr{expr: expr}, nil 175 } 176 177 func (p *pexpr) isStatic() bool { return p.expr.isStatic() } 178 179 func (p *pexpr) String() string { 180 return fmt.Sprintf("(%s)", p.expr) 181 } 182 183 func (p *pexpr) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) { 184 return p.expr.eval(execCtx, ctx) 185 } 186 187 //DONE newBetween 188 //LATER like newBetween, check all others have and use new* 189 190 func newBetween(expr, lo, hi interface{}, not bool) (expression, error) { 191 e, err := staticExpr(expr.(expression)) 192 if err != nil { 193 return nil, err 194 } 195 196 l, err := staticExpr(lo.(expression)) 197 if err != nil { 198 return nil, err 199 } 200 201 h, err := staticExpr(hi.(expression)) 202 if err != nil { 203 return nil, err 204 } 205 206 var a, b expression 207 op := andand 208 switch { 209 case not: // e < l || e > h 210 op = oror 211 if a, err = newBinaryOperation('<', e, l); err != nil { 212 return nil, err 213 } 214 215 if b, err = newBinaryOperation('>', e, h); err != nil { 216 return nil, err 217 } 218 default: // e >= l && e <= h 219 if a, err = newBinaryOperation(ge, e, l); err != nil { 220 return nil, err 221 } 222 223 if b, err = newBinaryOperation(le, e, h); err != nil { 224 return nil, err 225 } 226 } 227 228 if a, err = staticExpr(a); err != nil { 229 return nil, err 230 } 231 232 if b, err = staticExpr(b); err != nil { 233 return nil, err 234 } 235 236 ret, err := newBinaryOperation(op, a, b) 237 if err != nil { 238 return nil, err 239 } 240 241 return staticExpr(ret) 242 } 243 244 type pLike struct { 245 expr expression 246 pattern expression 247 re *regexp.Regexp 248 sexpr *string 249 } 250 251 func (p *pLike) clone(arg []interface{}, unqualify ...string) (expression, error) { 252 expr, err := p.expr.clone(arg, unqualify...) 253 if err != nil { 254 return nil, err 255 } 256 257 pattern, err := p.pattern.clone(arg, unqualify...) 258 if err != nil { 259 return nil, err 260 } 261 262 return &pLike{ 263 expr: expr, 264 pattern: pattern, 265 re: p.re, 266 sexpr: p.sexpr, 267 }, nil 268 } 269 270 func (p *pLike) isStatic() bool { return p.expr.isStatic() && p.pattern.isStatic() } 271 func (p *pLike) String() string { return fmt.Sprintf("%s LIKE %s", p.expr, p.pattern) } 272 273 func (p *pLike) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) { 274 var sexpr string 275 var ok bool 276 switch { 277 case p.sexpr != nil: 278 sexpr = *p.sexpr 279 default: 280 expr, err := expand1(p.expr.eval(execCtx, ctx)) 281 if err != nil { 282 return nil, err 283 } 284 285 if expr == nil { 286 return nil, nil 287 } 288 289 sexpr, ok = expr.(string) 290 if !ok { 291 return nil, fmt.Errorf("non-string expression in LIKE: %v (value of type %T)", expr, expr) 292 } 293 294 if p.expr.isStatic() { 295 p.sexpr = new(string) 296 *p.sexpr = sexpr 297 } 298 } 299 300 re := p.re 301 if re == nil { 302 pattern, err := expand1(p.pattern.eval(execCtx, ctx)) 303 if err != nil { 304 return nil, err 305 } 306 307 if pattern == nil { 308 return nil, nil 309 } 310 311 spattern, ok := pattern.(string) 312 if !ok { 313 return nil, fmt.Errorf("non-string pattern in LIKE: %v (value of type %T)", pattern, pattern) 314 } 315 316 if re, err = regexp.Compile(spattern); err != nil { 317 return nil, err 318 } 319 320 if p.pattern.isStatic() { 321 p.re = re 322 } 323 } 324 325 return re.MatchString(sexpr), nil 326 } 327 328 type binaryOperation struct { 329 op int 330 l, r expression 331 } 332 333 func newBinaryOperation0(op int, x, y interface{}) (v expression, err error) { 334 if op == eq { 335 if l, ok := x.(value); ok { 336 if b, ok := l.val.(bool); ok { 337 if b { // true == y: y 338 return y.(expression), nil 339 } 340 341 // false == y: !y 342 return newUnaryOperation('!', y) 343 } 344 } 345 346 if r, ok := y.(value); ok { 347 if b, ok := r.val.(bool); ok { 348 if b { // x == true: x 349 return x.(expression), nil 350 } 351 352 // x == false: !x 353 return newUnaryOperation('!', x) 354 } 355 } 356 } 357 358 if op == neq { 359 if l, ok := x.(value); ok { 360 if b, ok := l.val.(bool); ok { 361 if b { // true != y: !y 362 return newUnaryOperation('!', y) 363 } 364 365 // false != y: y 366 return y.(expression), nil 367 } 368 } 369 370 if r, ok := y.(value); ok { 371 if b, ok := r.val.(bool); ok { 372 if b { // x != true: !x 373 return newUnaryOperation('!', x) 374 } 375 376 // x != false: x 377 return x.(expression), nil 378 } 379 } 380 } 381 382 b := binaryOperation{op, x.(expression), y.(expression)} 383 var lv interface{} 384 if e := b.l; e.isStatic() { 385 if lv, err = e.eval(nil, nil); err != nil { 386 return nil, err 387 } 388 389 b.l = value{lv} 390 } 391 392 if e := b.r; e.isStatic() { 393 v, err := e.eval(nil, nil) 394 if err != nil { 395 return nil, err 396 } 397 398 if v == nil { 399 return value{nil}, nil 400 } 401 402 if op == '/' || op == '%' { 403 rb := binaryOperation{eq, e, value{idealInt(0)}} 404 val, err := rb.eval(nil, nil) 405 if err != nil { 406 return nil, err 407 } 408 409 if val.(bool) { 410 return nil, errDivByZero 411 } 412 } 413 414 if b.l.isStatic() && lv == nil { 415 return value{nil}, nil 416 } 417 418 b.r = value{v} 419 } 420 421 if !b.isStatic() { 422 return &b, nil 423 } 424 425 val, err := b.eval(nil, nil) 426 return value{val}, err 427 } 428 429 func newBinaryOperation(op int, x, y interface{}) (v expression, err error) { 430 expr, err := newBinaryOperation0(op, x, y) 431 if err != nil { 432 return nil, err 433 } 434 435 b, ok := expr.(*binaryOperation) 436 if !ok { 437 return expr, nil 438 } 439 440 if _, ok := b.l.(*ident); ok { 441 return expr, nil 442 } 443 444 if c, ok := b.l.(*call); ok && c.f == "id" { 445 return expr, nil 446 } 447 448 var r expression 449 if r, ok = b.r.(*ident); !ok { 450 r1, ok := b.r.(*call) 451 if !ok || r1.f != "id" || len(r1.arg) != 0 { 452 return expr, nil 453 } 454 455 r = r1 456 } 457 458 // Normalize expr relOp indent: ident invRelOp expr 459 switch b.op { 460 case '<': 461 return &binaryOperation{'>', r, b.l}, nil 462 case le: 463 return &binaryOperation{ge, r, b.l}, nil 464 case '>': 465 return &binaryOperation{'<', r, b.l}, nil 466 case ge: 467 return &binaryOperation{le, r, b.l}, nil 468 case eq, neq: 469 return &binaryOperation{b.op, r, b.l}, nil 470 default: 471 return expr, nil 472 } 473 } 474 475 func (o *binaryOperation) isIdentRelOpVal() (bool, string, interface{}, error) { 476 sid := "" 477 id, ok := o.l.(*ident) 478 if !ok { 479 f, ok := o.l.(*call) 480 if !ok || f.f != "id" || len(f.arg) != 0 { 481 return false, "", nil, nil 482 } 483 484 sid = "id()" 485 } else { 486 if id.isQualified() { 487 return false, "", nil, nil 488 } 489 490 sid = id.s 491 } 492 493 if v, ok := o.r.(value); ok { 494 switch o.op { 495 case '<', 496 le, 497 '>', 498 ge, 499 eq, 500 neq: 501 return true, sid, v.val, nil 502 default: 503 return false, "", nil, nil 504 } 505 } 506 507 return false, "", nil, nil 508 } 509 510 func (o *binaryOperation) clone(arg []interface{}, unqualify ...string) (expression, error) { 511 l, err := o.l.clone(arg, unqualify...) 512 if err != nil { 513 return nil, err 514 } 515 516 r, err := o.r.clone(arg, unqualify...) 517 if err != nil { 518 return nil, err 519 } 520 521 return newBinaryOperation(o.op, l, r) 522 } 523 524 func (o *binaryOperation) isStatic() bool { return o.l.isStatic() && o.r.isStatic() } 525 526 func (o *binaryOperation) String() string { 527 return fmt.Sprintf("%s %s %s", o.l, iop(o.op), o.r) 528 } 529 530 func (o *binaryOperation) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (r interface{}, err error) { 531 defer func() { 532 if e := recover(); e != nil { 533 switch x := e.(type) { 534 case error: 535 r, err = nil, x 536 default: 537 r, err = nil, fmt.Errorf("%v", x) 538 } 539 } 540 }() 541 542 switch op := o.op; op { 543 case andand: 544 a, err := expand1(o.l.eval(execCtx, ctx)) 545 if err != nil { 546 return nil, err 547 } 548 549 switch x := a.(type) { 550 case nil: 551 b, err := expand1(o.r.eval(execCtx, ctx)) 552 if err != nil { 553 return nil, err 554 } 555 556 switch y := b.(type) { 557 case nil: 558 return nil, nil 559 case bool: 560 if !y { 561 return false, nil 562 } 563 564 return nil, nil 565 default: 566 return invOp2(x, y, op) 567 } 568 case bool: 569 if !x { 570 return false, nil 571 } 572 573 b, err := expand1(o.r.eval(execCtx, ctx)) 574 if err != nil { 575 return nil, err 576 } 577 578 switch y := b.(type) { 579 case nil: 580 return nil, nil 581 case bool: 582 return y, nil 583 default: 584 return invOp2(x, y, op) 585 } 586 default: 587 return undOp(x, op) 588 } 589 case oror: 590 a, err := expand1(o.l.eval(execCtx, ctx)) 591 if err != nil { 592 return nil, err 593 } 594 595 switch x := a.(type) { 596 case nil: 597 b, err := expand1(o.r.eval(execCtx, ctx)) 598 if err != nil { 599 return nil, err 600 } 601 602 switch y := b.(type) { 603 case nil: 604 return nil, nil 605 case bool: 606 if y { 607 return y, nil 608 } 609 610 return nil, nil 611 default: 612 return invOp2(x, y, op) 613 } 614 case bool: 615 if x { 616 return x, nil 617 } 618 619 b, err := expand1(o.r.eval(execCtx, ctx)) 620 if err != nil { 621 return nil, err 622 } 623 624 switch y := b.(type) { 625 case nil: 626 return nil, nil 627 case bool: 628 return y, nil 629 default: 630 return invOp2(x, y, op) 631 } 632 default: 633 return undOp(x, op) 634 } 635 case '>': 636 a, b := o.get2(execCtx, ctx) 637 if a == nil || b == nil { 638 return 639 } 640 switch x := a.(type) { 641 //case nil: 642 case idealComplex: 643 return undOp2(a, b, op) 644 case idealFloat: 645 switch y := b.(type) { 646 case idealFloat: 647 return x > y, nil 648 default: 649 return invOp2(x, y, op) 650 } 651 case idealInt: 652 switch y := b.(type) { 653 case idealInt: 654 return x > y, nil 655 default: 656 return invOp2(x, y, op) 657 } 658 case idealRune: 659 switch y := b.(type) { 660 case idealRune: 661 return x > y, nil 662 default: 663 return invOp2(x, y, op) 664 } 665 case idealUint: 666 switch y := b.(type) { 667 case idealUint: 668 return x > y, nil 669 default: 670 return invOp2(x, y, op) 671 } 672 case bool: 673 return undOp2(a, b, op) 674 case complex64: 675 return undOp2(a, b, op) 676 case complex128: 677 return undOp2(a, b, op) 678 case float32: 679 switch y := b.(type) { 680 case float32: 681 return x > y, nil 682 default: 683 return invOp2(x, y, op) 684 } 685 case float64: 686 switch y := b.(type) { 687 case float64: 688 return x > y, nil 689 default: 690 return invOp2(x, y, op) 691 } 692 case int8: 693 switch y := b.(type) { 694 case int8: 695 return x > y, nil 696 default: 697 return invOp2(x, y, op) 698 } 699 case int16: 700 switch y := b.(type) { 701 case int16: 702 return x > y, nil 703 default: 704 return invOp2(x, y, op) 705 } 706 case int32: 707 switch y := b.(type) { 708 case int32: 709 return x > y, nil 710 default: 711 return invOp2(x, y, op) 712 } 713 case int64: 714 switch y := b.(type) { 715 case int64: 716 return x > y, nil 717 default: 718 return invOp2(x, y, op) 719 } 720 case string: 721 switch y := b.(type) { 722 case string: 723 return x > y, nil 724 default: 725 return invOp2(x, y, op) 726 } 727 case uint8: 728 switch y := b.(type) { 729 case uint8: 730 return x > y, nil 731 default: 732 return invOp2(x, y, op) 733 } 734 case uint16: 735 switch y := b.(type) { 736 case uint16: 737 return x > y, nil 738 default: 739 return invOp2(x, y, op) 740 } 741 case uint32: 742 switch y := b.(type) { 743 case uint32: 744 return x > y, nil 745 default: 746 return invOp2(x, y, op) 747 } 748 case uint64: 749 switch y := b.(type) { 750 case uint64: 751 return x > y, nil 752 default: 753 return invOp2(x, y, op) 754 } 755 case *big.Int: 756 switch y := b.(type) { 757 case *big.Int: 758 return x.Cmp(y) > 0, nil 759 default: 760 return invOp2(x, y, op) 761 } 762 case *big.Rat: 763 switch y := b.(type) { 764 case *big.Rat: 765 return x.Cmp(y) > 0, nil 766 default: 767 return invOp2(x, y, op) 768 } 769 case time.Duration: 770 switch y := b.(type) { 771 case time.Duration: 772 return x > y, nil 773 default: 774 return invOp2(x, y, op) 775 } 776 case time.Time: 777 switch y := b.(type) { 778 case time.Time: 779 return x.After(y), nil 780 default: 781 return invOp2(x, y, op) 782 } 783 case []byte: 784 switch y := b.(type) { 785 case []byte: 786 return bytes.Equal(x, y), nil 787 default: 788 return invOp2(x, y, op) 789 } 790 default: 791 return invOp2(a, b, op) 792 } 793 case '<': 794 a, b := o.get2(execCtx, ctx) 795 if a == nil || b == nil { 796 return 797 } 798 switch x := a.(type) { 799 //case nil: 800 case idealComplex: 801 return undOp2(a, b, op) 802 case idealFloat: 803 switch y := b.(type) { 804 case idealFloat: 805 return x < y, nil 806 default: 807 return invOp2(x, y, op) 808 } 809 case idealInt: 810 switch y := b.(type) { 811 case idealInt: 812 return x < y, nil 813 default: 814 return invOp2(x, y, op) 815 } 816 case idealRune: 817 switch y := b.(type) { 818 case idealRune: 819 return x < y, nil 820 default: 821 return invOp2(x, y, op) 822 } 823 case idealUint: 824 switch y := b.(type) { 825 case idealUint: 826 return x < y, nil 827 default: 828 return invOp2(x, y, op) 829 } 830 case bool: 831 return undOp2(a, b, op) 832 case complex64: 833 return undOp2(a, b, op) 834 case complex128: 835 return undOp2(a, b, op) 836 case float32: 837 switch y := b.(type) { 838 case float32: 839 return x < y, nil 840 default: 841 return invOp2(x, y, op) 842 } 843 case float64: 844 switch y := b.(type) { 845 case float64: 846 return x < y, nil 847 default: 848 return invOp2(x, y, op) 849 } 850 case int8: 851 switch y := b.(type) { 852 case int8: 853 return x < y, nil 854 default: 855 return invOp2(x, y, op) 856 } 857 case int16: 858 switch y := b.(type) { 859 case int16: 860 return x < y, nil 861 default: 862 return invOp2(x, y, op) 863 } 864 case int32: 865 switch y := b.(type) { 866 case int32: 867 return x < y, nil 868 default: 869 return invOp2(x, y, op) 870 } 871 case int64: 872 switch y := b.(type) { 873 case int64: 874 return x < y, nil 875 default: 876 return invOp2(x, y, op) 877 } 878 case string: 879 switch y := b.(type) { 880 case string: 881 return x < y, nil 882 default: 883 return invOp2(x, y, op) 884 } 885 case uint8: 886 switch y := b.(type) { 887 case uint8: 888 return x < y, nil 889 default: 890 return invOp2(x, y, op) 891 } 892 case uint16: 893 switch y := b.(type) { 894 case uint16: 895 return x < y, nil 896 default: 897 return invOp2(x, y, op) 898 } 899 case uint32: 900 switch y := b.(type) { 901 case uint32: 902 return x < y, nil 903 default: 904 return invOp2(x, y, op) 905 } 906 case uint64: 907 switch y := b.(type) { 908 case uint64: 909 return x < y, nil 910 default: 911 return invOp2(x, y, op) 912 } 913 case *big.Int: 914 switch y := b.(type) { 915 case *big.Int: 916 return x.Cmp(y) < 0, nil 917 default: 918 return invOp2(x, y, op) 919 } 920 case *big.Rat: 921 switch y := b.(type) { 922 case *big.Rat: 923 return x.Cmp(y) < 0, nil 924 default: 925 return invOp2(x, y, op) 926 } 927 case time.Duration: 928 switch y := b.(type) { 929 case time.Duration: 930 return x < y, nil 931 default: 932 return invOp2(x, y, op) 933 } 934 case time.Time: 935 switch y := b.(type) { 936 case time.Time: 937 return x.Before(y), nil 938 default: 939 return invOp2(x, y, op) 940 } 941 case []byte: 942 switch y := b.(type) { 943 case []byte: 944 return bytes.Compare(x, y) < 0, nil 945 default: 946 return invOp2(x, y, op) 947 } 948 default: 949 return invOp2(a, b, op) 950 } 951 case le: 952 a, b := o.get2(execCtx, ctx) 953 if a == nil || b == nil { 954 return 955 } 956 switch x := a.(type) { 957 //case nil: 958 case idealComplex: 959 return undOp2(a, b, op) 960 case idealFloat: 961 switch y := b.(type) { 962 case idealFloat: 963 return x <= y, nil 964 default: 965 return invOp2(x, y, op) 966 } 967 case idealInt: 968 switch y := b.(type) { 969 case idealInt: 970 return x <= y, nil 971 default: 972 return invOp2(x, y, op) 973 } 974 case idealRune: 975 switch y := b.(type) { 976 case idealRune: 977 return x <= y, nil 978 default: 979 return invOp2(x, y, op) 980 } 981 case idealUint: 982 switch y := b.(type) { 983 case idealUint: 984 return x <= y, nil 985 default: 986 return invOp2(x, y, op) 987 } 988 case bool: 989 return undOp2(a, b, op) 990 case complex64: 991 return undOp2(a, b, op) 992 case complex128: 993 return undOp2(a, b, op) 994 case float32: 995 switch y := b.(type) { 996 case float32: 997 return x <= y, nil 998 default: 999 return invOp2(x, y, op) 1000 } 1001 case float64: 1002 switch y := b.(type) { 1003 case float64: 1004 return x <= y, nil 1005 default: 1006 return invOp2(x, y, op) 1007 } 1008 case int8: 1009 switch y := b.(type) { 1010 case int8: 1011 return x <= y, nil 1012 default: 1013 return invOp2(x, y, op) 1014 } 1015 case int16: 1016 switch y := b.(type) { 1017 case int16: 1018 return x <= y, nil 1019 default: 1020 return invOp2(x, y, op) 1021 } 1022 case int32: 1023 switch y := b.(type) { 1024 case int32: 1025 return x <= y, nil 1026 default: 1027 return invOp2(x, y, op) 1028 } 1029 case int64: 1030 switch y := b.(type) { 1031 case int64: 1032 return x <= y, nil 1033 default: 1034 return invOp2(x, y, op) 1035 } 1036 case string: 1037 switch y := b.(type) { 1038 case string: 1039 return x <= y, nil 1040 default: 1041 return invOp2(x, y, op) 1042 } 1043 case uint8: 1044 switch y := b.(type) { 1045 case uint8: 1046 return x <= y, nil 1047 default: 1048 return invOp2(x, y, op) 1049 } 1050 case uint16: 1051 switch y := b.(type) { 1052 case uint16: 1053 return x <= y, nil 1054 default: 1055 return invOp2(x, y, op) 1056 } 1057 case uint32: 1058 switch y := b.(type) { 1059 case uint32: 1060 return x <= y, nil 1061 default: 1062 return invOp2(x, y, op) 1063 } 1064 case uint64: 1065 switch y := b.(type) { 1066 case uint64: 1067 return x <= y, nil 1068 default: 1069 return invOp2(x, y, op) 1070 } 1071 case *big.Int: 1072 switch y := b.(type) { 1073 case *big.Int: 1074 return x.Cmp(y) <= 0, nil 1075 default: 1076 return invOp2(x, y, op) 1077 } 1078 case *big.Rat: 1079 switch y := b.(type) { 1080 case *big.Rat: 1081 return x.Cmp(y) <= 0, nil 1082 default: 1083 return invOp2(x, y, op) 1084 } 1085 case time.Duration: 1086 switch y := b.(type) { 1087 case time.Duration: 1088 return x <= y, nil 1089 default: 1090 return invOp2(x, y, op) 1091 } 1092 case time.Time: 1093 switch y := b.(type) { 1094 case time.Time: 1095 return x.Before(y) || x.Equal(y), nil 1096 default: 1097 return invOp2(x, y, op) 1098 } 1099 case []byte: 1100 switch y := b.(type) { 1101 case []byte: 1102 return bytes.Compare(x, y) <= 0, nil 1103 default: 1104 return invOp2(x, y, op) 1105 } 1106 default: 1107 return invOp2(a, b, op) 1108 } 1109 case ge: 1110 a, b := o.get2(execCtx, ctx) 1111 if a == nil || b == nil { 1112 return 1113 } 1114 switch x := a.(type) { 1115 //case nil: 1116 case idealComplex: 1117 return undOp2(a, b, op) 1118 case idealFloat: 1119 switch y := b.(type) { 1120 case idealFloat: 1121 return x >= y, nil 1122 default: 1123 return invOp2(x, y, op) 1124 } 1125 case idealInt: 1126 switch y := b.(type) { 1127 case idealInt: 1128 return x >= y, nil 1129 default: 1130 return invOp2(x, y, op) 1131 } 1132 case idealRune: 1133 switch y := b.(type) { 1134 case idealRune: 1135 return x >= y, nil 1136 default: 1137 return invOp2(x, y, op) 1138 } 1139 case idealUint: 1140 switch y := b.(type) { 1141 case idealUint: 1142 return x >= y, nil 1143 default: 1144 return invOp2(x, y, op) 1145 } 1146 case bool: 1147 return undOp2(a, b, op) 1148 case complex64: 1149 return undOp2(a, b, op) 1150 case complex128: 1151 return undOp2(a, b, op) 1152 case float32: 1153 switch y := b.(type) { 1154 case float32: 1155 return x >= y, nil 1156 default: 1157 return invOp2(x, y, op) 1158 } 1159 case float64: 1160 switch y := b.(type) { 1161 case float64: 1162 return x >= y, nil 1163 default: 1164 return invOp2(x, y, op) 1165 } 1166 case int8: 1167 switch y := b.(type) { 1168 case int8: 1169 return x >= y, nil 1170 default: 1171 return invOp2(x, y, op) 1172 } 1173 case int16: 1174 switch y := b.(type) { 1175 case int16: 1176 return x >= y, nil 1177 default: 1178 return invOp2(x, y, op) 1179 } 1180 case int32: 1181 switch y := b.(type) { 1182 case int32: 1183 return x >= y, nil 1184 default: 1185 return invOp2(x, y, op) 1186 } 1187 case int64: 1188 switch y := b.(type) { 1189 case int64: 1190 return x >= y, nil 1191 default: 1192 return invOp2(x, y, op) 1193 } 1194 case string: 1195 switch y := b.(type) { 1196 case string: 1197 return x >= y, nil 1198 default: 1199 return invOp2(x, y, op) 1200 } 1201 case uint8: 1202 switch y := b.(type) { 1203 case uint8: 1204 return x >= y, nil 1205 default: 1206 return invOp2(x, y, op) 1207 } 1208 case uint16: 1209 switch y := b.(type) { 1210 case uint16: 1211 return x >= y, nil 1212 default: 1213 return invOp2(x, y, op) 1214 } 1215 case uint32: 1216 switch y := b.(type) { 1217 case uint32: 1218 return x >= y, nil 1219 default: 1220 return invOp2(x, y, op) 1221 } 1222 case uint64: 1223 switch y := b.(type) { 1224 case uint64: 1225 return x >= y, nil 1226 default: 1227 return invOp2(x, y, op) 1228 } 1229 case *big.Int: 1230 switch y := b.(type) { 1231 case *big.Int: 1232 return x.Cmp(y) >= 0, nil 1233 default: 1234 return invOp2(x, y, op) 1235 } 1236 case *big.Rat: 1237 switch y := b.(type) { 1238 case *big.Rat: 1239 return x.Cmp(y) >= 0, nil 1240 default: 1241 return invOp2(x, y, op) 1242 } 1243 case time.Duration: 1244 switch y := b.(type) { 1245 case time.Duration: 1246 return x >= y, nil 1247 default: 1248 return invOp2(x, y, op) 1249 } 1250 case time.Time: 1251 switch y := b.(type) { 1252 case time.Time: 1253 return x.After(y) || x.Equal(y), nil 1254 default: 1255 return invOp2(x, y, op) 1256 } 1257 case []byte: 1258 switch y := b.(type) { 1259 case []byte: 1260 return bytes.Compare(x, y) >= 0, nil 1261 default: 1262 return invOp2(x, y, op) 1263 } 1264 default: 1265 return invOp2(a, b, op) 1266 } 1267 case neq: 1268 a, b := o.get2(execCtx, ctx) 1269 if a == nil || b == nil { 1270 return 1271 } 1272 switch x := a.(type) { 1273 //case nil: 1274 case idealComplex: 1275 switch y := b.(type) { 1276 case idealComplex: 1277 return x != y, nil 1278 default: 1279 return invOp2(x, y, op) 1280 } 1281 case idealFloat: 1282 switch y := b.(type) { 1283 case idealFloat: 1284 return x != y, nil 1285 default: 1286 return invOp2(x, y, op) 1287 } 1288 case idealInt: 1289 switch y := b.(type) { 1290 case idealInt: 1291 return x != y, nil 1292 default: 1293 return invOp2(x, y, op) 1294 } 1295 case idealRune: 1296 switch y := b.(type) { 1297 case idealRune: 1298 return x != y, nil 1299 default: 1300 return invOp2(x, y, op) 1301 } 1302 case idealUint: 1303 switch y := b.(type) { 1304 case idealUint: 1305 return x != y, nil 1306 default: 1307 return invOp2(x, y, op) 1308 } 1309 case bool: 1310 switch y := b.(type) { 1311 case bool: 1312 return x != y, nil 1313 default: 1314 return invOp2(x, y, op) 1315 } 1316 case complex64: 1317 switch y := b.(type) { 1318 case complex64: 1319 return x != y, nil 1320 default: 1321 return invOp2(x, y, op) 1322 } 1323 case complex128: 1324 switch y := b.(type) { 1325 case complex128: 1326 return x != y, nil 1327 default: 1328 return invOp2(x, y, op) 1329 } 1330 case float32: 1331 switch y := b.(type) { 1332 case float32: 1333 return x != y, nil 1334 default: 1335 return invOp2(x, y, op) 1336 } 1337 case float64: 1338 switch y := b.(type) { 1339 case float64: 1340 return x != y, nil 1341 default: 1342 return invOp2(x, y, op) 1343 } 1344 case int8: 1345 switch y := b.(type) { 1346 case int8: 1347 return x != y, nil 1348 default: 1349 return invOp2(x, y, op) 1350 } 1351 case int16: 1352 switch y := b.(type) { 1353 case int16: 1354 return x != y, nil 1355 default: 1356 return invOp2(x, y, op) 1357 } 1358 case int32: 1359 switch y := b.(type) { 1360 case int32: 1361 return x != y, nil 1362 default: 1363 return invOp2(x, y, op) 1364 } 1365 case int64: 1366 switch y := b.(type) { 1367 case int64: 1368 return x != y, nil 1369 default: 1370 return invOp2(x, y, op) 1371 } 1372 case string: 1373 switch y := b.(type) { 1374 case string: 1375 return x != y, nil 1376 default: 1377 return invOp2(x, y, op) 1378 } 1379 case uint8: 1380 switch y := b.(type) { 1381 case uint8: 1382 return x != y, nil 1383 default: 1384 return invOp2(x, y, op) 1385 } 1386 case uint16: 1387 switch y := b.(type) { 1388 case uint16: 1389 return x != y, nil 1390 default: 1391 return invOp2(x, y, op) 1392 } 1393 case uint32: 1394 switch y := b.(type) { 1395 case uint32: 1396 return x != y, nil 1397 default: 1398 return invOp2(x, y, op) 1399 } 1400 case uint64: 1401 switch y := b.(type) { 1402 case uint64: 1403 return x != y, nil 1404 default: 1405 return invOp2(x, y, op) 1406 } 1407 case *big.Int: 1408 switch y := b.(type) { 1409 case *big.Int: 1410 return x.Cmp(y) != 0, nil 1411 default: 1412 return invOp2(x, y, op) 1413 } 1414 case *big.Rat: 1415 switch y := b.(type) { 1416 case *big.Rat: 1417 return x.Cmp(y) != 0, nil 1418 default: 1419 return invOp2(x, y, op) 1420 } 1421 case time.Duration: 1422 switch y := b.(type) { 1423 case time.Duration: 1424 return x != y, nil 1425 default: 1426 return invOp2(x, y, op) 1427 } 1428 case time.Time: 1429 switch y := b.(type) { 1430 case time.Time: 1431 return !x.Equal(y), nil 1432 default: 1433 return invOp2(x, y, op) 1434 } 1435 case []byte: 1436 switch y := b.(type) { 1437 case []byte: 1438 return !bytes.Equal(x, y), nil 1439 default: 1440 return invOp2(x, y, op) 1441 } 1442 default: 1443 return invOp2(a, b, op) 1444 } 1445 case eq: 1446 a, b := o.get2(execCtx, ctx) 1447 if a == nil || b == nil { 1448 return 1449 } 1450 switch x := a.(type) { 1451 //case nil: 1452 case idealComplex: 1453 switch y := b.(type) { 1454 case idealComplex: 1455 return x == y, nil 1456 default: 1457 return invOp2(x, y, op) 1458 } 1459 case idealFloat: 1460 switch y := b.(type) { 1461 case idealFloat: 1462 return x == y, nil 1463 default: 1464 return invOp2(x, y, op) 1465 } 1466 case idealInt: 1467 switch y := b.(type) { 1468 case idealInt: 1469 return x == y, nil 1470 default: 1471 return invOp2(x, y, op) 1472 } 1473 case idealRune: 1474 switch y := b.(type) { 1475 case idealRune: 1476 return x == y, nil 1477 default: 1478 return invOp2(x, y, op) 1479 } 1480 case idealUint: 1481 switch y := b.(type) { 1482 case idealUint: 1483 return x == y, nil 1484 default: 1485 return invOp2(x, y, op) 1486 } 1487 case bool: 1488 switch y := b.(type) { 1489 case bool: 1490 return x == y, nil 1491 default: 1492 return invOp2(x, y, op) 1493 } 1494 case complex64: 1495 switch y := b.(type) { 1496 case complex64: 1497 return x == y, nil 1498 default: 1499 return invOp2(x, y, op) 1500 } 1501 case complex128: 1502 switch y := b.(type) { 1503 case complex128: 1504 return x == y, nil 1505 default: 1506 return invOp2(x, y, op) 1507 } 1508 case float32: 1509 switch y := b.(type) { 1510 case float32: 1511 return x == y, nil 1512 default: 1513 return invOp2(x, y, op) 1514 } 1515 case float64: 1516 switch y := b.(type) { 1517 case float64: 1518 return x == y, nil 1519 default: 1520 return invOp2(x, y, op) 1521 } 1522 case int8: 1523 switch y := b.(type) { 1524 case int8: 1525 return x == y, nil 1526 default: 1527 return invOp2(x, y, op) 1528 } 1529 case int16: 1530 switch y := b.(type) { 1531 case int16: 1532 return x == y, nil 1533 default: 1534 return invOp2(x, y, op) 1535 } 1536 case int32: 1537 switch y := b.(type) { 1538 case int32: 1539 return x == y, nil 1540 default: 1541 return invOp2(x, y, op) 1542 } 1543 case int64: 1544 switch y := b.(type) { 1545 case int64: 1546 return x == y, nil 1547 default: 1548 return invOp2(x, y, op) 1549 } 1550 case string: 1551 switch y := b.(type) { 1552 case string: 1553 return x == y, nil 1554 default: 1555 return invOp2(x, y, op) 1556 } 1557 case uint8: 1558 switch y := b.(type) { 1559 case uint8: 1560 return x == y, nil 1561 default: 1562 return invOp2(x, y, op) 1563 } 1564 case uint16: 1565 switch y := b.(type) { 1566 case uint16: 1567 return x == y, nil 1568 default: 1569 return invOp2(x, y, op) 1570 } 1571 case uint32: 1572 switch y := b.(type) { 1573 case uint32: 1574 return x == y, nil 1575 default: 1576 return invOp2(x, y, op) 1577 } 1578 case uint64: 1579 switch y := b.(type) { 1580 case uint64: 1581 return x == y, nil 1582 default: 1583 return invOp2(x, y, op) 1584 } 1585 case *big.Int: 1586 switch y := b.(type) { 1587 case *big.Int: 1588 return x.Cmp(y) == 0, nil 1589 default: 1590 return invOp2(x, y, op) 1591 } 1592 case *big.Rat: 1593 switch y := b.(type) { 1594 case *big.Rat: 1595 return x.Cmp(y) == 0, nil 1596 default: 1597 return invOp2(x, y, op) 1598 } 1599 case time.Duration: 1600 switch y := b.(type) { 1601 case time.Duration: 1602 return x == y, nil 1603 default: 1604 return invOp2(x, y, op) 1605 } 1606 case time.Time: 1607 switch y := b.(type) { 1608 case time.Time: 1609 return x.Equal(y), nil 1610 default: 1611 return invOp2(x, y, op) 1612 } 1613 case []byte: 1614 switch y := b.(type) { 1615 case []byte: 1616 return bytes.Equal(x, y), nil 1617 default: 1618 return invOp2(x, y, op) 1619 } 1620 default: 1621 return invOp2(a, b, op) 1622 } 1623 case '+': 1624 a, b := o.get2(execCtx, ctx) 1625 if a == nil || b == nil { 1626 return 1627 } 1628 switch x := a.(type) { 1629 //case nil: 1630 case idealComplex: 1631 switch y := b.(type) { 1632 case idealComplex: 1633 return idealComplex(complex64(x) + complex64(y)), nil 1634 default: 1635 return invOp2(x, y, op) 1636 } 1637 case idealFloat: 1638 switch y := b.(type) { 1639 case idealFloat: 1640 return idealFloat(float64(x) + float64(y)), nil 1641 default: 1642 return invOp2(x, y, op) 1643 } 1644 case idealInt: 1645 switch y := b.(type) { 1646 case idealInt: 1647 return idealInt(int64(x) + int64(y)), nil 1648 default: 1649 return invOp2(x, y, op) 1650 } 1651 case idealRune: 1652 switch y := b.(type) { 1653 case idealRune: 1654 return idealRune(int64(x) + int64(y)), nil 1655 default: 1656 return invOp2(x, y, op) 1657 } 1658 case idealUint: 1659 switch y := b.(type) { 1660 case idealUint: 1661 return idealUint(uint64(x) + uint64(y)), nil 1662 default: 1663 return invOp2(x, y, op) 1664 } 1665 case bool: 1666 return undOp2(a, b, op) 1667 case complex64: 1668 switch y := b.(type) { 1669 case complex64: 1670 return x + y, nil 1671 default: 1672 return invOp2(x, y, op) 1673 } 1674 case complex128: 1675 switch y := b.(type) { 1676 case complex128: 1677 return x + y, nil 1678 default: 1679 return invOp2(x, y, op) 1680 } 1681 case float32: 1682 switch y := b.(type) { 1683 case float32: 1684 return x + y, nil 1685 default: 1686 return invOp2(x, y, op) 1687 } 1688 case float64: 1689 switch y := b.(type) { 1690 case float64: 1691 return x + y, nil 1692 default: 1693 return invOp2(x, y, op) 1694 } 1695 case int8: 1696 switch y := b.(type) { 1697 case int8: 1698 return x + y, nil 1699 default: 1700 return invOp2(x, y, op) 1701 } 1702 case int16: 1703 switch y := b.(type) { 1704 case int16: 1705 return x + y, nil 1706 default: 1707 return invOp2(x, y, op) 1708 } 1709 case int32: 1710 switch y := b.(type) { 1711 case int32: 1712 return x + y, nil 1713 default: 1714 return invOp2(x, y, op) 1715 } 1716 case int64: 1717 switch y := b.(type) { 1718 case int64: 1719 return x + y, nil 1720 default: 1721 return invOp2(x, y, op) 1722 } 1723 case string: 1724 switch y := b.(type) { 1725 case string: 1726 return x + y, nil 1727 default: 1728 return invOp2(x, y, op) 1729 } 1730 case uint8: 1731 switch y := b.(type) { 1732 case uint8: 1733 return x + y, nil 1734 default: 1735 return invOp2(x, y, op) 1736 } 1737 case uint16: 1738 switch y := b.(type) { 1739 case uint16: 1740 return x + y, nil 1741 default: 1742 return invOp2(x, y, op) 1743 } 1744 case uint32: 1745 switch y := b.(type) { 1746 case uint32: 1747 return x + y, nil 1748 default: 1749 return invOp2(x, y, op) 1750 } 1751 case uint64: 1752 switch y := b.(type) { 1753 case uint64: 1754 return x + y, nil 1755 default: 1756 return invOp2(x, y, op) 1757 } 1758 case *big.Int: 1759 switch y := b.(type) { 1760 case *big.Int: 1761 var z big.Int 1762 return z.Add(x, y), nil 1763 default: 1764 return invOp2(x, y, op) 1765 } 1766 case *big.Rat: 1767 switch y := b.(type) { 1768 case *big.Rat: 1769 var z big.Rat 1770 return z.Add(x, y), nil 1771 default: 1772 return invOp2(x, y, op) 1773 } 1774 case time.Duration: 1775 switch y := b.(type) { 1776 case time.Duration: 1777 return x + y, nil 1778 case time.Time: 1779 return y.Add(x), nil 1780 default: 1781 return invOp2(x, y, op) 1782 } 1783 case time.Time: 1784 switch y := b.(type) { 1785 case time.Duration: 1786 return x.Add(y), nil 1787 default: 1788 return invOp2(x, y, op) 1789 } 1790 default: 1791 return invOp2(a, b, op) 1792 } 1793 case '-': 1794 a, b := o.get2(execCtx, ctx) 1795 if a == nil || b == nil { 1796 return 1797 } 1798 switch x := a.(type) { 1799 //case nil: 1800 case idealComplex: 1801 switch y := b.(type) { 1802 case idealComplex: 1803 return idealComplex(complex64(x) - complex64(y)), nil 1804 default: 1805 return invOp2(x, y, op) 1806 } 1807 case idealFloat: 1808 switch y := b.(type) { 1809 case idealFloat: 1810 return idealFloat(float64(x) - float64(y)), nil 1811 default: 1812 return invOp2(x, y, op) 1813 } 1814 case idealInt: 1815 switch y := b.(type) { 1816 case idealInt: 1817 return idealInt(int64(x) - int64(y)), nil 1818 default: 1819 return invOp2(x, y, op) 1820 } 1821 case idealRune: 1822 switch y := b.(type) { 1823 case idealRune: 1824 return idealRune(int64(x) - int64(y)), nil 1825 default: 1826 return invOp2(x, y, op) 1827 } 1828 case idealUint: 1829 switch y := b.(type) { 1830 case idealUint: 1831 return idealUint(uint64(x) - uint64(y)), nil 1832 default: 1833 return invOp2(x, y, op) 1834 } 1835 case bool: 1836 return undOp2(a, b, op) 1837 case complex64: 1838 switch y := b.(type) { 1839 case complex64: 1840 return x - y, nil 1841 default: 1842 return invOp2(x, y, op) 1843 } 1844 case complex128: 1845 switch y := b.(type) { 1846 case complex128: 1847 return x - y, nil 1848 default: 1849 return invOp2(x, y, op) 1850 } 1851 case float32: 1852 switch y := b.(type) { 1853 case float32: 1854 return x - y, nil 1855 default: 1856 return invOp2(x, y, op) 1857 } 1858 case float64: 1859 switch y := b.(type) { 1860 case float64: 1861 return x - y, nil 1862 default: 1863 return invOp2(x, y, op) 1864 } 1865 case int8: 1866 switch y := b.(type) { 1867 case int8: 1868 return x - y, nil 1869 default: 1870 return invOp2(x, y, op) 1871 } 1872 case int16: 1873 switch y := b.(type) { 1874 case int16: 1875 return x - y, nil 1876 default: 1877 return invOp2(x, y, op) 1878 } 1879 case int32: 1880 switch y := b.(type) { 1881 case int32: 1882 return x - y, nil 1883 default: 1884 return invOp2(x, y, op) 1885 } 1886 case int64: 1887 switch y := b.(type) { 1888 case int64: 1889 return x - y, nil 1890 default: 1891 return invOp2(x, y, op) 1892 } 1893 case string: 1894 return undOp2(a, b, op) 1895 case uint8: 1896 switch y := b.(type) { 1897 case uint8: 1898 return x - y, nil 1899 default: 1900 return invOp2(x, y, op) 1901 } 1902 case uint16: 1903 switch y := b.(type) { 1904 case uint16: 1905 return x - y, nil 1906 default: 1907 return invOp2(x, y, op) 1908 } 1909 case uint32: 1910 switch y := b.(type) { 1911 case uint32: 1912 return x - y, nil 1913 default: 1914 return invOp2(x, y, op) 1915 } 1916 case uint64: 1917 switch y := b.(type) { 1918 case uint64: 1919 return x - y, nil 1920 default: 1921 return invOp2(x, y, op) 1922 } 1923 case *big.Int: 1924 switch y := b.(type) { 1925 case *big.Int: 1926 var z big.Int 1927 return z.Sub(x, y), nil 1928 default: 1929 return invOp2(x, y, op) 1930 } 1931 case *big.Rat: 1932 switch y := b.(type) { 1933 case *big.Rat: 1934 var z big.Rat 1935 return z.Sub(x, y), nil 1936 default: 1937 return invOp2(x, y, op) 1938 } 1939 case time.Duration: 1940 switch y := b.(type) { 1941 case time.Duration: 1942 return x - y, nil 1943 default: 1944 return invOp2(x, y, op) 1945 } 1946 case time.Time: 1947 switch y := b.(type) { 1948 case time.Duration: 1949 return x.Add(-y), nil 1950 case time.Time: 1951 return x.Sub(y), nil 1952 default: 1953 return invOp2(x, y, op) 1954 } 1955 default: 1956 return invOp2(a, b, op) 1957 } 1958 case rsh: 1959 a, b := eval2(o.l, o.r, execCtx, ctx) 1960 if a == nil || b == nil { 1961 return 1962 } 1963 1964 var cnt uint64 1965 switch y := b.(type) { 1966 //case nil: 1967 case idealComplex: 1968 return invShiftRHS(a, b) 1969 case idealFloat: 1970 return invShiftRHS(a, b) 1971 case idealInt: 1972 cnt = uint64(y) 1973 case idealRune: 1974 cnt = uint64(y) 1975 case idealUint: 1976 cnt = uint64(y) 1977 case bool: 1978 return invShiftRHS(a, b) 1979 case complex64: 1980 return invShiftRHS(a, b) 1981 case complex128: 1982 return invShiftRHS(a, b) 1983 case float32: 1984 return invShiftRHS(a, b) 1985 case float64: 1986 return invShiftRHS(a, b) 1987 case int8: 1988 return invShiftRHS(a, b) 1989 case int16: 1990 return invShiftRHS(a, b) 1991 case int32: 1992 return invShiftRHS(a, b) 1993 case int64: 1994 return invShiftRHS(a, b) 1995 case string: 1996 return invShiftRHS(a, b) 1997 case uint8: 1998 cnt = uint64(y) 1999 case uint16: 2000 cnt = uint64(y) 2001 case uint32: 2002 cnt = uint64(y) 2003 case uint64: 2004 cnt = y 2005 default: 2006 return invOp2(a, b, op) 2007 } 2008 2009 switch x := a.(type) { 2010 //case nil: 2011 case idealComplex: 2012 return undOp2(a, b, op) 2013 case idealFloat: 2014 return undOp2(a, b, op) 2015 case idealInt: 2016 return idealInt(int64(x) >> cnt), nil 2017 case idealRune: 2018 return idealRune(int64(x) >> cnt), nil 2019 case idealUint: 2020 return idealUint(uint64(x) >> cnt), nil 2021 case bool: 2022 return undOp2(a, b, op) 2023 case complex64: 2024 return undOp2(a, b, op) 2025 case complex128: 2026 return undOp2(a, b, op) 2027 case float32: 2028 return undOp2(a, b, op) 2029 case float64: 2030 return undOp2(a, b, op) 2031 case int8: 2032 return x >> cnt, nil 2033 case int16: 2034 return x >> cnt, nil 2035 case int32: 2036 return x >> cnt, nil 2037 case int64: 2038 return x >> cnt, nil 2039 case string: 2040 return undOp2(a, b, op) 2041 case uint8: 2042 return x >> cnt, nil 2043 case uint16: 2044 return x >> cnt, nil 2045 case uint32: 2046 return x >> cnt, nil 2047 case uint64: 2048 return x >> cnt, nil 2049 case *big.Int: 2050 var z big.Int 2051 return z.Rsh(x, uint(cnt)), nil 2052 case time.Duration: 2053 return x >> cnt, nil 2054 default: 2055 return invOp2(a, b, op) 2056 } 2057 case lsh: 2058 a, b := eval2(o.l, o.r, execCtx, ctx) 2059 if a == nil || b == nil { 2060 return 2061 } 2062 2063 var cnt uint64 2064 switch y := b.(type) { 2065 //case nil: 2066 case idealComplex: 2067 return invShiftRHS(a, b) 2068 case idealFloat: 2069 return invShiftRHS(a, b) 2070 case idealInt: 2071 cnt = uint64(y) 2072 case idealRune: 2073 cnt = uint64(y) 2074 case idealUint: 2075 cnt = uint64(y) 2076 case bool: 2077 return invShiftRHS(a, b) 2078 case complex64: 2079 return invShiftRHS(a, b) 2080 case complex128: 2081 return invShiftRHS(a, b) 2082 case float32: 2083 return invShiftRHS(a, b) 2084 case float64: 2085 return invShiftRHS(a, b) 2086 case int8: 2087 return invShiftRHS(a, b) 2088 case int16: 2089 return invShiftRHS(a, b) 2090 case int32: 2091 return invShiftRHS(a, b) 2092 case int64: 2093 return invShiftRHS(a, b) 2094 case string: 2095 return invShiftRHS(a, b) 2096 case uint8: 2097 cnt = uint64(y) 2098 case uint16: 2099 cnt = uint64(y) 2100 case uint32: 2101 cnt = uint64(y) 2102 case uint64: 2103 cnt = y 2104 default: 2105 return invOp2(a, b, op) 2106 } 2107 2108 switch x := a.(type) { 2109 //case nil: 2110 case idealComplex: 2111 return undOp2(a, b, op) 2112 case idealFloat: 2113 return undOp2(a, b, op) 2114 case idealInt: 2115 return idealInt(int64(x) << cnt), nil 2116 case idealRune: 2117 return idealRune(int64(x) << cnt), nil 2118 case idealUint: 2119 return idealUint(uint64(x) << cnt), nil 2120 case bool: 2121 return undOp2(a, b, op) 2122 case complex64: 2123 return undOp2(a, b, op) 2124 case complex128: 2125 return undOp2(a, b, op) 2126 case float32: 2127 return undOp2(a, b, op) 2128 case float64: 2129 return undOp2(a, b, op) 2130 case int8: 2131 return x << cnt, nil 2132 case int16: 2133 return x << cnt, nil 2134 case int32: 2135 return x << cnt, nil 2136 case int64: 2137 return x << cnt, nil 2138 case string: 2139 return undOp2(a, b, op) 2140 case uint8: 2141 return x << cnt, nil 2142 case uint16: 2143 return x << cnt, nil 2144 case uint32: 2145 return x << cnt, nil 2146 case uint64: 2147 return x << cnt, nil 2148 case *big.Int: 2149 var z big.Int 2150 return z.Lsh(x, uint(cnt)), nil 2151 case time.Duration: 2152 return x << cnt, nil 2153 default: 2154 return invOp2(a, b, op) 2155 } 2156 case '&': 2157 a, b := o.get2(execCtx, ctx) 2158 if a == nil || b == nil { 2159 return 2160 } 2161 switch x := a.(type) { 2162 //case nil: 2163 case idealComplex: 2164 return undOp2(a, b, op) 2165 case idealFloat: 2166 return undOp2(a, b, op) 2167 case idealInt: 2168 switch y := b.(type) { 2169 case idealInt: 2170 return idealInt(int64(x) & int64(y)), nil 2171 default: 2172 return invOp2(x, y, op) 2173 } 2174 case idealRune: 2175 switch y := b.(type) { 2176 case idealRune: 2177 return idealRune(int64(x) & int64(y)), nil 2178 default: 2179 return invOp2(x, y, op) 2180 } 2181 case idealUint: 2182 switch y := b.(type) { 2183 case idealUint: 2184 return idealUint(uint64(x) & uint64(y)), nil 2185 default: 2186 return invOp2(x, y, op) 2187 } 2188 case bool: 2189 return undOp2(a, b, op) 2190 case complex64: 2191 return undOp2(a, b, op) 2192 case complex128: 2193 return undOp2(a, b, op) 2194 case float32: 2195 return undOp2(a, b, op) 2196 case float64: 2197 return undOp2(a, b, op) 2198 case int8: 2199 switch y := b.(type) { 2200 case int8: 2201 return x & y, nil 2202 default: 2203 return invOp2(x, y, op) 2204 } 2205 case int16: 2206 switch y := b.(type) { 2207 case int16: 2208 return x & y, nil 2209 default: 2210 return invOp2(x, y, op) 2211 } 2212 case int32: 2213 switch y := b.(type) { 2214 case int32: 2215 return x & y, nil 2216 default: 2217 return invOp2(x, y, op) 2218 } 2219 case int64: 2220 switch y := b.(type) { 2221 case int64: 2222 return x & y, nil 2223 default: 2224 return invOp2(x, y, op) 2225 } 2226 case string: 2227 return undOp2(a, b, op) 2228 case uint8: 2229 switch y := b.(type) { 2230 case uint8: 2231 return x & y, nil 2232 default: 2233 return invOp2(x, y, op) 2234 } 2235 case uint16: 2236 switch y := b.(type) { 2237 case uint16: 2238 return x & y, nil 2239 default: 2240 return invOp2(x, y, op) 2241 } 2242 case uint32: 2243 switch y := b.(type) { 2244 case uint32: 2245 return x & y, nil 2246 default: 2247 return invOp2(x, y, op) 2248 } 2249 case uint64: 2250 switch y := b.(type) { 2251 case uint64: 2252 return x & y, nil 2253 default: 2254 return invOp2(x, y, op) 2255 } 2256 case *big.Int: 2257 switch y := b.(type) { 2258 case *big.Int: 2259 var z big.Int 2260 return z.And(x, y), nil 2261 default: 2262 return invOp2(x, y, op) 2263 } 2264 case time.Duration: 2265 switch y := b.(type) { 2266 case time.Duration: 2267 return x & y, nil 2268 default: 2269 return invOp2(x, y, op) 2270 } 2271 default: 2272 return invOp2(a, b, op) 2273 } 2274 case '|': 2275 a, b := o.get2(execCtx, ctx) 2276 if a == nil || b == nil { 2277 return 2278 } 2279 switch x := a.(type) { 2280 //case nil: 2281 case idealComplex: 2282 return undOp2(a, b, op) 2283 case idealFloat: 2284 return undOp2(a, b, op) 2285 case idealInt: 2286 switch y := b.(type) { 2287 case idealInt: 2288 return idealInt(int64(x) | int64(y)), nil 2289 default: 2290 return invOp2(x, y, op) 2291 } 2292 case idealRune: 2293 switch y := b.(type) { 2294 case idealRune: 2295 return idealRune(int64(x) | int64(y)), nil 2296 default: 2297 return invOp2(x, y, op) 2298 } 2299 case idealUint: 2300 switch y := b.(type) { 2301 case idealUint: 2302 return idealUint(uint64(x) | uint64(y)), nil 2303 default: 2304 return invOp2(x, y, op) 2305 } 2306 case bool: 2307 return undOp2(a, b, op) 2308 case complex64: 2309 return undOp2(a, b, op) 2310 case complex128: 2311 return undOp2(a, b, op) 2312 case float32: 2313 return undOp2(a, b, op) 2314 case float64: 2315 return undOp2(a, b, op) 2316 case int8: 2317 switch y := b.(type) { 2318 case int8: 2319 return x | y, nil 2320 default: 2321 return invOp2(x, y, op) 2322 } 2323 case int16: 2324 switch y := b.(type) { 2325 case int16: 2326 return x | y, nil 2327 default: 2328 return invOp2(x, y, op) 2329 } 2330 case int32: 2331 switch y := b.(type) { 2332 case int32: 2333 return x | y, nil 2334 default: 2335 return invOp2(x, y, op) 2336 } 2337 case int64: 2338 switch y := b.(type) { 2339 case int64: 2340 return x | y, nil 2341 default: 2342 return invOp2(x, y, op) 2343 } 2344 case string: 2345 return undOp2(a, b, op) 2346 case uint8: 2347 switch y := b.(type) { 2348 case uint8: 2349 return x | y, nil 2350 default: 2351 return invOp2(x, y, op) 2352 } 2353 case uint16: 2354 switch y := b.(type) { 2355 case uint16: 2356 return x | y, nil 2357 default: 2358 return invOp2(x, y, op) 2359 } 2360 case uint32: 2361 switch y := b.(type) { 2362 case uint32: 2363 return x | y, nil 2364 default: 2365 return invOp2(x, y, op) 2366 } 2367 case uint64: 2368 switch y := b.(type) { 2369 case uint64: 2370 return x | y, nil 2371 default: 2372 return invOp2(x, y, op) 2373 } 2374 case *big.Int: 2375 switch y := b.(type) { 2376 case *big.Int: 2377 var z big.Int 2378 return z.Or(x, y), nil 2379 default: 2380 return invOp2(x, y, op) 2381 } 2382 case time.Duration: 2383 switch y := b.(type) { 2384 case time.Duration: 2385 return x | y, nil 2386 default: 2387 return invOp2(x, y, op) 2388 } 2389 default: 2390 return invOp2(a, b, op) 2391 } 2392 case andnot: 2393 a, b := o.get2(execCtx, ctx) 2394 if a == nil || b == nil { 2395 return 2396 } 2397 switch x := a.(type) { 2398 //case nil: 2399 case idealComplex: 2400 return undOp2(a, b, op) 2401 case idealFloat: 2402 return undOp2(a, b, op) 2403 case idealInt: 2404 switch y := b.(type) { 2405 case idealInt: 2406 return idealInt(int64(x) &^ int64(y)), nil 2407 default: 2408 return invOp2(x, y, op) 2409 } 2410 case idealRune: 2411 switch y := b.(type) { 2412 case idealRune: 2413 return idealRune(int64(x) &^ int64(y)), nil 2414 default: 2415 return invOp2(x, y, op) 2416 } 2417 case idealUint: 2418 switch y := b.(type) { 2419 case idealUint: 2420 return idealUint(uint64(x) &^ uint64(y)), nil 2421 default: 2422 return invOp2(x, y, op) 2423 } 2424 case bool: 2425 return undOp2(a, b, op) 2426 case complex64: 2427 return undOp2(a, b, op) 2428 case complex128: 2429 return undOp2(a, b, op) 2430 case float32: 2431 return undOp2(a, b, op) 2432 case float64: 2433 return undOp2(a, b, op) 2434 case int8: 2435 switch y := b.(type) { 2436 case int8: 2437 return x &^ y, nil 2438 default: 2439 return invOp2(x, y, op) 2440 } 2441 case int16: 2442 switch y := b.(type) { 2443 case int16: 2444 return x &^ y, nil 2445 default: 2446 return invOp2(x, y, op) 2447 } 2448 case int32: 2449 switch y := b.(type) { 2450 case int32: 2451 return x &^ y, nil 2452 default: 2453 return invOp2(x, y, op) 2454 } 2455 case int64: 2456 switch y := b.(type) { 2457 case int64: 2458 return x &^ y, nil 2459 default: 2460 return invOp2(x, y, op) 2461 } 2462 case string: 2463 return undOp2(a, b, op) 2464 case uint8: 2465 switch y := b.(type) { 2466 case uint8: 2467 return x &^ y, nil 2468 default: 2469 return invOp2(x, y, op) 2470 } 2471 case uint16: 2472 switch y := b.(type) { 2473 case uint16: 2474 return x &^ y, nil 2475 default: 2476 return invOp2(x, y, op) 2477 } 2478 case uint32: 2479 switch y := b.(type) { 2480 case uint32: 2481 return x &^ y, nil 2482 default: 2483 return invOp2(x, y, op) 2484 } 2485 case uint64: 2486 switch y := b.(type) { 2487 case uint64: 2488 return x &^ y, nil 2489 default: 2490 return invOp2(x, y, op) 2491 } 2492 case *big.Int: 2493 switch y := b.(type) { 2494 case *big.Int: 2495 var z big.Int 2496 return z.AndNot(x, y), nil 2497 default: 2498 return invOp2(x, y, op) 2499 } 2500 case time.Duration: 2501 switch y := b.(type) { 2502 case time.Duration: 2503 return x &^ y, nil 2504 default: 2505 return invOp2(x, y, op) 2506 } 2507 default: 2508 return invOp2(a, b, op) 2509 } 2510 case '^': 2511 a, b := o.get2(execCtx, ctx) 2512 if a == nil || b == nil { 2513 return 2514 } 2515 switch x := a.(type) { 2516 //case nil: 2517 case idealComplex: 2518 return undOp2(a, b, op) 2519 case idealFloat: 2520 return undOp2(a, b, op) 2521 case idealInt: 2522 switch y := b.(type) { 2523 case idealInt: 2524 return idealInt(int64(x) ^ int64(y)), nil 2525 default: 2526 return invOp2(x, y, op) 2527 } 2528 case idealRune: 2529 switch y := b.(type) { 2530 case idealRune: 2531 return idealRune(int64(x) ^ int64(y)), nil 2532 default: 2533 return invOp2(x, y, op) 2534 } 2535 case idealUint: 2536 switch y := b.(type) { 2537 case idealUint: 2538 return idealUint(uint64(x) ^ uint64(y)), nil 2539 default: 2540 return invOp2(x, y, op) 2541 } 2542 case bool: 2543 return undOp2(a, b, op) 2544 case complex64: 2545 return undOp2(a, b, op) 2546 case complex128: 2547 return undOp2(a, b, op) 2548 case float32: 2549 return undOp2(a, b, op) 2550 case float64: 2551 return undOp2(a, b, op) 2552 case int8: 2553 switch y := b.(type) { 2554 case int8: 2555 return x ^ y, nil 2556 default: 2557 return invOp2(x, y, op) 2558 } 2559 case int16: 2560 switch y := b.(type) { 2561 case int16: 2562 return x ^ y, nil 2563 default: 2564 return invOp2(x, y, op) 2565 } 2566 case int32: 2567 switch y := b.(type) { 2568 case int32: 2569 return x ^ y, nil 2570 default: 2571 return invOp2(x, y, op) 2572 } 2573 case int64: 2574 switch y := b.(type) { 2575 case int64: 2576 return x ^ y, nil 2577 default: 2578 return invOp2(x, y, op) 2579 } 2580 case string: 2581 return undOp2(a, b, op) 2582 case uint8: 2583 switch y := b.(type) { 2584 case uint8: 2585 return x ^ y, nil 2586 default: 2587 return invOp2(x, y, op) 2588 } 2589 case uint16: 2590 switch y := b.(type) { 2591 case uint16: 2592 return x ^ y, nil 2593 default: 2594 return invOp2(x, y, op) 2595 } 2596 case uint32: 2597 switch y := b.(type) { 2598 case uint32: 2599 return x ^ y, nil 2600 default: 2601 return invOp2(x, y, op) 2602 } 2603 case uint64: 2604 switch y := b.(type) { 2605 case uint64: 2606 return x ^ y, nil 2607 default: 2608 return invOp2(x, y, op) 2609 } 2610 case *big.Int: 2611 switch y := b.(type) { 2612 case *big.Int: 2613 var z big.Int 2614 return z.Xor(x, y), nil 2615 default: 2616 return invOp2(x, y, op) 2617 } 2618 case time.Duration: 2619 switch y := b.(type) { 2620 case time.Duration: 2621 return x ^ y, nil 2622 default: 2623 return invOp2(x, y, op) 2624 } 2625 default: 2626 return invOp2(a, b, op) 2627 } 2628 case '%': 2629 a, b := o.get2(execCtx, ctx) 2630 if a == nil || b == nil { 2631 return 2632 } 2633 switch x := a.(type) { 2634 //case nil: 2635 case idealComplex: 2636 return undOp2(a, b, op) 2637 case idealFloat: 2638 return undOp2(a, b, op) 2639 case idealInt: 2640 switch y := b.(type) { 2641 case idealInt: 2642 return idealInt(int64(x) % int64(y)), nil 2643 default: 2644 return invOp2(x, y, op) 2645 } 2646 case idealRune: 2647 switch y := b.(type) { 2648 case idealRune: 2649 return idealRune(int64(x) % int64(y)), nil 2650 default: 2651 return invOp2(x, y, op) 2652 } 2653 case idealUint: 2654 switch y := b.(type) { 2655 case idealUint: 2656 return idealUint(uint64(x) % uint64(y)), nil 2657 default: 2658 return invOp2(x, y, op) 2659 } 2660 case bool: 2661 return undOp2(a, b, op) 2662 case complex64: 2663 return undOp2(a, b, op) 2664 case complex128: 2665 return undOp2(a, b, op) 2666 case float32: 2667 return undOp2(a, b, op) 2668 case float64: 2669 return undOp2(a, b, op) 2670 case int8: 2671 switch y := b.(type) { 2672 case int8: 2673 return x % y, nil 2674 default: 2675 return invOp2(x, y, op) 2676 } 2677 case int16: 2678 switch y := b.(type) { 2679 case int16: 2680 return x % y, nil 2681 default: 2682 return invOp2(x, y, op) 2683 } 2684 case int32: 2685 switch y := b.(type) { 2686 case int32: 2687 return x % y, nil 2688 default: 2689 return invOp2(x, y, op) 2690 } 2691 case int64: 2692 switch y := b.(type) { 2693 case int64: 2694 return x % y, nil 2695 default: 2696 return invOp2(x, y, op) 2697 } 2698 case string: 2699 return undOp2(a, b, op) 2700 case uint8: 2701 switch y := b.(type) { 2702 case uint8: 2703 return x % y, nil 2704 default: 2705 return invOp2(x, y, op) 2706 } 2707 case uint16: 2708 switch y := b.(type) { 2709 case uint16: 2710 return x % y, nil 2711 default: 2712 return invOp2(x, y, op) 2713 } 2714 case uint32: 2715 switch y := b.(type) { 2716 case uint32: 2717 return x % y, nil 2718 default: 2719 return invOp2(x, y, op) 2720 } 2721 case uint64: 2722 switch y := b.(type) { 2723 case uint64: 2724 return x % y, nil 2725 default: 2726 return invOp2(x, y, op) 2727 } 2728 case *big.Int: 2729 switch y := b.(type) { 2730 case *big.Int: 2731 if y.Sign() == 0 { 2732 return nil, errDivByZero 2733 } 2734 2735 var z big.Int 2736 return z.Mod(x, y), nil 2737 default: 2738 return invOp2(x, y, op) 2739 } 2740 case time.Duration: 2741 switch y := b.(type) { 2742 case time.Duration: 2743 return x % y, nil 2744 default: 2745 return invOp2(x, y, op) 2746 } 2747 default: 2748 return invOp2(a, b, op) 2749 } 2750 case '/': 2751 a, b := o.get2(execCtx, ctx) 2752 if a == nil || b == nil { 2753 return 2754 } 2755 switch x := a.(type) { 2756 //case nil: 2757 case idealComplex: 2758 switch y := b.(type) { 2759 case idealComplex: 2760 return idealComplex(complex64(x) / complex64(y)), nil 2761 default: 2762 return invOp2(x, y, op) 2763 } 2764 case idealFloat: 2765 switch y := b.(type) { 2766 case idealFloat: 2767 return idealFloat(float64(x) / float64(y)), nil 2768 default: 2769 return invOp2(x, y, op) 2770 } 2771 case idealInt: 2772 switch y := b.(type) { 2773 case idealInt: 2774 return idealInt(int64(x) / int64(y)), nil 2775 default: 2776 return invOp2(x, y, op) 2777 } 2778 case idealRune: 2779 switch y := b.(type) { 2780 case idealRune: 2781 return idealRune(int64(x) / int64(y)), nil 2782 default: 2783 return invOp2(x, y, op) 2784 } 2785 case idealUint: 2786 switch y := b.(type) { 2787 case idealUint: 2788 return idealUint(uint64(x) / uint64(y)), nil 2789 default: 2790 return invOp2(x, y, op) 2791 } 2792 case bool: 2793 return undOp2(a, b, op) 2794 case complex64: 2795 switch y := b.(type) { 2796 case complex64: 2797 return x / y, nil 2798 default: 2799 return invOp2(x, y, op) 2800 } 2801 case complex128: 2802 switch y := b.(type) { 2803 case complex128: 2804 return x / y, nil 2805 default: 2806 return invOp2(x, y, op) 2807 } 2808 case float32: 2809 switch y := b.(type) { 2810 case float32: 2811 return x / y, nil 2812 default: 2813 return invOp2(x, y, op) 2814 } 2815 case float64: 2816 switch y := b.(type) { 2817 case float64: 2818 return x / y, nil 2819 default: 2820 return invOp2(x, y, op) 2821 } 2822 case int8: 2823 switch y := b.(type) { 2824 case int8: 2825 return x / y, nil 2826 default: 2827 return invOp2(x, y, op) 2828 } 2829 case int16: 2830 switch y := b.(type) { 2831 case int16: 2832 return x / y, nil 2833 default: 2834 return invOp2(x, y, op) 2835 } 2836 case int32: 2837 switch y := b.(type) { 2838 case int32: 2839 return x / y, nil 2840 default: 2841 return invOp2(x, y, op) 2842 } 2843 case int64: 2844 switch y := b.(type) { 2845 case int64: 2846 return x / y, nil 2847 default: 2848 return invOp2(x, y, op) 2849 } 2850 case string: 2851 return undOp2(a, b, op) 2852 case uint8: 2853 switch y := b.(type) { 2854 case uint8: 2855 return x / y, nil 2856 default: 2857 return invOp2(x, y, op) 2858 } 2859 case uint16: 2860 switch y := b.(type) { 2861 case uint16: 2862 return x / y, nil 2863 default: 2864 return invOp2(x, y, op) 2865 } 2866 case uint32: 2867 switch y := b.(type) { 2868 case uint32: 2869 return x / y, nil 2870 default: 2871 return invOp2(x, y, op) 2872 } 2873 case uint64: 2874 switch y := b.(type) { 2875 case uint64: 2876 return x / y, nil 2877 default: 2878 return invOp2(x, y, op) 2879 } 2880 case *big.Int: 2881 switch y := b.(type) { 2882 case *big.Int: 2883 if y.Sign() == 0 { 2884 return nil, errDivByZero 2885 } 2886 2887 var z big.Int 2888 return z.Quo(x, y), nil 2889 default: 2890 return invOp2(x, y, op) 2891 } 2892 case *big.Rat: 2893 switch y := b.(type) { 2894 case *big.Rat: 2895 if y.Sign() == 0 { 2896 return nil, errDivByZero 2897 } 2898 2899 var z big.Rat 2900 return z.Quo(x, y), nil 2901 default: 2902 return invOp2(x, y, op) 2903 } 2904 case time.Duration: 2905 switch y := b.(type) { 2906 case time.Duration: 2907 return x / y, nil 2908 default: 2909 return invOp2(x, y, op) 2910 } 2911 default: 2912 return invOp2(a, b, op) 2913 } 2914 case '*': 2915 a, b := o.get2(execCtx, ctx) 2916 if a == nil || b == nil { 2917 return 2918 } 2919 switch x := a.(type) { 2920 //case nil: 2921 case idealComplex: 2922 switch y := b.(type) { 2923 case idealComplex: 2924 return idealComplex(complex64(x) * complex64(y)), nil 2925 default: 2926 return invOp2(x, y, op) 2927 } 2928 case idealFloat: 2929 switch y := b.(type) { 2930 case idealFloat: 2931 return idealFloat(float64(x) * float64(y)), nil 2932 default: 2933 return invOp2(x, y, op) 2934 } 2935 case idealInt: 2936 switch y := b.(type) { 2937 case idealInt: 2938 return idealInt(int64(x) * int64(y)), nil 2939 default: 2940 return invOp2(x, y, op) 2941 } 2942 case idealRune: 2943 switch y := b.(type) { 2944 case idealRune: 2945 return idealRune(int64(x) * int64(y)), nil 2946 default: 2947 return invOp2(x, y, op) 2948 } 2949 case idealUint: 2950 switch y := b.(type) { 2951 case idealUint: 2952 return idealUint(uint64(x) * uint64(y)), nil 2953 default: 2954 return invOp2(x, y, op) 2955 } 2956 case bool: 2957 return undOp2(a, b, op) 2958 case complex64: 2959 switch y := b.(type) { 2960 case complex64: 2961 return x * y, nil 2962 default: 2963 return invOp2(x, y, op) 2964 } 2965 case complex128: 2966 switch y := b.(type) { 2967 case complex128: 2968 return x * y, nil 2969 default: 2970 return invOp2(x, y, op) 2971 } 2972 case float32: 2973 switch y := b.(type) { 2974 case float32: 2975 return x * y, nil 2976 default: 2977 return invOp2(x, y, op) 2978 } 2979 case float64: 2980 switch y := b.(type) { 2981 case float64: 2982 return x * y, nil 2983 default: 2984 return invOp2(x, y, op) 2985 } 2986 case int8: 2987 switch y := b.(type) { 2988 case int8: 2989 return x * y, nil 2990 default: 2991 return invOp2(x, y, op) 2992 } 2993 case int16: 2994 switch y := b.(type) { 2995 case int16: 2996 return x * y, nil 2997 default: 2998 return invOp2(x, y, op) 2999 } 3000 case int32: 3001 switch y := b.(type) { 3002 case int32: 3003 return x * y, nil 3004 default: 3005 return invOp2(x, y, op) 3006 } 3007 case int64: 3008 switch y := b.(type) { 3009 case int64: 3010 return x * y, nil 3011 default: 3012 return invOp2(x, y, op) 3013 } 3014 case string: 3015 return undOp2(a, b, op) 3016 case uint8: 3017 switch y := b.(type) { 3018 case uint8: 3019 return x * y, nil 3020 default: 3021 return invOp2(x, y, op) 3022 } 3023 case uint16: 3024 switch y := b.(type) { 3025 case uint16: 3026 return x * y, nil 3027 default: 3028 return invOp2(x, y, op) 3029 } 3030 case uint32: 3031 switch y := b.(type) { 3032 case uint32: 3033 return x * y, nil 3034 default: 3035 return invOp2(x, y, op) 3036 } 3037 case uint64: 3038 switch y := b.(type) { 3039 case uint64: 3040 return x * y, nil 3041 default: 3042 return invOp2(x, y, op) 3043 } 3044 case *big.Int: 3045 switch y := b.(type) { 3046 case *big.Int: 3047 var z big.Int 3048 return z.Mul(x, y), nil 3049 default: 3050 return invOp2(x, y, op) 3051 } 3052 case *big.Rat: 3053 switch y := b.(type) { 3054 case *big.Rat: 3055 var z big.Rat 3056 return z.Mul(x, y), nil 3057 default: 3058 return invOp2(x, y, op) 3059 } 3060 case time.Duration: 3061 switch y := b.(type) { 3062 case time.Duration: 3063 return x * y, nil 3064 default: 3065 return invOp2(x, y, op) 3066 } 3067 default: 3068 return invOp2(a, b, op) 3069 } 3070 default: 3071 panic("internal error 037") 3072 } 3073 } 3074 3075 func (o *binaryOperation) get2(execCtx *execCtx, ctx map[interface{}]interface{}) (x, y interface{}) { 3076 x, y = eval2(o.l, o.r, execCtx, ctx) 3077 //dbg("get2 pIn - ", x, y) 3078 //defer func() {dbg("get2 coerced ", x, y)}() 3079 return coerce(x, y) 3080 } 3081 3082 type ident struct { 3083 s string 3084 } 3085 3086 func (i *ident) clone(arg []interface{}, unqualify ...string) (expression, error) { 3087 x := strings.IndexByte(i.s, '.') 3088 if x < 0 { 3089 return &ident{s: i.s}, nil 3090 } 3091 3092 q := i.s[:x] 3093 for _, v := range unqualify { 3094 if q == v { 3095 return &ident{i.s[x+1:]}, nil 3096 } 3097 } 3098 return &ident{s: i.s}, nil 3099 } 3100 3101 func (i *ident) isQualified() bool { return strings.Contains(i.s, ".") } 3102 3103 func (i *ident) isStatic() bool { return false } 3104 3105 func (i *ident) String() string { return i.s } 3106 3107 func (i *ident) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) { 3108 if _, ok := ctx["$agg0"]; ok { 3109 return int64(0), nil 3110 } 3111 3112 //defer func() { dbg("ident %q -> %v %v", i.s, v, err) }() 3113 v, ok := ctx[i.s] 3114 if !ok { 3115 err = fmt.Errorf("unknown field %s", i.s) 3116 } 3117 return 3118 } 3119 3120 type pInEval struct { 3121 m map[interface{}]struct{} // IN (SELECT...) results 3122 sample interface{} 3123 } 3124 3125 type pIn struct { 3126 expr expression 3127 list []expression 3128 not bool 3129 sel *selectStmt 3130 } 3131 3132 func (n *pIn) clone(arg []interface{}, unqualify ...string) (expression, error) { 3133 expr, err := n.expr.clone(arg, unqualify...) 3134 if err != nil { 3135 return nil, err 3136 } 3137 3138 list, err := cloneExpressionList(arg, n.list) 3139 if err != nil { 3140 return nil, err 3141 } 3142 3143 return &pIn{ 3144 expr: expr, 3145 list: list, 3146 not: n.not, 3147 sel: n.sel, 3148 }, nil 3149 } 3150 3151 func (n *pIn) isStatic() bool { 3152 if !n.expr.isStatic() || n.sel != nil { 3153 return false 3154 } 3155 3156 for _, v := range n.list { 3157 if !v.isStatic() { 3158 return false 3159 } 3160 } 3161 return true 3162 } 3163 3164 //LATER newIn 3165 3166 func (n *pIn) String() string { 3167 if n.sel == nil { 3168 a := []string{} 3169 for _, v := range n.list { 3170 a = append(a, v.String()) 3171 } 3172 if n.not { 3173 return fmt.Sprintf("%s NOT IN (%s)", n.expr, strings.Join(a, ",")) 3174 } 3175 3176 return fmt.Sprintf("%s IN (%s)", n.expr, strings.Join(a, ",")) 3177 } 3178 3179 if n.not { 3180 return fmt.Sprintf("%s NOT IN (%s)", n.expr, n.sel) 3181 } 3182 3183 return fmt.Sprintf("%s IN (%s)", n.expr, n.sel) 3184 } 3185 3186 func (n *pIn) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) { 3187 lhs, err := expand1(n.expr.eval(execCtx, ctx)) 3188 if err != nil { 3189 return nil, err 3190 } 3191 3192 if lhs == nil { 3193 return nil, nil //TODO Add test for NULL LHS. 3194 } 3195 3196 if n.sel == nil { 3197 for _, v := range n.list { 3198 b, err := newBinaryOperation(eq, value{lhs}, v) 3199 if err != nil { 3200 return nil, err 3201 } 3202 3203 eval, err := b.eval(execCtx, ctx) 3204 if err != nil { 3205 return nil, err 3206 } 3207 3208 if x, ok := eval.(bool); ok && x { 3209 return !n.not, nil 3210 } 3211 } 3212 return n.not, nil 3213 } 3214 3215 var ev *pInEval 3216 ev0 := ctx[n] 3217 if ev0 == nil { // SELECT not yet evaluated. 3218 r, err := n.sel.plan(execCtx) 3219 if err != nil { 3220 return nil, err 3221 } 3222 3223 if g, e := len(r.fieldNames()), 1; g != e { 3224 return false, fmt.Errorf("IN (%s): mismatched field count, have %d, need %d", n.sel, g, e) 3225 } 3226 3227 ev = &pInEval{m: map[interface{}]struct{}{}} 3228 ctx[n] = ev 3229 m := ev.m 3230 typechecked := false 3231 if err := r.do(execCtx, func(id interface{}, data []interface{}) (more bool, err error) { 3232 if typechecked { 3233 if data[0] == nil { 3234 return true, nil 3235 } 3236 3237 m[data[0]] = struct{}{} 3238 } 3239 3240 if data[0] == nil { 3241 return true, nil 3242 } 3243 3244 ev.sample = data[0] 3245 switch ev.sample.(type) { 3246 case bool, byte, complex128, complex64, float32, 3247 float64, int16, int32, int64, int8, 3248 string, uint16, uint32, uint64: 3249 typechecked = true 3250 m[ev.sample] = struct{}{} 3251 return true, nil 3252 default: 3253 return false, fmt.Errorf("IN (%s): invalid field type: %T", n.sel, data[0]) 3254 } 3255 3256 }); err != nil { 3257 return nil, err 3258 } 3259 } else { 3260 ev = ev0.(*pInEval) 3261 } 3262 3263 if len(ev.m) == 0 { 3264 return n.not, nil 3265 } 3266 3267 if ev.sample == nil { 3268 return nil, nil 3269 } 3270 3271 _, ok := ev.m[coerce1(lhs, ev.sample)] 3272 return ok != n.not, nil 3273 } 3274 3275 type value struct { 3276 val interface{} 3277 } 3278 3279 func (l value) clone(arg []interface{}, unqualify ...string) (expression, error) { 3280 return value{val: l.val}, nil 3281 } 3282 3283 func (l value) isStatic() bool { return true } 3284 3285 func (l value) String() string { 3286 switch x := l.val.(type) { 3287 case nil: 3288 return "NULL" 3289 case idealComplex: 3290 s := fmt.Sprint(x) 3291 return s[1 : len(s)-1] 3292 case complex64: 3293 s := fmt.Sprint(x) 3294 return s[1 : len(s)-1] 3295 case complex128: 3296 s := fmt.Sprint(x) 3297 return s[1 : len(s)-1] 3298 case string: 3299 return fmt.Sprintf("%q", x) 3300 case time.Duration: 3301 return fmt.Sprintf("duration(%q)", l.val) 3302 case time.Time: 3303 y, m, d := x.Date() 3304 zone, _ := x.Zone() 3305 return fmt.Sprintf("date(%v, %v, %v, %v, %v, %v, %v, %v)", y, m, d, x.Hour(), x.Minute(), x.Second(), x.Nanosecond(), zone) 3306 case *big.Rat: 3307 return fmt.Sprintf("bigrat(%q)", l.val) 3308 case *big.Int: 3309 return fmt.Sprintf(`bigint("%v")`, l.val) 3310 default: 3311 return fmt.Sprintf("%v", l.val) 3312 } 3313 } 3314 3315 func (l value) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (interface{}, error) { 3316 return l.val, nil 3317 } 3318 3319 type conversion struct { 3320 typ int 3321 val expression 3322 } 3323 3324 func (c *conversion) clone(arg []interface{}, unqualify ...string) (expression, error) { 3325 val, err := c.val.clone(arg, unqualify...) 3326 if err != nil { 3327 return nil, err 3328 } 3329 3330 return &conversion{typ: c.typ, val: val}, nil 3331 } 3332 3333 func (c *conversion) isStatic() bool { 3334 return c.val.isStatic() 3335 } 3336 3337 //LATER newConversion or fake unary op 3338 3339 func (c *conversion) String() string { 3340 return fmt.Sprintf("%s(%s)", typeStr(c.typ), c.val) 3341 } 3342 3343 func (c *conversion) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) { 3344 val, err := expand1(c.val.eval(execCtx, ctx)) 3345 if err != nil { 3346 return 3347 } 3348 3349 return convert(val, c.typ) 3350 } 3351 3352 type unaryOperation struct { 3353 op int 3354 v expression 3355 } 3356 3357 func newUnaryOperation(op int, x interface{}) (v expression, err error) { 3358 l, ok := x.(expression) 3359 if !ok { 3360 panic("internal error 038") 3361 } 3362 3363 for { 3364 pe, ok := l.(*pexpr) 3365 if ok { 3366 l = pe.expr 3367 continue 3368 } 3369 3370 break 3371 } 3372 3373 if l.isStatic() { 3374 val, err := l.eval(nil, nil) 3375 if err != nil { 3376 return nil, err 3377 } 3378 3379 l = value{val} 3380 } 3381 3382 if op == '!' { 3383 b, ok := l.(*binaryOperation) 3384 if ok { 3385 switch b.op { 3386 case eq: 3387 b.op = neq 3388 return b, nil 3389 case neq: 3390 b.op = eq 3391 return b, nil 3392 case '>': 3393 b.op = le 3394 return b, nil 3395 case ge: 3396 b.op = '<' 3397 return b, nil 3398 case '<': 3399 b.op = ge 3400 return b, nil 3401 case le: 3402 b.op = '>' 3403 return b, nil 3404 } 3405 } 3406 3407 u, ok := l.(*unaryOperation) 3408 if ok && u.op == '!' { // !!x: x 3409 return u.v, nil 3410 } 3411 } 3412 3413 return &unaryOperation{op, l}, nil 3414 } 3415 3416 func (u *unaryOperation) clone(arg []interface{}, unqualify ...string) (expression, error) { 3417 v, err := u.v.clone(arg, unqualify...) 3418 if err != nil { 3419 return nil, err 3420 } 3421 3422 return &unaryOperation{op: u.op, v: v}, nil 3423 } 3424 3425 func (u *unaryOperation) isStatic() bool { return u.v.isStatic() } 3426 3427 func (u *unaryOperation) String() string { 3428 switch u.v.(type) { 3429 case *binaryOperation: 3430 return fmt.Sprintf("%s(%s)", iop(u.op), u.v) 3431 default: 3432 return fmt.Sprintf("%s%s", iop(u.op), u.v) 3433 } 3434 } 3435 3436 func (u *unaryOperation) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (r interface{}, err error) { 3437 defer func() { 3438 if e := recover(); e != nil { 3439 switch x := e.(type) { 3440 case error: 3441 r, err = nil, x 3442 default: 3443 r, err = nil, fmt.Errorf("%v", x) 3444 } 3445 } 3446 }() 3447 3448 switch op := u.op; op { 3449 case '!': 3450 a := eval(u.v, execCtx, ctx) 3451 if a == nil { 3452 return 3453 } 3454 3455 switch x := a.(type) { 3456 case bool: 3457 return !x, nil 3458 default: 3459 return undOp(a, op) 3460 } 3461 case '^': 3462 a := eval(u.v, execCtx, ctx) 3463 if a == nil { 3464 return 3465 } 3466 3467 switch x := a.(type) { 3468 //case nil: 3469 case idealComplex: 3470 return undOp(a, op) 3471 case idealFloat: 3472 return undOp(a, op) 3473 case idealInt: 3474 return ^x, nil 3475 case idealRune: 3476 return ^x, nil 3477 case idealUint: 3478 return ^x, nil 3479 case bool: 3480 return undOp(a, op) 3481 case complex64: 3482 return undOp(a, op) 3483 case complex128: 3484 return undOp(a, op) 3485 case float32: 3486 return undOp(a, op) 3487 case float64: 3488 return undOp(a, op) 3489 case int8: 3490 return ^x, nil 3491 case int16: 3492 return ^x, nil 3493 case int32: 3494 return ^x, nil 3495 case int64: 3496 return ^x, nil 3497 case string: 3498 return undOp(a, op) 3499 case uint8: 3500 return ^x, nil 3501 case uint16: 3502 return ^x, nil 3503 case uint32: 3504 return ^x, nil 3505 case uint64: 3506 return ^x, nil 3507 case *big.Int: 3508 var z big.Int 3509 return z.Not(x), nil 3510 case time.Duration: 3511 return ^x, nil 3512 default: 3513 return undOp(a, op) 3514 } 3515 case '+': 3516 a := eval(u.v, execCtx, ctx) 3517 if a == nil { 3518 return 3519 } 3520 3521 switch x := a.(type) { 3522 //case nil: 3523 case idealComplex: 3524 return +x, nil 3525 case idealFloat: 3526 return +x, nil 3527 case idealInt: 3528 return +x, nil 3529 case idealRune: 3530 return +x, nil 3531 case idealUint: 3532 return +x, nil 3533 case bool: 3534 return undOp(a, op) 3535 case complex64: 3536 return +x, nil 3537 case complex128: 3538 return +x, nil 3539 case float32: 3540 return +x, nil 3541 case float64: 3542 return +x, nil 3543 case int8: 3544 return +x, nil 3545 case int16: 3546 return +x, nil 3547 case int32: 3548 return +x, nil 3549 case int64: 3550 return +x, nil 3551 case string: 3552 return undOp(a, op) 3553 case uint8: 3554 return +x, nil 3555 case uint16: 3556 return +x, nil 3557 case uint32: 3558 return +x, nil 3559 case uint64: 3560 return +x, nil 3561 case *big.Int: 3562 var z big.Int 3563 return z.Set(x), nil 3564 case *big.Rat: 3565 var z big.Rat 3566 return z.Set(x), nil 3567 case time.Duration: 3568 return x, nil 3569 default: 3570 return undOp(a, op) 3571 } 3572 case '-': 3573 a := eval(u.v, execCtx, ctx) 3574 if a == nil { 3575 return 3576 } 3577 3578 switch x := a.(type) { 3579 //case nil: 3580 case idealComplex: 3581 return -x, nil 3582 case idealFloat: 3583 return -x, nil 3584 case idealInt: 3585 return -x, nil 3586 case idealRune: 3587 return -x, nil 3588 case idealUint: 3589 return -x, nil 3590 case bool: 3591 return undOp(a, op) 3592 case complex64: 3593 return -x, nil 3594 case complex128: 3595 return -x, nil 3596 case float32: 3597 return -x, nil 3598 case float64: 3599 return -x, nil 3600 case int8: 3601 return -x, nil 3602 case int16: 3603 return -x, nil 3604 case int32: 3605 return -x, nil 3606 case int64: 3607 return -x, nil 3608 case string: 3609 return undOp(a, op) 3610 case uint8: 3611 return -x, nil 3612 case uint16: 3613 return -x, nil 3614 case uint32: 3615 return -x, nil 3616 case uint64: 3617 return -x, nil 3618 case *big.Int: 3619 var z big.Int 3620 return z.Neg(x), nil 3621 case *big.Rat: 3622 var z big.Rat 3623 return z.Neg(x), nil 3624 case time.Duration: 3625 return -x, nil 3626 default: 3627 return undOp(a, op) 3628 } 3629 default: 3630 panic("internal error 039") 3631 } 3632 } 3633 3634 type call struct { 3635 f string 3636 arg []expression 3637 } 3638 3639 func newCall(f string, arg []expression) (v expression, isAgg bool, err error) { 3640 x := builtin[f] 3641 if x.f == nil { 3642 return nil, false, fmt.Errorf("undefined: %s", f) 3643 } 3644 3645 isAgg = x.isAggregate 3646 if g, min, max := len(arg), x.minArgs, x.maxArgs; (g < min) || ((max != builtinNoMaximum) && (g > max)) { 3647 a := []interface{}{} 3648 for _, v := range arg { 3649 a = append(a, v) 3650 } 3651 return nil, false, badNArgs(min, f, a) 3652 } 3653 3654 c := call{f: f} 3655 for _, val := range arg { 3656 if !val.isStatic() { 3657 c.arg = append(c.arg, val) 3658 continue 3659 } 3660 3661 eval, err := val.eval(nil, nil) 3662 if err != nil { 3663 return nil, isAgg, err 3664 } 3665 3666 c.arg = append(c.arg, value{eval}) 3667 } 3668 3669 return &c, isAgg, nil 3670 } 3671 3672 func (c *call) clone(arg []interface{}, unqualify ...string) (expression, error) { 3673 list, err := cloneExpressionList(arg, c.arg) 3674 if err != nil { 3675 return nil, err 3676 } 3677 3678 return &call{f: c.f, arg: list}, nil 3679 } 3680 3681 func (c *call) isStatic() bool { 3682 v := builtin[c.f] 3683 if v.f == nil || !v.isStatic { 3684 return false 3685 } 3686 3687 for _, v := range c.arg { 3688 if !v.isStatic() { 3689 return false 3690 } 3691 } 3692 return true 3693 } 3694 3695 func (c *call) String() string { 3696 a := []string{} 3697 for _, v := range c.arg { 3698 a = append(a, v.String()) 3699 } 3700 return fmt.Sprintf("%s(%s)", c.f, strings.Join(a, ", ")) 3701 } 3702 3703 func (c *call) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) { 3704 f, ok := builtin[c.f] 3705 if !ok { 3706 return nil, fmt.Errorf("unknown function %s", c.f) 3707 } 3708 3709 isID := c.f == "id" 3710 a := make([]interface{}, len(c.arg)) 3711 for i, arg := range c.arg { 3712 if v, err = expand1(arg.eval(execCtx, ctx)); err != nil { 3713 if !isID { 3714 return nil, err 3715 } 3716 3717 if _, ok := arg.(*ident); !ok { 3718 return nil, err 3719 } 3720 3721 a[i] = arg 3722 continue 3723 } 3724 3725 a[i] = v 3726 } 3727 3728 if ctx != nil { 3729 ctx["$fn"] = c 3730 } 3731 return f.f(a, ctx) 3732 } 3733 3734 type parameter struct { 3735 n int 3736 } 3737 3738 func (p parameter) clone(arg []interface{}, unqualify ...string) (expression, error) { 3739 i := p.n - 1 3740 if i < len(arg) { 3741 return value{val: arg[i]}, nil 3742 } 3743 3744 return nil, fmt.Errorf("missing %s", p) 3745 } 3746 3747 func (parameter) isStatic() bool { return false } 3748 3749 func (p parameter) String() string { return fmt.Sprintf("$%d", p.n) } 3750 3751 func (p parameter) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) { 3752 i := p.n - 1 3753 if i < len(execCtx.arg) { 3754 return execCtx.arg[i], nil 3755 } 3756 3757 return nil, fmt.Errorf("missing %s", p) 3758 } 3759 3760 //MAYBE make it an unary operation 3761 type isNull struct { 3762 expr expression 3763 not bool 3764 } 3765 3766 //LATER newIsNull 3767 3768 func (is *isNull) clone(arg []interface{}, unqualify ...string) (expression, error) { 3769 expr, err := is.expr.clone(arg, unqualify...) 3770 if err != nil { 3771 return nil, err 3772 } 3773 3774 return &isNull{expr: expr, not: is.not}, nil 3775 } 3776 3777 func (is *isNull) isStatic() bool { return is.expr.isStatic() } 3778 3779 func (is *isNull) String() string { 3780 if is.not { 3781 return fmt.Sprintf("%s IS NOT NULL", is.expr) 3782 } 3783 3784 return fmt.Sprintf("%s IS NULL", is.expr) 3785 } 3786 3787 func (is *isNull) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) { 3788 val, err := is.expr.eval(execCtx, ctx) 3789 if err != nil { 3790 return 3791 } 3792 3793 return val == nil != is.not, nil 3794 } 3795 3796 type indexOp struct { 3797 expr, x expression 3798 } 3799 3800 func newIndex(sv, xv expression) (v expression, err error) { 3801 s, fs, i := "", false, uint64(0) 3802 x := indexOp{sv, xv} 3803 if x.expr.isStatic() { 3804 v, err := x.expr.eval(nil, nil) 3805 if err != nil { 3806 return nil, err 3807 } 3808 3809 if v == nil { 3810 return value{nil}, nil 3811 } 3812 3813 if s, fs = v.(string); !fs { 3814 return nil, invXOp(sv, xv) 3815 } 3816 3817 x.expr = value{s} 3818 } 3819 3820 if x.x.isStatic() { 3821 v, err := x.x.eval(nil, nil) 3822 if err != nil { 3823 return nil, err 3824 } 3825 3826 if v == nil { 3827 return value{nil}, nil 3828 } 3829 3830 var p *string 3831 if fs { 3832 p = &s 3833 } 3834 if i, err = indexExpr(p, v); err != nil { 3835 return nil, err 3836 } 3837 3838 x.x = value{i} 3839 } 3840 3841 return &x, nil 3842 } 3843 3844 func (x *indexOp) clone(arg []interface{}, unqualify ...string) (expression, error) { 3845 expr, err := x.expr.clone(arg, unqualify...) 3846 if err != nil { 3847 return nil, err 3848 } 3849 3850 x2, err := x.x.clone(arg, unqualify...) 3851 if err != nil { 3852 return nil, err 3853 } 3854 3855 return &indexOp{expr: expr, x: x2}, nil 3856 } 3857 3858 func (x *indexOp) isStatic() bool { 3859 return x.expr.isStatic() && x.x.isStatic() 3860 } 3861 3862 func (x *indexOp) String() string { return fmt.Sprintf("%s[%s]", x.expr, x.x) } 3863 3864 func (x *indexOp) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) { 3865 s0, err := x.expr.eval(execCtx, ctx) 3866 if err != nil { 3867 return nil, runErr(err) 3868 } 3869 3870 s, ok := s0.(string) 3871 if !ok { 3872 return nil, runErr(invXOp(s0, x.x)) 3873 } 3874 3875 i0, err := x.x.eval(execCtx, ctx) 3876 if err != nil { 3877 return nil, runErr(err) 3878 } 3879 3880 if i0 == nil { 3881 return nil, nil 3882 } 3883 3884 i, err := indexExpr(&s, i0) 3885 if err != nil { 3886 return nil, runErr(err) 3887 } 3888 3889 return s[i], nil 3890 } 3891 3892 type slice struct { 3893 expr expression 3894 lo, hi *expression 3895 } 3896 3897 func newSlice(e expression, lo, hi *expression) (v expression, err error) { 3898 y := slice{e, lo, hi} 3899 var val interface{} 3900 if e := y.expr; e.isStatic() { 3901 if val, err = e.eval(nil, nil); err != nil { 3902 return nil, err 3903 } 3904 3905 if val == nil { 3906 return value{nil}, nil 3907 } 3908 3909 y.expr = value{val} 3910 } 3911 3912 if p := y.lo; p != nil { 3913 if e := expr(*p); e.isStatic() { 3914 if val, err = e.eval(nil, nil); err != nil { 3915 return nil, err 3916 } 3917 3918 if val == nil { 3919 return value{nil}, nil 3920 } 3921 3922 v := expression(value{val}) 3923 y.lo = &v 3924 } 3925 } 3926 3927 if p := y.hi; p != nil { 3928 if e := expr(*p); e.isStatic() { 3929 if val, err = e.eval(nil, nil); err != nil { 3930 return nil, err 3931 } 3932 3933 if val == nil { 3934 return value{nil}, nil 3935 } 3936 3937 v := expression(value{val}) 3938 y.hi = &v 3939 } 3940 } 3941 return &y, nil 3942 } 3943 3944 func (s *slice) clone(arg []interface{}, unqualify ...string) (expression, error) { 3945 expr, err := s.expr.clone(arg, unqualify...) 3946 if err != nil { 3947 return nil, err 3948 } 3949 3950 r := &slice{expr: expr, lo: s.lo, hi: s.hi} 3951 if s.lo != nil { 3952 e, err := (*s.lo).clone(arg, unqualify...) 3953 if err != nil { 3954 return nil, err 3955 } 3956 3957 r.lo = &e 3958 } 3959 if s.hi != nil { 3960 e, err := (*s.hi).clone(arg, unqualify...) 3961 if err != nil { 3962 return nil, err 3963 } 3964 3965 r.hi = &e 3966 } 3967 return r, nil 3968 } 3969 3970 func (s *slice) eval(execCtx *execCtx, ctx map[interface{}]interface{}) (v interface{}, err error) { 3971 s0, err := s.expr.eval(execCtx, ctx) 3972 if err != nil { 3973 return 3974 } 3975 3976 if s0 == nil { 3977 return 3978 } 3979 3980 ss, ok := s0.(string) 3981 if !ok { 3982 return nil, runErr(invSOp(s0)) 3983 } 3984 3985 var iLo, iHi uint64 3986 if s.lo != nil { 3987 i, err := (*s.lo).eval(execCtx, ctx) 3988 if err != nil { 3989 return nil, err 3990 } 3991 3992 if i == nil { 3993 return nil, err 3994 } 3995 3996 if iLo, err = sliceExpr(&ss, i, 0); err != nil { 3997 return nil, err 3998 } 3999 } 4000 4001 iHi = uint64(len(ss)) 4002 if s.hi != nil { 4003 i, err := (*s.hi).eval(execCtx, ctx) 4004 if err != nil { 4005 return nil, err 4006 } 4007 4008 if i == nil { 4009 return nil, err 4010 } 4011 4012 if iHi, err = sliceExpr(&ss, i, 1); err != nil { 4013 return nil, err 4014 } 4015 } 4016 4017 return ss[iLo:iHi], nil 4018 } 4019 4020 func (s *slice) isStatic() bool { 4021 if !s.expr.isStatic() { 4022 return false 4023 } 4024 4025 if p := s.lo; p != nil && !(*p).isStatic() { 4026 return false 4027 } 4028 4029 if p := s.hi; p != nil && !(*p).isStatic() { 4030 return false 4031 } 4032 4033 return false 4034 } 4035 4036 func (s *slice) String() string { 4037 switch { 4038 case s.lo == nil && s.hi == nil: 4039 return fmt.Sprintf("%v[:]", s.expr) 4040 case s.lo == nil && s.hi != nil: 4041 return fmt.Sprintf("%v[:%v]", s.expr, *s.hi) 4042 case s.lo != nil && s.hi == nil: 4043 return fmt.Sprintf("%v[%v:]", s.expr, *s.lo) 4044 default: //case s.lo != nil && s.hi != nil: 4045 return fmt.Sprintf("%v[%v:%v]", s.expr, *s.lo, *s.hi) 4046 } 4047 }