github.com/neugram/ng@v0.0.0-20180309130942-d472ff93d872/eval/op.go (about) 1 // Copyright 2015 The Neugram Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package eval 6 7 import ( 8 "fmt" 9 "math/big" 10 "reflect" 11 12 "neugram.io/ng/syntax/token" 13 ) 14 15 // TODO redo 16 func valEq(x, y interface{}) bool { 17 if x == y { 18 return true 19 } 20 if x == nil || y == nil { 21 return false 22 } 23 switch x := x.(type) { 24 case UntypedInt: 25 switch y := y.(type) { 26 case UntypedInt: 27 return x.Int.Cmp(y.Int) == 0 28 } 29 case UntypedFloat: 30 switch y := y.(type) { 31 case UntypedFloat: 32 return x.Float.Cmp(y.Float) == 0 33 } 34 case *big.Int: 35 switch y := y.(type) { 36 case *big.Int: 37 return x.Cmp(y) == 0 38 } 39 case *big.Float: 40 switch y := y.(type) { 41 case *big.Float: 42 return x.Cmp(y) == 0 43 } 44 /*case *StructVal: 45 switch y := y.(type) { 46 case *StructVal: 47 if len(x.Fields) != len(y.Fields) { // TODO compare tipe.Type 48 return false 49 } 50 for i := range x.Fields { 51 if !valEq(x.Fields[i], y.Fields[i]) { 52 return false 53 } 54 } 55 return true 56 } 57 */ 58 } 59 return false 60 } 61 62 func binOp(op token.Token, x, y interface{}) (interface{}, error) { 63 switch op { 64 case token.Add: 65 switch x := x.(type) { 66 case int: 67 switch y := y.(type) { 68 case int: 69 return x + y, nil 70 } 71 case int8: 72 switch y := y.(type) { 73 case int8: 74 return x + y, nil 75 } 76 case int16: 77 switch y := y.(type) { 78 case int16: 79 return x + y, nil 80 } 81 case int32: 82 switch y := y.(type) { 83 case int32: 84 return x + y, nil 85 } 86 case int64: 87 switch y := y.(type) { 88 case int64: 89 return x + y, nil 90 } 91 case uint: 92 switch y := y.(type) { 93 case uint: 94 return x + y, nil 95 } 96 case uint8: 97 switch y := y.(type) { 98 case uint8: 99 return x + y, nil 100 } 101 case uint16: 102 switch y := y.(type) { 103 case uint16: 104 return x + y, nil 105 } 106 case uint32: 107 switch y := y.(type) { 108 case uint32: 109 return x + y, nil 110 } 111 case uint64: 112 switch y := y.(type) { 113 case uint64: 114 return x + y, nil 115 } 116 case float32: 117 switch y := y.(type) { 118 case float32: 119 return x + y, nil 120 } 121 case float64: 122 switch y := y.(type) { 123 case float64: 124 return x + y, nil 125 } 126 case complex64: 127 switch y := y.(type) { 128 case complex64: 129 return x + y, nil 130 } 131 case complex128: 132 switch y := y.(type) { 133 case complex128: 134 return x + y, nil 135 } 136 case UntypedInt: 137 switch y := y.(type) { 138 case UntypedFloat: 139 z := big.NewFloat(float64(x.Int.Int64())) 140 return UntypedFloat{z.Add(z, y.Float)}, nil 141 case UntypedInt: 142 z := big.NewInt(0) 143 return UntypedInt{z.Add(x.Int, y.Int)}, nil 144 } 145 case UntypedFloat: 146 z := big.NewFloat(0) 147 switch y := y.(type) { 148 case UntypedInt: 149 z.SetInt(y.Int) 150 return UntypedFloat{z.Add(z, x.Float)}, nil 151 case UntypedFloat: 152 return UntypedFloat{z.Add(x.Float, y.Float)}, nil 153 } 154 case UntypedComplex: 155 re := big.NewFloat(0) 156 im := big.NewFloat(0) 157 switch y := y.(type) { 158 case UntypedInt: 159 re.SetInt(y.Int) 160 return UntypedComplex{Real: re.Add(re, x.Real), Imag: im}, nil 161 case UntypedFloat: 162 re.Set(y.Float) 163 return UntypedComplex{Real: re.Add(re, x.Real), Imag: im}, nil 164 case UntypedComplex: 165 return UntypedComplex{ 166 Real: re.Add(x.Real, y.Real), 167 Imag: im.Add(x.Imag, y.Imag), 168 }, nil 169 } 170 case UntypedString: 171 switch y := y.(type) { 172 case UntypedString: 173 return UntypedString{x.String + y.String}, nil 174 case string: 175 return x.String + y, nil 176 } 177 case string: 178 switch y := y.(type) { 179 case UntypedString: 180 return x + y.String, nil 181 case string: 182 return x + y, nil 183 } 184 default: 185 xv := reflect.ValueOf(x) 186 yv := reflect.ValueOf(y) 187 res := xv.MethodByName("Add").Call([]reflect.Value{yv}) 188 return res[0].Interface(), nil 189 } 190 case token.Sub: 191 switch x := x.(type) { 192 case int: 193 switch y := y.(type) { 194 case int: 195 return x - y, nil 196 } 197 case int8: 198 switch y := y.(type) { 199 case int8: 200 return x - y, nil 201 } 202 case int16: 203 switch y := y.(type) { 204 case int16: 205 return x - y, nil 206 } 207 case int32: 208 switch y := y.(type) { 209 case int32: 210 return x - y, nil 211 } 212 case int64: 213 switch y := y.(type) { 214 case int64: 215 return x - y, nil 216 } 217 case uint: 218 switch y := y.(type) { 219 case uint: 220 return x - y, nil 221 } 222 case uint8: 223 switch y := y.(type) { 224 case uint8: 225 return x - y, nil 226 } 227 case uint16: 228 switch y := y.(type) { 229 case uint16: 230 return x - y, nil 231 } 232 case uint32: 233 switch y := y.(type) { 234 case uint32: 235 return x - y, nil 236 } 237 case uint64: 238 switch y := y.(type) { 239 case uint64: 240 return x - y, nil 241 } 242 case float32: 243 switch y := y.(type) { 244 case float32: 245 return x - y, nil 246 } 247 case float64: 248 switch y := y.(type) { 249 case float64: 250 return x - y, nil 251 } 252 case complex64: 253 switch y := y.(type) { 254 case complex64: 255 return x - y, nil 256 } 257 case complex128: 258 switch y := y.(type) { 259 case complex128: 260 return x - y, nil 261 } 262 case UntypedInt: 263 switch y := y.(type) { 264 case UntypedFloat: 265 z := big.NewFloat(0) 266 xf := big.NewFloat(float64(x.Int.Int64())) 267 return UntypedFloat{z.Sub(xf, y.Float)}, nil 268 case UntypedInt: 269 z := big.NewInt(0) 270 return UntypedInt{z.Sub(x.Int, y.Int)}, nil 271 case UntypedComplex: 272 re := big.NewFloat(0) 273 xf := big.NewFloat(float64(x.Int.Int64())) 274 im := big.NewFloat(0) 275 return UntypedComplex{re.Sub(xf, y.Real), im.Sub(im, y.Imag)}, nil 276 } 277 case UntypedFloat: 278 z := big.NewFloat(0) 279 switch y := y.(type) { 280 case UntypedInt: 281 yf := big.NewFloat(0) 282 yf.SetInt(y.Int) 283 return UntypedFloat{z.Sub(x.Float, yf)}, nil 284 case UntypedFloat: 285 return UntypedFloat{z.Sub(x.Float, y.Float)}, nil 286 case UntypedComplex: 287 return UntypedComplex{z.Sub(x.Float, y.Real), big.NewFloat(0)}, nil 288 } 289 case UntypedComplex: 290 re := big.NewFloat(0) 291 im := big.NewFloat(0) 292 switch y := y.(type) { 293 case UntypedInt: 294 yre := big.NewFloat(0) 295 yre.SetInt(y.Int) 296 return UntypedComplex{re.Sub(x.Real, yre), im}, nil 297 case UntypedFloat: 298 yre := big.NewFloat(0) 299 yre.Set(y.Float) 300 return UntypedComplex{re.Sub(x.Real, yre), im}, nil 301 case UntypedComplex: 302 return UntypedComplex{ 303 re.Sub(x.Real, y.Real), 304 im.Sub(x.Imag, y.Imag), 305 }, nil 306 } 307 } 308 case token.Mul: 309 switch x := x.(type) { 310 case int: 311 switch y := y.(type) { 312 case int: 313 return x * y, nil 314 } 315 case int8: 316 switch y := y.(type) { 317 case int8: 318 return x * y, nil 319 } 320 case int16: 321 switch y := y.(type) { 322 case int16: 323 return x * y, nil 324 } 325 case int32: 326 switch y := y.(type) { 327 case int32: 328 return x * y, nil 329 } 330 case int64: 331 switch y := y.(type) { 332 case int64: 333 return x * y, nil 334 } 335 case uint: 336 switch y := y.(type) { 337 case uint: 338 return x * y, nil 339 } 340 case uint8: 341 switch y := y.(type) { 342 case uint8: 343 return x * y, nil 344 } 345 case uint16: 346 switch y := y.(type) { 347 case uint16: 348 return x * y, nil 349 } 350 case uint32: 351 switch y := y.(type) { 352 case uint32: 353 return x * y, nil 354 } 355 case uint64: 356 switch y := y.(type) { 357 case uint64: 358 return x * y, nil 359 } 360 case float32: 361 switch y := y.(type) { 362 case float32: 363 return x * y, nil 364 } 365 case float64: 366 switch y := y.(type) { 367 case float64: 368 return x * y, nil 369 } 370 case complex64: 371 switch y := y.(type) { 372 case complex64: 373 return x * y, nil 374 } 375 case complex128: 376 switch y := y.(type) { 377 case complex128: 378 return x * y, nil 379 } 380 case *big.Int: 381 switch y := y.(type) { 382 case *big.Int: 383 z := big.NewInt(0) 384 return z.Mul(x, y), nil 385 } 386 case *big.Float: 387 switch y := y.(type) { 388 case *big.Float: 389 z := big.NewFloat(0) 390 return z.Mul(x, y), nil 391 } 392 case UntypedInt: 393 switch y := y.(type) { 394 case UntypedInt: 395 z := big.NewInt(0) 396 return UntypedInt{z.Mul(x.Int, y.Int)}, nil 397 case UntypedFloat: 398 z := big.NewFloat(0) 399 xf := big.NewFloat(float64(x.Int.Int64())) 400 return UntypedFloat{z.Mul(xf, y.Float)}, nil 401 case UntypedComplex: 402 re := big.NewFloat(0) 403 xf := big.NewFloat(float64(x.Int.Int64())) 404 im := big.NewFloat(0) 405 return UntypedComplex{re.Mul(xf, y.Real), im.Mul(xf, y.Imag)}, nil 406 } 407 case UntypedFloat: 408 z := big.NewFloat(0) 409 switch y := y.(type) { 410 case UntypedInt: 411 yf := big.NewFloat(0) 412 yf.SetInt(y.Int) 413 return UntypedFloat{z.Mul(x.Float, yf)}, nil 414 case UntypedFloat: 415 return UntypedFloat{z.Mul(x.Float, y.Float)}, nil 416 case UntypedComplex: 417 re := big.NewFloat(0) 418 im := big.NewFloat(0) 419 return UntypedComplex{re.Mul(x.Float, y.Real), im.Mul(x.Float, y.Imag)}, nil 420 } 421 case UntypedComplex: 422 re := big.NewFloat(0) 423 im := big.NewFloat(0) 424 switch y := y.(type) { 425 case UntypedInt: 426 yre := big.NewFloat(0) 427 yre.SetInt(y.Int) 428 return UntypedComplex{re.Mul(x.Real, yre), im.Mul(x.Imag, yre)}, nil 429 case UntypedFloat: 430 return UntypedComplex{re.Mul(x.Real, y.Float), im.Mul(x.Imag, y.Float)}, nil 431 case UntypedComplex: 432 xy := big.NewFloat(0) 433 yx := big.NewFloat(0) 434 xy.Mul(x.Real, y.Real) 435 yx.Mul(x.Imag, y.Imag) 436 re.Sub(xy, yx) 437 xy.Mul(x.Real, y.Imag) 438 yx.Mul(x.Imag, y.Real) 439 im.Add(xy, yx) 440 return UntypedComplex{re, im}, nil 441 } 442 } 443 case token.Div: 444 switch x := x.(type) { 445 case int: 446 switch y := y.(type) { 447 case int: 448 return x / y, nil 449 } 450 case float32: 451 switch y := y.(type) { 452 case float32: 453 return x / y, nil 454 } 455 case float64: 456 switch y := y.(type) { 457 case float64: 458 return x / y, nil 459 } 460 case complex64: 461 switch y := y.(type) { 462 case complex64: 463 return x / y, nil 464 } 465 case complex128: 466 switch y := y.(type) { 467 case complex128: 468 return x / y, nil 469 } 470 case UntypedInt: 471 switch y := y.(type) { 472 case UntypedInt: 473 z := big.NewInt(0) 474 return UntypedInt{z.Quo(x.Int, y.Int)}, nil 475 case UntypedFloat: 476 z := big.NewFloat(0) 477 xf := big.NewFloat(float64(x.Int.Int64())) 478 return UntypedFloat{z.Quo(xf, y.Float)}, nil 479 case UntypedComplex: 480 xf := big.NewFloat(float64(x.Int.Int64())) 481 yre2 := big.NewFloat(0) 482 yre2.Mul(y.Real, y.Real) 483 yim2 := big.NewFloat(0) 484 yim2.Mul(y.Imag, y.Imag) 485 den := big.NewFloat(0) 486 den.Add(yre2, yim2) 487 488 xyre := big.NewFloat(0) 489 xyre.Mul(xf, y.Real) 490 491 re := big.NewFloat(0) 492 re.Quo(xyre, den) 493 494 xyim := big.NewFloat(0) 495 xyim.Mul(xf, y.Imag) 496 497 im := big.NewFloat(0) 498 im.Quo(im.Sub(im, xyim), den) 499 return UntypedComplex{re, im}, nil 500 } 501 case UntypedFloat: 502 z := big.NewFloat(0) 503 switch y := y.(type) { 504 case UntypedInt: 505 yf := big.NewFloat(0) 506 yf.SetInt(y.Int) 507 return UntypedFloat{z.Quo(x.Float, yf)}, nil 508 case UntypedFloat: 509 return UntypedFloat{z.Quo(x.Float, y.Float)}, nil 510 case UntypedComplex: 511 yre2 := big.NewFloat(0) 512 yre2.Mul(y.Real, y.Real) 513 yim2 := big.NewFloat(0) 514 yim2.Mul(y.Imag, y.Imag) 515 den := big.NewFloat(0) 516 den.Add(yre2, yim2) 517 518 xyre := big.NewFloat(0) 519 xyre.Mul(x.Float, y.Real) 520 521 re := big.NewFloat(0) 522 re.Quo(xyre, den) 523 524 xyim := big.NewFloat(0) 525 xyim.Mul(x.Float, y.Imag) 526 527 im := big.NewFloat(0) 528 im.Quo(im.Sub(im, xyim), den) 529 return UntypedComplex{re, im}, nil 530 } 531 case UntypedComplex: 532 re := big.NewFloat(0) 533 im := big.NewFloat(0) 534 switch y := y.(type) { 535 case UntypedInt: 536 yre := big.NewFloat(0) 537 yre.SetInt(y.Int) 538 return UntypedComplex{re.Quo(x.Real, yre), im.Quo(x.Imag, yre)}, nil 539 case UntypedFloat: 540 return UntypedComplex{re.Quo(x.Real, y.Float), im.Quo(x.Imag, y.Float)}, nil 541 case UntypedComplex: 542 yre2 := big.NewFloat(0) 543 yre2.Mul(y.Real, y.Real) 544 yim2 := big.NewFloat(0) 545 yim2.Mul(y.Imag, y.Imag) 546 den := big.NewFloat(0) 547 den.Add(yre2, yim2) 548 549 xy := big.NewFloat(0) 550 yx := big.NewFloat(0) 551 xy.Mul(x.Real, y.Real) 552 yx.Mul(x.Imag, y.Imag) 553 554 re := big.NewFloat(0) 555 re.Quo(re.Add(xy, yx), den) 556 557 xy.Mul(x.Imag, y.Real) 558 yx.Mul(x.Real, y.Imag) 559 im := big.NewFloat(0) 560 im.Quo(im.Sub(xy, yx), den) 561 return UntypedComplex{re, im}, nil 562 } 563 } 564 case token.LogicalAnd, token.LogicalOr: 565 panic("logical ops processed before binOp") 566 case token.Equal: 567 return valEq(x, y), nil 568 case token.NotEqual: 569 return !valEq(x, y), nil 570 case token.Less: 571 switch x := x.(type) { 572 case int: 573 switch y := y.(type) { 574 case int: 575 return x < y, nil 576 } 577 case int8: 578 switch y := y.(type) { 579 case int8: 580 return x < y, nil 581 } 582 case int16: 583 switch y := y.(type) { 584 case int16: 585 return x < y, nil 586 } 587 case int32: 588 switch y := y.(type) { 589 case int32: 590 return x < y, nil 591 } 592 case int64: 593 switch y := y.(type) { 594 case int64: 595 return x < y, nil 596 } 597 case uint: 598 switch y := y.(type) { 599 case uint: 600 return x < y, nil 601 } 602 case uint8: 603 switch y := y.(type) { 604 case uint8: 605 return x < y, nil 606 } 607 case uint16: 608 switch y := y.(type) { 609 case uint16: 610 return x < y, nil 611 } 612 case uint32: 613 switch y := y.(type) { 614 case uint32: 615 return x < y, nil 616 } 617 case uint64: 618 switch y := y.(type) { 619 case uint64: 620 return x < y, nil 621 } 622 case float32: 623 switch y := y.(type) { 624 case float32: 625 return x < y, nil 626 } 627 case float64: 628 switch y := y.(type) { 629 case float64: 630 return x < y, nil 631 } 632 case *big.Int: 633 switch y := y.(type) { 634 case *big.Int: 635 return x.Cmp(y) == -1, nil 636 } 637 case *big.Float: 638 switch y := y.(type) { 639 case *big.Float: 640 return x.Cmp(y) == -1, nil 641 } 642 } 643 case token.LessEqual: 644 switch x := x.(type) { 645 case int: 646 switch y := y.(type) { 647 case int: 648 return x <= y, nil 649 } 650 case int8: 651 switch y := y.(type) { 652 case int8: 653 return x <= y, nil 654 } 655 case int16: 656 switch y := y.(type) { 657 case int16: 658 return x <= y, nil 659 } 660 case int32: 661 switch y := y.(type) { 662 case int32: 663 return x <= y, nil 664 } 665 case int64: 666 switch y := y.(type) { 667 case int64: 668 return x <= y, nil 669 } 670 case uint: 671 switch y := y.(type) { 672 case uint: 673 return x <= y, nil 674 } 675 case uint8: 676 switch y := y.(type) { 677 case uint8: 678 return x <= y, nil 679 } 680 case uint16: 681 switch y := y.(type) { 682 case uint16: 683 return x <= y, nil 684 } 685 case uint32: 686 switch y := y.(type) { 687 case uint32: 688 return x <= y, nil 689 } 690 case uint64: 691 switch y := y.(type) { 692 case uint64: 693 return x <= y, nil 694 } 695 case float32: 696 switch y := y.(type) { 697 case float32: 698 return x <= y, nil 699 } 700 case float64: 701 switch y := y.(type) { 702 case float64: 703 return x <= y, nil 704 } 705 case *big.Int: 706 switch y := y.(type) { 707 case *big.Int: 708 return x.Cmp(y) <= 0, nil 709 } 710 case *big.Float: 711 switch y := y.(type) { 712 case *big.Float: 713 return x.Cmp(y) <= 0, nil 714 } 715 case UntypedInt: 716 switch y := y.(type) { 717 case UntypedInt: 718 return x.Cmp(y.Int) <= 0, nil 719 case UntypedFloat: 720 xf := big.NewFloat(float64(x.Int.Int64())) 721 return xf.Cmp(y.Float) <= 0, nil 722 } 723 case UntypedFloat: 724 switch y := y.(type) { 725 case UntypedInt: 726 yf := big.NewFloat(float64(y.Int.Int64())) 727 return x.Cmp(yf) <= 0, nil 728 case UntypedFloat: 729 return x.Cmp(y.Float) <= 0, nil 730 } 731 } 732 case token.Greater: 733 switch x := x.(type) { 734 case int: 735 switch y := y.(type) { 736 case int: 737 return x > y, nil 738 } 739 case int8: 740 switch y := y.(type) { 741 case int8: 742 return x > y, nil 743 } 744 case int16: 745 switch y := y.(type) { 746 case int16: 747 return x > y, nil 748 } 749 case int32: 750 switch y := y.(type) { 751 case int32: 752 return x > y, nil 753 } 754 case int64: 755 switch y := y.(type) { 756 case int64: 757 return x > y, nil 758 } 759 case uint: 760 switch y := y.(type) { 761 case uint: 762 return x > y, nil 763 } 764 case uint8: 765 switch y := y.(type) { 766 case uint8: 767 return x > y, nil 768 } 769 case uint16: 770 switch y := y.(type) { 771 case uint16: 772 return x > y, nil 773 } 774 case uint32: 775 switch y := y.(type) { 776 case uint32: 777 return x > y, nil 778 } 779 case uint64: 780 switch y := y.(type) { 781 case uint64: 782 return x > y, nil 783 } 784 case float32: 785 switch y := y.(type) { 786 case float32: 787 return x > y, nil 788 } 789 case float64: 790 switch y := y.(type) { 791 case float64: 792 return x > y, nil 793 } 794 case *big.Int: 795 switch y := y.(type) { 796 case *big.Int: 797 return x.Cmp(y) == 1, nil 798 } 799 case *big.Float: 800 switch y := y.(type) { 801 case *big.Float: 802 return x.Cmp(y) == 1, nil 803 } 804 } 805 case token.Pipe: 806 switch x := x.(type) { 807 case int: 808 switch y := y.(type) { 809 case int: 810 return x | y, nil 811 } 812 case int8: 813 switch y := y.(type) { 814 case int8: 815 return x | y, nil 816 } 817 case int16: 818 switch y := y.(type) { 819 case int16: 820 return x | y, nil 821 } 822 case int32: 823 switch y := y.(type) { 824 case int32: 825 return x | y, nil 826 } 827 case int64: 828 switch y := y.(type) { 829 case int64: 830 return x | y, nil 831 } 832 case uint: 833 switch y := y.(type) { 834 case uint: 835 return x | y, nil 836 } 837 case uint8: 838 switch y := y.(type) { 839 case uint8: 840 return x | y, nil 841 } 842 case uint16: 843 switch y := y.(type) { 844 case uint16: 845 return x | y, nil 846 } 847 case uint32: 848 switch y := y.(type) { 849 case uint32: 850 return x | y, nil 851 } 852 case uint64: 853 switch y := y.(type) { 854 case uint64: 855 return x | y, nil 856 } 857 case UntypedInt: 858 switch y := y.(type) { 859 case UntypedInt: 860 z := big.NewInt(0) 861 return UntypedInt{z.Or(x.Int, y.Int)}, nil 862 } 863 default: 864 xv := reflect.ValueOf(x) 865 yv := reflect.ValueOf(y) 866 res := xv.MethodByName("Or").Call([]reflect.Value{yv}) 867 return res[0].Interface(), nil 868 } 869 case token.Pow: 870 switch x := x.(type) { 871 case int: 872 switch y := y.(type) { 873 case int: 874 return x ^ y, nil 875 } 876 case int8: 877 switch y := y.(type) { 878 case int8: 879 return x ^ y, nil 880 } 881 case int16: 882 switch y := y.(type) { 883 case int16: 884 return x ^ y, nil 885 } 886 case int32: 887 switch y := y.(type) { 888 case int32: 889 return x ^ y, nil 890 } 891 case int64: 892 switch y := y.(type) { 893 case int64: 894 return x ^ y, nil 895 } 896 case uint: 897 switch y := y.(type) { 898 case uint: 899 return x ^ y, nil 900 } 901 case uint8: 902 switch y := y.(type) { 903 case uint8: 904 return x ^ y, nil 905 } 906 case uint16: 907 switch y := y.(type) { 908 case uint16: 909 return x ^ y, nil 910 } 911 case uint32: 912 switch y := y.(type) { 913 case uint32: 914 return x ^ y, nil 915 } 916 case uint64: 917 switch y := y.(type) { 918 case uint64: 919 return x ^ y, nil 920 } 921 case UntypedInt: 922 switch y := y.(type) { 923 case UntypedInt: 924 z := big.NewInt(0) 925 return UntypedInt{z.Xor(x.Int, y.Int)}, nil 926 } 927 default: 928 xv := reflect.ValueOf(x) 929 yv := reflect.ValueOf(y) 930 res := xv.MethodByName("Xor").Call([]reflect.Value{yv}) 931 return res[0].Interface(), nil 932 } 933 case token.Ref: 934 switch x := x.(type) { 935 case int: 936 switch y := y.(type) { 937 case int: 938 return x & y, nil 939 } 940 case int8: 941 switch y := y.(type) { 942 case int8: 943 return x & y, nil 944 } 945 case int16: 946 switch y := y.(type) { 947 case int16: 948 return x & y, nil 949 } 950 case int32: 951 switch y := y.(type) { 952 case int32: 953 return x & y, nil 954 } 955 case int64: 956 switch y := y.(type) { 957 case int64: 958 return x & y, nil 959 } 960 case uint: 961 switch y := y.(type) { 962 case uint: 963 return x & y, nil 964 } 965 case uint8: 966 switch y := y.(type) { 967 case uint8: 968 return x & y, nil 969 } 970 case uint16: 971 switch y := y.(type) { 972 case uint16: 973 return x & y, nil 974 } 975 case uint32: 976 switch y := y.(type) { 977 case uint32: 978 return x & y, nil 979 } 980 case uint64: 981 switch y := y.(type) { 982 case uint64: 983 return x & y, nil 984 } 985 case UntypedInt: 986 switch y := y.(type) { 987 case UntypedInt: 988 z := big.NewInt(0) 989 return UntypedInt{z.And(x.Int, y.Int)}, nil 990 } 991 default: 992 xv := reflect.ValueOf(x) 993 yv := reflect.ValueOf(y) 994 res := xv.MethodByName("And").Call([]reflect.Value{yv}) 995 return res[0].Interface(), nil 996 } 997 case token.Rem: 998 switch x := x.(type) { 999 case int: 1000 switch y := y.(type) { 1001 case int: 1002 return x % y, nil 1003 } 1004 case int8: 1005 switch y := y.(type) { 1006 case int8: 1007 return x % y, nil 1008 } 1009 case int16: 1010 switch y := y.(type) { 1011 case int16: 1012 return x % y, nil 1013 } 1014 case int32: 1015 switch y := y.(type) { 1016 case int32: 1017 return x % y, nil 1018 } 1019 case int64: 1020 switch y := y.(type) { 1021 case int64: 1022 return x % y, nil 1023 } 1024 case uint: 1025 switch y := y.(type) { 1026 case uint: 1027 return x % y, nil 1028 } 1029 case uint8: 1030 switch y := y.(type) { 1031 case uint8: 1032 return x % y, nil 1033 } 1034 case uint16: 1035 switch y := y.(type) { 1036 case uint16: 1037 return x % y, nil 1038 } 1039 case uint32: 1040 switch y := y.(type) { 1041 case uint32: 1042 return x % y, nil 1043 } 1044 case uint64: 1045 switch y := y.(type) { 1046 case uint64: 1047 return x % y, nil 1048 } 1049 case UntypedInt: 1050 switch y := y.(type) { 1051 case UntypedInt: 1052 z := big.NewInt(0) 1053 return UntypedInt{z.Rem(x.Int, y.Int)}, nil 1054 } 1055 default: 1056 xv := reflect.ValueOf(x) 1057 yv := reflect.ValueOf(y) 1058 res := xv.MethodByName("Rem").Call([]reflect.Value{yv}) 1059 return res[0].Interface(), nil 1060 } 1061 case token.TwoLess: 1062 switch x := x.(type) { 1063 case int: 1064 switch y := y.(type) { 1065 case uint: 1066 return x << y, nil 1067 case uint8: 1068 return x << y, nil 1069 case uint16: 1070 return x << y, nil 1071 case uint32: 1072 return x << y, nil 1073 case uint64: 1074 return x << y, nil 1075 case UntypedInt: 1076 v := uint64(y.Int64()) 1077 return x << v, nil 1078 } 1079 case int8: 1080 switch y := y.(type) { 1081 case uint: 1082 return x << y, nil 1083 case uint8: 1084 return x << y, nil 1085 case uint16: 1086 return x << y, nil 1087 case uint32: 1088 return x << y, nil 1089 case uint64: 1090 return x << y, nil 1091 case UntypedInt: 1092 v := uint64(y.Int64()) 1093 return x << v, nil 1094 } 1095 case int16: 1096 switch y := y.(type) { 1097 case uint: 1098 return x << y, nil 1099 case uint8: 1100 return x << y, nil 1101 case uint16: 1102 return x << y, nil 1103 case uint32: 1104 return x << y, nil 1105 case uint64: 1106 return x << y, nil 1107 case UntypedInt: 1108 v := uint64(y.Int64()) 1109 return x << v, nil 1110 } 1111 case int32: 1112 switch y := y.(type) { 1113 case uint: 1114 return x << y, nil 1115 case uint8: 1116 return x << y, nil 1117 case uint16: 1118 return x << y, nil 1119 case uint32: 1120 return x << y, nil 1121 case uint64: 1122 return x << y, nil 1123 case UntypedInt: 1124 v := uint64(y.Int64()) 1125 return x << v, nil 1126 } 1127 case int64: 1128 switch y := y.(type) { 1129 case uint: 1130 return x << y, nil 1131 case uint8: 1132 return x << y, nil 1133 case uint16: 1134 return x << y, nil 1135 case uint32: 1136 return x << y, nil 1137 case uint64: 1138 return x << y, nil 1139 case UntypedInt: 1140 v := uint64(y.Int64()) 1141 return x << v, nil 1142 } 1143 case uint: 1144 switch y := y.(type) { 1145 case uint: 1146 return x << y, nil 1147 case uint8: 1148 return x << y, nil 1149 case uint16: 1150 return x << y, nil 1151 case uint32: 1152 return x << y, nil 1153 case uint64: 1154 return x << y, nil 1155 case UntypedInt: 1156 v := uint64(y.Int64()) 1157 return x << v, nil 1158 } 1159 case uint8: 1160 switch y := y.(type) { 1161 case uint: 1162 return x << y, nil 1163 case uint8: 1164 return x << y, nil 1165 case uint16: 1166 return x << y, nil 1167 case uint32: 1168 return x << y, nil 1169 case uint64: 1170 return x << y, nil 1171 case UntypedInt: 1172 v := uint64(y.Int64()) 1173 return x << v, nil 1174 } 1175 case uint16: 1176 switch y := y.(type) { 1177 case uint: 1178 return x << y, nil 1179 case uint8: 1180 return x << y, nil 1181 case uint16: 1182 return x << y, nil 1183 case uint32: 1184 return x << y, nil 1185 case uint64: 1186 return x << y, nil 1187 case UntypedInt: 1188 v := uint64(y.Int64()) 1189 return x << v, nil 1190 } 1191 case uint32: 1192 switch y := y.(type) { 1193 case uint: 1194 return x << y, nil 1195 case uint8: 1196 return x << y, nil 1197 case uint16: 1198 return x << y, nil 1199 case uint32: 1200 return x << y, nil 1201 case uint64: 1202 return x << y, nil 1203 case UntypedInt: 1204 v := uint64(y.Int64()) 1205 return x << v, nil 1206 } 1207 case uint64: 1208 switch y := y.(type) { 1209 case uint: 1210 return x << y, nil 1211 case uint8: 1212 return x << y, nil 1213 case uint16: 1214 return x << y, nil 1215 case uint32: 1216 return x << y, nil 1217 case uint64: 1218 return x << y, nil 1219 case UntypedInt: 1220 v := uint64(y.Int64()) 1221 return x << v, nil 1222 } 1223 case UntypedInt: 1224 switch y := y.(type) { 1225 case uint: 1226 z := big.NewInt(0) 1227 return UntypedInt{z.Lsh(x.Int, y)}, nil 1228 case uint8: 1229 z := big.NewInt(0) 1230 return UntypedInt{z.Lsh(x.Int, uint(y))}, nil 1231 case uint16: 1232 z := big.NewInt(0) 1233 return UntypedInt{z.Lsh(x.Int, uint(y))}, nil 1234 case uint32: 1235 z := big.NewInt(0) 1236 return UntypedInt{z.Lsh(x.Int, uint(y))}, nil 1237 case uint64: 1238 z := big.NewInt(0) 1239 return UntypedInt{z.Lsh(x.Int, uint(y))}, nil 1240 case UntypedInt: 1241 z := big.NewInt(0) 1242 n := uint(y.Int.Int64()) 1243 return UntypedInt{z.Lsh(x.Int, n)}, nil 1244 } 1245 default: 1246 xv := reflect.ValueOf(x) 1247 yv := reflect.ValueOf(y) 1248 res := xv.MethodByName("Lsh").Call([]reflect.Value{yv}) 1249 return res[0].Interface(), nil 1250 } 1251 case token.TwoGreater: 1252 switch x := x.(type) { 1253 case int: 1254 switch y := y.(type) { 1255 case uint: 1256 return x >> y, nil 1257 case uint8: 1258 return x >> y, nil 1259 case uint16: 1260 return x >> y, nil 1261 case uint32: 1262 return x >> y, nil 1263 case uint64: 1264 return x >> y, nil 1265 case UntypedInt: 1266 v := uint64(y.Int64()) 1267 return x >> v, nil 1268 } 1269 case int8: 1270 switch y := y.(type) { 1271 case uint: 1272 return x >> y, nil 1273 case uint8: 1274 return x >> y, nil 1275 case uint16: 1276 return x >> y, nil 1277 case uint32: 1278 return x >> y, nil 1279 case uint64: 1280 return x >> y, nil 1281 case UntypedInt: 1282 v := uint64(y.Int64()) 1283 return x >> v, nil 1284 } 1285 case int16: 1286 switch y := y.(type) { 1287 case uint: 1288 return x >> y, nil 1289 case uint8: 1290 return x >> y, nil 1291 case uint16: 1292 return x >> y, nil 1293 case uint32: 1294 return x >> y, nil 1295 case uint64: 1296 return x >> y, nil 1297 case UntypedInt: 1298 v := uint64(y.Int64()) 1299 return x >> v, nil 1300 } 1301 case int32: 1302 switch y := y.(type) { 1303 case uint: 1304 return x >> y, nil 1305 case uint8: 1306 return x >> y, nil 1307 case uint16: 1308 return x >> y, nil 1309 case uint32: 1310 return x >> y, nil 1311 case uint64: 1312 return x >> y, nil 1313 case UntypedInt: 1314 v := uint64(y.Int64()) 1315 return x >> v, nil 1316 } 1317 case int64: 1318 switch y := y.(type) { 1319 case uint: 1320 return x >> y, nil 1321 case uint8: 1322 return x >> y, nil 1323 case uint16: 1324 return x >> y, nil 1325 case uint32: 1326 return x >> y, nil 1327 case uint64: 1328 return x >> y, nil 1329 case UntypedInt: 1330 v := uint64(y.Int64()) 1331 return x >> v, nil 1332 } 1333 case uint: 1334 switch y := y.(type) { 1335 case uint: 1336 return x >> y, nil 1337 case uint8: 1338 return x >> y, nil 1339 case uint16: 1340 return x >> y, nil 1341 case uint32: 1342 return x >> y, nil 1343 case uint64: 1344 return x >> y, nil 1345 case UntypedInt: 1346 v := uint64(y.Int64()) 1347 return x >> v, nil 1348 } 1349 case uint8: 1350 switch y := y.(type) { 1351 case uint: 1352 return x >> y, nil 1353 case uint8: 1354 return x >> y, nil 1355 case uint16: 1356 return x >> y, nil 1357 case uint32: 1358 return x >> y, nil 1359 case uint64: 1360 return x >> y, nil 1361 case UntypedInt: 1362 v := uint64(y.Int64()) 1363 return x >> v, nil 1364 } 1365 case uint16: 1366 switch y := y.(type) { 1367 case uint: 1368 return x >> y, nil 1369 case uint8: 1370 return x >> y, nil 1371 case uint16: 1372 return x >> y, nil 1373 case uint32: 1374 return x >> y, nil 1375 case uint64: 1376 return x >> y, nil 1377 case UntypedInt: 1378 v := uint64(y.Int64()) 1379 return x >> v, nil 1380 } 1381 case uint32: 1382 switch y := y.(type) { 1383 case uint: 1384 return x >> y, nil 1385 case uint8: 1386 return x >> y, nil 1387 case uint16: 1388 return x >> y, nil 1389 case uint32: 1390 return x >> y, nil 1391 case uint64: 1392 return x >> y, nil 1393 case UntypedInt: 1394 v := uint64(y.Int64()) 1395 return x >> v, nil 1396 } 1397 case uint64: 1398 switch y := y.(type) { 1399 case uint: 1400 return x >> y, nil 1401 case uint8: 1402 return x >> y, nil 1403 case uint16: 1404 return x >> y, nil 1405 case uint32: 1406 return x >> y, nil 1407 case uint64: 1408 return x >> y, nil 1409 case UntypedInt: 1410 v := uint64(y.Int64()) 1411 return x >> v, nil 1412 } 1413 case UntypedInt: 1414 switch y := y.(type) { 1415 case uint: 1416 z := big.NewInt(0) 1417 return UntypedInt{z.Rsh(x.Int, y)}, nil 1418 case uint8: 1419 z := big.NewInt(0) 1420 return UntypedInt{z.Rsh(x.Int, uint(y))}, nil 1421 case uint16: 1422 z := big.NewInt(0) 1423 return UntypedInt{z.Rsh(x.Int, uint(y))}, nil 1424 case uint32: 1425 z := big.NewInt(0) 1426 return UntypedInt{z.Rsh(x.Int, uint(y))}, nil 1427 case uint64: 1428 z := big.NewInt(0) 1429 return UntypedInt{z.Rsh(x.Int, uint(y))}, nil 1430 case UntypedInt: 1431 z := big.NewInt(0) 1432 n := uint(y.Int.Int64()) 1433 return UntypedInt{z.Rsh(x.Int, n)}, nil 1434 } 1435 default: 1436 xv := reflect.ValueOf(x) 1437 yv := reflect.ValueOf(y) 1438 res := xv.MethodByName("Rsh").Call([]reflect.Value{yv}) 1439 return res[0].Interface(), nil 1440 } 1441 case token.RefPow: 1442 switch x := x.(type) { 1443 case int: 1444 switch y := y.(type) { 1445 case int: 1446 return x &^ y, nil 1447 } 1448 case int8: 1449 switch y := y.(type) { 1450 case int8: 1451 return x &^ y, nil 1452 } 1453 case int16: 1454 switch y := y.(type) { 1455 case int16: 1456 return x &^ y, nil 1457 } 1458 case int32: 1459 switch y := y.(type) { 1460 case int32: 1461 return x &^ y, nil 1462 } 1463 case int64: 1464 switch y := y.(type) { 1465 case int64: 1466 return x &^ y, nil 1467 } 1468 case uint: 1469 switch y := y.(type) { 1470 case uint: 1471 return x &^ y, nil 1472 } 1473 case uint8: 1474 switch y := y.(type) { 1475 case uint8: 1476 return x &^ y, nil 1477 } 1478 case uint16: 1479 switch y := y.(type) { 1480 case uint16: 1481 return x &^ y, nil 1482 } 1483 case uint32: 1484 switch y := y.(type) { 1485 case uint32: 1486 return x &^ y, nil 1487 } 1488 case uint64: 1489 switch y := y.(type) { 1490 case uint64: 1491 return x &^ y, nil 1492 } 1493 case UntypedInt: 1494 switch y := y.(type) { 1495 case UntypedInt: 1496 z := big.NewInt(0) 1497 return UntypedInt{z.AndNot(x.Int, y.Int)}, nil 1498 } 1499 default: 1500 xv := reflect.ValueOf(x) 1501 yv := reflect.ValueOf(y) 1502 res := xv.MethodByName("AndNot").Call([]reflect.Value{yv}) 1503 return res[0].Interface(), nil 1504 } 1505 } 1506 //return nil, fmt.Errorf("type mismatch Left: %T, Right: %T", x, y) 1507 panic(fmt.Sprintf("binOp type mismatch Left: %+v (%T), Right: %+v (%T) op: %v", x, x, y, y, op)) 1508 } 1509 1510 func typeConv(t reflect.Type, v reflect.Value) (res reflect.Value) { 1511 if v.Type() == t { 1512 return v 1513 } 1514 if v.Kind() == t.Kind() { 1515 // named type conversion 1516 res = reflect.New(t).Elem() 1517 switch v.Kind() { 1518 case reflect.Bool: 1519 res.SetBool(v.Bool()) 1520 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1521 res.SetInt(v.Int()) 1522 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1523 res.SetUint(v.Uint()) 1524 case reflect.Float32, reflect.Float64: 1525 res.SetFloat(v.Float()) 1526 case reflect.Complex64, reflect.Complex128: 1527 res.SetComplex(v.Complex()) 1528 case reflect.String: 1529 res.SetString(v.String()) 1530 case reflect.Chan: 1531 res.Set(v) 1532 default: 1533 panic(interpPanic{fmt.Errorf("TODO typeConv same kind: %v", v.Kind())}) 1534 } 1535 return res 1536 } 1537 switch t.Kind() { 1538 case reflect.Int: 1539 switch v.Kind() { 1540 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1541 return reflect.ValueOf(int(v.Int())) 1542 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1543 return reflect.ValueOf(int(v.Uint())) 1544 case reflect.Float32, reflect.Float64: 1545 return reflect.ValueOf(int(v.Float())) 1546 } 1547 case reflect.Int8: 1548 switch v.Kind() { 1549 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1550 return reflect.ValueOf(int8(v.Int())) 1551 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1552 return reflect.ValueOf(int8(v.Uint())) 1553 case reflect.Float32, reflect.Float64: 1554 return reflect.ValueOf(int8(v.Float())) 1555 } 1556 case reflect.Int16: 1557 switch v.Kind() { 1558 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1559 return reflect.ValueOf(int16(v.Int())) 1560 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1561 return reflect.ValueOf(int16(v.Uint())) 1562 case reflect.Float32, reflect.Float64: 1563 return reflect.ValueOf(int16(v.Float())) 1564 } 1565 case reflect.Int32: 1566 switch v.Kind() { 1567 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1568 return reflect.ValueOf(int32(v.Int())) 1569 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1570 return reflect.ValueOf(int32(v.Uint())) 1571 case reflect.Float32, reflect.Float64: 1572 return reflect.ValueOf(int32(v.Float())) 1573 } 1574 case reflect.Int64: 1575 switch v.Kind() { 1576 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1577 return reflect.ValueOf(int64(v.Int())) 1578 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1579 return reflect.ValueOf(int64(v.Uint())) 1580 case reflect.Float32, reflect.Float64: 1581 return reflect.ValueOf(int64(v.Float())) 1582 } 1583 case reflect.Uint: 1584 switch v.Kind() { 1585 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1586 return reflect.ValueOf(uint(v.Int())) 1587 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1588 return reflect.ValueOf(uint(v.Uint())) 1589 case reflect.Float32, reflect.Float64: 1590 return reflect.ValueOf(uint(v.Float())) 1591 } 1592 case reflect.Uint8: 1593 switch v.Kind() { 1594 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1595 return reflect.ValueOf(uint8(v.Int())) 1596 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1597 return reflect.ValueOf(uint8(v.Uint())) 1598 case reflect.Float32, reflect.Float64: 1599 return reflect.ValueOf(uint8(v.Float())) 1600 } 1601 case reflect.Uint16: 1602 switch v.Kind() { 1603 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1604 return reflect.ValueOf(uint16(v.Int())) 1605 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1606 return reflect.ValueOf(uint16(v.Uint())) 1607 case reflect.Float32, reflect.Float64: 1608 return reflect.ValueOf(uint16(v.Float())) 1609 } 1610 case reflect.Uint32: 1611 switch v.Kind() { 1612 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1613 return reflect.ValueOf(uint32(v.Int())) 1614 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1615 return reflect.ValueOf(uint32(v.Uint())) 1616 case reflect.Float32, reflect.Float64: 1617 return reflect.ValueOf(uint32(v.Float())) 1618 } 1619 case reflect.Uint64: 1620 switch v.Kind() { 1621 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1622 return reflect.ValueOf(uint64(v.Int())) 1623 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 1624 return reflect.ValueOf(uint64(v.Uint())) 1625 case reflect.Float32, reflect.Float64: 1626 return reflect.ValueOf(uint64(v.Float())) 1627 } 1628 case reflect.Float64: 1629 if v.Kind() == reflect.Float64 { 1630 res = reflect.New(t).Elem() 1631 res.SetFloat(v.Float()) 1632 return res 1633 } 1634 return reflect.ValueOf(float64(v.Int())) 1635 case reflect.Complex64: 1636 switch v.Kind() { 1637 case reflect.Complex64, reflect.Complex128: 1638 res = reflect.New(t).Elem() 1639 res.SetComplex(v.Complex()) 1640 return res 1641 } 1642 return reflect.ValueOf(complex(float32(v.Int()), 0)) 1643 case reflect.Complex128: 1644 switch v.Kind() { 1645 case reflect.Complex64, reflect.Complex128: 1646 res = reflect.New(t).Elem() 1647 res.SetComplex(v.Complex()) 1648 return res 1649 } 1650 return reflect.ValueOf(complex(float64(v.Int()), 0)) 1651 case reflect.Interface: 1652 return reflect.ValueOf(v.Interface()) 1653 case reflect.String: 1654 switch src := v.Interface().(type) { 1655 case []byte: 1656 return reflect.ValueOf(string(src)) 1657 case rune: 1658 return reflect.ValueOf(string(src)) 1659 } 1660 } 1661 if t == reflect.TypeOf([]byte(nil)) { 1662 switch src := v.Interface().(type) { 1663 case string: 1664 return reflect.ValueOf([]byte(src)) 1665 } 1666 } 1667 panic(interpPanic{fmt.Errorf("unknown type conv: %v <- %v", t, v.Type())}) 1668 }