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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "glk/hugo/hugo.h"
namespace Glk {
namespace Hugo {
int Hugo::EvalExpr(int p) {
int n1, n2;
int oper;
short result = 0; /* must be 16 bits */
/* for precedence stacking/unstacking */
int next_prec, this_prec, temp_lp;
if (!evalcount) return 0; /* no expression */
do
{
if (eval[p]==1)
{
if (eval[p+1]==OPEN_BRACKET_T ||
eval[p+1]==OPEN_SQUARE_T)
{
eval[p] = 0;
eval[p+1] = EvalExpr(p+2);
TrimExpr(p+2);
}
else if (eval[p+1]==MINUS_T)
{
TrimExpr(p);
eval[p+1] = -eval[p+1];
}
}
if (evalcount<=p+2)
{
result = eval[p+1];
TrimExpr(p);
eval[p] = 0;
eval[p+1] = result;
goto ReturnResult;
}
n1 = eval[p+1];
oper = eval[p+3];
/* At this point, <n1> holds the first value, and <oper> holds
the token number of the operator.
*/
if (eval[p+4]==1 && (eval[p+5]==OPEN_BRACKET_T
|| eval[p+5]==OPEN_SQUARE_T))
{
eval[p+4] = 0;
eval[p+5] = EvalExpr(p+6);
TrimExpr(p+6);
}
n2 = eval[p+5];
if (evalcount > p+7)
{
if (eval[p+3]==CLOSE_BRACKET_T && eval[p+2]==1)
{
TrimExpr(p+2);
return eval[p+1];
}
/* eval[p+7] holds the next operator, i.e., the "*"
in: "x + y * z"
This way, we can check if the upcoming operator
takes precedence over the current one.
*/
if ((next_prec = Precedence(eval[p+7]))
< (this_prec = Precedence(oper)))
{
if (next_prec >= last_precedence)
{
#if defined (DEBUG_PRECEDENCE)
sprintf(line, "Not preferring %s to %s because of previous level %d", token[eval[p+7]], token[oper], last_precedence);
Printout(line);
#endif
goto ReturnResult;
}
#if defined (DEBUG_PRECEDENCE)
sprintf(line, "Preferring %s to %s", token[eval[p+7]], token[oper]);
Printout(line);
#endif
temp_lp = last_precedence;
last_precedence = this_prec;
n2 = EvalExpr(p+4);
last_precedence = temp_lp;
}
}
else if (Precedence(oper)>=last_precedence)
{
goto ReturnResult;
}
#if defined (DEBUG_PRECEDENCE)
sprintf(line, "Solving %d %s %d", n1, token[oper], n2);
Printout(line);
#endif
switch (oper)
{
case DECIMAL_T:
{
result = GetProp(n1, n2, 1, 0);
break;
}
case EQUALS_T:
{
result = (n1==n2);
break;
}
case MINUS_T:
{
result = n1 - n2;
break;
}
case PLUS_T:
{
result = n1 + n2;
break;
}
case ASTERISK_T:
{
result = n1 * n2;
break;
}
case FORWARD_SLASH_T:
{
if (n2==0)
#if defined (DEBUGGER)
{
RuntimeWarning("Division by zero: invalid result");
result = 0;
}
#else
FatalError(DIVIDE_E);
#endif
result = n1 / n2;
break;
}
case PIPE_T:
{
result = n1 | n2;
break;
}
case GREATER_EQUAL_T:
{
result = (n1>=n2);
break;
}
case LESS_EQUAL_T:
{
result = (n1<=n2);
break;
}
case NOT_EQUAL_T:
{
result = (n1!=n2);
break;
}
case AMPERSAND_T:
{
result = n1 & n2;
break;
}
case GREATER_T:
{
result = (n1 > n2);
break;
}
case LESS_T:
{
result = (n1 < n2);
break;
}
case AND_T:
{
result = (n1 && n2);
break;
}
case OR_T:
{
result = (n1 || n2);
break;
}
default:
{
result = n1;
}
}
#if defined (DEBUGGER)
if ((debug_eval) && debug_eval_error) return 0;
#endif
TrimExpr(p+4); /* second value */
TrimExpr(p+2); /* operator */
eval[p] = 0;
eval[p+1] = result;
/* Keep looping while there are expression elements, or until there
is a ")", "]", or end of line
*/
} while ((evalcount>p+2) && !(eval[p+2]==1 &&
(eval[p+3]==CLOSE_BRACKET_T || eval[p+3]==CLOSE_SQUARE_T ||
eval[p+3]==255)));
result = eval[p+1];
TrimExpr(p); /* first value */
ReturnResult:
#if defined (DEBUG_EXPR_EVAL)
if (p==0 && exprt)
{
Common::sprintf_s(line, " = %d", result);
AP(line);
}
#endif
return result;
}
int Hugo::GetVal() {
char a = 0;
char tempinexpr, tempgetaddress, tempinobj;
int i, j;
int tempret;
unsigned short routineaddr, arrayaddr; /* must be 16 bits */
short val = 0;
char inctype = 0;
int preincdec; /* pre-increment/decrement */
defseg = gameseg;
tempret = ret;
tempinexpr = inexpr;
inexpr = 0;
preincdec = incdec;
incdec = 0;
switch (MEM(codeptr))
{
case AMPERSAND_T: /* an address */
{codeptr++;
getaddress = true;
val = GetValue();
getaddress = false;
break;}
case ROUTINE_T:
case CALL_T:
{
if (MEM(codeptr)==ROUTINE_T)
{
if (tail_recursion==0 && MEM(codeptr-1)==RETURN_T)
{
/* We may be able to tail-recurse this return
statement if it's simply 'return Routine(...)'
*/
tail_recursion = TAIL_RECURSION_ROUTINE;
}
routineaddr = PeekWord(++codeptr);
codeptr += 2;
if (getaddress)
{val = routineaddr;
getaddress = false;
break;}
}
else
{
codeptr++;
routineaddr = GetValue();
}
#if defined (DEBUGGER)
if (debug_eval)
{
debug_eval_error = true;
val = 0;
break;
}
#endif
val = CallRoutine(routineaddr);
break;
}
case OPEN_BRACKET_T:
{
codeptr++;
inexpr = 1;
tempgetaddress = getaddress;
getaddress = false;
SetupExpr();
inexpr = 0;
val = EvalExpr(0);
getaddress = tempgetaddress;
break;
}
case MINUS_T:
{
codeptr++;
j = inexpr; /* don't reuse tempinexpr */
inexpr = 1;
val = -GetValue();
inexpr = (char)j;
break;
}
case VALUE_T: /* integer 0 - 65535 */
case OBJECTNUM_T:
case DICTENTRY_T:
{
val = PeekWord(++codeptr);
codeptr += 2;
break;
}
case ATTR_T:
case PROP_T:
{
val = MEM(++codeptr);
codeptr++;
break;
}
case VAR_T: /* variable */
{
val = var[(i=MEM(++codeptr))];
if (game_version >= 22)
{
/* Pre-v2.4 included linelength and pagelength as
global variables after objectcount
*/
if (i <= ((game_version>=24)?objectcount:objectcount+2))
{
if (i==wordcount) val = words;
else if (i==objectcount) val = objects;
/* i.e., pre-v2.4 only */
else if (i==objectcount+1)
{
#if defined (ACTUAL_LINELENGTH)
val = ACTUAL_LINELENGTH();
#else
val = SCREENWIDTH/charwidth;
#endif
}
else if (i==objectcount+2)
val = SCREENHEIGHT/lineheight;
}
}
codeptr++;
if (!inobj) inctype = IsIncrement(codeptr);
/* don't operate on, e.g., ++variable.property as
(++variable).property
*/
if ((incdec || preincdec) && MEM(codeptr)!=DECIMAL_T)
{
if (i < MAXGLOBALS) SaveUndo(VAR_T, i, val, 0, 0);
if (inctype) val = Increment(val, inctype);
/* still a post-increment hanging around */
var[i] = (val+=preincdec) + incdec;
incdec = preincdec = 0;
}
break;
}
case TRUE_T:
val = 1;
codeptr++;
break;
case FALSE_T:
val = 0;
codeptr++;
break;
case TILDE_T:
codeptr++;
val = ~GetValue();
break;
case NOT_T:
codeptr++;
val = !GetValue();
break;
case ARRAYDATA_T:
case ARRAY_T:
{
unsigned int element;
if (MEM(codeptr)==ARRAY_T)
{
codeptr++;
arrayaddr = GetValue();
}
else
{
arrayaddr = PeekWord(++codeptr);
codeptr += 2;
}
if (MEM(codeptr)!=OPEN_SQUARE_T)
{val = arrayaddr;
break;}
if (game_version>=22)
{
/* convert to word value */
arrayaddr*=2;
if (game_version>=23)
/* space for array length */
a = 2;
}
/* check if this is array[] (i.e., array length) */
if (MEM(++codeptr)==CLOSE_SQUARE_T)
{
defseg = arraytable;
val = PeekWord(arrayaddr);
codeptr++;
break;
}
tempinobj = inobj;
inobj = 0;
j = GetValue();
inobj = tempinobj;
/* The array element we're after: */
element = arrayaddr+a + j*2;
defseg = arraytable;
#if defined (DEBUGGER)
CheckinRange(element, debug_workspace, "array data");
#endif
/* Check to make sure we've got a sane element number */
if ((element>0) && (element < (unsigned int)(dicttable-arraytable)*16))
val = PeekWord(element);
else
val = 0;
codeptr++;
if (!inobj) inctype = IsIncrement(codeptr);
/* Don't operate on the array on:
++a[n].property
*/
if ((incdec || preincdec) && MEM(codeptr)!=DECIMAL_T)
{
/* Same sanity check for element number */
if ((element>0) && (element < (unsigned)(dicttable-arraytable)*16))
{
if (inctype) val = Increment(val, inctype);
/* still a post-increment hanging around */
SaveUndo(ARRAYDATA_T, arrayaddr+a, j, val, 0);
PokeWord(element, (val+=preincdec) + incdec);
incdec = preincdec = 0;
}
}
break;
}
case RANDOM_T:
{
codeptr += 2; /* skip the "(" */
val = GetValue();
if (val!=0)
#if !defined (RANDOM)
val = (hugo_rand() % val)+1;
#else
val = (RANDOM() % val)+1;
#endif
if (MEM(codeptr)==2) codeptr++;
break;
}
case WORD_T:
{
codeptr += 2; /* skip the "[" */
if (MEM(codeptr)==CLOSE_SQUARE_T) /* words[] */
{
val = words;
break;
}
val = wd[GetValue()];
if (MEM(codeptr)==CLOSE_SQUARE_T) codeptr++;
break;
}
case CHILDREN_T:
{
codeptr += 2; /* skip the "(" */
val = GetValue();
if (MEM(codeptr)==CLOSE_BRACKET_T) codeptr++;
val = Children(val);
break;
}
case PARENT_T:
case SIBLING_T:
case CHILD_T:
case YOUNGEST_T:
case ELDEST_T:
case YOUNGER_T:
case ELDER_T:
{
i = MEM(codeptr);
codeptr += 2; /* skip the "(" */
val = GetValue();
if (MEM(codeptr)==CLOSE_BRACKET_T) codeptr++;
switch (i)
{
case PARENT_T:
val = Parent(val);
break;
case SIBLING_T:
case YOUNGER_T:
val = Sibling(val);
break;
case CHILD_T:
case ELDEST_T:
val = Child(val);
break;
case YOUNGEST_T:
val = Youngest(val);
break;
case ELDER_T:
val = Elder(val);
break;
}
break;
}
case SAVE_T:
val = RunSave();
codeptr++;
break;
case RESTORE_T:
val = RunRestore();
codeptr++;
break;
case SCRIPTON_T:
case SCRIPTOFF_T:
val = RunScriptSet();
codeptr++;
break;
case RESTART_T:
val = RunRestart();
codeptr++;
break;
case STRING_T:
val = RunString();
break;
case UNDO_T:
val = Undo();
codeptr++;
break;
case DICT_T:
val = Dict();
if (MEM(codeptr)==CLOSE_BRACKET_T) codeptr++;
break;
case RECORDON_T:
case RECORDOFF_T:
case PLAYBACK_T:
val = RecordCommands();
codeptr++;
break;
case READVAL_T:
{
val = 0;
if (ioblock)
{
#ifdef TODO
int low, high;
if ((ioblock==1)
|| (low = hugo_fgetc(io))==EOF
|| (high = hugo_fgetc(io))==EOF)
{
ioerror = true;
retflag = true;
}
else val = low + high*256;
#else
error("TODO: file io");
#endif
}
codeptr++;
break;
}
case PARSE_T:
{
val = (short)PARSE_STRING_VAL;
codeptr++;
break;
}
case SERIAL_T:
{
val = (short)SERIAL_STRING_VAL;
codeptr++;
break;
}
case SYSTEM_T:
{
val = RunSystem();
codeptr++;
break;
}
default:
{
#if defined (DEBUGGER)
if (debug_eval)
debug_eval_error = true;
else
#endif
FatalError(EXPECT_VAL_E);
#if defined (DEBUGGER)
runtime_error = true;
codeptr++;
return 0;
#endif
}
}
defseg = gameseg;
ret = tempret;
inexpr = tempinexpr;
incdec = preincdec;
return val;
}
int Hugo::GetValue() {
char noself = 0;
int p, n;
char inctype; int preincdec;
int nattr = 0, attr;
unsigned int pa, val;
long tempptr;
short g; /* must be 16 bits */
int potential_tail_recursion = 0;
/* Check to see if this may be a valid tail-recursion */
if (tail_recursion==0 && MEM(codeptr-1)==RETURN_T)
{
/* We may be able to tail-recurse this return statement if
it's simply 'return object.property[.property...]'
*/
potential_tail_recursion = TAIL_RECURSION_PROPERTY;
}
IsIncrement(codeptr); /* check for ++, -- */
tempptr = codeptr;
g = GetVal();
preincdec = incdec;
incdec = 0;
if (inobj==0)
{
switch (MEM(codeptr))
{
case DECIMAL_T: /* object.property */
{
DetermineProperty:
if (MEM(++codeptr)==DECIMAL_T) /* object..property */
{
noself = true;
codeptr++;
}
if (MEM(codeptr)==POUND_T) /* object.#property */
{
codeptr++;
inobj = true;
p = GetValue();
inobj = false;
pa = PropAddr(g, p, 0);
if (pa)
{
defseg = proptable;
g = Peek(pa + 1);
if (g==PROP_ROUTINE) g = 1;
defseg = gameseg;
}
else
g = 0;
}
else
{
inobj = true;
p = GetValue();
inobj = false;
if (MEM(codeptr) != POUND_T)
n = 1;
else /* object.property #x */
{
codeptr++;
/* Not GetValue(), since that might
botch "obj.property #n is attr"
*/
n = GetVal();
}
/* We checked this at the start of the function, but
GetValue() for the property would've cleared it
*/
tail_recursion = potential_tail_recursion;
val = GetProp(g, p, n, noself);
inctype = IsIncrement(codeptr);
/* Increment/decrement an object.property, although
only if this is the last property in, e.g.,
object.property.property...
*/
if ((incdec || preincdec) && MEM(codeptr)!=DECIMAL_T)
{
SaveUndo(PROP_T, g, p, n, val);
if (inctype) val = Increment(val, inctype);
/* Still a post-increment hanging around */
pa = PropAddr(g, p, 0);
defseg = proptable;
/* Only change it if not a routine */
if (Peek(pa+1)!=PROP_ROUTINE)
PokeWord(pa+n*2, (val+=preincdec)+incdec);
defseg = gameseg;
incdec = preincdec = 0;
}
g = val;
}
if (MEM(codeptr)==IS_T) goto CheckAttribute;
break;
}
case IS_T:
{
CheckAttribute:
if (!inobj)
{
codeptr++;
if (MEM(codeptr)==NOT_T)
{
nattr = 1;
codeptr++;
}
attr = GetValue();
#if defined (DEBUGGER)
CheckinRange((unsigned)attr, (unsigned)attributes, "attribute");
#endif
g = TestAttribute(g, attr, nattr);
break;
}
}
}
switch (MEM(codeptr))
{
/* This comes here (again) in order to process
object.property1.property2...
*/
case DECIMAL_T: goto DetermineProperty;
case NOT_T:
if (!inobj)
{nattr = 1;
codeptr++;}
// fall through
case IN_T:
{
if (!inobj)
{
codeptr++;
p = GetValue(); /* testing parent */
g = (p==Parent(g));
if (nattr)
g = !g;
}
}
}
} /* end of "if (inobj==0)" */
n = MEM(codeptr);
/* See if we have an implicit expression that needs to be
taken as a single value, i.e., "n + 1" where we've just
read n
*/
if (((n>=MINUS_T && n<=PIPE_T) || n==AMPERSAND_T) &&
((!inexpr)) && !inobj)
/*
#if !defined (DEBUGGER)
((!inexpr)) && !inobj)
#else
((!inexpr)) && !inobj && !debug_eval)
#endif
*/
{
inexpr = 2;
codeptr = tempptr;
SetupExpr();
g = EvalExpr(0);
inexpr = 0;
}
/* Not a tail-recursive 'return object.property' */
if (tail_recursion_addr==0)
tail_recursion = 0;
return g;
}
int Hugo::Increment(int a, char inctype) {
short v; /* must be 16 bits */
v = a;
switch (inctype)
{
case MINUS_T: {v -= incdec; break;}
case PLUS_T: {v += incdec; break;}
case ASTERISK_T: {v *= incdec; break;}
case AMPERSAND_T: {v &= incdec; break;}
case PIPE_T: {v |= incdec; break;}
case FORWARD_SLASH_T:
{
#if defined (DEBUGGER)
if (incdec==0)
{
RuntimeWarning("Division by zero: invalid result");
v = 0;
}
else
#endif
v /= incdec;
break;
}
}
if (inctype!=1) incdec = 0;
return v;
}
char Hugo::IsIncrement(long addr) {
unsigned char a, t = 0;
incdec = 0;
switch (a = MEM(addr))
{
case MINUS_T:
case PLUS_T:
case ASTERISK_T:
case FORWARD_SLASH_T:
case AMPERSAND_T:
case PIPE_T:
{
/* ++, -- */
if ((a==MINUS_T || a==PLUS_T) && MEM(addr+1)==a)
{
codeptr = addr + 2;
if (a==PLUS_T) incdec = 1;
else incdec = -1;
t = 1;
break;
}
/* +=, -=, etc. */
else if (MEM(addr+1)==EQUALS_T)
{
codeptr = addr + 2;
incdec = GetValue();
t = a;
}
}
}
#if defined (DEBUGGER)
if (t && debug_eval)
{
debug_eval_error = true;
Common::sprintf_s(debug_line, "'%s%s' illegal in watch/assignment", token[a], token[MEM(addr+1)]);
DebugMessageBox("Expression Error", debug_line);
t = 0;
}
#endif
return t;
}
int Hugo::Precedence(int t) {
switch (t)
{
case DECIMAL_T:
return 1;
case ASTERISK_T:
case FORWARD_SLASH_T:
return 2;
case MINUS_T:
case PLUS_T:
return 3;
case PIPE_T:
case TILDE_T:
case AMPERSAND_T:
return 4;
case EQUALS_T:
case GREATER_EQUAL_T:
case LESS_EQUAL_T:
case NOT_EQUAL_T:
case GREATER_T:
case LESS_T:
return 5;
default:
return 6;
}
}
#if defined (DEBUG_EXPR_EVAL)
/* PRINTEXPR
Prints the current expression during expression tracing.
*/
void PrintExpr(void)
{
char e[261];
int i, bracket = 0;
if (!evalcount) return;
strcpy(e, "( ");
for (i=0; i<=evalcount; i+=2)
{
switch (eval[i])
{
case 0:
{
Common::sprintf_s(line, "%d ", eval[i + 1]);
strcat(e, line);
break;
}
case 1:
{
if (eval[i+1]==OPEN_BRACKET_T) bracket++;
if (eval[i+1]==CLOSE_BRACKET_T)
{bracket--;
if (bracket<0) goto ExitPrintExpr;}
if (token[eval[i+1]][0]=='~')
strcat(e, "\\");
if (eval[i+1] != 255)
{sprintf(line, "%s ", token[eval[i+1]]);
strcat(e, line);}
break;
}
}
}
ExitPrintExpr:
strcat(e, ")\\;");
AP(e);
}
#endif
void Hugo::SetupExpr() {
char justgotvalue = 1;
int j, t, bracket = 0;
int tempret;
int tempeval[MAX_EVAL_ELEMENTS];
int tempevalcount;
last_precedence = 10;
tempret = ret;
tempevalcount = 0;
inobj = false;
if (!inexpr) inexpr = 1;
do
{
justgotvalue++;
switch (t = MEM(codeptr))
{
/* Various indications that we've hit the
end of the expression:
*/
case EOL_T:
arrexpr = false;
// fall through
case COMMA_T:
multiprop = false;
// fall through
case SEMICOLON_T:
case CLOSE_SQUARE_T:
case JUMP_T:
{
if (t==EOL_T || t==COMMA_T || t==JUMP_T)
codeptr++;
LeaveSetupExpr:
for (j=0; j<tempevalcount; j++)
eval[j] = tempeval[j];
evalcount = tempevalcount;
eval[evalcount] = 1;
eval[evalcount + 1] = 255;
#if defined (DEBUG_EXPR_EVAL)
if (exprt) PrintExpr();
#endif
ret = tempret;
return;
}
/* Otherwise we have a value: */
case OPEN_BRACKET_T:
case MINUS_T:
case PLUS_T:
case TILDE_T:
case AMPERSAND_T:
case NOT_T:
case PARENT_T:
case SIBLING_T:
case CHILD_T:
case YOUNGEST_T:
case ELDEST_T:
case YOUNGER_T:
case ELDER_T:
case CHILDREN_T:
case RANDOM_T:
case SYSTEM_T:
case PROP_T:
case ATTR_T:
case VAR_T:
case DICTENTRY_T:
case ROUTINE_T:
case OBJECTNUM_T:
case VALUE_T:
case ARRAYDATA_T:
case ARRAY_T:
case WORD_T:
case CALL_T:
case SAVE_T:
case RESTORE_T:
case SCRIPTON_T:
case SCRIPTOFF_T:
case RESTART_T:
case UNDO_T:
case READVAL_T:
case STRING_T:
case DICT_T:
case PARSE_T:
case SERIAL_T:
{
if ((t==AMPERSAND_T || t==MINUS_T ||
t==PLUS_T) && justgotvalue==1)
goto SomeSymbolorToken;
tempeval[tempevalcount] = 0;
tempeval[tempevalcount + 1] = GetValue();
#if defined (DEBUGGER)
if ((debug_eval) && debug_eval_error)
return;
#endif
tempevalcount += 2;
if (tempevalcount > MAX_EVAL_ELEMENTS-2)
FatalError(OVERFLOW_E);
justgotvalue = 0;
break;
}
/* Logical constants */
case TRUE_T:
case FALSE_T:
{
tempeval[tempevalcount] = 0;
if (Peek(codeptr)==TRUE_T)
tempeval[tempevalcount + 1] = 1;
else
tempeval[tempevalcount + 1] = 0;
codeptr++;
tempevalcount += 2;
if (tempevalcount > MAX_EVAL_ELEMENTS-2)
FatalError(OVERFLOW_E);
break;
}
/* Some symbol or token */
default:
{
SomeSymbolorToken:
tempeval[tempevalcount] = 1;
tempeval[tempevalcount + 1] = MEM(codeptr++);
tempevalcount += 2;
if (tempevalcount > MAX_EVAL_ELEMENTS-2)
FatalError(OVERFLOW_E);
switch (MEM(codeptr-1))
{
case OPEN_BRACKET_T:
{bracket++;
break;}
case CLOSE_BRACKET_T:
{bracket--;
justgotvalue = 0;
if (inexpr==2)
codeptr--;}
}
if (bracket < 0) goto LeaveSetupExpr;
break;
}
}
}
while (true); /* endless loop */
}
void Hugo::TrimExpr(int ptr) {
int i;
for (i=ptr; i<=evalcount; i+=2)
{
eval[i] = eval[i+2];
eval[i+1] = eval[i+3];
}
evalcount -= 2;
}
} // End of namespace Hugo
} // End of namespace Glk
|