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
|
/* 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/jacl/jacl.h"
#include "glk/jacl/language.h"
#include "glk/jacl/types.h"
#include "glk/jacl/prototypes.h"
namespace Glk {
namespace JACL {
#ifdef GLK
extern uint status_width, status_height;
extern winid_t statuswin;
#endif
#ifdef __NDS__
extern int screen_width;
extern int screen_depth;
#endif
extern struct object_type *object[];
extern struct integer_type *integer_table;
extern struct integer_type *integer[];
extern struct cinteger_type *cinteger_table;
extern struct attribute_type *attribute_table;
extern struct string_type *string_table;
extern struct string_type *cstring_table;
extern struct function_type *function_table;
extern struct function_type *executing_function;
extern struct command_type *completion_list;
extern struct word_type *grammar_table;
extern struct synonym_type *synonym_table;
extern struct filter_type *filter_table;
extern char function_name[];
extern char temp_buffer[];
extern char error_buffer[];
extern char integer_buffer[16];
#ifndef GLK
#ifndef __NDS__
extern char game_url[];
extern char user_id[];
#endif
#endif
extern int noun[];
extern int quoted[];
extern int percented[];
extern const char *word[];
extern int resolved_attribute;
extern int objects;
extern int integers;
extern int player;
extern int oec;
extern int *object_element_address;
extern int *object_backup_address;
extern int value_resolved;
char macro_function[84];
int value_has_been_resolved;
int *container_resolve(const char *container_name) {
container_name = arg_text_of(container_name);
/* IN JACL, A 'CONTAINER' IS ANYTHING THAT CAN STORE AN INTEGER */
struct integer_type *resolved_integer;
if ((resolved_integer = integer_resolve(container_name)) != nullptr)
return (&resolved_integer->value);
else if (object_element_resolve(container_name))
return (object_element_address);
else if (!strcmp(container_name, "noun1"))
return (&noun[0]);
else if (!strcmp(container_name, "noun2"))
return (&noun[1]);
else if (!strcmp(container_name, "noun3"))
return (&noun[2]);
else if (!strcmp(container_name, "noun4"))
return (&noun[3]);
else if (!strcmp(container_name, "player"))
return (&player);
else if (!strcmp(container_name, "here"))
return (&object[player]->PARENT);
else
return ((int *) nullptr);
}
const char *var_text_of_word(int wordnumber) {
const char *value;
if (percented[wordnumber] == FALSE) {
return (word[wordnumber]);
} else {
value_has_been_resolved = TRUE;
value = arg_text_of(word[wordnumber]);
while (value_has_been_resolved && percented[wordnumber]) {
value = arg_text_of(value);
percented[wordnumber]--;
}
return (value);
}
}
const char *arg_text_of_word(int wordnumber) {
const char *value;
if (quoted[wordnumber] == 1) {
return (word[wordnumber]);
} else {
value_has_been_resolved = TRUE;
value = arg_text_of(word[wordnumber]);
while (value_has_been_resolved && percented[wordnumber]) {
value = arg_text_of(value);
percented[wordnumber]--;
}
return (value);
}
}
const char *text_of_word(int wordnumber) {
const char *value;
if (quoted[wordnumber] == 1) {
return (word[wordnumber]);
} else {
value_has_been_resolved = TRUE;
value = text_of(word[wordnumber]);
while (value_has_been_resolved && percented[wordnumber]) {
value = text_of(value);
percented[wordnumber]--;
}
return (value);
}
}
const char *text_of(const char *string) {
struct integer_type *resolved_integer;
struct cinteger_type *resolved_cinteger;
struct string_type *resolved_string;
struct string_type *resolved_cstring;
char *return_string;
int index;
/* CHECK IF THE SUPPLIED STRING IS THE NAME OF A STRING CONSTANT,
* IF NOT, RETURN THE STRING LITERAL */
if ((return_string = macro_resolve(string)) != nullptr) {
value_has_been_resolved = FALSE;
return (return_string);
} else if ((resolved_integer = integer_resolve(string)) != nullptr) {
value_has_been_resolved = FALSE;
integer_buffer[0] = 0;
Common::sprintf_s(integer_buffer, "%d", resolved_integer->value);
return (integer_buffer);
} else if ((resolved_cinteger = cinteger_resolve(string)) != nullptr) {
value_has_been_resolved = FALSE;
integer_buffer[0] = 0;
Common::sprintf_s(integer_buffer, "%d", resolved_cinteger->value);
return (integer_buffer);
} else if (object_element_resolve(string)) {
value_has_been_resolved = FALSE;
integer_buffer[0] = 0;
Common::sprintf_s(integer_buffer, "%d", oec);
return (integer_buffer);
} else if ((index = object_resolve(string)) != -1) {
value_has_been_resolved = FALSE;
if (index < 1 || index > objects) {
badptrrun(string, index);
return ("");
} else {
return (object[index]->label);
}
} else if ((resolved_string = string_resolve(string)) != nullptr) {
return (resolved_string->value);
} else if ((resolved_cstring = cstring_resolve(string)) != nullptr) {
return (resolved_cstring->value);
} else if (function_resolve(string) != nullptr) {
value_has_been_resolved = FALSE;
Common::sprintf_s(integer_buffer, "%d", execute(string));
return (integer_buffer);
#ifndef GLK
#ifndef __NDS__
} else if (!strcmp(string, "$url")) {
value_has_been_resolved = FALSE;
return (game_url);
} else if (!strcmp(string, "$user_id")) {
value_has_been_resolved = FALSE;
return (user_id);
#endif
#endif
} else {
value_has_been_resolved = FALSE;
return (string);
}
}
const char *arg_text_of(const char *string) {
struct string_type *resolved_string;
struct string_type *resolved_cstring;
char *macro_text;
/* CHECK IF THE SUPPLIED STRING IS THE NAME OF A STRING CONSTANT,
* IF NOT, RETURN THE STRING LITERAL */
if ((macro_text = macro_resolve(string)) != nullptr) {
value_has_been_resolved = FALSE;
return (macro_text);
} else if ((resolved_string = string_resolve(string)) != nullptr) {
return (resolved_string->value);
} else if ((resolved_cstring = cstring_resolve(string)) != nullptr) {
value_has_been_resolved = FALSE;
return (resolved_cstring->value);
} else {
value_has_been_resolved = FALSE;
return (string);
}
}
int validate(const char *string) {
int index,
count;
if (string == nullptr) {
return (FALSE);
}
/* CHECK IF THE SUPPLIED STRING IS A VALID INTEGER */
count = strlen(string);
/* LOOP OVER THE WHOLE STRING MAKING SURE THAT EACH CHARACTER IS EITHER
* A DIGIT OR A MINUS SIGN */
for (index = 0; index < count; index++) {
if (!Common::isDigit((int) * (string + index)) && string[index] != '-') {
//printf ("'%c' is not a digit\n", *(string + index));
return (FALSE);
}
}
return (TRUE);
}
long value_of(const char *value, int run_time) {
long compare;
value_resolved = TRUE;
value = arg_text_of(value);
/* RETURN THE INTEGER VALUE OF A STRING */
struct integer_type *resolved_integer;
struct cinteger_type *resolved_cinteger;
if (!strcmp(value, "**held")) {
return (FALSE);
} else if (!strcmp(value, "**here")) {
return (FALSE);
} else if (!strcmp(value, "**anywhere")) {
return (FALSE);
} else if (!strcmp(value, "**present")) {
return (FALSE);
} else if (!strcmp(value, "*held")) {
return (FALSE);
} else if (!strcmp(value, "*here")) {
return (FALSE);
} else if (!strcmp(value, "*anywhere")) {
return (FALSE);
} else if (!strcmp(value, "*present")) {
return (FALSE);
} else if (!strcmp(value, "random")) {
return random_number();
#ifdef GLK
} else if (!strcmp(value, "status_height")) {
g_vm->glk_window_get_size(statuswin, &status_width, &status_height);
return status_height;
} else if (!strcmp(value, "status_width")) {
g_vm->glk_window_get_size(statuswin, &status_width, &status_height);
return status_width;
#else
#ifdef __NDS__
} else if (!strcmp(value, "status_height")) {
return screen_depth;
} else if (!strcmp(value, "status_width")) {
return screen_width;
#else
} else if (!strcmp(value, "status_height")) {
value_resolved = FALSE;
return -1;
} else if (!strcmp(value, "status_width")) {
value_resolved = FALSE;
return -1;
#endif
#endif
} else if (!strcmp(value, "unixtime")) {
return g_system->getMillis() / 1000;
} else if (validate(value)) {
return (atoi(value));
} else if ((resolved_cinteger = cinteger_resolve(value)) != nullptr) {
return (resolved_cinteger->value);
} else if ((resolved_integer = integer_resolve(value)) != nullptr) {
return (resolved_integer->value);
} else if (function_resolve(value) != nullptr) {
return (execute(value));
} else if (object_element_resolve(value)) {
return (oec);
} else if ((compare = attribute_resolve(value))) {
resolved_attribute = SYSTEM_ATTRIBUTE;
return (compare);
} else if ((compare = user_attribute_resolve(value))) {
resolved_attribute = USER_ATTRIBUTE;
return (compare);
} else if ((compare = object_resolve(value)) != -1) {
return (compare);
} else if (*value == '@') {
return (count_resolve(value));
} else {
if (run_time) {
unkvarrun(value);
}
value_resolved = FALSE;
return (-1);
}
}
struct integer_type *integer_resolve(const char *name) {
int index,
iterator,
counter;
int delimiter = 0;
char expression[84];
strncpy(expression, name, 80);
counter = strlen(expression);
for (index = 0; index < counter; index++) {
if (expression[index] == '[') {
/* THIS MAY STILL BE AN OBJECT ELEMENT IF A CLOSING ] */
/* IS FOUND BEFORE AN OPENING ( */
expression[index] = 0;
delimiter = index + 1;
/* LOOK FOR THE CLOSING ], BUT IF YOU FIND A ( FIRST */
/* THEN THIS EXPRESSION IS NOT AN ARRAY */
for (iterator = counter; iterator > 0; iterator--) {
if (expression[iterator] == ']') {
expression[iterator] = 0;
break;
} else if (expression[iterator] == '(') {
/* NOT A VARIABLE ARRAY */
return (FALSE);
}
}
break;
} else if (expression[index] == '<') {
/* HIT A < BEFORE A [ THEREFORE */
/* IS A FUNCTION CALL, NOT AN ARRAY */
return (nullptr);
} else if (expression[index] == '(') {
/* HIT A ( BEFORE A [ THEREFORE */
/* IS AN OBJECT ELEMENT, NOT AN ARRAY */
return (nullptr);
} else if (expression[index] == ' ')
return (nullptr);
}
// NO DELIMITER FOUND, TRY AS UNINDEXED VARIABLE
if (delimiter == 0) {
return (integer_resolve_indexed(name, 0));
}
// NO STRING BEFORE DELIMITER
if (delimiter == 1) {
return (nullptr);
}
counter = value_of(&expression[delimiter], TRUE);
if (counter > -1) {
return (integer_resolve_indexed(expression, counter));
} else {
/* INDEX OUT OF RANGE */
return (nullptr);
}
}
struct integer_type *integer_resolve_indexed(const char *name, int index) {
struct integer_type *pointer = integer_table;
if (pointer == nullptr)
return (nullptr);
do {
if (!strcmp(name, pointer->name)) {
if (index == 0) {
return (pointer);
} else {
/* THIS VARIABLE DOES MATCH, BUT WERE NOT AT THE
* RIGHT INDEX YET SO MOVE ON */
pointer = pointer->next_integer;
index--;
}
} else
pointer = pointer->next_integer;
} while (pointer != nullptr);
/* IF index != 0, INDEX OUT OF RANGE, OTHERWISE NOT VARIABLE */
return (nullptr);
}
struct cinteger_type *cinteger_resolve(const char *name) {
int index,
iterator,
counter;
int delimiter = 0;
char expression[84];
strncpy(expression, name, 80);
counter = strlen(expression);
for (index = 0; index < counter; index++) {
if (expression[index] == '[') {
/* THIS MAY STILL BE AN OBJECT ELEMENT IF A CLOSING ] */
/* IS FOUND BEFORE AN OPENING ( */
expression[index] = 0;
delimiter = index + 1;
/* LOOK FOR THE CLOSING ], BUT IF YOU FIND A ( FIRST */
/* THEN THIS EXPRESSION IS NOT AN ARRAY */
for (iterator = counter; iterator > 0; iterator--) {
if (expression[iterator] == ']') {
expression[iterator] = 0;
break;
} else if (expression[iterator] == '(') {
/* NOT A CONSTANT ARRAY */
return (FALSE);
}
}
break;
} else if (expression[index] == '<') {
/* HIT A < BEFORE A [ THEREFORE */
/* IS A FUNCTION CALL, NOT AN ARRAY */
return (nullptr);
} else if (expression[index] == '(') {
/* HIT A ( BEFORE A [ THEREFORE */
/* IS AN OBJECT ELEMENT, NOT AN ARRAY */
return (nullptr);
} else if (expression[index] == ' ')
return (nullptr);
}
// NO DELIMITER FOUND, TRY AS UNINDEXED CONSTANT
if (delimiter == 0) {
return (cinteger_resolve_indexed(name, 0));
}
// NO STRING BEFORE DELIMITER
if (delimiter == 1) {
return (nullptr);
}
counter = value_of(&expression[delimiter], TRUE);
if (counter > -1) {
return (cinteger_resolve_indexed(expression, counter));
} else {
/* INDEX OUT OF RANGE */
return (nullptr);
}
}
struct cinteger_type *cinteger_resolve_indexed(const char *name, int index) {
struct cinteger_type *pointer = cinteger_table;
if (pointer == nullptr)
return (nullptr);
do {
if (!strcmp(name, pointer->name)) {
if (index == 0) {
return (pointer);
} else {
/* THIS VARIABLE DOES MATCH, BUT WERE NOT AT THE
* RIGHT INDEX YET SO MOVE ON */
pointer = pointer->next_cinteger;
index--;
}
} else
pointer = pointer->next_cinteger;
} while (pointer != nullptr);
/* IF index != 0, INDEX OUT OF RANGE, OTHERWISE NOT VARIABLE */
return (nullptr);
}
struct string_type *string_resolve(const char *name) {
int index,
iterator,
counter;
int delimiter = 0;
char expression[84];
strncpy(expression, name, 80);
counter = strlen(expression);
for (index = 0; index < counter; index++) {
if (expression[index] == '[') {
expression[index] = 0;
delimiter = index + 1;
for (iterator = counter; iterator > 0; iterator--) {
if (expression[iterator] == ']') {
expression[iterator] = 0;
break;
}
}
break;
} else if (expression[index] == '<') {
/* HIT A < BEFORE A [ THEREFORE */
/* IS A FUNCTION CALL, NOT AN ARRAY */
return (nullptr);
} else if (expression[index] == '(') {
/* HIT A ( BEFORE A [ THEREFORE */
/* IS AN OBJECT ELEMENT, NOT AN ARRAY */
return (nullptr);
} else if (expression[index] == ' ')
return (nullptr);
}
if (delimiter == 0) {
/* NO DELIMITER FOUND, TRY AS UNINDEXED VARIABLE */
return (string_resolve_indexed(name, 0));
}
if (delimiter == 1) {
/* NO STRING BEFORE DELIMITER */
return (nullptr);
}
counter = value_of(&expression[delimiter], TRUE);
if (counter > -1) {
return (string_resolve_indexed(expression, counter));
} else
return (nullptr);
}
struct string_type *string_resolve_indexed(const char *name, int index) {
struct string_type *pointer = string_table;
if (pointer == nullptr)
return (nullptr);
do {
if (!strcmp(name, pointer->name)) {
if (index == 0) {
return (pointer);
} else {
/* THIS STRING DOES MATCH, BUT WERE NOT AT THE
* RIGHT INDEX YET SO MOVE ON */
pointer = pointer->next_string;
index--;
}
} else {
pointer = pointer->next_string;
}
} while (pointer != nullptr);
return (nullptr);
}
struct string_type *cstring_resolve(const char *name) {
int index,
iterator,
counter;
int delimiter = 0;
char expression[84];
strncpy(expression, name, 80);
counter = strlen(expression);
for (index = 0; index < counter; index++) {
if (expression[index] == '[') {
expression[index] = 0;
delimiter = index + 1;
for (iterator = counter; iterator > 0; iterator--) {
if (expression[iterator] == ']') {
expression[iterator] = 0;
break;
}
}
break;
} else if (expression[index] == '<') {
/* HIT A < BEFORE A [ THEREFORE */
/* IS A FUNCTION CALL, NOT AN ARRAY */
return (nullptr);
} else if (expression[index] == '(') {
/* HIT A ( BEFORE A [ THEREFORE */
/* IS AN OBJECT ELEMENT, NOT AN ARRAY */
return (nullptr);
} else if (expression[index] == ' ')
return (nullptr);
}
if (delimiter == 0) {
/* NO DELIMITER FOUND, TRY AS UNINDEXED VARIABLE */
return (cstring_resolve_indexed(name, 0));
}
if (delimiter == 1) {
/* NO STRING BEFORE DELIMITER */
return (nullptr);
}
counter = value_of(&expression[delimiter], TRUE);
if (counter > -1) {
return (cstring_resolve_indexed(expression, counter));
} else
return (nullptr);
}
struct string_type *cstring_resolve_indexed(const char *name, int index) {
struct string_type *pointer = cstring_table;
if (pointer == nullptr)
return (nullptr);
do {
if (!strcmp(name, pointer->name)) {
if (index == 0) {
return (pointer);
} else {
/* THIS STRING DOES MATCH, BUT WERE NOT AT THE
* RIGHT INDEX YET SO MOVE ON */
pointer = pointer->next_string;
index--;
}
} else {
pointer = pointer->next_string;
}
} while (pointer != nullptr);
return (nullptr);
}
struct function_type *function_resolve(const char *name) {
const char *full_name;
char core_name[84];
int index;
struct function_type *pointer = function_table;
if (function_table == nullptr)
return (nullptr);
/* STRIP ARGUMENTS OFF FIRST, THEN EXPAND RESOLVE NAME */
index = 0;
while (*name && index < 80) {
if (*name == '<') {
break;
} else {
core_name[index++] = *name++;
}
}
core_name[index] = 0;
/* GET A POINTER TO A STRING THAT REPRESENTS THE EXPANDED NAME OF THE FUNCTION */
full_name = (const char *)expand_function(core_name);
/* LOOP THROUGH ALL THE FUNCTIONS LOOKING FOR A FUNCTION THAT
* HAS THIS EXPANDED FULL NAME */
do {
if (!strcmp(full_name, pointer->name))
return (pointer);
else
pointer = pointer->next_function;
} while (pointer != nullptr);
/* RETURN A POINTER TO THE STRUCTURE THAT ENCAPSULATES THE FUNCTION */
return (nullptr);
}
const char *expand_function(const char *name) {
/* THIS FUNCTION TAKES A SCOPE FUNCTION CALL SUCH AS noun1.function
* AND REOLVE THE ACTUAL FUNCTION NAME SUCH AS function_key */
int index,
counter;
int delimiter = 0;
char expression[84];
strncpy(expression, name, 80);
counter = strlen(expression);
for (index = 0; index < counter; index++) {
if (expression[index] == '.') {
expression[index] = 0;
delimiter = index + 1;
break;
}
}
if (delimiter == FALSE) {
/* THIS FUNCTION DOESN'T CONTAIN A '.', SO RETURN IT AS IS */
return (arg_text_of(name));
}
/* THE ORIGINAL STRING IS NOW CUT INTO TWO STRINGS:
* expression.delimiter */
index = value_of(expression, TRUE);
if (index < 1 || index > objects) {
return ((const char *) name);
}
if (cinteger_resolve(&expression[delimiter]) != nullptr ||
integer_resolve(&expression[delimiter]) != nullptr ||
object_element_resolve(&expression[delimiter])) {
/* THE DELIMETER RESOLVES TO A CONSTANT, VARIABLE OR OBJECT
* ELEMENT, SO TAKE NOTE OF THAT */
Common::sprintf_s(function_name, 81, "%ld", value_of(&expression[delimiter], TRUE));
} else {
Common::strcpy_s(function_name, 81, &expression[delimiter]);
}
Common::strcat_s(function_name, 81, "_");
Common::strcat_s(function_name, 81, object[index]->label);
return ((const char *) function_name);
}
char *macro_resolve(const char *testString) {
int index,
counter;
int delimiter = 0;
char expression[84];
strncpy(expression, testString, 80);
counter = strlen(expression);
for (index = 0; index < counter; index++) {
if (expression[index] == '{' || expression[index] == '}') {
expression[index] = 0;
if (!delimiter)
delimiter = index + 1;
}
}
if (delimiter == FALSE)
return (nullptr);
if (*expression != 0) {
index = value_of(expression, TRUE);
} else {
index = 0;
}
if (!strcmp(&expression[delimiter], "list")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (list_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "plain")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (plain_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "long")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (long_output(index));
}
} else if (!strcmp(&expression[delimiter], "sub")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (sub_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "obj")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (obj_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "that")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (that_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "it")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (it_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "doesnt")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (doesnt_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "does")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (does_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "isnt")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (isnt_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "is")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (is_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "the")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (sentence_output(index, FALSE));
}
} else if (!strcmp(&expression[delimiter], "s")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
if (object[index]->attributes & PLURAL) {
temp_buffer[0] = '\0';
} else {
temp_buffer[0] = 's';
temp_buffer[1] = '\0';
}
return (temp_buffer);
}
} else if (!strcmp(&expression[delimiter], "names")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (object_names(index, temp_buffer));
}
} else if (!strcmp(&expression[delimiter], "label")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (object[index]->label);
}
} else if (!strcmp(&expression[delimiter], "List")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (list_output(index, TRUE));
}
} else if (!strcmp(&expression[delimiter], "Plain")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (plain_output(index, TRUE));
}
} else if (!strcmp(&expression[delimiter], "Sub")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (sub_output(index, TRUE));
}
} else if (!strcmp(&expression[delimiter], "Obj")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (obj_output(index, TRUE));
}
} else if (!strcmp(&expression[delimiter], "That")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (that_output(index, TRUE));
}
} else if (!strcmp(&expression[delimiter], "It")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (it_output(index, TRUE));
}
} else if (!strcmp(&expression[delimiter], "Doesnt")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (doesnt_output(index, TRUE));
}
} else if (!strcmp(&expression[delimiter], "Does")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (does_output(index, TRUE));
}
} else if (!strcmp(&expression[delimiter], "Isnt")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (isnt_output(index, TRUE));
}
} else if (!strcmp(&expression[delimiter], "Is")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (is_output(index, TRUE));
}
} else if (!strcmp(&expression[delimiter], "The")) {
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (nullptr);
} else {
return (sentence_output(index, TRUE));
}
} else {
Common::strcpy_s(macro_function, "+macro_");
Common::strcat_s(macro_function, &expression[delimiter]);
Common::strcat_s(macro_function, "<");
Common::sprintf_s(temp_buffer, 1024, "%d", index);
Common::strcat_s(macro_function, temp_buffer);
// BUILD THE FUNCTION NAME AND PASS THE OBJECT AS
// THE ONLY ARGUMENT
if (execute(macro_function)) {
return (string_resolve("return_value")->value);
}
}
return (nullptr);
}
int count_resolve(const char *testString) {
struct function_type *resolved_function = nullptr;
if (*(testString + 1) == 0) {
// @ ON ITS OWN, SO RETURN THE CALL COUNT OF THE CURRENTLY EXECUTING
// FUNCTION
return (executing_function->call_count);
} else if ((resolved_function = function_resolve(testString + 1)) != nullptr) {
return (resolved_function->call_count);
} else {
return array_length_resolve(testString);
}
}
int array_length_resolve(const char *testString) {
int counter = 0;
const char *array_name = &testString[1];
struct integer_type *integer_pointer = integer_table;
struct cinteger_type *cinteger_pointer = cinteger_table;
struct string_type *string_pointer = string_table;
struct string_type *cstring_pointer = cstring_table;
if (integer_pointer != nullptr) {
do {
if (!strcmp(array_name, integer_pointer->name)) {
counter++;
}
integer_pointer = integer_pointer->next_integer;
} while (integer_pointer != nullptr);
}
/* IF ONE OR MORE INTEGERS WITH THIS NAME WERE FOUND
RETURN THE COUNT */
if (counter)
return (counter);
if (string_pointer != nullptr) {
do {
if (!strcmp(array_name, string_pointer->name)) {
counter++;
}
string_pointer = string_pointer->next_string;
} while (string_pointer != nullptr);
}
/* IF ONE OR MORE STRINGS WITH THIS NAME WERE FOUND
RETURN THE COUNT */
if (counter)
return (counter);
if (cinteger_pointer != nullptr) {
do {
if (!strcmp(array_name, cinteger_pointer->name)) {
counter++;
}
cinteger_pointer = cinteger_pointer->next_cinteger;
} while (cinteger_pointer != nullptr);
}
/* IF ONE OR MORE INTEGER CONSTANTS WITH THIS NAME WERE FOUND
RETURN THE COUNT */
if (counter)
return (counter);
if (cstring_pointer != nullptr) {
do {
if (!strcmp(array_name, cstring_pointer->name)) {
counter++;
}
cstring_pointer = cstring_pointer->next_string;
} while (cstring_pointer != nullptr);
}
/* IF ONE OR MORE STRING CONSTANTS WITH THIS NAME WERE FOUND
RETURN THE COUNT */
if (counter)
return (counter);
/* NO VARIABLES OR STRINGS FOUND */
return (0);
}
int object_element_resolve(const char *testString) {
int index,
iterator,
counter;
int delimiter = 0;
char expression[84];
struct integer_type *resolved_integer;
struct cinteger_type *resolved_cinteger;
strncpy(expression, testString, 80);
//sprintf(temp_buffer, "incoming = %s^", testString);
//write_text (temp_buffer);
counter = strlen(expression);
for (index = 0; index < counter; index++) {
if (expression[index] == '(') {
expression[index] = 0;
delimiter = index + 1;
for (iterator = counter; iterator > 0; iterator--) {
if (expression[iterator] == ')') {
expression[iterator] = 0;
break;
}
}
break;
} else if (expression[index] == '<') {
/* HIT A < BEFORE A [ THEREFORE */
/* IS A FUNCTION CALL, NOT AN ARRAY */
return (FALSE);
} else if (expression[index] == '[') {
/* HIT A [ BEFORE A ( THEREFORE */
/* THIS EXPRESSION IS AN ARRAY, NOT AN OBJECT ELEMENT */
/* UNLESS A CLOSING ] IS FOUND BEFORE THE OPENING ( */
/* ie. COULD BE AN array[index](element) FORMAT */
/* SEARCH FORWARD... */
for (; index < counter; index++) {
if (expression[index] == ']') {
/* BREAK OUT AND KEEP LOOKING FOR A ( */
break;
} else if (expression[index] == '(') {
/* THIS EXPRESSION IS DEFINITELY AN ARRAY WITH AN */
/* OBJECT ELEMENT AS THE INDEX */
return (FALSE);
}
}
} else if (expression[index] == ' ')
return (FALSE);
}
// NO DELIMITER FOUND OR NO STRING BEFORE DELIMITER
if (delimiter == FALSE || delimiter == 1)
return (FALSE);
index = object_resolve(expression);
if (index == -1) {
//sprintf(temp_buffer, "expression %s is not an object^", expression);
//write_text(temp_buffer);
// COULDN'T BE RESOLVED AS AN OBJECT, TRY AS A VARIABLE
if ((resolved_integer = integer_resolve(expression)) != nullptr) {
index = resolved_integer->value;
} else if ((resolved_cinteger = cinteger_resolve(expression)) != nullptr) {
index = resolved_cinteger->value;
}
}
if (index < 1 || index > objects) {
badptrrun(expression, index);
return (FALSE);
}
counter = value_of(&expression[delimiter], TRUE);
if (counter < 0 || counter > 15) {
Common::sprintf_s(error_buffer, 1024,
"ERROR: In function \"%s\", element \"%s\" out of range (%d).^",
executing_function->name, &expression[delimiter], counter);
write_text(error_buffer);
return (FALSE);
} else {
oec = object[index]->integer[counter];
object_element_address = &object[index]->integer[counter];
return (TRUE);
}
}
int object_resolve(const char *object_string) {
int index;
if (!strcmp(object_string, "noun1"))
return (noun[0]);
else if (!strcmp(object_string, "noun2"))
return (noun[1]);
else if (!strcmp(object_string, "noun3"))
return (noun[2]);
else if (!strcmp(object_string, "noun4"))
return (noun[3]);
else if (!strcmp(object_string, "player"))
return (player);
else if (!strcmp(object_string, "here"))
return (HERE);
else if (!strcmp(object_string, "self") ||
!strcmp(object_string, "this")) {
if (executing_function != nullptr && executing_function->self == 0) {
Common::sprintf_s(error_buffer, 1024,
"ERROR: Reference to 'self' from global function \"%s\".^",
executing_function->name);
write_text(error_buffer);
} else
return (executing_function->self);
} else {
for (index = 1; index <= objects; index++) {
if (!strcmp(object_string, object[index]->label))
return (index);
}
}
return (-1);
}
long attribute_resolve(const char *attribute) {
long bit_mask;
if (!strcmp(attribute, "VISITED"))
return (VISITED);
else if (!strcmp(attribute, "DARK"))
return (DARK);
else if (!strcmp(attribute, "ON_WATER"))
return (ON_WATER);
else if (!strcmp(attribute, "UNDER_WATER"))
return (UNDER_WATER);
else if (!strcmp(attribute, "WITHOUT_AIR"))
return (WITHOUT_AIR);
else if (!strcmp(attribute, "OUTDOORS"))
return (OUTDOORS);
else if (!strcmp(attribute, "MID_AIR"))
return (MID_AIR);
else if (!strcmp(attribute, "TIGHT_ROPE"))
return (TIGHT_ROPE);
else if (!strcmp(attribute, "POLLUTED"))
return (POLLUTED);
else if (!strcmp(attribute, "SOLVED"))
return (SOLVED);
else if (!strcmp(attribute, "MID_WATER"))
return (MID_WATER);
else if (!strcmp(attribute, "DARKNESS")) {
bit_mask = DARKNESS;
if (check_light(HERE)) {
bit_mask = ~bit_mask;
object[HERE]->attributes = object[HERE]->attributes & bit_mask;
} else {
object[HERE]->attributes = object[HERE]->attributes | bit_mask;
}
return (DARKNESS);
} else if (!strcmp(attribute, "MAPPED"))
return (MAPPED);
else if (!strcmp(attribute, "KNOWN"))
return (KNOWN);
else if (!strcmp(attribute, "CLOSED"))
return (CLOSED);
else if (!strcmp(attribute, "LOCKED"))
return (LOCKED);
else if (!strcmp(attribute, "DEAD"))
return (DEAD);
else if (!strcmp(attribute, "IGNITABLE"))
return (IGNITABLE);
else if (!strcmp(attribute, "WORN"))
return (WORN);
else if (!strcmp(attribute, "CONCEALING"))
return (CONCEALING);
else if (!strcmp(attribute, "LUMINOUS"))
return (LUMINOUS);
else if (!strcmp(attribute, "WEARABLE"))
return (WEARABLE);
else if (!strcmp(attribute, "CLOSABLE"))
return (CLOSABLE);
else if (!strcmp(attribute, "LOCKABLE"))
return (LOCKABLE);
else if (!strcmp(attribute, "ANIMATE"))
return (ANIMATE);
else if (!strcmp(attribute, "LIQUID"))
return (LIQUID);
else if (!strcmp(attribute, "CONTAINER"))
return (CONTAINER);
else if (!strcmp(attribute, "SURFACE"))
return (SURFACE);
else if (!strcmp(attribute, "PLURAL"))
return (PLURAL);
else if (!strcmp(attribute, "FLAMMABLE"))
return (FLAMMABLE);
else if (!strcmp(attribute, "BURNING"))
return (BURNING);
else if (!strcmp(attribute, "LOCATION"))
return (LOCATION);
else if (!strcmp(attribute, "ON"))
return (ON);
else if (!strcmp(attribute, "DAMAGED"))
return (DAMAGED);
else if (!strcmp(attribute, "FEMALE"))
return (FEMALE);
else if (!strcmp(attribute, "POSSESSIVE"))
return (POSSESSIVE);
else if (!strcmp(attribute, "OUT_OF_REACH"))
return (OUT_OF_REACH);
else if (!strcmp(attribute, "TOUCHED"))
return (TOUCHED);
else if (!strcmp(attribute, "SCORED"))
return (SCORED);
else if (!strcmp(attribute, "SITTING"))
return (SITTING);
else if (!strcmp(attribute, "NPC"))
return (NPC);
else if (!strcmp(attribute, "DONE"))
return (DONE);
else if (!strcmp(attribute, "GAS"))
return (MAPPED);
else if (!strcmp(attribute, "NO_TAB"))
return (NO_TAB);
else if (!strcmp(attribute, "NOT_IMPORTANT"))
return (NOT_IMPORTANT);
else
return (0);
}
long user_attribute_resolve(const char *name) {
struct attribute_type *pointer = attribute_table;
if (pointer == nullptr)
return (0);
do {
if (!strcmp(name, pointer->name)) {
return (pointer->value);
} else
pointer = pointer->next_attribute;
} while (pointer != nullptr);
/* ATTRIBUTE NOT FOUND */
return (0);
}
} // End of namespace JACL
} // End of namespace Glk
|