| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "parse.h" |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <ctype.h> |
| 14 |
|
| 15 |
#include "AltInfo.h" |
| 16 |
#include "inter.h" |
| 17 |
#include "current.h" |
| 18 |
#include "act.h" |
| 19 |
#include "term.h" |
| 20 |
#include "lists.h" |
| 21 |
#include "options.h" |
| 22 |
#include "syserr.h" |
| 23 |
#include "Location.h" |
| 24 |
#include "instance.h" |
| 25 |
#include "class.h" |
| 26 |
#include "memory.h" |
| 27 |
#include "output.h" |
| 28 |
#include "dictionary.h" |
| 29 |
#include "syntax.h" |
| 30 |
#include "word.h" |
| 31 |
#include "msg.h" |
| 32 |
#include "literal.h" |
| 33 |
#include "ParameterPosition.h" |
| 34 |
#include "utils.h" |
| 35 |
#include "compatibility.h" |
| 36 |
#include "scan.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
typedef struct PronounEntry { |
| 47 |
int pronoun; |
| 48 |
int instance; |
| 49 |
} Pronoun; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static void clearPronounList(Pronoun list[]) { |
| 54 |
implementationOfSetEndOfArray((Aword *)list); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
typedef Aint *(*ReferencesFinder)(int wordIndex); |
| 59 |
typedef void (*ParameterParser)(Parameter parameters[]); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static Pronoun *pronouns = NULL; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static Parameter *previousMultipleParameters; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
static void addParameterForWord(Parameter *parameters, int wordIndex) { |
| 78 |
Parameter *parameter = findEndOfParameterArray(parameters); |
| 79 |
|
| 80 |
createStringLiteral(pointerTo(dictionary[playerWords[wordIndex].code].string)); |
| 81 |
parameter->instance = instanceFromLiteral(litCount); |
| 82 |
parameter->useWords = true; |
| 83 |
parameter->firstWord = parameter->lastWord = wordIndex; |
| 84 |
setEndOfArray(parameter+1); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
static Pronoun *allocatePronounArray(Pronoun *currentList) { |
| 90 |
if (currentList == NULL) |
| 91 |
currentList = allocate(sizeof(Pronoun)*(MAXPARAMS+1)); |
| 92 |
clearPronounList(currentList); |
| 93 |
return currentList; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
static bool endOfWords(int wordIndex) { |
| 99 |
return isEndOfArray(&playerWords[wordIndex]); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
static bool endOfPlayerCommand(int wordIndex) { |
| 104 |
return endOfWords(wordIndex) || isConjunctionWord(wordIndex); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
static void handleDirectionalCommand() { |
| 109 |
verbWord = playerWords[currentWordIndex].code; |
| 110 |
currentWordIndex++; |
| 111 |
if (!endOfPlayerCommand(currentWordIndex)) |
| 112 |
error(M_WHAT); |
| 113 |
else |
| 114 |
go(current.location, dictionary[playerWords[currentWordIndex-1].code].code); |
| 115 |
if (!endOfWords(currentWordIndex)) |
| 116 |
currentWordIndex++; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static void errorWhichOne(Parameter alternative[]) { |
| 122 |
int p; |
| 123 |
ParameterArray parameters = newParameterArray(); |
| 124 |
|
| 125 |
parameters[0] = alternative[0]; |
| 126 |
setEndOfArray(¶meters[1]); |
| 127 |
printMessageWithParameters(M_WHICH_ONE_START, parameters); |
| 128 |
for (p = 1; !isEndOfArray(&alternative[p+1]); p++) { |
| 129 |
clearParameterArray(parameters); |
| 130 |
addParameterToParameterArray(parameters, &alternative[p]); |
| 131 |
printMessageWithParameters(M_WHICH_ONE_COMMA, parameters); |
| 132 |
} |
| 133 |
clearParameterArray(parameters); |
| 134 |
addParameterToParameterArray(parameters, &alternative[p]); |
| 135 |
printMessageWithParameters(M_WHICH_ONE_OR, parameters); |
| 136 |
freeParameterArray(parameters); |
| 137 |
abortPlayerCommand(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
static void errorWhichPronoun(int pronounWordIndex, Parameter alternatives[]) { |
| 142 |
int p; |
| 143 |
Parameter *messageParameters = newParameterArray(); |
| 144 |
|
| 145 |
addParameterForWord(messageParameters, pronounWordIndex); |
| 146 |
printMessageWithParameters(M_WHICH_PRONOUN_START, messageParameters); |
| 147 |
|
| 148 |
clearParameterArray(messageParameters); |
| 149 |
addParameterToParameterArray(messageParameters, &alternatives[0]); |
| 150 |
|
| 151 |
printMessageWithParameters(M_WHICH_PRONOUN_FIRST, messageParameters); |
| 152 |
|
| 153 |
for (p = 1; !isEndOfArray(&alternatives[p+1]); p++) { |
| 154 |
clearParameterArray(messageParameters); |
| 155 |
addParameterToParameterArray(messageParameters, &alternatives[p]); |
| 156 |
printMessageWithParameters(M_WHICH_ONE_COMMA, messageParameters); |
| 157 |
} |
| 158 |
clearParameterArray(messageParameters); |
| 159 |
addParameterToParameterArray(messageParameters, &alternatives[p]); |
| 160 |
printMessageWithParameters(M_WHICH_ONE_OR, messageParameters); |
| 161 |
freeParameterArray(messageParameters); |
| 162 |
abortPlayerCommand(); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
static void errorWhat(int playerWordIndex) { |
| 167 |
Parameter *messageParameters = newParameterArray(); |
| 168 |
|
| 169 |
addParameterForWord(messageParameters, playerWordIndex); |
| 170 |
printMessageWithParameters(M_WHAT_WORD, messageParameters); |
| 171 |
freeParameterArray(messageParameters); |
| 172 |
abortPlayerCommand(); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
static void errorAfterExcept(int butWordIndex) { |
| 177 |
Parameter *messageParameters = newParameterArray(); |
| 178 |
addParameterForWord(messageParameters, butWordIndex); |
| 179 |
printMessageWithParameters(M_AFTER_BUT, messageParameters); |
| 180 |
freeParameterArray(messageParameters); |
| 181 |
abortPlayerCommand(); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
static int fakePlayerWordForAll() { |
| 186 |
|
| 187 |
|
| 188 |
int p, d; |
| 189 |
|
| 190 |
for (p = 0; !isEndOfArray(&playerWords[p]); p++) |
| 191 |
; |
| 192 |
setEndOfArray(&playerWords[p+1]); |
| 193 |
for (d = 0; d < dictionarySize; d++) |
| 194 |
if (isAll(d)) { |
| 195 |
playerWords[p].code = d; |
| 196 |
return p; |
| 197 |
} |
| 198 |
syserr("No ALLWORD found"); |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
static void errorButAfterAll(int butWordIndex) { |
| 204 |
Parameter *messageParameters = newParameterArray(); |
| 205 |
addParameterForWord(messageParameters, butWordIndex); |
| 206 |
addParameterForWord(messageParameters, fakePlayerWordForAll()); |
| 207 |
printMessageWithParameters(M_BUT_ALL, messageParameters); |
| 208 |
freeParameterArray(messageParameters); |
| 209 |
abortPlayerCommand(); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
static Aint findInstanceForNoun(int wordIndex) { |
| 214 |
DictionaryEntry *d = &dictionary[wordIndex]; |
| 215 |
if (d->nounRefs == 0 || d->nounRefs == EOF) |
| 216 |
syserr("No references for noun"); |
| 217 |
return *(Aint*) pointerTo(d->nounRefs); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
static void errorNoSuch(Parameter parameter) { |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if (parameter.instance == -1) |
| 226 |
parameter.instance = 0; |
| 227 |
if (parameter.instance == 0) |
| 228 |
parameter.instance = findInstanceForNoun(playerWords[parameter.lastWord].code); |
| 229 |
parameter.useWords = true; |
| 230 |
|
| 231 |
clearParameterArray(globalParameters); |
| 232 |
addParameterToParameterArray(globalParameters, ¶meter); |
| 233 |
error(M_NO_SUCH); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
static void buildAllHere(Parameter list[]) { |
| 238 |
int instance; |
| 239 |
bool found = false; |
| 240 |
int word = list[0].firstWord; |
| 241 |
|
| 242 |
for (instance = 1; instance <= header->instanceMax; instance++) |
| 243 |
if (isHere(instance, false)) { |
| 244 |
Parameter *parameter = newParameter(instance); |
| 245 |
addParameterToParameterArray(list, parameter); |
| 246 |
deallocate(parameter); |
| 247 |
found = true; |
| 248 |
} |
| 249 |
if (!found) |
| 250 |
errorWhat(word); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
static bool endOfPronouns(int pronounIndex) { |
| 256 |
return isEndOfArray(&pronouns[pronounIndex]); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
static int getPronounInstances(int word, Parameter instanceParameters[]) { |
| 262 |
|
| 263 |
|
| 264 |
int p; |
| 265 |
int instanceCount = 0; |
| 266 |
|
| 267 |
clearParameterArray(instanceParameters); |
| 268 |
for (p = 0; !endOfPronouns(p); p++) |
| 269 |
if (pronouns[p].instance != 0 && dictionary[word].code == pronouns[p].pronoun) { |
| 270 |
instanceParameters[instanceCount].instance = pronouns[p].instance; |
| 271 |
instanceParameters[instanceCount].useWords = false; |
| 272 |
|
| 273 |
setEndOfArray(&instanceParameters[++instanceCount]); |
| 274 |
} |
| 275 |
return instanceCount; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
static bool inOpaqueContainer(int originalInstance) { |
| 280 |
int instance = admin[originalInstance].location; |
| 281 |
|
| 282 |
while (isAContainer(instance)) { |
| 283 |
|
| 284 |
if (getInstanceAttribute(instance, OPAQUEATTRIBUTE)) |
| 285 |
return true; |
| 286 |
instance = admin[instance].location; |
| 287 |
} |
| 288 |
return false; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
static bool reachable(int instance) { |
| 293 |
if (isA(instance, THING) || isA(instance, LOCATION)) |
| 294 |
return isHere(instance, TRANSITIVE) && !inOpaqueContainer(instance); |
| 295 |
else |
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
static Aint *nounReferencesForWord(int wordIndex) { |
| 301 |
return (Aint *) pointerTo(dictionary[playerWords[wordIndex].code].nounRefs); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
static Aint *adjectiveReferencesForWord(int wordIndex) { |
| 307 |
return (Aint *) pointerTo(dictionary[playerWords[wordIndex].code].adjectiveRefs); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static void parseLiteral(Parameter parameters[]) { |
| 313 |
parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++; |
| 314 |
parameters[0].instance = 0; |
| 315 |
parameters[0].isLiteral = true; |
| 316 |
setEndOfArray(¶meters[1]); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
static void parsePronoun(Parameter parameters[]) { |
| 322 |
parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++; |
| 323 |
parameters[0].instance = 0; |
| 324 |
parameters[0].isPronoun = true; |
| 325 |
setEndOfArray(¶meters[1]); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
static bool anotherAdjective(int wordIndex) { |
| 331 |
return !endOfWords(wordIndex) && isAdjectiveWord(wordIndex); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
static bool lastPossibleNoun(int wordIndex) { |
| 337 |
return isNounWord(wordIndex) && (endOfWords(wordIndex+1) || !isNounWord(wordIndex+1)); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
static void updateWithReferences(Parameter result[], int wordIndex, Aint *(*referenceFinder)(int wordIndex)) { |
| 343 |
static Parameter *references = NULL; |
| 344 |
references = ensureParameterArrayAllocated(references); |
| 345 |
|
| 346 |
copyReferencesToParameterArray(referenceFinder(wordIndex), references); |
| 347 |
if (lengthOfParameterArray(result) == 0) |
| 348 |
copyParameterArray(result, references); |
| 349 |
else |
| 350 |
intersectParameterArrays(result, references); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
static void filterOutNonReachable(Parameter filteredCandidates[], bool (*reachable)(int)) { |
| 356 |
int i; |
| 357 |
for (i = 0; !isEndOfArray(&filteredCandidates[i]); i++) |
| 358 |
if (!reachable(filteredCandidates[i].instance)) |
| 359 |
filteredCandidates[i].instance = 0; |
| 360 |
compressParameterArray(filteredCandidates); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
static void disambiguateCandidatesForPosition(ParameterPosition parameterPositions[], int position, Parameter candidates[]) { |
| 377 |
int i; |
| 378 |
Parameter *parameters = newParameterArray(); |
| 379 |
|
| 380 |
convertPositionsToParameters(parameterPositions, parameters); |
| 381 |
for (i = 0; !isEndOfArray(&candidates[i]); i++) { |
| 382 |
if (candidates[i].instance != 0) { |
| 383 |
copyParameter(¶meters[position], &candidates[i]); |
| 384 |
|
| 385 |
if (!reachable(candidates[i].instance) || !possible(current.verb, parameters, parameterPositions)) |
| 386 |
candidates[i].instance = 0; |
| 387 |
} |
| 388 |
} |
| 389 |
compressParameterArray(candidates); |
| 390 |
freeParameterArray(parameters); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
static bool parseAnyAdjectives(Parameter parameters[]) { |
| 396 |
bool adjectiveOrNounFound = false; |
| 397 |
while (anotherAdjective(currentWordIndex)) { |
| 398 |
if (lastPossibleNoun(currentWordIndex)) |
| 399 |
break; |
| 400 |
adjectiveOrNounFound = true; |
| 401 |
currentWordIndex++; |
| 402 |
} |
| 403 |
return adjectiveOrNounFound; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
static void parseAdjectivesAndNoun(Parameter parameters[]) { |
| 411 |
int firstWord, lastWord; |
| 412 |
bool adjectiveOrNounFound = false; |
| 413 |
|
| 414 |
firstWord = currentWordIndex; |
| 415 |
|
| 416 |
adjectiveOrNounFound = parseAnyAdjectives(parameters); |
| 417 |
|
| 418 |
if (!endOfWords(currentWordIndex)) { |
| 419 |
if (isNounWord(currentWordIndex)) { |
| 420 |
adjectiveOrNounFound = true; |
| 421 |
currentWordIndex++; |
| 422 |
} else |
| 423 |
error(M_NOUN); |
| 424 |
} else if (adjectiveOrNounFound) { |
| 425 |
|
| 426 |
if (isNounWord(currentWordIndex - 1)) { |
| 427 |
|
| 428 |
printf("DEBUG: When does this get executed? Found adjective or Noun and the previous word could also be a noun...\n"); |
| 429 |
} else |
| 430 |
error(M_NOUN); |
| 431 |
} |
| 432 |
|
| 433 |
if (adjectiveOrNounFound) { |
| 434 |
lastWord = currentWordIndex - 1; |
| 435 |
|
| 436 |
parameters[0].firstWord = firstWord; |
| 437 |
parameters[0].lastWord = lastWord; |
| 438 |
parameters[0].instance = 0; |
| 439 |
setEndOfArray(¶meters[1]); |
| 440 |
} else |
| 441 |
setEndOfArray(¶meters[0]); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
static void parseReference(Parameter parameters[]) { |
| 447 |
clearParameterArray(parameters); |
| 448 |
|
| 449 |
if (isLiteralWord(currentWordIndex)) { |
| 450 |
parseLiteral(parameters); |
| 451 |
} else if (isPronounWord(currentWordIndex)) { |
| 452 |
parsePronoun(parameters); |
| 453 |
} else { |
| 454 |
parseAdjectivesAndNoun(parameters); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
static void getPreviousMultipleParameters(Parameter parameters[]) { |
| 461 |
int i; |
| 462 |
for (i = 0; !isEndOfArray(&previousMultipleParameters[i]); i++) { |
| 463 |
parameters[i].candidates = ensureParameterArrayAllocated(parameters[i].candidates); |
| 464 |
setEndOfArray(¶meters[i].candidates[0]); |
| 465 |
if (!reachable(previousMultipleParameters[i].instance)) |
| 466 |
parameters[i].instance = 0; |
| 467 |
else |
| 468 |
parameters[i].instance = previousMultipleParameters[i].instance; |
| 469 |
} |
| 470 |
setEndOfArray(¶meters[i]); |
| 471 |
compressParameterArray(parameters); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
static void parseReferenceToPreviousMultipleParameters(Parameter parameters[]) { |
| 477 |
parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++; |
| 478 |
parameters[0].instance = 0; |
| 479 |
parameters[0].isThem = true; |
| 480 |
setEndOfArray(¶meters[1]); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
static bool parseOneParameter(Parameter parameters[], int parameterIndex) { |
| 486 |
Parameter *parameter = newParameterArray(); |
| 487 |
|
| 488 |
|
| 489 |
if (isThemWord(currentWordIndex) && (!isPronounWord(currentWordIndex) || |
| 490 |
(isPronounWord(currentWordIndex) && lengthOfParameterArray(previousMultipleParameters) > 0))) { |
| 491 |
|
| 492 |
|
| 493 |
parseReferenceToPreviousMultipleParameters(parameter); |
| 494 |
} else { |
| 495 |
parseReference(parameter); |
| 496 |
if (lengthOfParameterArray(parameter) == 0) { |
| 497 |
freeParameterArray(parameter); |
| 498 |
return false; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
parameters[parameterIndex] = parameter[0]; |
| 504 |
setEndOfArray(¶meters[parameterIndex+1]); |
| 505 |
freeParameterArray(parameter); |
| 506 |
return true; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
static void simpleParameterParser(Parameter parameters[]) { |
| 530 |
|
| 531 |
|
| 532 |
int parameterIndex; |
| 533 |
for (parameterIndex = 0;; parameterIndex++) { |
| 534 |
|
| 535 |
if(!parseOneParameter(parameters, parameterIndex)) |
| 536 |
return; |
| 537 |
|
| 538 |
if (endOfWords(currentWordIndex)) |
| 539 |
return; |
| 540 |
|
| 541 |
int index = currentWordIndex; |
| 542 |
if (isConjunctionWord(index)) |
| 543 |
index ++; |
| 544 |
else |
| 545 |
return; |
| 546 |
|
| 547 |
if (endOfWords(index)) |
| 548 |
return; |
| 549 |
|
| 550 |
if (isConjunctionWord(index)) |
| 551 |
index ++; |
| 552 |
|
| 553 |
if (endOfWords(index)) |
| 554 |
return; |
| 555 |
|
| 556 |
if (isAdjectiveWord(index) || isNounWord(index)) { |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
currentWordIndex = index; |
| 561 |
} else |
| 562 |
|
| 563 |
return; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
static void parseExceptions(ParameterPosition *parameterPosition, ParameterParser simpleParameterParser) { |
| 570 |
int exceptWordIndex = currentWordIndex; |
| 571 |
currentWordIndex++; |
| 572 |
parameterPosition->exceptions = ensureParameterArrayAllocated(parameterPosition->exceptions); |
| 573 |
simpleParameterParser(parameterPosition->exceptions); |
| 574 |
if (lengthOfParameterArray(parameterPosition->exceptions) == 0) |
| 575 |
errorAfterExcept(exceptWordIndex); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
static void complexParameterParserDelegate(ParameterPosition *parameterPosition, ParameterParser simpleParameterParser) { |
| 591 |
parameterPosition->parameters = ensureParameterArrayAllocated(parameterPosition->parameters); |
| 592 |
|
| 593 |
parameterPosition->all = false; |
| 594 |
parameterPosition->them = false; |
| 595 |
parameterPosition->explicitMultiple = false; |
| 596 |
|
| 597 |
if (isAllWord(currentWordIndex)) { |
| 598 |
parameterPosition->all = true; |
| 599 |
parameterPosition->explicitMultiple = true; |
| 600 |
parameterPosition->parameters[0].firstWord = currentWordIndex; |
| 601 |
parameterPosition->parameters[0].lastWord = currentWordIndex; |
| 602 |
currentWordIndex++; |
| 603 |
parameterPosition->parameters[0].instance = 0; |
| 604 |
setEndOfArray(¶meterPosition->parameters[1]); |
| 605 |
if (!endOfWords(currentWordIndex) && isExceptWord(currentWordIndex)) |
| 606 |
parseExceptions(parameterPosition, simpleParameterParser); |
| 607 |
} else { |
| 608 |
simpleParameterParser(parameterPosition->parameters); |
| 609 |
if (lengthOfParameterArray(parameterPosition->parameters) > 1) |
| 610 |
parameterPosition->explicitMultiple = true; |
| 611 |
} |
| 612 |
|
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
static void complexReferencesParser(ParameterPosition *parameterPosition) { |
| 617 |
complexParameterParserDelegate(parameterPosition, simpleParameterParser); |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
static char *classNameAndId(int classId) { |
| 624 |
static char buffer[1000] = ""; |
| 625 |
|
| 626 |
if (classId != -1) |
| 627 |
sprintf(buffer, "%s[%d]", idOfClass(classId), classId); |
| 628 |
else |
| 629 |
sprintf(buffer, "Container"); |
| 630 |
return buffer; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
static char *parameterNumberAndName(int parameterNumber) { |
| 636 |
static char buffer[1000] = ""; |
| 637 |
|
| 638 |
char *parameterName = parameterNameInSyntax(current.syntax, parameterNumber); |
| 639 |
|
| 640 |
if (parameterName != NULL) |
| 641 |
sprintf(buffer, "%s(#%d)", parameterName, parameterNumber); |
| 642 |
else |
| 643 |
sprintf(buffer, "#%d", parameterNumber); |
| 644 |
return buffer; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
static void traceRestriction(RestrictionEntry *restriction, int classId, bool condition) { |
| 650 |
printf("\n<SYNTAX RESTRICTION WHERE parameter %s Isa %s, %s>\n", |
| 651 |
parameterNumberAndName(restriction->parameterNumber), |
| 652 |
classNameAndId(classId), condition?"PASSED":"FAILED:"); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
static bool restrictionCheck(RestrictionEntry *restriction, int instance) { |
| 658 |
if (restriction->class == RESTRICTIONCLASS_CONTAINER) { |
| 659 |
if (traceSectionOption) |
| 660 |
traceRestriction(restriction, -1, isAContainer(instance)); |
| 661 |
return isAContainer(instance); |
| 662 |
} else { |
| 663 |
if (traceSectionOption) |
| 664 |
traceRestriction(restriction, restriction->class, isA(instance, restriction->class)); |
| 665 |
return isA(instance, restriction->class); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
static void runRestriction(RestrictionEntry *restriction, Parameter parameters[]) { |
| 672 |
if (restriction->stms) { |
| 673 |
setGlobalParameters(parameters); |
| 674 |
interpret(restriction->stms); |
| 675 |
} else |
| 676 |
error(M_CANT0); |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
static int remapParameterOrder(int syntaxNumber, ParameterPosition parameterPositions[]) { |
| 682 |
|
| 683 |
ParameterMapEntry *parameterMapTable; |
| 684 |
Aword *parameterMap; |
| 685 |
Aint parameterNumber; |
| 686 |
ParameterPosition savedParameterPositions[MAXPARAMS+1]; |
| 687 |
|
| 688 |
for (parameterMapTable = pointerTo(header->parameterMapAddress); !isEndOfArray(parameterMapTable); parameterMapTable++) |
| 689 |
if (parameterMapTable->syntaxNumber == syntaxNumber) |
| 690 |
break; |
| 691 |
if (isEndOfArray(parameterMapTable)) |
| 692 |
syserr("Could not find syntax in mapping table."); |
| 693 |
|
| 694 |
parameterMap = pointerTo(parameterMapTable->parameterMapping); |
| 695 |
|
| 696 |
copyParameterPositions(parameterPositions, savedParameterPositions); |
| 697 |
|
| 698 |
for (parameterNumber = 1; !savedParameterPositions[parameterNumber-1].endOfList; parameterNumber++) { |
| 699 |
parameterPositions[parameterNumber-1] = savedParameterPositions[parameterMap[parameterNumber-1]-1]; |
| 700 |
} |
| 701 |
|
| 702 |
return parameterMapTable->verbCode; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
static bool hasBit(Aword flags, Aint bit) { |
| 708 |
return (flags & bit) != 0; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
static bool multipleAllowed(Aword flags) { |
| 713 |
return hasBit(flags, MULTIPLEBIT); |
| 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 |
static void parseParameterPosition(ParameterPosition *parameterPosition, Aword flags, void (*complexReferencesParser)(ParameterPosition *parameterPosition)) { |
| 740 |
parameterPosition->parameters = ensureParameterArrayAllocated(parameterPosition->parameters); |
| 741 |
|
| 742 |
complexReferencesParser(parameterPosition); |
| 743 |
if (lengthOfParameterArray(parameterPosition->parameters) == 0) |
| 744 |
error(M_WHAT); |
| 745 |
|
| 746 |
if (parameterPosition->explicitMultiple && !multipleAllowed(flags)) |
| 747 |
error(M_MULTIPLE); |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
static ElementEntry *elementForParameter(ElementEntry *elms) { |
| 752 |
|
| 753 |
while (!isEndOfArray(elms) && elms->code != 0) |
| 754 |
elms++; |
| 755 |
if (isEndOfArray(elms)) |
| 756 |
return NULL; |
| 757 |
return elms; |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
static ElementEntry *elementForEndOfSyntax(ElementEntry *elms) { |
| 762 |
while (!isEndOfArray(elms) && elms->code != EOS) |
| 763 |
elms++; |
| 764 |
if (isEndOfArray(elms)) |
| 765 |
return NULL; |
| 766 |
return elms; |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
static ElementEntry *elementForWord(ElementEntry *elms, Aint wordCode) { |
| 771 |
while (!isEndOfArray(elms) && elms->code != wordCode) |
| 772 |
elms++; |
| 773 |
if (isEndOfArray(elms)) |
| 774 |
return NULL; |
| 775 |
return elms; |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
static bool isInstanceReferenceWord(int wordIndex) { |
| 781 |
return isNounWord(wordIndex) || isAdjectiveWord(wordIndex) || isAllWord(wordIndex) |
| 782 |
|| isLiteralWord(wordIndex) || isItWord(wordIndex) || isThemWord(wordIndex) || isPronounWord(wordIndex); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
static ElementEntry *parseInputAccordingToSyntax(SyntaxEntry *syntax, ParameterPosition parameterPositions[]) { |
| 788 |
ElementEntry *currentElement = elementTreeOf(syntax); |
| 789 |
ElementEntry *nextElement = currentElement; |
| 790 |
|
| 791 |
int parameterCount = 0; |
| 792 |
while (nextElement != NULL) { |
| 793 |
|
| 794 |
parameterPositions[parameterCount].endOfList = true; |
| 795 |
|
| 796 |
if (endOfPlayerCommand(currentWordIndex)) { |
| 797 |
|
| 798 |
return elementForEndOfSyntax(currentElement); |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
if (isInstanceReferenceWord(currentWordIndex)) { |
| 803 |
|
| 804 |
nextElement = elementForParameter(currentElement); |
| 805 |
if (nextElement != NULL) { |
| 806 |
|
| 807 |
|
| 808 |
ParameterPosition *parameterPosition = ¶meterPositions[parameterCount]; |
| 809 |
parseParameterPosition(parameterPosition, nextElement->flags, complexReferencesParser); |
| 810 |
parameterPosition->flags = nextElement->flags; |
| 811 |
parameterPosition->endOfList = false; |
| 812 |
|
| 813 |
currentElement = (ElementEntry *) pointerTo(nextElement->next); |
| 814 |
parameterCount++; |
| 815 |
continue; |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
if (isPrepositionWord(currentWordIndex) || isVerbWord(currentWordIndex)) { |
| 821 |
|
| 822 |
nextElement = elementForWord(currentElement, dictionary[playerWords[currentWordIndex].code].code); |
| 823 |
if (nextElement != NULL) { |
| 824 |
currentWordIndex++; |
| 825 |
currentElement = (ElementEntry *) pointerTo(nextElement->next); |
| 826 |
continue; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
if (isExceptWord(currentWordIndex)) |
| 832 |
errorButAfterAll(currentWordIndex); |
| 833 |
|
| 834 |
|
| 835 |
nextElement = NULL; |
| 836 |
} |
| 837 |
return NULL; |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
static bool anyExplicitMultiple(ParameterPosition parameterPositions[]) { |
| 843 |
int i; |
| 844 |
|
| 845 |
for (i = 0; !parameterPositions[i].endOfList; i++) |
| 846 |
if (parameterPositions[i].explicitMultiple) |
| 847 |
return true; |
| 848 |
return false; |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
static bool anyAll(ParameterPosition parameterPositions[]) { |
| 854 |
int i; |
| 855 |
|
| 856 |
for (i = 0; !parameterPositions[i].endOfList; i++) |
| 857 |
if (parameterPositions[i].all) |
| 858 |
return true; |
| 859 |
return false; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
static void checkRestrictedParameters(ParameterPosition parameterPositions[], ElementEntry elms[]) { |
| 865 |
RestrictionEntry *restriction; |
| 866 |
static Parameter *localParameters = NULL; |
| 867 |
int i; |
| 868 |
|
| 869 |
localParameters = ensureParameterArrayAllocated(localParameters); |
| 870 |
clearParameterArray(localParameters); |
| 871 |
|
| 872 |
for (i=0; !parameterPositions[i].endOfList; i++) |
| 873 |
addParameterToParameterArray(localParameters, ¶meterPositions[i].parameters[0]); |
| 874 |
|
| 875 |
for (restriction = (RestrictionEntry *) pointerTo(elms->next); !isEndOfArray(restriction); restriction++) { |
| 876 |
ParameterPosition *parameterPosition = ¶meterPositions[restriction->parameterNumber-1]; |
| 877 |
if (parameterPosition->explicitMultiple) { |
| 878 |
|
| 879 |
int i; |
| 880 |
for (i = 0; !isEndOfArray(¶meterPosition->parameters[i]); i++) { |
| 881 |
copyParameter(&localParameters[restriction->parameterNumber-1], ¶meterPosition->parameters[i]); |
| 882 |
if (!restrictionCheck(restriction, parameterPosition->parameters[i].instance)) { |
| 883 |
|
| 884 |
if (!parameterPosition->all) { |
| 885 |
char marker[80]; |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
sprintf(marker, "($%ld)", (unsigned long) restriction->parameterNumber); |
| 890 |
setGlobalParameters(localParameters); |
| 891 |
output(marker); |
| 892 |
runRestriction(restriction, localParameters); |
| 893 |
para(); |
| 894 |
} |
| 895 |
parameterPosition->parameters[i].instance = 0; |
| 896 |
} |
| 897 |
} |
| 898 |
} else { |
| 899 |
if (!restrictionCheck(restriction, parameterPosition->parameters[0].instance)) { |
| 900 |
runRestriction(restriction, localParameters); |
| 901 |
abortPlayerCommand(); |
| 902 |
} |
| 903 |
} |
| 904 |
parameterPositions[restriction->parameterNumber - 1].checked = true; |
| 905 |
} |
| 906 |
freeParameterArray(localParameters); |
| 907 |
localParameters = NULL; |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
static void impossibleWith(ParameterPosition parameterPositions[], int positionIndex) { |
| 913 |
if (isPreBeta2(header->version)) { |
| 914 |
error(M_CANT0); |
| 915 |
} else { |
| 916 |
printMessageWithInstanceParameter(M_IMPOSSIBLE_WITH, parameterPositions[positionIndex].parameters[0].instance); |
| 917 |
error(NO_MSG); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
static void checkNonRestrictedParameters(ParameterPosition parameterPositions[]) { |
| 925 |
int positionIndex; |
| 926 |
for (positionIndex = 0; !parameterPositions[positionIndex].endOfList; positionIndex++) |
| 927 |
if (!parameterPositions[positionIndex].checked) { |
| 928 |
if (parameterPositions[positionIndex].explicitMultiple) { |
| 929 |
|
| 930 |
int i; |
| 931 |
for (i = 0; !isEndOfArray(¶meterPositions[positionIndex].parameters[i]); i++) |
| 932 |
if (parameterPositions[positionIndex].parameters[i].instance != 0) |
| 933 |
if (!isAObject(parameterPositions[positionIndex].parameters[i].instance)) |
| 934 |
parameterPositions[positionIndex].parameters[i].instance = 0; |
| 935 |
} else if (!isAObject(parameterPositions[positionIndex].parameters[0].instance)) |
| 936 |
impossibleWith(parameterPositions, positionIndex); |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
static void restrictParametersAccordingToSyntax(ParameterPosition parameterPositions[], ElementEntry *elms) { |
| 943 |
uncheckAllParameterPositions(parameterPositions); |
| 944 |
checkRestrictedParameters(parameterPositions, elms); |
| 945 |
checkNonRestrictedParameters(parameterPositions); |
| 946 |
int positionIndex; |
| 947 |
for (positionIndex = 0; !parameterPositions[positionIndex].endOfList; positionIndex++) |
| 948 |
compressParameterArray(parameterPositions[positionIndex].parameters); |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
static void matchPronoun(Parameter *parameter) { |
| 954 |
static Parameter *pronounInstances = NULL; |
| 955 |
pronounInstances = ensureParameterArrayAllocated(pronounInstances); |
| 956 |
|
| 957 |
int pronounCandidateCount = getPronounInstances(playerWords[parameter->firstWord].code, pronounInstances); |
| 958 |
if (pronounCandidateCount == 0) |
| 959 |
errorWhat(parameter->firstWord); |
| 960 |
else if (pronounCandidateCount > 1) |
| 961 |
errorWhichPronoun(parameter->firstWord, pronounInstances); |
| 962 |
else { |
| 963 |
parameter->candidates[0] = pronounInstances[0]; |
| 964 |
setEndOfArray(¶meter->candidates[1]); |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
static void matchNounPhrase(Parameter *parameter, ReferencesFinder adjectiveReferencesFinder, ReferencesFinder nounReferencesFinder) { |
| 971 |
int i; |
| 972 |
|
| 973 |
for (i = parameter->firstWord; i < parameter->lastWord; i++) |
| 974 |
updateWithReferences(parameter->candidates, i, adjectiveReferencesFinder); |
| 975 |
updateWithReferences(parameter->candidates, parameter->lastWord, nounReferencesFinder); |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
static void instanceMatcher(Parameter *parameter) { |
| 981 |
Parameter *candidates = parameter->candidates; |
| 982 |
int i; |
| 983 |
|
| 984 |
if (parameter->isLiteral) { |
| 985 |
candidates[0].instance = instanceFromLiteral(playerWords[parameter->firstWord].code - dictionarySize); |
| 986 |
setEndOfArray(&candidates[1]); |
| 987 |
} else if (parameter->isPronoun) { |
| 988 |
matchPronoun(parameter); |
| 989 |
} else |
| 990 |
matchNounPhrase(parameter, adjectiveReferencesForWord, nounReferencesForWord); |
| 991 |
|
| 992 |
|
| 993 |
candidates[0].firstWord = parameter->firstWord; |
| 994 |
candidates[0].lastWord = parameter->lastWord; |
| 995 |
for (i = 0; i < lengthOfParameterArray(candidates); i++) { |
| 996 |
candidates[i].firstWord = parameter->firstWord; |
| 997 |
candidates[i].lastWord = parameter->lastWord; |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
static void findCandidates(Parameter parameters[], void (*instanceMatcher)(Parameter *parameter)) |
| 1004 |
{ |
| 1005 |
int i; |
| 1006 |
|
| 1007 |
for (i = 0; i < lengthOfParameterArray(parameters); i++) { |
| 1008 |
parameters[i].candidates = ensureParameterArrayAllocated(parameters[i].candidates); |
| 1009 |
instanceMatcher(¶meters[i]); |
| 1010 |
parameters[i].candidates[0].isPronoun = parameters[i].isPronoun; |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
static void handleFailedParse(ElementEntry *elms) { |
| 1017 |
if (elms == NULL) |
| 1018 |
error(M_WHAT); |
| 1019 |
if (elms->next == 0) { |
| 1020 |
|
| 1021 |
error(M_CANT0); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
static void convertMultipleCandidatesToMultipleParameters(ParameterPosition parameterPositions[], Parameter multipleParameters[]) { |
| 1028 |
int parameterIndex; |
| 1029 |
for (parameterIndex=0; !parameterPositions[parameterIndex].endOfList; parameterIndex++) |
| 1030 |
if (parameterPositions[parameterIndex].explicitMultiple) { |
| 1031 |
compressParameterArray(parameterPositions[parameterIndex].parameters); |
| 1032 |
copyParameterArray(multipleParameters, parameterPositions[parameterIndex].parameters); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
static ElementEntry *parseInput(ParameterPosition *parameterPositions) { |
| 1039 |
ElementEntry *element; |
| 1040 |
SyntaxEntry *stx; |
| 1041 |
|
| 1042 |
stx = findSyntaxTreeForVerb(verbWordCode); |
| 1043 |
element = parseInputAccordingToSyntax(stx, parameterPositions); |
| 1044 |
handleFailedParse(element); |
| 1045 |
current.syntax = element->flags; |
| 1046 |
current.verb = remapParameterOrder(current.syntax, parameterPositions); |
| 1047 |
return element; |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
static void findCandidatesForPlayerWords(ParameterPosition *parameterPosition) { |
| 1053 |
ParameterArray parameters = parameterPosition->parameters; |
| 1054 |
|
| 1055 |
if (!parameterArrayIsEmpty(parameters)) { |
| 1056 |
if (parameters[0].isThem) { |
| 1057 |
parameterPosition->them = true; |
| 1058 |
getPreviousMultipleParameters(parameters); |
| 1059 |
if (lengthOfParameterArray(parameters) == 0) |
| 1060 |
errorWhat(parameters[0].firstWord); |
| 1061 |
if (lengthOfParameterArray(parameters) > 1) |
| 1062 |
parameterPosition->explicitMultiple = true; |
| 1063 |
} else if (parameterPosition->all) { |
| 1064 |
buildAllHere(parameters); |
| 1065 |
if (!parameterArrayIsEmpty(parameterPosition->exceptions)) |
| 1066 |
findCandidates(parameterPosition->exceptions, instanceMatcher); |
| 1067 |
} else |
| 1068 |
findCandidates(parameters, instanceMatcher); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
static void handleMultiplePosition(ParameterPosition parameterPositions[]) { |
| 1075 |
int multiplePosition = findMultipleParameterPosition(parameterPositions); |
| 1076 |
if (anyAll(parameterPositions)) { |
| 1077 |
|
| 1078 |
disambiguateCandidatesForPosition(parameterPositions, multiplePosition, parameterPositions[multiplePosition].parameters); |
| 1079 |
if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0) |
| 1080 |
errorWhat(parameterPositions[multiplePosition].parameters[0].firstWord); |
| 1081 |
subtractParameterArrays(parameterPositions[multiplePosition].parameters, parameterPositions[multiplePosition].exceptions); |
| 1082 |
if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0) |
| 1083 |
error(M_NOT_MUCH); |
| 1084 |
} else if (anyExplicitMultiple(parameterPositions)) { |
| 1085 |
compressParameterArray(parameterPositions[multiplePosition].parameters); |
| 1086 |
if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0) { |
| 1087 |
|
| 1088 |
|
| 1089 |
abortPlayerCommand(); |
| 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 |
typedef Parameter *DisambiguationHandler(Parameter allCandidates[], Parameter presentCandidates[]); |
| 1129 |
typedef DisambiguationHandler *DisambiguationHandlerTable[3][3][2]; |
| 1130 |
|
| 1131 |
static Parameter *disambiguate00N(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1132 |
if (allCandidates[0].isPronoun) |
| 1133 |
errorWhat(allCandidates[0].firstWord); |
| 1134 |
else |
| 1135 |
errorNoSuch(allCandidates[0]); |
| 1136 |
return NULL; |
| 1137 |
} |
| 1138 |
static Parameter *disambiguate01N(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1139 |
if (allCandidates[0].isPronoun) |
| 1140 |
errorWhat(allCandidates[0].firstWord); |
| 1141 |
else |
| 1142 |
errorNoSuch(allCandidates[0]); |
| 1143 |
return NULL; |
| 1144 |
} |
| 1145 |
static Parameter *disambiguate0MN(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1146 |
if (allCandidates[0].isPronoun) |
| 1147 |
errorWhat(allCandidates[0].firstWord); |
| 1148 |
else |
| 1149 |
errorNoSuch(allCandidates[0]); |
| 1150 |
return NULL; |
| 1151 |
} |
| 1152 |
static Parameter *disambiguate10N(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1153 |
return presentCandidates; |
| 1154 |
} |
| 1155 |
static Parameter *disambiguate11N(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1156 |
return presentCandidates; |
| 1157 |
} |
| 1158 |
static Parameter *disambiguate1MN(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1159 |
return presentCandidates; |
| 1160 |
} |
| 1161 |
static Parameter *disambiguateM0N(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1162 |
errorWhichOne(presentCandidates); |
| 1163 |
return NULL; |
| 1164 |
} |
| 1165 |
static Parameter *disambiguateM1N(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1166 |
errorWhichOne(presentCandidates); |
| 1167 |
return NULL; |
| 1168 |
} |
| 1169 |
static Parameter *disambiguateMMN(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1170 |
errorWhichOne(presentCandidates); |
| 1171 |
return NULL; |
| 1172 |
} |
| 1173 |
static Parameter *disambiguate00Y(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1174 |
errorNoSuch(allCandidates[0]); |
| 1175 |
return NULL; |
| 1176 |
} |
| 1177 |
static Parameter *disambiguate01Y(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1178 |
return allCandidates; |
| 1179 |
} |
| 1180 |
static Parameter *disambiguate0MY(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1181 |
errorWhichOne(allCandidates); |
| 1182 |
return NULL; |
| 1183 |
} |
| 1184 |
static Parameter *disambiguate10Y(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1185 |
return presentCandidates; |
| 1186 |
} |
| 1187 |
static Parameter *disambiguate11Y(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1188 |
return presentCandidates; |
| 1189 |
} |
| 1190 |
static Parameter *disambiguate1MY(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1191 |
return presentCandidates; |
| 1192 |
} |
| 1193 |
static Parameter *disambiguateM0Y(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1194 |
errorWhichOne(presentCandidates); |
| 1195 |
return NULL; |
| 1196 |
} |
| 1197 |
static Parameter *disambiguateM1Y(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1198 |
errorWhichOne(presentCandidates); |
| 1199 |
return NULL; |
| 1200 |
} |
| 1201 |
static Parameter *disambiguateMMY(Parameter allCandidates[], Parameter presentCandidates[]) { |
| 1202 |
errorWhichOne(presentCandidates);return NULL; |
| 1203 |
} |
| 1204 |
|
| 1205 |
static DisambiguationHandlerTable disambiguationHandlerTable = |
| 1206 |
{ |
| 1207 |
{ |
| 1208 |
{ |
| 1209 |
disambiguate00N, disambiguate00Y}, |
| 1210 |
{ |
| 1211 |
disambiguate01N, disambiguate01Y}, |
| 1212 |
{ |
| 1213 |
disambiguate0MN, disambiguate0MY}}, |
| 1214 |
{ |
| 1215 |
{ |
| 1216 |
disambiguate10N, disambiguate10Y}, |
| 1217 |
{ |
| 1218 |
disambiguate11N, disambiguate11Y}, |
| 1219 |
{ |
| 1220 |
disambiguate1MN, disambiguate1MY}}, |
| 1221 |
{ |
| 1222 |
{ |
| 1223 |
disambiguateM0N, disambiguateM0Y}, |
| 1224 |
{ |
| 1225 |
disambiguateM1N, disambiguateM1Y}, |
| 1226 |
{ |
| 1227 |
disambiguateMMN, disambiguateMMY}} |
| 1228 |
}; |
| 1229 |
|
| 1230 |
|
| 1231 |
static void disambiguateCandidates(Parameter *allCandidates, bool omnipotent, bool (*reachable)(int), DisambiguationHandlerTable handler) { |
| 1232 |
static Parameter *presentCandidates = NULL; |
| 1233 |
int present; |
| 1234 |
int distant; |
| 1235 |
Parameter *result; |
| 1236 |
|
| 1237 |
presentCandidates = ensureParameterArrayAllocated(presentCandidates); |
| 1238 |
|
| 1239 |
copyParameterArray(presentCandidates, allCandidates); |
| 1240 |
filterOutNonReachable(presentCandidates, reachable); |
| 1241 |
|
| 1242 |
present = lengthOfParameterArray(presentCandidates); |
| 1243 |
if (present > 1) present = 2; |
| 1244 |
|
| 1245 |
distant = lengthOfParameterArray(allCandidates) - present; |
| 1246 |
if (distant > 1) distant = 2; |
| 1247 |
|
| 1248 |
result = handler[present][distant][omnipotent](allCandidates, presentCandidates); |
| 1249 |
|
| 1250 |
|
| 1251 |
allCandidates[0] = result[0]; |
| 1252 |
setEndOfArray(&allCandidates[1]); |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
static void disambiguate(ParameterPosition parameterPositions[], ElementEntry *element) { |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
int position; |
| 1262 |
for (position = 0; !parameterPositions[position].endOfList; position++) { |
| 1263 |
findCandidatesForPlayerWords(¶meterPositions[position]); |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
for (position = 0; !parameterPositions[position].endOfList; position++) { |
| 1272 |
ParameterPosition *parameterPosition = ¶meterPositions[position]; |
| 1273 |
bool omni = hasBit(parameterPosition->flags, OMNIBIT); |
| 1274 |
if (!parameterPosition->all && !parameterPosition->them) { |
| 1275 |
Parameter *parameters = parameterPosition->parameters; |
| 1276 |
int p; |
| 1277 |
for (p = 0; p < lengthOfParameterArray(parameters); p++) { |
| 1278 |
Parameter *parameter = ¶meters[p]; |
| 1279 |
Parameter *candidates = parameter->candidates; |
| 1280 |
disambiguateCandidates(candidates, omni, reachable, disambiguationHandlerTable); |
| 1281 |
parameter->instance = candidates[0].instance; |
| 1282 |
} |
| 1283 |
} |
| 1284 |
if (parameterPosition->all) { |
| 1285 |
Parameter *exceptions = parameterPosition->exceptions; |
| 1286 |
int p; |
| 1287 |
for (p = 0; p < lengthOfParameterArray(exceptions); p++) { |
| 1288 |
Parameter *parameter = &exceptions[p]; |
| 1289 |
Parameter *candidates = parameter->candidates; |
| 1290 |
disambiguateCandidates(candidates, omni, reachable, disambiguationHandlerTable); |
| 1291 |
parameter->instance = candidates[0].instance; |
| 1292 |
} |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
handleMultiplePosition(parameterPositions); |
| 1297 |
|
| 1298 |
|
| 1299 |
restrictParametersAccordingToSyntax(parameterPositions, element); |
| 1300 |
} |
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
static void try(Parameter parameters[], Parameter multipleParameters[]) { |
| 1305 |
ElementEntry *element; |
| 1306 |
static ParameterPosition *parameterPositions = NULL; |
| 1307 |
if (parameterPositions != NULL) |
| 1308 |
deallocateParameterPositions(parameterPositions); |
| 1309 |
|
| 1310 |
|
| 1311 |
parameterPositions = allocate(sizeof(ParameterPosition)*(MAXPARAMS+1)); |
| 1312 |
parameterPositions[0].endOfList = true; |
| 1313 |
|
| 1314 |
element = parseInput(parameterPositions); |
| 1315 |
|
| 1316 |
disambiguate(parameterPositions, element); |
| 1317 |
|
| 1318 |
|
| 1319 |
convertPositionsToParameters(parameterPositions, parameters); |
| 1320 |
markExplicitMultiple(parameterPositions, parameters); |
| 1321 |
convertMultipleCandidatesToMultipleParameters(parameterPositions, multipleParameters); |
| 1322 |
|
| 1323 |
deallocateParameterPositions(parameterPositions); |
| 1324 |
parameterPositions = NULL; |
| 1325 |
} |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
static void parseOneCommand(Parameter parameters[], Parameter multipleParameters[]) |
| 1330 |
{ |
| 1331 |
try(parameters, multipleParameters); |
| 1332 |
|
| 1333 |
|
| 1334 |
if (!endOfWords(currentWordIndex)) { |
| 1335 |
if (isConjunctionWord(currentWordIndex)) |
| 1336 |
currentWordIndex++; |
| 1337 |
if (endOfWords(currentWordIndex)) |
| 1338 |
error(M_WHAT); |
| 1339 |
if (isConjunctionWord(currentWordIndex)) |
| 1340 |
currentWordIndex++; |
| 1341 |
if (endOfWords(currentWordIndex)) |
| 1342 |
error(M_WHAT); |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
void initParsing(void) { |
| 1348 |
currentWordIndex = 0; |
| 1349 |
continued = false; |
| 1350 |
ensureSpaceForPlayerWords(0); |
| 1351 |
clearWordList(playerWords); |
| 1352 |
|
| 1353 |
pronouns = allocatePronounArray(pronouns); |
| 1354 |
globalParameters = ensureParameterArrayAllocated(globalParameters); |
| 1355 |
previousMultipleParameters = ensureParameterArrayAllocated(previousMultipleParameters); |
| 1356 |
} |
| 1357 |
|
| 1358 |
|
| 1359 |
static void addPronounForInstance(int thePronoun, int instanceCode) { |
| 1360 |
int i; |
| 1361 |
|
| 1362 |
for (i = 0; !endOfPronouns(i); i++) |
| 1363 |
if (pronouns[i].pronoun == thePronoun && pronouns[i].instance == instanceCode) |
| 1364 |
|
| 1365 |
return; |
| 1366 |
pronouns[i].pronoun = thePronoun; |
| 1367 |
pronouns[i].instance = instanceCode; |
| 1368 |
setEndOfArray(&pronouns[i + 1]); |
| 1369 |
} |
| 1370 |
|
| 1371 |
|
| 1372 |
static void notePronounsForParameters(Parameter parameters[]) { |
| 1373 |
|
| 1374 |
Parameter *p; |
| 1375 |
|
| 1376 |
clearPronounList(pronouns); |
| 1377 |
for (p = parameters; !isEndOfArray(p); p++) { |
| 1378 |
int w; |
| 1379 |
|
| 1380 |
for (w = 0; w < dictionarySize; w++) |
| 1381 |
if (isPronoun(w)) { |
| 1382 |
Aword *reference = pointerTo(dictionary[w].pronounRefs); |
| 1383 |
while (*reference != EOF) { |
| 1384 |
if (*reference == p->instance) |
| 1385 |
addPronounForInstance(dictionary[w].code, p->instance); |
| 1386 |
reference++; |
| 1387 |
} |
| 1388 |
} |
| 1389 |
} |
| 1390 |
} |
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
static void parseVerbCommand(Parameter parameters[], Parameter multipleParameters[]) { |
| 1395 |
verbWord = playerWords[currentWordIndex].code; |
| 1396 |
verbWordCode = dictionary[verbWord].code; |
| 1397 |
if (isPreBeta2(header->version)) |
| 1398 |
|
| 1399 |
|
| 1400 |
currentWordIndex++; |
| 1401 |
parseOneCommand(parameters, multipleParameters); |
| 1402 |
notePronounsForParameters(parameters); |
| 1403 |
fail = false; |
| 1404 |
} |
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
static void parseInstanceCommand(Parameter parameters[], Parameter multipleParameters[]) { |
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
verbWordCode = 0; |
| 1414 |
parseOneCommand(parameters, multipleParameters); |
| 1415 |
notePronounsForParameters(parameters); |
| 1416 |
fail = false; |
| 1417 |
} |
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
void parse(void) { |
| 1422 |
|
| 1423 |
static Parameter *parameters = NULL; |
| 1424 |
static Parameter *multipleParameters = NULL; |
| 1425 |
parameters = ensureParameterArrayAllocated(parameters); |
| 1426 |
multipleParameters = ensureParameterArrayAllocated(multipleParameters); |
| 1427 |
|
| 1428 |
if (endOfWords(currentWordIndex)) { |
| 1429 |
currentWordIndex = 0; |
| 1430 |
scan(); |
| 1431 |
} else if (anyOutput) |
| 1432 |
para(); |
| 1433 |
|
| 1434 |
capitalize = true; |
| 1435 |
|
| 1436 |
firstWord = currentWordIndex; |
| 1437 |
if (isDirectionWord(currentWordIndex)) { |
| 1438 |
clearParameterArray(previousMultipleParameters); |
| 1439 |
clearPronounList(pronouns); |
| 1440 |
handleDirectionalCommand(); |
| 1441 |
} else if (isVerbWord(currentWordIndex)) { |
| 1442 |
parseVerbCommand(parameters, multipleParameters); |
| 1443 |
action(current.verb, parameters, multipleParameters); |
| 1444 |
} else if (isInstanceReferenceWord(currentWordIndex)) { |
| 1445 |
parseInstanceCommand(parameters, multipleParameters); |
| 1446 |
action(current.verb, parameters, multipleParameters); |
| 1447 |
} else |
| 1448 |
error(M_WHAT); |
| 1449 |
|
| 1450 |
if (fail) error(NO_MSG); |
| 1451 |
|
| 1452 |
lastWord = currentWordIndex - 1; |
| 1453 |
if (isConjunctionWord(lastWord)) |
| 1454 |
lastWord--; |
| 1455 |
|
| 1456 |
if (lengthOfParameterArray(parameters) > 0) |
| 1457 |
copyParameterArray(previousMultipleParameters, multipleParameters); |
| 1458 |
else |
| 1459 |
clearParameterArray(previousMultipleParameters); |
| 1460 |
|
| 1461 |
freeParameterArray(parameters); |
| 1462 |
parameters = NULL; |
| 1463 |
freeParameterArray(multipleParameters); |
| 1464 |
multipleParameters = NULL; |
| 1465 |
} |