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