Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17751x 17751x 17751x 17751x 1x 1x 1x 1x 1x 1x 1x 1x 7948x 7948x 7948x 7948x 7948x 7948x 7948x 1x 1x 5917x 5917x 5917x 5917x 5917x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4301x 4301x 4301x 4301x 4301x 4301x 4301x 4301x 4301x 4301x 5938x 5938x 5938x 3604x 5938x 774x 774x 774x 774x 2334x 1471x 1471x 182x 182x 182x 1471x 180x 180x 180x 1289x 1059x 1109x 50x 50x 50x 1560x 89x 89x 89x 89x 5938x 4301x 4251x 4251x 4251x 4301x 795x 795x 795x 795x 4301x 4251x 4251x 1x 1x 1x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1616x 1872x 1872x 1872x 475x 1872x 125x 125x 125x 125x 1397x 1266x 1266x 32x 32x 32x 32x 1266x 30x 30x 30x 30x 1234x 550x 550x 1204x 654x 654x 654x 1272x 6x 6x 6x 6x 1872x 1616x 962x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 70x 70x 70x 28x 28x 1x 1x 308x 14x 14x 308x 14x 14x 308x 308x 1x 1x 38x 38x 1x 1x 19x 19x 1x 1x 19x 19x 1x 1x 19x 19x 1x 1x 19x 19x 1x 1x 19x 19x 1x 1x 19x 19x 1x 1x 19x 19x 1x 1x 271x 271x 271x 271x 1x 1x 54x 54x 54x 1x 1x 19x 19x 1x 1x 38x 38x 1x 1x 19x 19x 1x 1x 64x 64x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 1x 1x 14x 14x 1x 1x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 1x 1x 5990x 1x 1x 1x 1626x 1x 1x 1x 4362x 4362x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 5990x 9196x 3513x 4x 4x 3509x 3509x 3509x 3509x 9196x 1074x 70x 64x 64x 64x 64x 64x 70x 1074x 1004x 976x 33x 33x 943x 943x 943x 943x 5683x 622x 622x 622x 622x 622x 4609x 104x 104x 104x 6x 6x 6x 6x 6x 104x 104x 3983x 80x 80x 80x 80x 3879x 3799x 3799x 3799x 37x 37x 37x 37x 3799x 2769x 2x 2x 2767x 2767x 2767x 2767x 3762x 993x 4x 4x 4x 4x 4x 4x 4x 9196x 5990x 26x 26x 5990x 906x 906x 906x 5990x 5917x 5917x 5917x 5917x 5917x 5917x 5917x 1x 1x 1x 1x 1x 1x 73x 73x 73x 1x 1x 1x 1x 1x 1050x 1050x 1050x 1050x 1050x 1050x 1050x 162x 140x 162x 22x 22x 162x 1050x 1x 1x 1x 9196x 9196x 9196x 9196x 9196x 9196x 9196x 9196x 9196x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 17043x 5842x 17043x 11256x 11256x 11256x 266x 11256x 211x 211x 11256x 11256x 11256x 11256x 11256x 11256x 11256x 11256x 3212x 3212x 3212x 11256x 726x 726x 11256x 228x 228x 228x 228x 228x 11256x 77x 58x 58x 58x 77x 11256x 11256x 11256x 11256x 11239x 11256x 19x 19x 19x 19x 19x 11256x 11256x 11256x 11256x 11256x 11256x 11256x 11256x 11256x 11256x 11256x 11256x 6861x 11256x 17043x 5787x 5787x 17043x 9196x 9196x 38x 38x 19x 19x 19x 19x 19x 19x 9177x 9177x 9196x 42x 42x 9196x 9196x 176x 176x 176x 9001x 1x 1x 1x 1x 626x 626x 626x 626x 2897x 2897x 2897x 186x 186x 17x 17x 186x 34x 34x 186x 17x 17x 186x 17x 17x 186x 17x 17x 186x 29x 29x 186x 17x 17x 186x 17x 17x 186x 17x 17x 17x 17x 17x 186x 4x 4x 4x 4x 186x 182x 2897x 2711x 186x 2711x 2525x 2525x 2711x 2897x 622x 622x 1x 1x 1x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 626x 626x 626x 3653x 3653x 3027x 3027x 3653x 626x 626x 626x 626x 626x 3653x 626x 4605x 1x 1x 1x 3799x 3799x 3799x 3799x 3799x 3799x 3799x 3799x 3799x 3799x 3799x 3799x 3799x 3799x 3799x 3799x 1x 1x 1x 5683x 5683x 316x 316x 316x 5683x 375x 375x 375x 5367x 38x 38x 19x 19x 19x 19x 19x 19x 19x 19x 4992x 62x 62x 62x 4954x 47x 47x 47x 4892x 19x 19x 19x 4845x 46x 46x 46x 4826x 19x 19x 19x 4780x 38x 38x 38x 4761x 19x 19x 19x 4723x 19x 19x 19x 19x 19x 19x 4704x 76x 76x 38x 38x 38x 19x 19x 19x 19x 19x 19x 19x 38x 76x 38x 38x 38x 19x 19x 19x 19x 19x 19x 19x 38x 4685x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 4609x 1074x 1074x 1x 1x 1x 1074x 1074x 691x 691x 691x 383x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 70x 70x 64x 64x 64x 6x 1x 1x 1x 3983x 3983x 104x 104x 104x 104x 104x 3879x 1x 1x 1x 3879x 3879x 80x 80x 80x 80x 80x 3799x 1x 1x 1x 3799x 3799x 3799x 1x 1x 1x 993x 993x 989x 989x 989x 4x 1x 1x 1x 3799x 3799x 10413x 10413x 2719x 2148x 2148x 2719x 8265x 8265x 3799x 37x 37x 37x 37x 37x 3762x 1x 1x 1x 3799x 3799x 10413x 10413x 2719x 2148x 2148x 2719x 8265x 8265x 3799x 3799x 1x 1x 1x 3762x 3762x 12123x 12123x 176x 176x 176x 12123x 2558x 1721x 1721x 2558x 10226x 10226x 3762x 2769x 2769x 2769x 2593x 2769x 176x 176x 2769x 2769x 993x 1x 1x 1x 1004x 1004x 28x 28x 28x 28x 976x 1x 1x | /*
Based on ndef.parser, by Raphael Graf(r@undefined.ch)
http://www.undefined.ch/mparser/index.html
Ported to JavaScript and modified by Matthew Crumley (email@matthewcrumley.com, http://silentmatt.com/)
You are free to use and modify this code in anyway you find useful. Please leave this comment in the code
to acknowledge its original source. If you feel like it, I enjoy hearing about projects that use my code,
but don't feel like you have to let me know or ask permission.
*/
// Added by stlsmiths 6/13/2011
// re-define Array.indexOf, because IE doesn't know it ...
//
// from http://stellapower.net/content/javascript-support-and-arrayindexof-ie
//obsolete polyfill
/*
if (!Array.indexOf) {
Array.prototype.indexOf = function (obj, start) {
for (let i = start || 0; i < this.length; i++) {
if (this[i] === obj) {
return i;
}
}
return -1;
};
}
*/
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
const TNUMBER = 0;
const TOP1 = 1;
const TOP2 = 2;
const TVAR = 3;
const TFUNCALL = 4;
export function Token(type_, index_, prio_, number_) {
this.type_ = type_;
this.index_ = index_ || 0;
this.prio_ = prio_ || 0;
this.number_ = number_ !== undefined && number_ !== null ? number_ : 0;
this.toString = function () {
switch (this.type_) {
case TNUMBER:
return this.number_;
case TOP1:
case TOP2:
case TVAR:
return this.index_;
case TFUNCALL:
return "CALL";
default:
return "Invalid Token";
}
};
}
function Expression(tokens, ops1, ops2, functions) {
this.tokens = tokens;
this.ops1 = ops1;
this.ops2 = ops2;
this.functions = functions;
}
// Based on http://www.json.org/json2.js
let cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
escapable = /[\\\'\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
meta = {
// table of character substitutions
"\b": "\\b",
"\t": "\\t",
"\n": "\\n",
"\f": "\\f",
"\r": "\\r",
"'": "\\'",
"\\": "\\\\",
};
/*
function escapeValue(v) {
if (typeof v === "string") {
escapable.lastIndex = 0;
return escapable.test(v)
? "'" +
v.replace(escapable, function (a) {
let c = meta[a];
return typeof c === "string"
? c
: "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
}) +
"'"
: "'" + v + "'";
}
return v;
}
*/
Expression.prototype = {
/*
simplify: function (values) {
values = values || {};
let nstack = [];
let newexpression = [];
let n1;
let n2;
let f;
let L = this.tokens.length;
let item;
let i = 0;
for (i = 0; i < L; i++) {
item = this.tokens[i];
let type_ = item.type_;
if (type_ === TNUMBER) {
nstack.push(item);
} else if (type_ === Tlet && item.index_ in values) {
item = new Token(TNUMBER, 0, 0, values[item.index_]);
nstack.push(item);
} else if (type_ === TOP2 && nstack.length > 1) {
n2 = nstack.pop();
n1 = nstack.pop();
f = this.ops2[item.index_];
item = new Token(TNUMBER, 0, 0, f(n1.number_, n2.number_));
nstack.push(item);
} else if (type_ === TOP1 && nstack.length > 0) {
n1 = nstack.pop();
f = this.ops1[item.index_];
item = new Token(TNUMBER, 0, 0, f(n1.number_));
nstack.push(item);
} else {
while (nstack.length > 0) {
newexpression.push(nstack.shift());
}
newexpression.push(item);
}
}
while (nstack.length > 0) {
newexpression.push(nstack.shift());
}
return new Expression(
newexpression,
object(this.ops1),
object(this.ops2),
object(this.functions)
);
},
substitute: function (variable, expr) {
if (!(expr instanceof Expression)) {
expr = new Parser().parse(String(expr));
}
let newexpression = [];
let L = this.tokens.length;
let item;
let i = 0;
for (i = 0; i < L; i++) {
item = this.tokens[i];
let type_ = item.type_;
if (type_ === Tlet && item.index_ === variable) {
for (let j = 0; j < expr.tokens.length; j++) {
let expritem = expr.tokens[j];
let replitem = new Token(
expritem.type_,
expritem.index_,
expritem.prio_,
expritem.number_
);
newexpression.push(replitem);
}
} else {
newexpression.push(item);
}
}
let ret = new Expression(
newexpression,
object(this.ops1),
object(this.ops2),
object(this.functions)
);
return ret;
},
*/
evaluate: function (values) {
values = values || {};
let nstack = [];
let n1;
let n2;
let f;
let L = this.tokens.length;
let item;
let i = 0;
//console.log("EVAL2", this.tokens);
for (i = 0; i < L; i++) {
item = this.tokens[i];
let type_ = item.type_;
if (type_ === TNUMBER) {
nstack.push(item.number_);
} else if (type_ === TOP2) {
n2 = nstack.pop();
n1 = nstack.pop();
f = this.ops2[item.index_];
nstack.push(f(n1, n2));
} else if (type_ === TVAR) {
item.index_ = item.index_.toUpperCase();
if (item.index_[0] === "<") {
if (item.index_.substr(1) in values) {
nstack.push(values[item.index_.substr(1)] % 256);
}
} else if (item.index_[0] === ">") {
if (item.index_.substr(1) in values) {
nstack.push(Math.floor(values[item.index_.substr(1)] / 256));
}
} else if (item.index_ in values) {
nstack.push(values[item.index_]);
} else if (item.index_ in this.functions) {
nstack.push(this.functions[item.index_]);
} else {
throw {msg: "undefined variable: " + item.index_};
}
} else if (type_ === TOP1) {
n1 = nstack.pop();
f = this.ops1[item.index_];
nstack.push(f(n1));
} else if (type_ === TFUNCALL) {
n1 = nstack.pop();
f = nstack.pop();
if (f.apply && f.call) {
if (Object.prototype.toString.call(n1) == "[object Array]") {
nstack.push(f.apply(undefined, n1));
} else {
nstack.push(f.call(undefined, n1));
}
} else {
throw {msg: f + " is not a function"};
}
} else {
throw {msg: "invalid Expression"};
}
}
if (nstack.length > 1) {
throw {msg: "invalid Expression (parity)"};
}
let ev = nstack[0];
let pragmas = values.__PRAGMAS;
//console.log(pragmas)
if (pragmas && typeof ev == "number") {
if (pragmas.indexOf("ROUNDFLOAT") >= 0) ev = Math.round(ev);
if (pragmas.indexOf("FLOAT") >= 0) return ev;
if (pragmas.indexOf("NOFLOAT") >= 0) return parseInt(ev);
}
if (typeof ev == "number") ev = parseInt(ev);
//console.log(nstack, this.tokens, this, values);
return ev;
},
usage: function (values) {
values = values || {};
let xref = [];
let nstack = [];
let n1;
let n2;
let f;
let L = this.tokens.length;
let item;
let i = 0;
for (i = 0; i < L; i++) {
item = this.tokens[i];
let type_ = item.type_;
if (type_ === TNUMBER) {
nstack.push(item.number_);
} else if (type_ === TOP2) {
n2 = nstack.pop();
n1 = nstack.pop();
f = this.ops2[item.index_];
nstack.push(f(n1, n2));
} else if (type_ === TVAR) {
item.index_ = item.index_.toUpperCase();
if (item.index_[0] === "<") {
if (item.index_.substr(1) in values) {
nstack.push(values[item.index_.substr(1)] % 256);
xref.push(item.index_.substr(1));
}
} else if (item.index_[0] === ">") {
if (item.index_.substr(1) in values) {
nstack.push(Math.floor(values[item.index_.substr(1)] / 256));
xref.push(item.index_.substr(1));
}
} else if (item.index_ in values) {
nstack.push(values[item.index_]);
xref.push(item.index_);
} else if (item.index_ in this.functions) {
nstack.push(this.functions[item.index_]);
xref.push(item.index_);
} else {
throw {msg: "undefined variable: " + item.index_};
}
} else if (type_ === TOP1) {
n1 = nstack.pop();
f = this.ops1[item.index_];
nstack.push(f(n1));
} else if (type_ === TFUNCALL) {
n1 = nstack.pop();
f = nstack.pop();
if (f.apply && f.call) {
if (Object.prototype.toString.call(n1) == "[object Array]") {
nstack.push(f.apply(undefined, n1));
} else {
nstack.push(f.call(undefined, n1));
}
} else {
throw {msg: f + " is not a function"};
}
} else {
throw {msg: "invalid Expression"};
}
}
if (nstack.length > 1) {
throw {msg: "invalid Expression (parity)"};
}
return xref;
},
/*
toString: function (toJS) {
let nstack = [];
let n1;
let n2;
let f;
let L = this.tokens.length;
let item;
let i = 0;
for (i = 0; i < L; i++) {
item = this.tokens[i];
let type_ = item.type_;
if (type_ === TNUMBER) {
nstack.push(escapeValue(item.number_));
} else if (type_ === TOP2) {
n2 = nstack.pop();
n1 = nstack.pop();
f = item.index_;
if (toJS && f == "^") {
nstack.push("Math.pow(" + n1 + "," + n2 + ")");
} else {
nstack.push("(" + n1 + f + n2 + ")");
}
} else if (type_ === TVAR) {
nstack.push(item.index_);
} else if (type_ === TOP1) {
n1 = nstack.pop();
f = item.index_;
if (f === "-") {
nstack.push("(" + f + n1 + ")");
} else {
nstack.push(f + "(" + n1 + ")");
}
} else if (type_ === TFUNCALL) {
n1 = nstack.pop();
f = nstack.pop();
nstack.push(f + "(" + n1 + ")");
} else {
throw {msg: "invalid Expression"};
}
}
if (nstack.length > 1) {
throw {msg: "invalid Expression (parity)"};
}
return nstack[0];
},
*/
/*
variables: function () {
let L = this.tokens.length;
let vars = [];
for (let i = 0; i < L; i++) {
let item = this.tokens[i];
if (item.type_ === Tlet && vars.indexOf(item.index_) == -1) {
vars.push(item.index_);
}
}
return vars;
},
*/
/*
toJSFunction: function (param, variables) {
let f = new Function(
param,
"with(Parser.values) { return " +
this.simplify(variables).toString(true) +
"; }"
);
return f;
},
*/
};
function stringCode(s) {
let o = 0;
for (let i = 0; i < s.length; i++) {
o *= 256;
o += s.charCodeAt(i);
}
return o;
}
function add(a, b) {
if (typeof a == "string") {
a = stringCode(a);
}
if (typeof b == "string") {
b = stringCode(b);
}
return Number(a) + Number(b);
}
function fand(a, b) {
return Number(a) & Number(b);
}
function fnebo(a, b) {
return Number(a) | Number(b);
}
function fbequ(a, b) {
return Number(a) == Number(b) ? 1 : 0;
}
function fbnequ(a, b) {
return Number(a) == Number(b) ? 0 : 1;
}
function fblt(a, b) {
return Number(a) < Number(b) ? 1 : 0;
}
function fbgt(a, b) {
return Number(a) > Number(b) ? 1 : 0;
}
function fble(a, b) {
return Number(a) <= Number(b) ? 1 : 0;
}
function fbge(a, b) {
return Number(a) >= Number(b) ? 1 : 0;
}
function sub(a, b) {
if (typeof a == "string") {
a = stringCode(a);
}
if (typeof b == "string") {
b = stringCode(b);
}
return Number(a) - Number(b);
}
function mul(a, b) {
if (typeof a == "string") {
let out = "";
for (let l = 0; l < b; l++) out += a;
return out;
}
return a * b;
}
function div(a, b) {
return a / b;
}
function mod(a, b) {
return a % b;
}
function concat(a, b) {
return "" + a + b;
}
function neg(a) {
return -a;
}
function random(a) {
return Math.random() * (a || 1);
}
function fac(a) {
//a!
a = Math.floor(a);
let b = a;
while (a > 1) {
b = b * --a;
}
return b;
}
// TODO: use hypot that doesn't overflow
function pyt(a, b) {
return Math.sqrt(a * a + b * b);
}
function near(d) {
//let d = x[0]-x[1];
if (d > 127) return 0;
if (d < -128) return 0;
return 1;
}
function append(a, b) {
if (Object.prototype.toString.call(a) != "[object Array]") {
return [a, b];
}
a = a.slice();
a.push(b);
return a;
}
function lsb(a) {
return a % 256;
}
function msb(a) {
return (a >> 8) & 0xff;
}
export function Parser() {
this.success = false;
this.errormsg = "";
this.expression = "";
this.pos = 0;
this.tokennumber = 0;
this.tokenprio = 0;
this.tokenindex = 0;
this.tmpprio = 0;
this.ops1 = {
//"lsb": function(x){Math.floor(x%256);},
lsb: lsb,
msb: msb,
sin: Math.sin,
cos: Math.cos,
tan: Math.tan,
asin: Math.asin,
acos: Math.acos,
atan: Math.atan,
sqrt: Math.sqrt,
log: Math.log,
abs: Math.abs,
ceil: Math.ceil,
floor: Math.floor,
round: Math.round,
isnear: near,
"-": neg,
exp: Math.exp,
};
this.ops2 = {
"+": add,
"-": sub,
"*": mul,
"/": div,
"%": mod,
"#": mod,
"^": Math.pow,
",": append,
"=": fbequ,
"!=": fbnequ,
"<": fblt,
">": fbgt,
"<=": fble,
">=": fbge,
"&": fand,
"|": fnebo,
"||": concat,
};
this.functions = {
random: random,
fac: fac,
min: Math.min,
max: Math.max,
pyt: pyt,
isnear: near,
pow: Math.pow,
atan2: Math.atan2,
};
this.consts = {
//"E": Math.E,
//"PI": Math.PI
};
}
Parser.parse = function (expr) {
return new Parser().parse(expr);
};
Parser.usage = function (expr, variables) {
return Parser.parse(expr).usage(variables);
};
Parser.evaluate = function (expr, variables) {
//console.log(Parser.parse(expr));
return Parser.parse(expr).evaluate(variables);
};
Parser.Expression = Expression;
Parser.values = {
lsb: function (x) {
Math.floor(x % 256);
},
msb: function (x) {
Math.floor(x / 256);
},
sin: Math.sin,
cos: Math.cos,
tan: Math.tan,
asin: Math.asin,
acos: Math.acos,
atan: Math.atan,
sqrt: Math.sqrt,
log: Math.log,
abs: Math.abs,
ceil: Math.ceil,
floor: Math.floor,
round: Math.round,
random: random,
fac: fac,
exp: Math.exp,
min: Math.min,
max: Math.max,
pyt: pyt,
isnear: near,
pow: Math.pow,
atan2: Math.atan2,
E: Math.E,
PI: Math.PI,
};
let PRIMARY = 1 << 0;
let OPERATOR = 1 << 1;
let FUNCTION = 1 << 2;
let LPAREN = 1 << 3;
let RPAREN = 1 << 4;
let COMMA = 1 << 5;
let SIGN = 1 << 6;
let CALL = 1 << 7;
let NULLARY_CALL = 1 << 8;
Parser.prototype = {
parse: function (expr) {
this.errormsg = "";
this.success = true;
let operstack = [];
let tokenstack = [];
this.tmpprio = 0;
let expected = PRIMARY | LPAREN | FUNCTION | SIGN;
let noperators = 0;
this.expression = expr;
this.pos = 0;
if (!this.expression)
throw {msg: "Empty expression, probably missing argument"};
while (this.pos < this.expression.length) {
if (this.isNumber()) {
if ((expected & PRIMARY) === 0) {
this.error_parsing(this.pos, "unexpected number");
}
let token = new Token(TNUMBER, 0, 0, this.tokennumber);
tokenstack.push(token);
expected = OPERATOR | RPAREN | COMMA;
} else if (this.isOperator()) {
if (this.isSign() && expected & SIGN) {
if (this.isNegativeSign()) {
this.tokenprio = 2;
this.tokenindex = "-";
noperators++;
this.addfunc(tokenstack, operstack, TOP1);
}
expected = PRIMARY | LPAREN | FUNCTION | SIGN;
} else if (this.isComment()) {
} else {
if ((expected & OPERATOR) === 0) {
this.error_parsing(this.pos, "unexpected operator");
}
noperators += 2;
this.addfunc(tokenstack, operstack, TOP2);
expected = PRIMARY | LPAREN | FUNCTION | SIGN;
}
} else if (this.isString()) {
if ((expected & PRIMARY) === 0) {
this.error_parsing(this.pos, "unexpected string");
}
let token = new Token(TNUMBER, 0, 0, this.tokennumber);
tokenstack.push(token);
expected = OPERATOR | RPAREN | COMMA;
} else if (this.isLeftParenth()) {
if ((expected & LPAREN) === 0) {
this.error_parsing(this.pos, 'unexpected "("');
}
if (expected & CALL) {
noperators += 2;
this.tokenprio = -2;
this.tokenindex = -1;
this.addfunc(tokenstack, operstack, TFUNCALL);
}
expected = PRIMARY | LPAREN | FUNCTION | SIGN | NULLARY_CALL;
} else if (this.isRightParenth()) {
if (expected & NULLARY_CALL) {
let token = new Token(TNUMBER, 0, 0, []);
tokenstack.push(token);
} else if ((expected & RPAREN) === 0) {
this.error_parsing(this.pos, 'unexpected ")"');
}
expected = OPERATOR | RPAREN | COMMA | LPAREN | CALL;
} else if (this.isComma()) {
if ((expected & COMMA) === 0) {
this.error_parsing(this.pos, 'unexpected ","');
}
this.addfunc(tokenstack, operstack, TOP2);
noperators += 2;
expected = PRIMARY | LPAREN | FUNCTION | SIGN;
} else if (this.isConst()) {
if ((expected & PRIMARY) === 0) {
this.error_parsing(this.pos, "unexpected constant");
}
let consttoken = new Token(TNUMBER, 0, 0, this.tokennumber);
tokenstack.push(consttoken);
expected = OPERATOR | RPAREN | COMMA;
} else if (this.isOp2()) {
if ((expected & FUNCTION) === 0) {
this.error_parsing(this.pos, "unexpected function");
}
this.addfunc(tokenstack, operstack, TOP2);
noperators += 2;
expected = LPAREN;
} else if (this.isOp1()) {
if ((expected & FUNCTION) === 0) {
this.error_parsing(this.pos, "unexpected function");
}
this.addfunc(tokenstack, operstack, TOP1);
noperators++;
expected = LPAREN;
} else if (this.isVar()) {
if ((expected & PRIMARY) === 0) {
this.error_parsing(this.pos, "unexpected variable");
}
let vartoken = new Token(TVAR, this.tokenindex, 0, 0);
tokenstack.push(vartoken);
expected = OPERATOR | RPAREN | COMMA | LPAREN | CALL;
} else if (this.isWhite()) {
} else {
if (this.errormsg === "") {
this.error_parsing(
this.pos,
"unknown character in " + this.expression
);
} else {
this.error_parsing(this.pos, this.errormsg);
}
}
}
if (this.tmpprio < 0 || this.tmpprio >= 10) {
this.error_parsing(this.pos, 'unmatched "()"');
}
while (operstack.length > 0) {
let tmp = operstack.pop();
tokenstack.push(tmp);
}
if (noperators + 1 !== tokenstack.length) {
//print(noperators + 1);
//print(tokenstack);
this.error_parsing(this.pos, "parity");
}
return new Expression(
tokenstack,
object(this.ops1),
object(this.ops2),
object(this.functions)
);
},
evaluate: function (expr, variables) {
//console.log(this.parse(expr));
let value = this.parse(expr).evaluate(variables);
return value;
},
error_parsing: function (column, msg) {
this.success = false;
this.errormsg = "parse error [column " + column + "]: " + msg;
throw {msg: this.errormsg};
},
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
addfunc: function (tokenstack, operstack, type_) {
let operator = new Token(
type_,
this.tokenindex,
this.tokenprio + this.tmpprio,
0
);
while (operstack.length > 0) {
if (operator.prio_ <= operstack[operstack.length - 1].prio_) {
tokenstack.push(operstack.pop());
} else {
break;
}
}
operstack.push(operator);
},
isNumber: function () {
let r = false;
let str = "";
let firstok = 0;
let firstcode = 0;
let base = 10;
let shouldbehex = false;
let bakpos = this.pos;
let strx;
while (this.pos < this.expression.length) {
let code = this.expression.charCodeAt(this.pos);
//console.log(this.pos, code, firstok);
if (firstok === 0) firstcode = code;
if (
(code >= 48 && code <= 57) ||
code === 46 ||
(firstok === 0 && code === 36) || //$1123
(firstok === 0 && code === 37) || //%11010
(firstok === 1 && code === 88 && firstcode === 48) || //0X
(firstok === 1 && code === 120 && firstcode === 48) ||
(firstok > 0 && code === 72) || //...H
(firstok > 0 && code === 104) || //...h
(firstok > 0 && code === 66) || //...B
(firstok > 0 && code === 98) || //...b
(firstok > 0 && code === 81) || //...Q
(firstok > 0 && code === 113) || //...q
(firstok > 0 && code === 79) || //...O
(firstok > 0 && code === 111) || //...o
(firstok > 0 && code >= 65 && code <= 70) ||
(firstok > 0 && code >= 97 && code <= 102)
) {
if (
((firstok > 0 && code >= 65 && code <= 70) ||
(firstok > 0 && code >= 97 && code <= 102)) &&
!(base === 16)
) {
shouldbehex = true;
}
firstok++;
str += this.expression.charAt(this.pos);
this.pos++;
//num syntax fixes
strx = str;
if (str[0] === "$") {
strx = "0x" + str.substr(1);
base = 16;
}
if (str[1] === "x" || str[1] === "X") {
base = 16;
}
if (str[str.length - 1] === "h" || str[str.length - 1] === "H") {
if (base == 10 || base == 2) {
strx = "0x" + str.substr(0, str.length - 1);
base = 16;
}
}
if (str[str.length - 1] === "b" || str[str.length - 1] === "B") {
if (base == 10) {
strx = str.substr(0, str.length - 1);
base = 2;
}
}
if (
str[str.length - 1] === "q" ||
str[str.length - 1] === "Q" ||
str[str.length - 1] === "o" ||
str[str.length - 1] === "O"
) {
if (base == 10) {
strx = str.substr(0, str.length - 1);
base = 8;
}
}
/*
if (str[0] === "%") {
console.log("OOO", str, strx)
if (str.length < 2) {
this.pos = bakpos;
return false;
}
strx = str.substr(1);
base = 2;
}
*/
if (base != 10) this.tokennumber = parseInt(strx, base);
else this.tokennumber = parseFloat(strx);
r = true;
} else {
break;
}
}
if (str[0] === "%") {
//console.log("OOO", str, strx)
if (str.length < 2) {
this.pos = bakpos;
return false;
}
strx = str.substr(1);
this.tokennumber = parseInt(strx, 2);
}
//console.log(shouldbehex, base);
if (shouldbehex && base === 2) {
shouldbehex = false;
}
if (shouldbehex && base !== 16) {
this.pos = bakpos;
return false;
}
if (strx === "0x") {
this.pos = bakpos;
return false;
}
return r;
},
// Ported from the yajjl JSON parser at http://code.google.com/p/yajjl/
unescape: function (v, pos) {
let buffer = [];
let escaping = false;
for (let i = 0; i < v.length; i++) {
let c = v.charAt(i);
if (escaping) {
switch (c) {
case "'":
buffer.push("'");
break;
case "\\":
buffer.push("\\");
break;
case "/":
buffer.push("/");
break;
case "b":
buffer.push("\b");
break;
case "f":
buffer.push("\f");
break;
case "n":
buffer.push("\n");
break;
case "r":
buffer.push("\r");
break;
case "t":
buffer.push("\t");
break;
case "u":
// interpret the following 4 characters as the hex of the unicode code point
let codePoint = parseInt(v.substring(i + 1, i + 5), 16);
buffer.push(String.fromCharCode(codePoint));
i += 4;
break;
default:
throw this.error_parsing(
pos + i,
"Illegal escape sequence: '\\" + c + "'"
);
}
escaping = false;
} else {
if (c == "\\") {
escaping = true;
} else {
buffer.push(c);
}
}
}
return buffer.join("");
},
isString: function () {
let r = false;
let str = "";
let startpos = this.pos;
if (
(this.pos < this.expression.length &&
this.expression.charAt(this.pos) == "'") ||
this.expression.charAt(this.pos) == '"'
) {
let delim = this.expression.charAt(this.pos);
this.pos++;
while (this.pos < this.expression.length) {
let code = this.expression.charAt(this.pos);
if (code != delim || str.slice(-1) == "\\") {
str += this.expression.charAt(this.pos);
this.pos++;
} else {
this.pos++;
this.tokennumber = this.unescape(str, startpos);
r = true;
break;
}
}
}
return r;
},
isConst: function () {
return false; //false positive on E, PI
/*
let str;
for (let i in this.consts) {
if (true) {
let L = i.length;
str = this.expression.substr(this.pos, L);
if (i === str) {
this.tokennumber = this.consts[i];
this.pos += L;
return true;
}
}
}
return false;
*/
},
isOperator: function () {
let code = this.expression.charCodeAt(this.pos);
if (code === 43) {
// +
this.tokenprio = 0;
this.tokenindex = "+";
} else if (code === 45) {
// -
this.tokenprio = 0;
this.tokenindex = "-";
} else if (code === 124) {
// |
if (this.expression.charCodeAt(this.pos + 1) === 124) {
this.pos++;
this.tokenprio = 0;
this.tokenindex = "||";
} else {
//return false;
this.tokenprio = 5;
this.tokenindex = "|";
}
} else if (code === 42) {
// *
this.tokenprio = 1;
this.tokenindex = "*";
} else if (code === 47) {
// /
this.tokenprio = 2;
this.tokenindex = "/";
} else if (code === 37) {
// %
this.tokenprio = 2;
this.tokenindex = "%";
} else if (code === 35) {
// #
this.tokenprio = 2;
this.tokenindex = "#";
} else if (code === 94) {
// ^
this.tokenprio = 3;
this.tokenindex = "^";
} else if (code === 38) {
// &
this.tokenprio = 4;
this.tokenindex = "&";
} else if (code === 61) {
// =
this.tokenprio = -1;
this.tokenindex = "=";
} else if (code === 33) {
// !
if (this.expression.charCodeAt(this.pos + 1) === 61) {
this.pos++;
this.tokenprio = -1;
this.tokenindex = "!=";
} else {
//return false;
this.tokenprio = 5;
this.tokenindex = "!";
}
} else if (code === 63) {
// ?
if (this.expression.charCodeAt(this.pos + 1) === 60) {
// <
this.pos++;
if (this.expression.charCodeAt(this.pos + 1) === 61) {
this.pos++;
this.tokenprio = -1;
this.tokenindex = "<=";
} else {
this.tokenprio = -1;
this.tokenindex = "<";
}
}
if (this.expression.charCodeAt(this.pos + 1) === 62) {
// >
this.pos++;
if (this.expression.charCodeAt(this.pos + 1) === 61) {
this.pos++;
this.tokenprio = -1;
this.tokenindex = ">=";
} else {
this.tokenprio = -1;
this.tokenindex = ">";
}
}
} else {
/*
else if (code === 60) { // <
if (this.expression.charCodeAt(this.pos + 1) === 61) {
this.pos++;
this.tokenprio = -1;
this.tokenindex = "<=";
}
else {
this.tokenprio = -1;
this.tokenindex = "<";
}
}
*/
return false;
}
this.pos++;
return true;
},
isSign: function () {
let code = this.expression.charCodeAt(this.pos - 1);
if (code === 45 || code === 43) {
// -
return true;
}
return false;
},
/*
isPositiveSign: function () {
let code = this.expression.charCodeAt(this.pos - 1);
if (code === 43) {
// -
return true;
}
return false;
},
*/
isNegativeSign: function () {
let code = this.expression.charCodeAt(this.pos - 1);
if (code === 45) {
// -
return true;
}
return false;
},
isLeftParenth: function () {
let code = this.expression.charCodeAt(this.pos);
if (code === 40) {
// (
this.pos++;
this.tmpprio += 10;
return true;
}
return false;
},
isRightParenth: function () {
let code = this.expression.charCodeAt(this.pos);
if (code === 41) {
// )
this.pos++;
this.tmpprio -= 10;
return true;
}
return false;
},
isComma: function () {
let code = this.expression.charCodeAt(this.pos);
if (code === 44) {
// ,
this.pos++;
this.tokenprio = -1;
this.tokenindex = ",";
return true;
}
return false;
},
isWhite: function () {
let code = this.expression.charCodeAt(this.pos);
if (code === 32 || code === 9 || code === 10 || code === 13) {
this.pos++;
return true;
}
return false;
},
isOp1: function () {
let str = "";
for (let i = this.pos; i < this.expression.length; i++) {
let c = this.expression.charAt(i);
if (c.toUpperCase() === c.toLowerCase()) {
if (i === this.pos || (c != "_" && (c < "0" || c > "9"))) {
break;
}
}
str += c;
}
if (str.length > 0 && str in this.ops1) {
this.tokenindex = str;
this.tokenprio = 5;
this.pos += str.length;
return true;
}
return false;
},
isOp2: function () {
let str = "";
for (let i = this.pos; i < this.expression.length; i++) {
let c = this.expression.charAt(i);
if (c.toUpperCase() === c.toLowerCase()) {
if (i === this.pos || (c != "_" && (c < "0" || c > "9"))) {
break;
}
}
str += c;
}
if (str.length > 0 && str in this.ops2) {
this.tokenindex = str;
this.tokenprio = 5;
this.pos += str.length;
return true;
}
return false;
},
isVar: function () {
let str = "";
for (let i = this.pos; i < this.expression.length; i++) {
let c = this.expression.charAt(i);
if (c === "$") {
str = "_PC";
break;
}
if (c.toUpperCase() === c.toLowerCase() && c !== "<" && c !== ">") {
if (i === this.pos || (c != "_" && (c < "0" || c > "9"))) {
break;
}
}
str += c;
}
if (str.length > 0) {
this.tokenindex = str;
this.tokenprio = 4;
if (str !== "_PC") {
this.pos += str.length;
} else {
this.pos++;
}
return true;
}
return false;
},
isComment: function () {
let code = this.expression.charCodeAt(this.pos - 1);
if (code === 47 && this.expression.charCodeAt(this.pos) === 42) {
this.pos = this.expression.indexOf("*/", this.pos) + 2;
if (this.pos === 1) {
this.pos = this.expression.length;
}
return true;
}
return false;
},
};
|