Debian Codegraph @

terps/alan3/parse.c

raw symbols SCIP
1 /*----------------------------------------------------------------------
2
3 parse.c
4
5 Command line parser unit for Alan interpreter ARUN
6
7 ----------------------------------------------------------------------*/
8 #include "parse.h"
9
10
11 /* IMPORTS */
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 /* PRIVATE CONSTANTS */
40
41
42 /* PUBLIC DATA */
43
44
45 /* PRIVATE TYPES */
46 typedef struct PronounEntry { /* To remember parameter/pronoun relations */
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 /* PRIVATE DATA */
64 static Pronoun *pronouns = NULL;
65
66
67 /* Syntax Parameters */
68 static Parameter *previousMultipleParameters; /* Previous multiple list */
69
70
71 /* For parameters that are literals we need to trick message handling to
72 * output the word and create a string literal instance if anyone wants to
73 * refer to an attribute of it (literals inherit from entity so application
74 * may have added an attribute) */
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); /* A faked literal */
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; /* Index into the list of alternatives */
123 ParameterArray parameters = newParameterArray();
124
125 parameters[0] = alternative[0];
126 setEndOfArray(&parameters[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(); /* Return with empty error message */
138 }
139
140 /*----------------------------------------------------------------------*/
141 static void errorWhichPronoun(int pronounWordIndex, Parameter alternatives[]) {
142 int p; /* Index into the list of alternatives */
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 /* Look through the dictionary and find any ALL_WORD, then add a
187 player word so that it can be used in the message */
188 int p, d;
189
190 for (p = 0; !isEndOfArray(&playerWords[p]); p++)
191 ;
192 setEndOfArray(&playerWords[p+1]); /* Make room for one more word */
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 /* If there was no instance, assume the last word used is the noun,
224 * then find any instance with the noun he used */
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; /* Indicate to use words and not names */
230
231 clearParameterArray(globalParameters);
232 addParameterToParameterArray(globalParameters, &parameter);
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 /* Find the instance that the pronoun word could refer to, return 0
263 if none or multiple */
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; /* Can't use words since they are gone, pronouns
272 refer to parameters in previous command */
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 // TODO : isOpaque()
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(&parameters[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(&parameters[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; /* Instances referenced by a word */
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 * There are various ways the player can refer to things, some are
366 * explicit, in which case they should be kept in the input. If he said
367 * 'them', 'all' or some such the list is inferred so we must filter
368 * it w.r.t what it can mean. A special case is when he said 'the ball'
369 * and there is only one ball here, but multiple in the game. We need
370 * to be able to distinguish between these cases!!! 'them' is
371 * explicit, 'all' is inferred, exceptions can never be inferred,
372 * maybe 'all' is the only inferred?
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) { /* Already empty? */
383 copyParameter(&parameters[position], &candidates[i]);
384 // DISAMBIGUATION!!
385 if (!reachable(candidates[i].instance) || !possible(current.verb, parameters, parameterPositions))
386 candidates[i].instance = 0; /* Then remove this candidate from list */
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 /* Parse the input and note the word indices in the parameters,
409 matching will be done by the match* functions */
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 /* Perhaps the last word could also be interpreted as a noun? */
426 if (isNounWord(currentWordIndex - 1)) {
427 // TODO When does this get executed? Maybe if conjunctions can be nouns? Or nouns be adjectives?
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; /* No instance yet! */
439 setEndOfArray(&parameters[1]);
440 } else
441 setEndOfArray(&parameters[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(&parameters[i].candidates[0]); /* No candidates */
465 if (!reachable(previousMultipleParameters[i].instance))
466 parameters[i].instance = 0;
467 else
468 parameters[i].instance = previousMultipleParameters[i].instance;
469 }
470 setEndOfArray(&parameters[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(&parameters[1]);
481 }
482
483
484 /*----------------------------------------------------------------------*/
485 static bool parseOneParameter(Parameter parameters[], int parameterIndex) {
486 Parameter *parameter = newParameterArray();
487
488 // TODO Maybe this should go in the complex()?
489 if (isThemWord(currentWordIndex) && (!isPronounWord(currentWordIndex) ||
490 (isPronounWord(currentWordIndex) && lengthOfParameterArray(previousMultipleParameters) > 0))) {
491 // "them" is also a common pronoun for some instances, but if there
492 // are previous multiple parameters we give precedence to those
493 parseReferenceToPreviousMultipleParameters(parameter);
494 } else {
495 parseReference(parameter);
496 if (lengthOfParameterArray(parameter) == 0) { /* Failed to find any exceptions! */
497 freeParameterArray(parameter);
498 return false;
499 }
500 }
501
502 /* Add the one we found to the parameters */
503 parameters[parameterIndex] = parameter[0];
504 setEndOfArray(&parameters[parameterIndex+1]);
505 freeParameterArray(parameter);
506 return true;
507 }
508
509
510 /*
511 * A "simple" parameter is in one of three forms:
512 *
513 * 1) adjectives and nouns referencing a single instance (might
514 * actually match multiple instances in the game...)
515 *
516 * 2) multiple of 1) separated by conjunctions ("a and b and c")
517 *
518 * 3) a pronoun referencing a single instance
519
520 * We also need to handle "them" here since it is also a common
521 * pronoun for some instances, e.g. scissor, trouser, money,
522 * glasses, but it can also refer to the set of instances from the
523 * previous command. If it is a pronoun but there are also multiple
524 * parameter from the previous command, we give precedence to the
525 * multiple previous.
526 */
527
528 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
529 static void simpleParameterParser(Parameter parameters[]) {
530
531 /* This will loop until all references are collected (typically "a and b and c") */
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 /* Since there was one or two conjunctions and the next
558 seems to be another instance reference, let's continue
559 with that by eating the conjunction(s) */
560 currentWordIndex = index;
561 } else
562 /* Otherwise it was not a conjuction of parameters... */
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 * Complex instance references are of the form:
581 *
582 * 1) all
583 *
584 * 2) all except
585 *
586 * 3) a simple (see above) instance reference(s)
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(&parameterPosition->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 /* HERE SHOULD BE current.syntax */
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 /* Find the syntax map, use the verb code from it and remap the parameters */
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 * There are a number of ways that the number of parameters might
720 * be more than one:
721 *
722 * 1) Player used ALL and it matched more than one
723 *
724 * 2) Player explicitly refered to multiple objects
725 *
726 * 3) Player did a single (or multiple) reference that was ambiguous
727 * in which case we need to disambiguate it (them). If we want to do
728 * this after complete parsing we must be able to see the possible
729 * parameters for each of these references, e.g.:
730 *
731 * > take the vase and the book
732 *
733 * In this case it is a single parameterPosition, but multiple
734 * explicit references and each of those might match multiple
735 * instances in the game.
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) /* No object!? */
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 /* Require a parameter if elms->code == 0! */
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)) /* No match for EOS! */
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 /* Traverse the possible branches of currentElement to find a match, let the actual input control what we look for */
794 parameterPositions[parameterCount].endOfList = true;
795
796 if (endOfPlayerCommand(currentWordIndex)) {
797 // TODO If a conjunction word is also some other type of word, like noun? What happens?
798 return elementForEndOfSyntax(currentElement);
799 }
800
801 /* Or an instance reference ? */
802 if (isInstanceReferenceWord(currentWordIndex)) {
803 /* If so, save word info for this parameterPosition */
804 nextElement = elementForParameter(currentElement);
805 if (nextElement != NULL) {
806 // Create parameter structure for the parameter position based on player words
807 // but without resolving them
808 ParameterPosition *parameterPosition = &parameterPositions[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 /* Or maybe preposition? */
820 if (isPrepositionWord(currentWordIndex) || isVerbWord(currentWordIndex)) {
821 /* A preposition? Or rather, an intermediate word? */
822 nextElement = elementForWord(currentElement, dictionary[playerWords[currentWordIndex].code].code);
823 if (nextElement != NULL) {
824 currentWordIndex++; /* Word matched, go to next */
825 currentElement = (ElementEntry *) pointerTo(nextElement->next);
826 continue;
827 }
828 }
829
830 /* Anything else is an error, but we'll handle 'but' specially here */
831 if (isExceptWord(currentWordIndex))
832 errorButAfterAll(currentWordIndex);
833
834 /* If we get here we couldn't match anything... */
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, &parameterPositions[i].parameters[0]);
874
875 for (restriction = (RestrictionEntry *) pointerTo(elms->next); !isEndOfArray(restriction); restriction++) {
876 ParameterPosition *parameterPosition = &parameterPositions[restriction->parameterNumber-1];
877 if (parameterPosition->explicitMultiple) {
878 /* This was a multiple parameter position, so check all multipleCandidates */
879 int i;
880 for (i = 0; !isEndOfArray(&parameterPosition->parameters[i]); i++) {
881 copyParameter(&localParameters[restriction->parameterNumber-1], &parameterPosition->parameters[i]);
882 if (!restrictionCheck(restriction, parameterPosition->parameters[i].instance)) {
883 /* Multiple could be both an explicit list of instance references and an expansion of ALL */
884 if (!parameterPosition->all) {
885 char marker[80];
886 /* It wasn't ALL, we need to say something about it, so
887 * prepare a printout with $1/2/3
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; /* In any case remove it from the list */
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 /* This was a multiple parameter position, check all multiple candidates and remove failing */
930 int i;
931 for (i = 0; !isEndOfArray(&parameterPositions[positionIndex].parameters[i]); i++)
932 if (parameterPositions[positionIndex].parameters[i].instance != 0) /* Skip any empty slots */
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(&parameter->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 // Ensure that every candidate have the words, even if there where no candidates
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(&parameters[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) { /* No verb code, verb not declared! */
1020 /* TODO Does this ever happen? */
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 /* If the player used ALL, try to find out what was applicable */
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 /* If there where multiple parameters but non left, exit without a */
1088 /* word, assuming we have already said enough */
1089 abortPlayerCommand();
1090 }
1091 }
1092 }
1093
1094
1095 /*
1096 * Disambiguation is hard: there are a couple of different cases that
1097 * we want to handle: Omnipotent parameter position, multiple present
1098 * and non-present objects etc. The following table will show which
1099 * message we would like to give in the various situations.
1100 *
1101 * p = present, n = non-present, 1 = single, m = multiple
1102 * (1p1n = single present, single non-present)
1103 *
1104 * p, n, omni, result, why?
1105 * -----------------------------------------------------------------
1106 * 0, 0, no, errorNoSuch(w)/errorWhat(w)
1107 * 0, 1, no, errorNoSuch(w)/errorWhat(w)
1108 * 0, m, no, errorNoSuch(w)/errorWhat(w)
1109 * 1, 0, no, ok(p)
1110 * 1, 1, no, ok(p)
1111 * 1, m, no, ok(p)
1112 * m, 0, no, errorWhichOne(p)
1113 * m, 1, no, errorWhichOne(p) only present objects should be revealed
1114 * m, m, no, errorWhichOne(p) d:o
1115
1116 * 0, 0, yes, errorNoSuch(w)
1117 * 0, 1, yes, ok(n)
1118 * 0, m, yes, errorWhichOne(n) already looking "beyond" presence, might reveal undiscovered distant objects
1119 * 1, 0, yes, ok(p)
1120 * 1, 1, yes, ok(p) present objects have priority
1121 * 1, m, yes, ok(p) present objects have priority
1122 * m, 0, yes, errorWhichOne(p)
1123 * m, 1, yes, errorWhichOne(p) present objects have priority, but only list present
1124 * m, m, yes, errorWhichOne(p) present objects have priority, but only list present
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 { // Present == 0
1208 { // Distant == 0
1209 disambiguate00N, disambiguate00Y},
1210 { // Distant == 1
1211 disambiguate01N, disambiguate01Y},
1212 { // Distant == M
1213 disambiguate0MN, disambiguate0MY}},
1214 { // Present == 1
1215 { // Distant == 0
1216 disambiguate10N, disambiguate10Y},
1217 { // Distant == 1
1218 disambiguate11N, disambiguate11Y},
1219 { // Distant == M
1220 disambiguate1MN, disambiguate1MY}},
1221 { // Present == M
1222 { // Distant == 0
1223 disambiguateM0N, disambiguateM0Y},
1224 { // Distant == 1
1225 disambiguateM1N, disambiguateM1Y},
1226 { // Distant == M
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; /* 2 = M */
1244
1245 distant = lengthOfParameterArray(allCandidates) - present;
1246 if (distant > 1) distant = 2; /* 2 = M */
1247
1248 result = handler[present][distant][omnipotent](allCandidates, presentCandidates);
1249
1250 // If we returned then it's ok, use the single candidate found
1251 allCandidates[0] = result[0];
1252 setEndOfArray(&allCandidates[1]);
1253 }
1254
1255
1256 /*----------------------------------------------------------------------*/
1257 static void disambiguate(ParameterPosition parameterPositions[], ElementEntry *element) {
1258 /* The New Strategy! Parsing has only collected word indications,
1259 not built anything, so we need to match parameters to instances here */
1260
1261 int position;
1262 for (position = 0; !parameterPositions[position].endOfList; position++) {
1263 findCandidatesForPlayerWords(&parameterPositions[position]);
1264 }
1265
1266 /* Now we have candidates for everything the player said, except
1267 if he used ALL or THEM, then we have built those as parameters, or he
1268 referred to the multiple parameters of the previous command
1269 using 'them, if so, they too are stored as parameters */
1270
1271 for (position = 0; !parameterPositions[position].endOfList; position++) {
1272 ParameterPosition *parameterPosition = &parameterPositions[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 = &parameters[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 /* Now perform restriction checks */
1299 restrictParametersAccordingToSyntax(parameterPositions, element);
1300 }
1301
1302
1303 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1304 static void try(Parameter parameters[], Parameter multipleParameters[]) {
1305 ElementEntry *element; /* Pointer to element list */
1306 static ParameterPosition *parameterPositions = NULL;
1307 if (parameterPositions != NULL)
1308 deallocateParameterPositions(parameterPositions);
1309
1310 // TODO newParameterPositionArray()!!!! Or even reallocatePP.. or cleanPP..
1311 parameterPositions = allocate(sizeof(ParameterPosition)*(MAXPARAMS+1));
1312 parameterPositions[0].endOfList = true;
1313
1314 element = parseInput(parameterPositions);
1315
1316 disambiguate(parameterPositions, element);
1317
1318 // TODO: Now we need to convert back to legacy parameter and multipleParameter format
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); /* ... to understand what he said */
1332
1333 /* More on this line? Must be conjunctions... */
1334 if (!endOfWords(currentWordIndex)) {
1335 if (isConjunctionWord(currentWordIndex))
1336 currentWordIndex++; /* If so skip the conjunction */
1337 if (endOfWords(currentWordIndex))
1338 error(M_WHAT);
1339 if (isConjunctionWord(currentWordIndex))
1340 currentWordIndex++; /* Could be another "and then", if so, skip that too */
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 // Don't add the same instance twice for the same pronoun
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 /* For all parameters note which ones can be referred to by a pronoun */
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 /* Pre-beta2 didn't generate syntax elements for verb words,
1399 need to skip first word which should be the verb */
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 /* Pick up the parse tree for the syntaxes that start with an
1410 instance reference and parse according to that. The
1411 verbWord code is set to 0 to indicate that it is not a verb
1412 but an instance that starts the command. */
1413 verbWordCode = 0;
1414 parseOneCommand(parameters, multipleParameters);
1415 notePronounsForParameters(parameters);
1416 fail = false;
1417 }
1418
1419
1420 /*======================================================================*/
1421 void parse(void) {
1422 /* longjmp's ahead so these need to survive to not leak memory */
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 }