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
|
/* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/endian.h"
#include "common/str.h"
#include "gob/gob.h"
#include "gob/expression.h"
#include "gob/global.h"
#include "gob/game.h"
#include "gob/script.h"
#include "gob/inter.h"
namespace Gob {
Expression::Stack::Stack(size_t size) {
opers = new byte[size];
values = new int32[size];
memset(opers , 0, size * sizeof(byte ));
memset(values, 0, size * sizeof(int32));
}
Expression::Stack::~Stack() {
delete[] opers;
delete[] values;
}
Expression::StackFrame::StackFrame(const Stack &stack) {
opers = stack.opers - 1;
values = stack.values - 1;
pos = -1;
}
void Expression::StackFrame::push(int count) {
opers += count;
values += count;
pos += count;
}
void Expression::StackFrame::pop(int count) {
opers -= count;
values -= count;
pos -= count;
}
Expression::Expression(GobEngine *vm) : _vm(vm) {
_resultStr[0] = 0;
_resultInt = 0;
}
int32 Expression::encodePtr(byte *ptr, int type) {
int32 offset = 0;
switch (type) {
case kExecPtr:
offset = _vm->_game->_script->getOffset(ptr);
break;
case kInterVar:
offset = ptr - ((byte *)_vm->_inter->_variables->getAddressOff8(0));
break;
case kResStr:
offset = ptr - ((byte *)_resultStr);
break;
default:
error("Expression::encodePtr(): Unknown pointer type");
}
assert((offset & 0xF0000000) == 0);
return (type << 28) | offset;
}
byte *Expression::decodePtr(int32 n) {
byte *ptr;
switch (n >> 28) {
case kExecPtr:
return _vm->_game->_script->getData((n & 0x0FFFFFFF));
case kInterVar:
ptr = (byte *)_vm->_inter->_variables->getAddressOff8(0);
break;
case kResStr:
ptr = (byte *)_resultStr;
break;
default:
error("Expression::decodePtr(): Unknown pointer type");
}
return ptr + (n & 0x0FFFFFFF);
}
void Expression::skipExpr(char stopToken) {
int16 dimCount;
byte operation;
int16 num;
int16 dim;
num = 0;
while (true) {
operation = _vm->_game->_script->readByte();
if ((operation >= 14) && (operation <= OP_FUNC)) {
switch (operation) {
case 14:
_vm->_game->_script->skip(4);
if (_vm->_game->_script->peekByte() == 97)
_vm->_game->_script->skip(1);
break;
case OP_LOAD_VAR_INT16:
case OP_LOAD_VAR_INT8:
case OP_LOAD_IMM_INT16:
case OP_LOAD_VAR_INT32:
case OP_LOAD_VAR_INT32_AS_INT16:
_vm->_game->_script->skip(2);
break;
case OP_LOAD_IMM_INT32:
_vm->_game->_script->skip(4);
break;
case OP_LOAD_IMM_INT8:
_vm->_game->_script->skip(1);
break;
case OP_LOAD_IMM_STR:
_vm->_game->_script->skip(strlen(_vm->_game->_script->peekString()) + 1);
break;
case OP_LOAD_VAR_STR:
_vm->_game->_script->skip(2);
if (_vm->_game->_script->peekByte() == 13) {
_vm->_game->_script->skip(1);
skipExpr(OP_END_MARKER);
}
break;
case 15:
_vm->_game->_script->skip(2);
case OP_ARRAY_INT8:
case OP_ARRAY_INT32:
case OP_ARRAY_INT16:
case OP_ARRAY_STR:
dimCount = _vm->_game->_script->peekByte(2);
// skip header and dimensions
_vm->_game->_script->skip(3 + dimCount);
// skip indices
for (dim = 0; dim < dimCount; dim++)
skipExpr(OP_END_MARKER);
if ((operation == OP_ARRAY_STR) && (_vm->_game->_script->peekByte() == 13)) {
_vm->_game->_script->skip(1);
skipExpr(OP_END_MARKER);
}
break;
case OP_FUNC:
_vm->_game->_script->skip(1);
skipExpr(OP_END_EXPR);
}
continue;
} // if ((operation >= OP_ARRAY_INT8) && (operation <= OP_FUNC))
if (operation == OP_BEGIN_EXPR) {
num++;
continue;
}
if ((operation == OP_NOT) || ((operation >= OP_NEG) && (operation <= 8)))
continue;
if ((operation >= OP_OR) && (operation <= OP_NEQ))
continue;
if (operation == OP_END_EXPR)
num--;
if (operation != stopToken)
continue;
if ((stopToken != OP_END_EXPR) || (num < 0))
return;
}
}
void Expression::printExpr(char stopToken) {
// Expression printing disabled by default
#if 0
int32 savedPos = _vm->_game->_script->pos();
printExpr_internal(stopToken);
// restore IP to start of expression
_vm->_game->_script->seek(savedPos);
#endif
}
void Expression::printExpr_internal(char stopToken) {
int16 dimCount;
byte operation;
int16 num;
int16 dim;
byte *arrDesc;
byte func;
num = 0;
while (true) {
operation = _vm->_game->_script->readByte();
if ((operation >= OP_ARRAY_INT8) && (operation <= OP_FUNC)) {
// operands
switch (operation) {
case OP_LOAD_VAR_INT16: // int16 variable load
debugN(5, "var16_%d", _vm->_game->_script->readUint16());
break;
case OP_LOAD_VAR_INT8: // int8 variable load:
debugN(5, "var8_%d", _vm->_game->_script->readUint16());
break;
case OP_LOAD_IMM_INT32: // int32/uint32 immediate
debugN(5, "%d", _vm->_game->_script->readInt32());
break;
case OP_LOAD_IMM_INT16: // int16 immediate
debugN(5, "%d", _vm->_game->_script->readInt16());
break;
case OP_LOAD_IMM_INT8: // int8 immediate
debugN(5, "%d", _vm->_game->_script->readInt8());
break;
case OP_LOAD_IMM_STR: // string immediate
debugN(5, "\42%s\42", _vm->_game->_script->readString());
break;
case OP_LOAD_VAR_INT32:
case OP_LOAD_VAR_INT32_AS_INT16:
debugN(5, "var_%d", _vm->_game->_script->readUint16());
break;
case OP_LOAD_VAR_STR: // string variable load
debugN(5, "(&var_%d)", _vm->_game->_script->readUint16());
if (_vm->_game->_script->peekByte() == 13) {
_vm->_game->_script->skip(1);
debugN(5, "{");
printExpr_internal(OP_END_MARKER); // this also prints the closing }
}
break;
case OP_ARRAY_INT8: // int8 array access
case OP_ARRAY_INT32: // int32 array access
case OP_ARRAY_INT16: // int16 array access
case OP_ARRAY_STR: // string array access
debugN(5, "\n");
if (operation == OP_ARRAY_STR)
debugN(5, "(&");
debugN(5, "var_%d[", _vm->_game->_script->readInt16());
dimCount = _vm->_game->_script->readByte();
arrDesc = _vm->_game->_script->getData() + _vm->_game->_script->pos();
_vm->_game->_script->skip(dimCount);
for (dim = 0; dim < dimCount; dim++) {
printExpr_internal(OP_END_MARKER);
debugN(5, " of %d", (int16) arrDesc[dim]);
if (dim != dimCount - 1)
debugN(5, ",");
}
debugN(5, "]");
if (operation == OP_ARRAY_STR)
debugN(5, ")");
if ((operation == OP_ARRAY_STR) && (_vm->_game->_script->peekByte() == 13)) {
_vm->_game->_script->skip(1);
debugN(5, "{");
printExpr_internal(OP_END_MARKER); // this also prints the closing }
}
break;
case OP_FUNC: // function
func = _vm->_game->_script->readByte();
if (func == FUNC_SQR)
debugN(5, "sqr(");
else if (func == FUNC_RAND)
debugN(5, "rand(");
else if (func == FUNC_ABS)
debugN(5, "abs(");
else if ((func == FUNC_SQRT1) || (func == FUNC_SQRT2) || (func == FUNC_SQRT3))
debugN(5, "sqrt(");
else
debugN(5, "id(");
printExpr_internal(OP_END_EXPR);
break;
}
continue;
} // if ((operation >= OP_ARRAY_INT8) && (operation <= OP_FUNC))
// operators
switch (operation) {
case OP_BEGIN_EXPR:
debugN(5, "(");
break;
case OP_NOT:
debugN(5, "!");
break;
case OP_END_EXPR:
debugN(5, ")");
break;
case OP_NEG:
debugN(5, "-");
break;
case OP_ADD:
debugN(5, "+");
break;
case OP_SUB:
debugN(5, "-");
break;
case OP_BITOR:
debugN(5, "|");
break;
case OP_MUL:
debugN(5, "*");
break;
case OP_DIV:
debugN(5, "/");
break;
case OP_MOD:
debugN(5, "%%");
break;
case OP_BITAND:
debugN(5, "&");
break;
case OP_OR:
debugN(5, "||");
break;
case 31:
debugN(5, "&&");
break;
case OP_LESS:
debugN(5, "<");
break;
case OP_LEQ:
debugN(5, "<=");
break;
case OP_GREATER:
debugN(5, ">");
break;
case OP_GEQ:
debugN(5, ">=");
break;
case OP_EQ:
debugN(5, "==");
break;
case OP_NEQ:
debugN(5, "!=");
break;
case 99:
debugN(5, "\n");
break;
case OP_END_MARKER:
debugN(5, "}");
if (stopToken != OP_END_MARKER) {
debugN(5, "Closing paren without opening?");
}
break;
default:
debugN(5, "<%d>", (int16) operation);
error("Expression::printExpr(): invalid operator in expression");
break;
}
if (operation == OP_BEGIN_EXPR) {
num++;
continue;
}
if ((operation == OP_NOT) || ((operation >= OP_NEG) && (operation <= 8)))
continue;
if ((operation >= OP_OR) && (operation <= OP_NEQ))
continue;
if (operation == OP_END_EXPR)
num--;
if (operation == stopToken) {
if ((stopToken != OP_END_EXPR) || (num < 0)) {
return;
}
}
}
}
void Expression::printVarIndex() {
byte *arrDesc;
int16 dim;
int16 dimCount;
int16 operation;
int16 temp;
int32 pos = _vm->_game->_script->pos();
operation = _vm->_game->_script->readByte();
switch (operation) {
case OP_LOAD_VAR_INT32:
case OP_LOAD_VAR_STR:
temp = _vm->_game->_script->readUint16() * 4;
debugN(5, "&var_%d", temp);
if ((operation == OP_LOAD_VAR_STR) && (_vm->_game->_script->peekByte() == 13)) {
_vm->_game->_script->skip(1);
debugN(5, "+");
printExpr(OP_END_MARKER);
}
break;
case OP_ARRAY_INT32:
case OP_ARRAY_STR:
debugN(5, "&var_%d[", _vm->_game->_script->readUint16());
dimCount = _vm->_game->_script->readByte();
arrDesc = _vm->_game->_script->getData() + _vm->_game->_script->pos();
_vm->_game->_script->skip(dimCount);
for (dim = 0; dim < dimCount; dim++) {
printExpr(OP_END_MARKER);
debugN(5, " of %d", (int16) arrDesc[dim]);
if (dim != dimCount - 1)
debugN(5, ",");
}
debugN(5, "]");
if ((operation == OP_ARRAY_STR) && (_vm->_game->_script->peekByte() == 13)) {
_vm->_game->_script->skip(1);
debugN(5, "+");
printExpr(OP_END_MARKER);
}
break;
default:
debugN(5, "var_0");
break;
}
debugN(5, "\n");
_vm->_game->_script->seek(pos);
return;
}
int Expression::cmpHelper(const StackFrame &stackFrame) {
byte type = stackFrame.opers[-3];
int cmpTemp = 0;
if (type == OP_LOAD_IMM_INT16) {
cmpTemp = (int)stackFrame.values[-3] - (int)stackFrame.values[-1];
} else if (type == OP_LOAD_IMM_STR) {
if ((char *)decodePtr(stackFrame.values[-3]) != _resultStr) {
Common::strlcpy(_resultStr, (char *)decodePtr(stackFrame.values[-3]), sizeof(_resultStr));
stackFrame.values[-3] = encodePtr((byte *)_resultStr, kResStr);
}
cmpTemp = strcmp(_resultStr, (char *)decodePtr(stackFrame.values[-1]));
}
return cmpTemp;
}
bool Expression::getVarBase(uint32 &varBase, bool mindStop,
uint16 *size, uint16 *type) {
varBase = 0;
byte operation = _vm->_game->_script->peekByte();
while ((operation == 14) || (operation == 15)) {
_vm->_game->_script->skip(1);
if (operation == 14) {
// Add a direct offset
varBase += _vm->_game->_script->readInt16() * 4;
if (size)
*size = _vm->_game->_script->peekUint16();
if (type)
*type = 14;
_vm->_game->_script->skip(2);
debugC(2, kDebugExpression, "varBase: %d, by %d", varBase, operation);
if (_vm->_game->_script->peekByte() != 97) {
if (mindStop)
return true;
} else
_vm->_game->_script->skip(1);
} else if (operation == 15) {
// Add an offset from an array
varBase += _vm->_game->_script->readInt16() * 4;
uint16 offset1 = _vm->_game->_script->readUint16();
if (size)
*size = offset1;
if (type)
*type = 15;
uint8 dimCount = _vm->_game->_script->readByte();
byte *dimArray = _vm->_game->_script->getData() + _vm->_game->_script->pos();
_vm->_game->_script->skip(dimCount);
uint16 offset2 = 0;
for (int i = 0; i < dimCount; i++) {
int16 dim = CLIP<int>(parseValExpr(OP_END_MARKER), 0, dimArray[i] - 1);
offset2 = offset2 * dimArray[i] + dim;
}
varBase += offset2 * offset1 * 4;
debugC(2, kDebugExpression, "varBase: %d, by %d", varBase, operation);
if (_vm->_game->_script->peekByte() != 97) {
if (mindStop)
return true;
} else
_vm->_game->_script->skip(1);
}
operation = _vm->_game->_script->peekByte();
}
return false;
}
int16 Expression::parseVarIndex(uint16 *size, uint16 *type) {
int16 temp2;
byte *arrDesc;
int16 dim;
int16 dimCount;
int16 operation;
int16 temp;
int16 offset;
int16 val;
uint32 varBase;
if (getVarBase(varBase, true, size, type))
return varBase;
operation = _vm->_game->_script->readByte();
if (size)
*size = 0;
if (type)
*type = operation;
debugC(5, kDebugExpression, "var parse = %d", operation);
switch (operation) {
case OP_ARRAY_INT8:
case OP_ARRAY_INT32:
case OP_ARRAY_INT16:
case OP_ARRAY_STR:
temp = _vm->_game->_script->readInt16();
dimCount = _vm->_game->_script->readByte();
arrDesc = _vm->_game->_script->getData() + _vm->_game->_script->pos();
_vm->_game->_script->skip(dimCount);
offset = 0;
for (dim = 0; dim < dimCount; dim++) {
temp2 = parseValExpr(OP_END_MARKER);
offset = arrDesc[dim] * offset + temp2;
}
if (operation == OP_ARRAY_INT8)
return varBase + temp + offset;
if (operation == OP_ARRAY_INT32)
return varBase + (temp + offset) * 4;
if (operation == OP_ARRAY_INT16)
return varBase + (temp + offset) * 2;
temp *= 4;
offset *= 4;
if (_vm->_game->_script->peekByte() == 13) {
_vm->_game->_script->skip(1);
temp += parseValExpr(OP_END_MARKER);
}
return varBase + offset * _vm->_global->_inter_animDataSize + temp;
case OP_LOAD_VAR_INT16:
return varBase + _vm->_game->_script->readUint16() * 2;
case OP_LOAD_VAR_INT8:
return varBase + _vm->_game->_script->readUint16();
case OP_LOAD_VAR_INT32:
case OP_LOAD_VAR_INT32_AS_INT16:
case OP_LOAD_VAR_STR:
temp = _vm->_game->_script->readUint16() * 4;
debugC(5, kDebugExpression, "oper = %d", _vm->_game->_script->peekInt16());
if ((operation == OP_LOAD_VAR_STR) && (_vm->_game->_script->peekByte() == 13)) {
_vm->_game->_script->skip(1);
val = parseValExpr(OP_END_MARKER);
temp += val;
debugC(5, kDebugExpression, "parse subscript = %d", val);
}
return varBase + temp;
default:
return 0;
}
}
int16 Expression::parseValExpr(byte stopToken) {
parseExpr(stopToken, 0);
return _resultInt;
}
// Load a value according to the operation
void Expression::loadValue(byte operation, uint32 varBase, const StackFrame &stackFrame) {
int16 dimCount;
uint16 temp;
int16 temp2;
int16 offset;
int16 dim;
byte *arrDescPtr;
int32 prevPrevVal;
int32 prevVal;
int32 curVal;
switch (operation) {
case OP_ARRAY_INT8:
case OP_ARRAY_INT32:
case OP_ARRAY_INT16:
case OP_ARRAY_STR:
*stackFrame.opers = (operation == OP_ARRAY_STR) ? OP_LOAD_IMM_STR : OP_LOAD_IMM_INT16;
temp = _vm->_game->_script->readInt16();
dimCount = _vm->_game->_script->readByte();
arrDescPtr = _vm->_game->_script->getData() + _vm->_game->_script->pos();
_vm->_game->_script->skip(dimCount);
offset = 0;
for (dim = 0; dim < dimCount; dim++) {
temp2 = parseValExpr(OP_END_MARKER);
offset = offset * arrDescPtr[dim] + temp2;
}
if (operation == OP_ARRAY_INT8)
*stackFrame.values = (int8) READ_VARO_UINT8(varBase + temp + offset);
else if (operation == OP_ARRAY_INT32)
*stackFrame.values = READ_VARO_UINT32(varBase + temp * 4 + offset * 4);
else if (operation == OP_ARRAY_INT16)
*stackFrame.values = (int16) READ_VARO_UINT16(varBase + temp * 2 + offset * 2);
else if (operation == OP_ARRAY_STR) {
*stackFrame.values = encodePtr(_vm->_inter->_variables->getAddressOff8(
varBase + temp * 4 + offset * _vm->_global->_inter_animDataSize * 4),
kInterVar);
if (_vm->_game->_script->peekByte() == 13) {
_vm->_game->_script->skip(1);
temp2 = parseValExpr(OP_END_MARKER);
*stackFrame.opers = OP_LOAD_IMM_INT16;
*stackFrame.values = READ_VARO_UINT8(varBase + temp * 4 +
offset * 4 * _vm->_global->_inter_animDataSize + temp2);
}
}
break;
case OP_LOAD_VAR_INT16:
*stackFrame.opers = OP_LOAD_IMM_INT16;
*stackFrame.values = (int16) READ_VARO_UINT16(varBase + _vm->_game->_script->readUint16() * 2);
break;
case OP_LOAD_VAR_INT8:
*stackFrame.opers = OP_LOAD_IMM_INT16;
*stackFrame.values = (int8) READ_VARO_UINT8(varBase + _vm->_game->_script->readUint16());
break;
case OP_LOAD_IMM_INT32:
*stackFrame.opers = OP_LOAD_IMM_INT16;
*stackFrame.values = _vm->_game->_script->readInt32();
break;
case OP_LOAD_IMM_INT16:
*stackFrame.opers = OP_LOAD_IMM_INT16;
*stackFrame.values = _vm->_game->_script->readInt16();
break;
case OP_LOAD_IMM_INT8:
*stackFrame.opers = OP_LOAD_IMM_INT16;
*stackFrame.values = _vm->_game->_script->readInt8();
break;
case OP_LOAD_IMM_STR:
*stackFrame.opers = OP_LOAD_IMM_STR;
*stackFrame.values = encodePtr((byte *)_vm->_game->_script->readString(), kExecPtr);
break;
case OP_LOAD_VAR_INT32:
*stackFrame.opers = OP_LOAD_IMM_INT16;
*stackFrame.values = READ_VARO_UINT32(varBase + _vm->_game->_script->readUint16() * 4);
break;
case OP_LOAD_VAR_INT32_AS_INT16:
*stackFrame.opers = OP_LOAD_IMM_INT16;
*stackFrame.values = (int16) READ_VARO_UINT16(varBase + _vm->_game->_script->readUint16() * 4);
break;
case OP_LOAD_VAR_STR:
*stackFrame.opers = OP_LOAD_IMM_STR;
temp = _vm->_game->_script->readUint16() * 4;
*stackFrame.values = encodePtr(_vm->_inter->_variables->getAddressOff8(varBase + temp), kInterVar);
if (_vm->_game->_script->peekByte() == 13) {
_vm->_game->_script->skip(1);
temp += parseValExpr(OP_END_MARKER);
*stackFrame.opers = OP_LOAD_IMM_INT16;
*stackFrame.values = READ_VARO_UINT8(varBase + temp);
}
break;
case OP_FUNC:
operation = _vm->_game->_script->readByte();
parseExpr(OP_END_EXPR, 0);
switch (operation) {
case FUNC_SQRT1:
case FUNC_SQRT2:
case FUNC_SQRT3:
curVal = 1;
prevVal = 1;
do {
prevPrevVal = prevVal;
prevVal = curVal;
curVal = (curVal + _resultInt / curVal) / 2;
} while ((curVal != prevVal) && (curVal != prevPrevVal));
_resultInt = curVal;
break;
case FUNC_SQR:
_resultInt =
_resultInt * _resultInt;
break;
case FUNC_ABS:
if (_resultInt < 0)
_resultInt = -_resultInt;
break;
case FUNC_RAND:
_resultInt =
_vm->_util->getRandom(_resultInt);
break;
}
*stackFrame.opers = OP_LOAD_IMM_INT16;
*stackFrame.values = _resultInt;
break;
}
}
void Expression::simpleArithmetic1(StackFrame &stackFrame) {
switch (stackFrame.opers[-1]) {
case OP_ADD:
if (stackFrame.opers[-2] == OP_LOAD_IMM_STR) {
if ((char *)decodePtr(stackFrame.values[-2]) != _resultStr) {
Common::strlcpy(_resultStr, (char *)decodePtr(stackFrame.values[-2]), sizeof(_resultStr));
stackFrame.values[-2] = encodePtr((byte *)_resultStr, kResStr);
}
Common::strlcat(_resultStr, (char *)decodePtr(stackFrame.values[0]), sizeof(_resultStr));
stackFrame.pop(2);
}
break;
case OP_MUL:
stackFrame.values[-2] *= stackFrame.values[0];
stackFrame.pop(2);
break;
case OP_DIV:
stackFrame.values[-2] /= stackFrame.values[0];
stackFrame.pop(2);
break;
case OP_MOD:
stackFrame.values[-2] %= stackFrame.values[0];
stackFrame.pop(2);
break;
case OP_BITAND:
stackFrame.values[-2] &= stackFrame.values[0];
stackFrame.pop(2);
break;
}
}
void Expression::simpleArithmetic2(StackFrame &stackFrame) {
if (stackFrame.pos > 1) {
if (stackFrame.opers[-2] == OP_NEG) {
stackFrame.opers[-2] = OP_LOAD_IMM_INT16;
stackFrame.values[-2] = -stackFrame.values[-1];
stackFrame.pop();
} else if (stackFrame.opers[-2] == OP_NOT) {
stackFrame.opers[-2] = (stackFrame.opers[-1] == GOB_FALSE) ? GOB_TRUE : GOB_FALSE;
stackFrame.pop();
}
}
if (stackFrame.pos > 2) {
switch (stackFrame.opers[-2]) {
case OP_MUL:
stackFrame.values[-3] *= stackFrame.values[-1];
stackFrame.pop(2);
break;
case OP_DIV:
stackFrame.values[-3] /= stackFrame.values[-1];
stackFrame.pop(2);
break;
case OP_MOD:
stackFrame.values[-3] %= stackFrame.values[-1];
stackFrame.pop(2);
break;
case OP_BITAND:
stackFrame.values[-3] &= stackFrame.values[-1];
stackFrame.pop(2);
break;
}
}
}
// Complex arithmetics with brackets
bool Expression::complexArithmetic(Stack &stack, StackFrame &stackFrame, int16 brackStart) {
switch (stackFrame.opers[-2]) {
case OP_ADD:
if (stack.opers[brackStart] == OP_LOAD_IMM_INT16) {
stack.values[brackStart] += stackFrame.values[-1];
} else if (stack.opers[brackStart] == OP_LOAD_IMM_STR) {
if ((char *)decodePtr(stack.values[brackStart]) != _resultStr) {
Common::strlcpy(_resultStr, (char *)decodePtr(stack.values[brackStart]), sizeof(_resultStr));
stack.values[brackStart] =
encodePtr((byte *)_resultStr, kResStr);
}
Common::strlcat(_resultStr, (char *)decodePtr(stackFrame.values[-1]), sizeof(_resultStr));
}
stackFrame.pop(2);
break;
case OP_SUB:
stack.values[brackStart] -= stackFrame.values[-1];
stackFrame.pop(2);
break;
case OP_BITOR:
stack.values[brackStart] |= stackFrame.values[-1];
stackFrame.pop(2);
break;
case OP_MUL:
stackFrame.values[-3] *= stackFrame.values[-1];
stackFrame.pop(2);
break;
case OP_DIV:
stackFrame.values[-3] /= stackFrame.values[-1];
stackFrame.pop(2);
break;
case OP_MOD:
stackFrame.values[-3] %= stackFrame.values[-1];
stackFrame.pop(2);
break;
case OP_BITAND:
stackFrame.values[-3] &= stackFrame.values[-1];
stackFrame.pop(2);
break;
case OP_OR:
// (x OR false) == x
// (x OR true) == true
if (stackFrame.opers[-3] == GOB_FALSE)
stackFrame.opers[-3] = stackFrame.opers[-1];
stackFrame.pop(2);
break;
case OP_AND:
// (x AND false) == false
// (x AND true) == x
if (stackFrame.opers[-3] == GOB_TRUE)
stackFrame.opers[-3] = stackFrame.opers[-1];
stackFrame.pop(2);
break;
case OP_LESS:
stackFrame.opers[-3] = (cmpHelper(stackFrame) < 0) ? GOB_TRUE : GOB_FALSE;
stackFrame.pop(2);
break;
case OP_LEQ:
stackFrame.opers[-3] = (cmpHelper(stackFrame) <= 0) ? GOB_TRUE : GOB_FALSE;
stackFrame.pop(2);
break;
case OP_GREATER:
stackFrame.opers[-3] = (cmpHelper(stackFrame) > 0) ? GOB_TRUE : GOB_FALSE;
stackFrame.pop(2);
break;
case OP_GEQ:
stackFrame.opers[-3] = (cmpHelper(stackFrame) >= 0) ? GOB_TRUE : GOB_FALSE;
stackFrame.pop(2);
break;
case OP_EQ:
stackFrame.opers[-3] = (cmpHelper(stackFrame) == 0) ? GOB_TRUE : GOB_FALSE;
stackFrame.pop(2);
break;
case OP_NEQ:
stackFrame.opers[-3] = (cmpHelper(stackFrame) != 0) ? GOB_TRUE : GOB_FALSE;
stackFrame.pop(2);
break;
default:
return true;
}
return false;
}
// Assign the result to the appropriate _result variable
void Expression::getResult(byte operation, int32 value, byte *type) {
if (type != 0)
*type = operation;
switch (operation) {
case OP_NOT:
if (type != 0)
*type ^= 1;
break;
case OP_LOAD_IMM_INT16:
_resultInt = value;
break;
case OP_LOAD_IMM_STR:
if ((char *)decodePtr(value) != _resultStr)
Common::strlcpy(_resultStr, (char *)decodePtr(value), sizeof(_resultStr));
break;
case OP_LOAD_VAR_INT32:
case OP_LOAD_VAR_INT32_AS_INT16:
break;
default:
_resultInt = 0;
if (type != 0)
*type = OP_LOAD_IMM_INT16;
break;
}
}
int16 Expression::parseExpr(byte stopToken, byte *type) {
Stack stack;
StackFrame stackFrame(stack);
byte operation;
int16 brackStart;
uint32 varBase;
while (true) {
getVarBase(varBase);
stackFrame.push();
operation = _vm->_game->_script->readByte();
if ((operation >= OP_ARRAY_INT8) && (operation <= OP_FUNC)) {
loadValue(operation, varBase, stackFrame);
if ((stackFrame.pos > 0) && ((stackFrame.opers[-1] == OP_NEG) || (stackFrame.opers[-1] == OP_NOT))) {
stackFrame.pop();
if (*stackFrame.opers == OP_NEG) {
*stackFrame.opers = OP_LOAD_IMM_INT16;
stackFrame.values[0] = -stackFrame.values[1];
} else
*stackFrame.opers = (stackFrame.opers[1] == GOB_FALSE) ? GOB_TRUE : GOB_FALSE;
}
if (stackFrame.pos <= 0)
continue;
simpleArithmetic1(stackFrame);
continue;
} // (op >= OP_ARRAY_INT8) && (op <= OP_FUNC)
if ((operation == stopToken) || (operation == OP_OR) ||
(operation == OP_AND) || (operation == OP_END_EXPR)) {
while (stackFrame.pos >= 2) {
if ((stackFrame.opers[-2] == OP_BEGIN_EXPR) &&
((operation == OP_END_EXPR) || (operation == stopToken))) {
stackFrame.opers[-2] = stackFrame.opers[-1];
if ((stackFrame.opers[-2] == OP_LOAD_IMM_INT16) || (stackFrame.opers[-2] == OP_LOAD_IMM_STR))
stackFrame.values[-2] = stackFrame.values[-1];
stackFrame.pop();
simpleArithmetic2(stackFrame);
if (operation != stopToken)
break;
} // if ((stackFrame.opers[-2] == OP_BEGIN_EXPR) && ...)
for (brackStart = (stackFrame.pos - 2); (brackStart > 0) &&
(stack.opers[brackStart] < OP_OR) && (stack.opers[brackStart] != OP_BEGIN_EXPR);
brackStart--)
;
if ((stack.opers[brackStart] >= OP_OR) || (stack.opers[brackStart] == OP_BEGIN_EXPR))
brackStart++;
if (complexArithmetic(stack, stackFrame, brackStart))
break;
} // while (stackFrame.pos >= 2)
if ((operation == OP_OR) || (operation == OP_AND)) {
if (stackFrame.opers[-1] == OP_LOAD_IMM_INT16) {
if (stackFrame.values[-1] != 0)
stackFrame.opers[-1] = GOB_TRUE;
else
stackFrame.opers[-1] = GOB_FALSE;
}
if (((operation == OP_OR) && (stackFrame.opers[-1] == GOB_TRUE)) ||
((operation == OP_AND) && (stackFrame.opers[-1] == GOB_FALSE))) {
if ((stackFrame.pos > 1) && (stackFrame.opers[-2] == OP_BEGIN_EXPR)) {
skipExpr(OP_END_EXPR);
stackFrame.opers[-2] = stackFrame.opers[-1];
stackFrame.pop(2);
} else {
skipExpr(stopToken);
}
operation = _vm->_game->_script->peekByte(-1);
if ((stackFrame.pos > 0) && (stackFrame.opers[-1] == OP_NOT)) {
if (stackFrame.opers[0] == GOB_FALSE)
stackFrame.opers[-1] = GOB_TRUE;
else
stackFrame.opers[-1] = GOB_FALSE;
stackFrame.pop();
}
} else
stackFrame.opers[0] = operation;
} else
stackFrame.pop();
if (operation != stopToken)
continue;
getResult(stack.opers[0], stack.values[0], type);
return 0;
} // (operation == stopToken) || (operation == OP_OR) || (operation == OP_AND) || (operation == OP_END_EXPR)
if ((operation < OP_NEG) || (operation > OP_NOT)) {
if ((operation < OP_LESS) || (operation > OP_NEQ))
continue;
if (stackFrame.pos > 2) {
if (stackFrame.opers[-2] == OP_ADD) {
if (stackFrame.opers[-3] == OP_LOAD_IMM_INT16) {
stackFrame.values[-3] += stackFrame.values[-1];
} else if (stackFrame.opers[-3] == OP_LOAD_IMM_STR) {
if ((char *)decodePtr(stackFrame.values[-3]) != _resultStr) {
Common::strlcpy(_resultStr, (char *)decodePtr(stackFrame.values[-3]), sizeof(_resultStr));
stackFrame.values[-3] = encodePtr((byte *)_resultStr, kResStr);
}
Common::strlcat(_resultStr, (char *)decodePtr(stackFrame.values[-1]), sizeof(_resultStr));
}
stackFrame.pop(2);
} else if (stackFrame.opers[-2] == OP_SUB) {
stackFrame.values[-3] -= stackFrame.values[-1];
stackFrame.pop(2);
} else if (stackFrame.opers[-2] == OP_BITOR) {
stackFrame.values[-3] |= stackFrame.values[-1];
stackFrame.pop(2);
}
}
}
*stackFrame.opers = operation;
}
}
int32 Expression::getResultInt() {
return _resultInt;
}
char *Expression::getResultStr() {
return _resultStr;
}
} // End of namespace Gob
|