aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan3/parse.cpp
blob: efa5464c33c737ddcd2e8b5d782f78738b4657a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "common/array.h"
#include "glk/alan3/parse.h"
#include "glk/alan3/act.h"
#include "glk/alan3/alt_info.h"
#include "glk/alan3/class.h"
#include "glk/alan3/compatibility.h"
#include "glk/alan3/current.h"
#include "glk/alan3/dictionary.h"
#include "glk/alan3/glkio.h"
#include "glk/alan3/instance.h"
#include "glk/alan3/inter.h"
#include "glk/alan3/jumps.h"
#include "glk/alan3/lists.h"
#include "glk/alan3/literal.h"
#include "glk/alan3/location.h"
#include "glk/alan3/memory.h"
#include "glk/alan3/msg.h"
#include "glk/alan3/options.h"
#include "glk/alan3/output.h"
#include "glk/alan3/parameter_position.h"
#include "glk/alan3/syntax.h"
#include "glk/alan3/scan.h"
#include "glk/alan3/syserr.h"
#include "glk/alan3/utils.h"
#include "glk/alan3/word.h"

namespace Glk {
namespace Alan3 {

/* PRIVATE TYPES */
/* To remember parameter/pronoun relations */
struct Pronoun {
	int pronoun;
	int instance;
};

/*----------------------------------------------------------------------*/
static void clearPronounList(Pronoun list[]) {
	implementationOfSetEndOfArray((Aword *)list);
}


typedef Aint *(*ReferencesFinder)(int wordIndex);
typedef void (*ParameterParser)(CONTEXT, Parameter parameters[]);



/* PRIVATE DATA */
static Pronoun *pronouns = NULL;


/* Syntax Parameters */
static Parameter *previousMultipleParameters; /* Previous multiple list */


/* For parameters that are literals we need to trick message handling to
 * output the word and create a string literal instance if anyone wants to
 * refer to an attribute of it (literals inherit from entity so application
 * can have added an attribute) */

/*----------------------------------------------------------------------*/
static void addParameterForWord(Parameter *parameters, int wordIndex) {
	Parameter *parameter = findEndOfParameterArray(parameters);

	createStringLiteral((char *)pointerTo(dictionary[playerWords[wordIndex].code].string));
	parameter->instance = instanceFromLiteral(litCount); /* A faked literal */
	parameter->useWords = TRUE;
	parameter->firstWord = parameter->lastWord = wordIndex;
	setEndOfArray(parameter + 1);
}


/*----------------------------------------------------------------------*/
static Pronoun *allocatePronounArray(Pronoun *currentList) {
	if (currentList == NULL)
		currentList = (Pronoun *)allocate(sizeof(Pronoun) * (MAXPARAMS + 1));
	clearPronounList(currentList);
	return currentList;
}


/*----------------------------------------------------------------------*/
static bool endOfWords(int wordIndex) {
	return isEndOfArray(&playerWords[wordIndex]);
}


/*----------------------------------------------------------------------*/
static void handleDirectionalCommand(CONTEXT) {
	currentWordIndex++;
	if (!endOfWords(currentWordIndex) && !isConjunctionWord(currentWordIndex)) {
		CALL1(error, M_WHAT)
	} else {
		CALL2(go, current.location, dictionary[playerWords[currentWordIndex - 1].code].code)
	}

	if (!endOfWords(currentWordIndex))
		currentWordIndex++;
}


/*----------------------------------------------------------------------*/
static void errorWhichOne(CONTEXT, Parameter alternative[]) {
	int p; /* Index into the list of alternatives */
	ParameterArray parameters = newParameterArray();

	parameters[0] = alternative[0];
	setEndOfArray(&parameters[1]);
	printMessageWithParameters(M_WHICH_ONE_START, parameters);
	for (p = 1; !isEndOfArray(&alternative[p + 1]); p++) {
		clearParameterArray(parameters);
		addParameterToParameterArray(parameters, &alternative[p]);
		printMessageWithParameters(M_WHICH_ONE_COMMA, parameters);
	}
	clearParameterArray(parameters);
	addParameterToParameterArray(parameters, &alternative[p]);
	printMessageWithParameters(M_WHICH_ONE_OR, parameters);
	freeParameterArray(parameters);
	CALL0(abortPlayerCommand) /* Return with empty error message */
}

/*----------------------------------------------------------------------*/
static void errorWhichPronoun(CONTEXT, int pronounWordIndex, Parameter alternatives[]) {
	int p; /* Index into the list of alternatives */
	Parameter *messageParameters = newParameterArray();

	addParameterForWord(messageParameters, pronounWordIndex);
	printMessageWithParameters(M_WHICH_PRONOUN_START, messageParameters);

	clearParameterArray(messageParameters);
	addParameterToParameterArray(messageParameters, &alternatives[0]);

	printMessageWithParameters(M_WHICH_PRONOUN_FIRST, messageParameters);

	for (p = 1; !isEndOfArray(&alternatives[p + 1]); p++) {
		clearParameterArray(messageParameters);
		addParameterToParameterArray(messageParameters, &alternatives[p]);
		printMessageWithParameters(M_WHICH_ONE_COMMA, messageParameters);
	}
	clearParameterArray(messageParameters);
	addParameterToParameterArray(messageParameters, &alternatives[p]);
	printMessageWithParameters(M_WHICH_ONE_OR, messageParameters);
	freeParameterArray(messageParameters);
	CALL0(abortPlayerCommand)
}

/*----------------------------------------------------------------------*/
static void errorWhat(CONTEXT, int playerWordIndex) {
	Parameter *messageParameters = newParameterArray();

	addParameterForWord(messageParameters, playerWordIndex);
	printMessageWithParameters(M_WHAT_WORD, messageParameters);
	freeParameterArray(messageParameters);
	CALL0(abortPlayerCommand)
}

/*----------------------------------------------------------------------*/
static void errorAfterExcept(CONTEXT, int butWordIndex) {
	Parameter *messageParameters = newParameterArray();
	addParameterForWord(messageParameters, butWordIndex);
	printMessageWithParameters(M_AFTER_BUT, messageParameters);
	freeParameterArray(messageParameters);
	CALL0(abortPlayerCommand)
}

/*----------------------------------------------------------------------*/
static int fakePlayerWordForAll() {
	/* Look through the dictionary and find any ALL_WORD, then add a
	   player word so that it can be used in the message */
	int p, d;

	for (p = 0; !isEndOfArray(&playerWords[p]); p++)
		;
	setEndOfArray(&playerWords[p + 1]); /* Make room for one more word */
	for (d = 0; d < dictionarySize; d++)
		if (isAll(d)) {
			playerWords[p].code = d;
			return p;
		}
	syserr("No ALLWORD found");
	return 0;
}

/*----------------------------------------------------------------------*/
static void errorButAfterAll(CONTEXT, int butWordIndex) {
	Parameter *messageParameters = newParameterArray();
	addParameterForWord(messageParameters, butWordIndex);
	addParameterForWord(messageParameters, fakePlayerWordForAll());
	printMessageWithParameters(M_BUT_ALL, messageParameters);
	freeParameterArray(messageParameters);
	CALL0(abortPlayerCommand)
}

/*----------------------------------------------------------------------*/
static Aint findInstanceForNoun(int wordIndex) {
	DictionaryEntry *d = &dictionary[wordIndex];
	if (d->nounRefs == 0 || d->nounRefs == EOD)
		syserr("No references for noun");
	return *(Aint *) pointerTo(d->nounRefs);
}

/*----------------------------------------------------------------------*/
static void errorNoSuch(CONTEXT, Parameter parameter) {

	/* If there was no instance, assume the last word used is the noun,
	 * then find any instance with the noun he used */
	if (parameter.instance == (Aid) - 1)
		parameter.instance = 0;
	if (parameter.instance == 0)
		parameter.instance = findInstanceForNoun(playerWords[parameter.lastWord].code);
	parameter.useWords = TRUE; /* Indicate to use words and not names */

	clearParameterArray(globalParameters);
	addParameterToParameterArray(globalParameters, &parameter);
	CALL1(error, M_NO_SUCH)
}

/*----------------------------------------------------------------------*/
static void buildAllHere(CONTEXT, Parameter list[]) {
	uint instance;
	bool found = FALSE;
	int word = list[0].firstWord;

	for (instance = 1; instance <= header->instanceMax; instance++)
		if (isHere(instance, /*FALSE*/ TRANSITIVE)) {
			Parameter *parameter = newParameter(instance);
			addParameterToParameterArray(list, parameter);
			deallocate(parameter);
			found = TRUE;
		}
	if (!found)
		errorWhat(context, word);
}


/*----------------------------------------------------------------------*/
static bool endOfPronouns(int pronounIndex) {
	return isEndOfArray(&pronouns[pronounIndex]);
}


/*----------------------------------------------------------------------*/
static int getPronounInstances(int word, Parameter instanceParameters[]) {
	/* Find the instance that the pronoun word could refer to, return 0
	   if none or multiple */
	int p;
	int instanceCount = 0;

	clearParameterArray(instanceParameters);
	for (p = 0; !endOfPronouns(p); p++)
		if (pronouns[p].instance != 0 && dictionary[word].code == (Aword)pronouns[p].pronoun) {
			instanceParameters[instanceCount].instance = pronouns[p].instance;
			instanceParameters[instanceCount].useWords = FALSE; /* Can't use words since they are gone, pronouns
                                                                   refer to parameters in previous command */
			setEndOfArray(&instanceParameters[++instanceCount]);
		}
	return instanceCount;
}

/*----------------------------------------------------------------------*/
static bool inOpaqueContainer(int originalInstance) {
	int instance = admin[originalInstance].location;

	while (isAContainer(instance)) {
		// TODO : isOpaque()
		if (getInstanceAttribute(instance, OPAQUEATTRIBUTE))
			return TRUE;
		instance = admin[instance].location;
	}
	return FALSE;
}

/*----------------------------------------------------------------------*/
static bool reachable(int instance) {
	if (isA(instance, THING) || isA(instance, LOCATION))
		return isHere(instance, TRANSITIVE) && !inOpaqueContainer(instance);
	else
		return TRUE;
}

/*----------------------------------------------------------------------*/
static Aint *nounReferencesForWord(int wordIndex) {
	return (Aint *) pointerTo(dictionary[playerWords[wordIndex].code].nounRefs);
}


/*----------------------------------------------------------------------*/
static Aint *adjectiveReferencesForWord(int wordIndex) {
	return (Aint *) pointerTo(dictionary[playerWords[wordIndex].code].adjectiveRefs);
}


/*----------------------------------------------------------------------*/
static void parseLiteral(Parameter parameters[]) {
	parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++;
	parameters[0].instance = 0;
	parameters[0].isLiteral = TRUE;
	setEndOfArray(&parameters[1]);
}


/*----------------------------------------------------------------------*/
static void parsePronoun(Parameter parameters[]) {
	parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++;
	parameters[0].instance = 0;
	parameters[0].isPronoun = TRUE;
	setEndOfArray(&parameters[1]);
}


/*----------------------------------------------------------------------*/
static bool anotherAdjective(int wordIndex) {
	return !endOfWords(wordIndex) && isAdjectiveWord(wordIndex);
}


/*----------------------------------------------------------------------*/
static bool lastPossibleNoun(int wordIndex) {
	return isNounWord(wordIndex) && (endOfWords(wordIndex + 1) || !isNounWord(wordIndex + 1));
}


/*----------------------------------------------------------------------*/
static void updateWithReferences(Parameter result[], int wordIndex, Aint * (*referenceFinder)(int wordIndex)) {
	static Parameter *references = NULL; /* Instances referenced by a word */
	references = ensureParameterArrayAllocated(references);

	copyReferencesToParameterArray(referenceFinder(wordIndex), references);
	if (lengthOfParameterArray(result) == 0)
		copyParameterArray(result, references);
	else
		intersectParameterArrays(result, references);
}


/*----------------------------------------------------------------------*/
static void filterOutNonReachable(Parameter filteredCandidates[], bool (*reachable)(int)) {
	int i;
	for (i = 0; !isEndOfArray(&filteredCandidates[i]); i++)
		if (!reachable(filteredCandidates[i].instance))
			filteredCandidates[i].instance = 0;
	compressParameterArray(filteredCandidates);
}


/*
 * There are various ways the player can refer to things, some are
 * explicit, in which case they should be kept in the input. If he said
 * 'them', 'all' or some such the list is inferred so we must filter
 * it w.r.t what it can mean. A special case is when he said 'the ball'
 * and there is only one ball here, but multiple in the game.  We need
 * to be able to distinguish between these cases!!!  'them' is
 * explicit, 'all' is inferred, exceptions can never be inferred,
 * maybe 'all' is the only inferred?
 */

/*----------------------------------------------------------------------*/
static void disambiguateCandidatesForPosition(CONTEXT, ParameterPosition parameterPositions[],
		int position, Parameter candidates[]) {
	int i;
	Parameter *parameters = newParameterArray();

	convertPositionsToParameters(parameterPositions, parameters);
	for (i = 0; !isEndOfArray(&candidates[i]); i++) {
		if (candidates[i].instance != 0) { /* Already empty? */
			copyParameter(&parameters[position], &candidates[i]);
			// DISAMBIGUATION!!
			if (!reachable(candidates[i].instance)) {
				// Then remove this candidate from list
				candidates[i].instance = 0;
			} else {
				bool flag;
				FUNC3(possible, flag, current.verb, parameters, parameterPositions)
				if (!flag)
					candidates[i].instance = 0;
			}
		}
	}
	compressParameterArray(candidates);
	freeParameterArray(parameters);
}


/*----------------------------------------------------------------------*/
static bool parseAnyAdjectives(Parameter parameters[]) {
	bool adjectiveOrNounFound = FALSE;
	while (anotherAdjective(currentWordIndex)) {
		if (lastPossibleNoun(currentWordIndex))
			break;
		adjectiveOrNounFound = TRUE;
		currentWordIndex++;
	}
	return adjectiveOrNounFound;
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* Parse the input and note the word indices in the parameters,
   matching will be done by the match* functions */
static void parseAdjectivesAndNoun(CONTEXT, Parameter parameters[]) {
	int wordFirst, wordLast;
	bool adjectiveOrNounFound = FALSE;

	wordFirst = currentWordIndex;

	adjectiveOrNounFound = parseAnyAdjectives(parameters);

	if (!endOfWords(currentWordIndex)) {
		if (isNounWord(currentWordIndex)) {
			adjectiveOrNounFound = TRUE;
			currentWordIndex++;
		} else
			CALL1(error, M_NOUN)
	} else if (adjectiveOrNounFound) {
		/* Perhaps the last word could also be interpreted as a noun? */
		if (isNounWord(currentWordIndex - 1)) {
			// TODO When does this get executed? Maybe if conjunctions can be nouns? Or nouns be adjectives?
			printf("DEBUG: When does this get executed? Found adjective or Noun and the previous word could also be a noun...\n");
		} else
			CALL1(error, M_NOUN)
	}

	if (adjectiveOrNounFound) {
		wordLast = currentWordIndex - 1;

		parameters[0].firstWord = wordFirst;
		parameters[0].lastWord = wordLast;
		parameters[0].instance = 0; /* No instance yet! */
		setEndOfArray(&parameters[1]);
	} else
		setEndOfArray(&parameters[0]);
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void parseReference(CONTEXT, Parameter parameters[]) {
	clearParameterArray(parameters);

	if (isLiteralWord(currentWordIndex)) {
		parseLiteral(parameters);
	} else if (isPronounWord(currentWordIndex)) {
		parsePronoun(parameters);
	} else {
		CALL1(parseAdjectivesAndNoun, parameters)
	}
}


/*----------------------------------------------------------------------*/
static void getPreviousMultipleParameters(Parameter parameters[]) {
	int i;
	for (i = 0; !isEndOfArray(&previousMultipleParameters[i]); i++) {
		parameters[i].candidates = ensureParameterArrayAllocated(parameters[i].candidates);
		setEndOfArray(&parameters[i].candidates[0]); /* No candidates */
		if (!reachable(previousMultipleParameters[i].instance))
			parameters[i].instance = 0;
		else
			parameters[i].instance = previousMultipleParameters[i].instance;
	}
	setEndOfArray(&parameters[i]);
	compressParameterArray(parameters);
}


/*----------------------------------------------------------------------*/
static void parseReferenceToPreviousMultipleParameters(Parameter parameters[]) {
	parameters[0].firstWord = parameters[0].lastWord = currentWordIndex++;
	parameters[0].instance = 0;
	parameters[0].isThem = TRUE;
	setEndOfArray(&parameters[1]);
}


/*----------------------------------------------------------------------*/
static bool parseOneParameter(CONTEXT, Parameter parameters[], int parameterIndex) {
	Parameter *parameter = newParameterArray();

	// TODO Maybe this should go in the complex()?
	if (isThemWord(currentWordIndex) && (!isPronounWord(currentWordIndex) ||
	                                     (isPronounWord(currentWordIndex) && lengthOfParameterArray(previousMultipleParameters) > 0))) {
		// "them" is also a common pronoun for some instances, but if there
		// are previous multiple parameters we give precedence to those
		parseReferenceToPreviousMultipleParameters(parameter);
	} else {
		R0CALL1(parseReference, parameter)
		if (lengthOfParameterArray(parameter) == 0) { /* Failed to find any exceptions! */
			freeParameterArray(parameter);
			return FALSE;
		}
	}

	/* Add the one we found to the parameters */
	parameters[parameterIndex] = parameter[0];
	setEndOfArray(&parameters[parameterIndex + 1]);
	freeParameterArray(parameter);
	return TRUE;
}


/*
 * A "simple" parameter is in one of three forms:
 *
 * 1) adjectives and nouns referencing a single instance (might
 * actually match multiple instances in the game...)
 *
 * 2) multiple of 1) separated by conjunctions ("a and b and c")
 *
 * 3) a pronoun referencing a single instance

 * We also need to handle "them" here since it is also a common
 * pronoun for some instances, e.g. scissor, trouser, money,
 * glasses, but it can also refer to the set of instances from the
 * previous command. If it is a pronoun but there are also multiple
 * parameter from the previous command, we give precedence to the
 * multiple previous.
 */

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void simpleParameterParser(CONTEXT, Parameter parameters[]) {
	bool flag;

	/* This will loop until all references are collected (typically "a and b and c") */
	int parameterIndex;
	for (parameterIndex = 0;; parameterIndex++) {
		FUNC2(parseOneParameter, flag, parameters, parameterIndex)
		if (!flag)
			return;

		if (!endOfWords(currentWordIndex)
		        && (isConjunctionWord(currentWordIndex) && (isAdjectiveWord(currentWordIndex + 1)
		                || isNounWord(currentWordIndex + 1)))) {
			/* Since this is a conjunction and the next seems to be another instance reference,
			   let's continue with that by eating the conjunction */
			currentWordIndex++;
		} else {
			return;
		}
	}
}


/*----------------------------------------------------------------------*/
static void parseExceptions(CONTEXT, ParameterPosition *parameterPosition, ParameterParser simpleParameterParser) {
	int exceptWordIndex = currentWordIndex;
	currentWordIndex++;
	parameterPosition->exceptions = ensureParameterArrayAllocated(parameterPosition->exceptions);
	CALL1(simpleParameterParser, parameterPosition->exceptions)
	
	if (lengthOfParameterArray(parameterPosition->exceptions) == 0)
		errorAfterExcept(context, exceptWordIndex);
}


/*
 * Complex instance references are of the form:
 *
 * 1) all
 *
 * 2) all except
 *
 * 3) a simple (see above) instance reference(s)
 */

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void complexParameterParserDelegate(CONTEXT, ParameterPosition *parameterPosition,
		ParameterParser simpleParameterParser) {
	parameterPosition->parameters = ensureParameterArrayAllocated(parameterPosition->parameters);

	parameterPosition->all = FALSE;
	parameterPosition->them = FALSE;
	parameterPosition->explicitMultiple = FALSE;

	if (isAllWord(currentWordIndex)) {
		parameterPosition->all = TRUE;
		parameterPosition->explicitMultiple = TRUE;
		parameterPosition->parameters[0].firstWord = currentWordIndex;
		parameterPosition->parameters[0].lastWord = currentWordIndex;
		currentWordIndex++;
		parameterPosition->parameters[0].instance = 0;
		setEndOfArray(&parameterPosition->parameters[1]);
		if (!endOfWords(currentWordIndex) && isExceptWord(currentWordIndex))
			CALL2(parseExceptions, parameterPosition, simpleParameterParser)
	} else {
		CALL1(simpleParameterParser, parameterPosition->parameters)
		if (lengthOfParameterArray(parameterPosition->parameters) > 1)
			parameterPosition->explicitMultiple = TRUE;
	}

}

/*----------------------------------------------------------------------*/
static void complexReferencesParser(CONTEXT, ParameterPosition *parameterPosition) {
	complexParameterParserDelegate(context, parameterPosition, simpleParameterParser);
}



/*----------------------------------------------------------------------*/
static char *classNameAndId(int classId) {
	static char buffer[1000] = "";

	if (classId != -1)
		sprintf(buffer, "%s[%d]", idOfClass(classId), classId);
	else
		sprintf(buffer, "Container");
	return buffer;
}


/*----------------------------------------------------------------------*/
static char *parameterNumberAndName(int parameterNumber) {
	static char buffer[1000] = "";
	/* HERE SHOULD BE current.syntax */
	char *parameterName = parameterNameInSyntax(current.syntax, parameterNumber);

	if (parameterName != NULL)
		sprintf(buffer, "%s(#%d)", parameterName, parameterNumber);
	else
		sprintf(buffer, "#%d", parameterNumber);
	return buffer;
}


/*----------------------------------------------------------------------*/
static void traceRestriction(RestrictionEntry *restriction, int classId, bool condition) {
	printf("\n<SYNTAX RESTRICTION WHERE parameter %s Isa %s, %s>\n",
	       parameterNumberAndName(restriction->parameterNumber),
	       classNameAndId(classId), condition ? "PASSED" : "FAILED:");
}


/*----------------------------------------------------------------------*/
static bool restrictionCheck(RestrictionEntry *restriction, int instance) {
	if (restriction->_class == RESTRICTIONCLASS_CONTAINER) {
		if (traceSectionOption)
			traceRestriction(restriction, -1, isAContainer(instance));
		return isAContainer(instance);
	} else {
		if (traceSectionOption)
			traceRestriction(restriction, restriction->_class, isA(instance, restriction->_class));
		return isA(instance, restriction->_class);
	}
}


/*----------------------------------------------------------------------*/
static void runRestriction(CONTEXT, RestrictionEntry *restriction, Parameter parameters[]) {
	if (restriction->stms) {
		setGlobalParameters(parameters);
		interpret(context, restriction->stms);
	} else {
		error(context, M_CANT0);
	}
}


/*----------------------------------------------------------------------*/
static int remapParameterOrder(int syntaxNumber, ParameterPosition parameterPositions[]) {
	/* Find the syntax map, use the verb code from it and remap the parameters */
	ParameterMapEntry *parameterMapTable;
	Aword *parameterMap;
	Aint parameterNumber;
	Common::Array<ParameterPosition> savedParameterPositions;
	savedParameterPositions.resize(MAXPARAMS + 1);

	for (parameterMapTable = (ParameterMapEntry *)pointerTo(header->parameterMapAddress); !isEndOfArray(parameterMapTable); parameterMapTable++)
		if (parameterMapTable->syntaxNumber == syntaxNumber)
			break;
	if (isEndOfArray(parameterMapTable))
		syserr("Could not find syntax in mapping table.");

	parameterMap = (Aword *)pointerTo(parameterMapTable->parameterMapping);

	copyParameterPositions(parameterPositions, &savedParameterPositions[0]);

	for (parameterNumber = 1; !savedParameterPositions[parameterNumber - 1].endOfList; parameterNumber++) {
		parameterPositions[parameterNumber - 1] = savedParameterPositions[parameterMap[parameterNumber - 1] - 1];
	}

	return parameterMapTable->verbCode;
}


/*----------------------------------------------------------------------*/
static bool hasBit(Aword flags, Aint bit) {
	return (flags & bit) != 0;
}

/*----------------------------------------------------------------------*/
static bool multipleAllowed(Aword flags) {
	return hasBit(flags, MULTIPLEBIT);
}



/*
 * There are a number of ways that the number of parameters might
 * be more than one:
 *
 * 1) Player used ALL and it matched more than one
 *
 * 2) Player explicitly refered to multiple objects
 *
 * 3) Player did a single (or multiple) reference that was ambiguous
 * in which case we need to disambiguate it (them). If we want to do
 * this after complete parsing we must be able to see the possible
 * parameters for each of these references, e.g.:
 *
 * > take the vase and the book
 *
 * In this case it is a single parameterPosition, but multiple
 * explicit references and each of those might match multiple
 * instances in the game.
 */

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void parseParameterPosition(CONTEXT, ParameterPosition *parameterPosition,
		Aword flags, void (*complexReferencesParser)(CONTEXT, ParameterPosition *parameterPosition)) {
	parameterPosition->parameters = ensureParameterArrayAllocated(parameterPosition->parameters);

	CALL1(complexReferencesParser, parameterPosition)
	if (lengthOfParameterArray(parameterPosition->parameters) == 0) /* No object!? */
		CALL1(error, M_WHAT)

	if (parameterPosition->explicitMultiple && !multipleAllowed(flags))
		CALL1(error, M_MULTIPLE)
}

/*----------------------------------------------------------------------*/
static ElementEntry *elementForParameter(ElementEntry *elms) {
	/* Require a parameter if elms->code == 0! */
	while (!isEndOfArray(elms) && elms->code != 0)
		elms++;
	if (isEndOfArray(elms))
		return NULL;
	return elms;
}

/*----------------------------------------------------------------------*/
static ElementEntry *elementForEndOfSyntax(ElementEntry *elms) {
	while (!isEndOfArray(elms) && (Aword)elms->code != EOS)
		elms++;
	if (isEndOfArray(elms)) /* No match for EOS! */
		return NULL;
	return elms;
}

/*----------------------------------------------------------------------*/
static ElementEntry *elementForWord(ElementEntry *elms, Aint wordCode) {
	while (!isEndOfArray(elms) && elms->code != wordCode)
		elms++;
	if (isEndOfArray(elms))
		return NULL;
	return elms;
}


/*----------------------------------------------------------------------*/
static bool isInstanceReferenceWord(int wordIndex) {
	return isNounWord(wordIndex) || isAdjectiveWord(wordIndex) || isAllWord(wordIndex)
	       || isLiteralWord(wordIndex) || isItWord(wordIndex) || isThemWord(wordIndex) || isPronounWord(wordIndex);
}


/*----------------------------------------------------------------------*/
static bool endOfPlayerCommand(int wordIndex) {
	return endOfWords(wordIndex) || isConjunctionWord(wordIndex);
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static ElementEntry *parseInputAccordingToSyntax(CONTEXT, SyntaxEntry *syntax, ParameterPosition parameterPositions[]) {
	ElementEntry *currentElement = elementTreeOf(syntax);
	ElementEntry *nextElement = currentElement;

	int parameterCount = 0;
	while (nextElement != NULL) {
		/* Traverse the possible branches of currentElement to find a match, let the actual input control what we look for */
		parameterPositions[parameterCount].endOfList = TRUE;

		if (endOfPlayerCommand(currentWordIndex)) {
			// TODO If a conjunction word is also some other type of word, like noun? What happens?
			return elementForEndOfSyntax(currentElement);
		}

		/* Or an instance reference ? */
		if (isInstanceReferenceWord(currentWordIndex)) {
			/* If so, save word info for this parameterPosition */
			nextElement = elementForParameter(currentElement);
			if (nextElement != NULL) {
				// Create parameter structure for the parameter position based on player words
				// but without resolving them
				ParameterPosition *parameterPosition = &parameterPositions[parameterCount];
				R0CALL3(parseParameterPosition, parameterPosition, nextElement->flags, complexReferencesParser)

				parameterPosition->flags = nextElement->flags;
				parameterPosition->endOfList = FALSE;

				currentElement = (ElementEntry *) pointerTo(nextElement->next);
				parameterCount++;
				continue;
			}
		}

		/* Or maybe preposition? */
		if (isPrepositionWord(currentWordIndex) || isVerbWord(currentWordIndex)) {
			/* A preposition? Or rather, an intermediate word? */
			nextElement = elementForWord(currentElement, dictionary[playerWords[currentWordIndex].code].code);
			if (nextElement != NULL) {
				currentWordIndex++; /* Word matched, go to next */
				currentElement = (ElementEntry *) pointerTo(nextElement->next);
				continue;
			}
		}

		/* Anything else is an error, but we'll handle 'but' specially here */
		if (isExceptWord(currentWordIndex))
			R0CALL1(errorButAfterAll, currentWordIndex)

		/* If we get here we couldn't match anything... */
		nextElement = NULL;
	}
	return NULL;
}


/*----------------------------------------------------------------------*/
static bool anyExplicitMultiple(ParameterPosition parameterPositions[]) {
	int i;

	for (i = 0; !parameterPositions[i].endOfList; i++)
		if (parameterPositions[i].explicitMultiple)
			return TRUE;
	return FALSE;
}


/*----------------------------------------------------------------------*/
static bool anyAll(ParameterPosition parameterPositions[]) {
	int i;

	for (i = 0; !parameterPositions[i].endOfList; i++)
		if (parameterPositions[i].all)
			return TRUE;
	return FALSE;
}


/*----------------------------------------------------------------------*/
static void checkRestrictedParameters(CONTEXT, ParameterPosition parameterPositions[], ElementEntry elms[]) {
	RestrictionEntry *restriction;
	static Parameter *localParameters = NULL;
	int i;

	localParameters = ensureParameterArrayAllocated(localParameters);
	clearParameterArray(localParameters);

	for (i = 0; !parameterPositions[i].endOfList; i++)
		addParameterToParameterArray(localParameters, &parameterPositions[i].parameters[0]);

	for (restriction = (RestrictionEntry *) pointerTo(elms->next); !isEndOfArray(restriction); restriction++) {
		ParameterPosition *parameterPosition = &parameterPositions[restriction->parameterNumber - 1];
		if (parameterPosition->explicitMultiple) {
			/* This was a multiple parameter position, so check all multipleCandidates */
			for (i = 0; !isEndOfArray(&parameterPosition->parameters[i]); i++) {
				copyParameter(&localParameters[restriction->parameterNumber - 1], &parameterPosition->parameters[i]);
				if (!restrictionCheck(restriction, parameterPosition->parameters[i].instance)) {
					/* Multiple could be both an explicit list of instance references and an expansion of ALL */
					if (!parameterPosition->all) {
						char marker[80];
						/* It wasn't ALL, we need to say something about it, so
						 * prepare a printout with $1/2/3
						 */
						sprintf(marker, "($%ld)", (unsigned long) restriction->parameterNumber);
						setGlobalParameters(localParameters);
						output(marker);
						CALL2(runRestriction, restriction, localParameters)
						para();
					}
					parameterPosition->parameters[i].instance = 0; /* In any case remove it from the list */
				}
			}
		} else {
			if (!restrictionCheck(restriction, parameterPosition->parameters[0].instance)) {
				CALL2(runRestriction, restriction, localParameters)
				CALL0(abortPlayerCommand)
			}
		}
		parameterPositions[restriction->parameterNumber - 1].checked = TRUE;
	}
	freeParameterArray(localParameters);
	localParameters = NULL;
}


/*----------------------------------------------------------------------*/
static void impossibleWith(CONTEXT, ParameterPosition parameterPositions[], int positionIndex) {
	if (isPreBeta2(header->version)) {
		error(context, M_CANT0);
	} else {
		printMessageWithInstanceParameter(M_IMPOSSIBLE_WITH, parameterPositions[positionIndex].parameters[0].instance);
		error(context, NO_MSG);
	}
}



/*----------------------------------------------------------------------*/
static void checkNonRestrictedParameters(CONTEXT, ParameterPosition parameterPositions[]) {
	int positionIndex;
	for (positionIndex = 0; !parameterPositions[positionIndex].endOfList; positionIndex++)
		if (!parameterPositions[positionIndex].checked) {
			if (parameterPositions[positionIndex].explicitMultiple) {
				/* This was a multiple parameter position, check all multiple candidates and remove failing */
				int i;
				for (i = 0; !isEndOfArray(&parameterPositions[positionIndex].parameters[i]); i++)
					if (parameterPositions[positionIndex].parameters[i].instance != 0) /* Skip any empty slots */
						if (!isAObject(parameterPositions[positionIndex].parameters[i].instance))
							parameterPositions[positionIndex].parameters[i].instance = 0;
			} else if (!isAObject(parameterPositions[positionIndex].parameters[0].instance))
				impossibleWith(context, parameterPositions, positionIndex);
		}
}


/*----------------------------------------------------------------------*/
static void restrictParametersAccordingToSyntax(CONTEXT, ParameterPosition parameterPositions[], ElementEntry *elms) {
	uncheckAllParameterPositions(parameterPositions);
	CALL2(checkRestrictedParameters, parameterPositions, elms)
	CALL1(checkNonRestrictedParameters, parameterPositions)

	int positionIndex;
	for (positionIndex = 0; !parameterPositions[positionIndex].endOfList; positionIndex++)
		compressParameterArray(parameterPositions[positionIndex].parameters);
}


/*----------------------------------------------------------------------*/
static void matchPronoun(CONTEXT, Parameter *parameter) {
	static Parameter *pronounInstances = NULL;
	pronounInstances = ensureParameterArrayAllocated(pronounInstances);

	int pronounCandidateCount = getPronounInstances(playerWords[parameter->firstWord].code, pronounInstances);
	if (pronounCandidateCount == 0) {
		CALL1(errorWhat, parameter->firstWord)
	} else if (pronounCandidateCount > 1) {
		CALL2(errorWhichPronoun, parameter->firstWord, pronounInstances)
	} else {
		parameter->candidates[0] = pronounInstances[0];
		setEndOfArray(&parameter->candidates[1]);
	}
}


/*----------------------------------------------------------------------*/
static void matchNounPhrase(Parameter *parameter, ReferencesFinder adjectiveReferencesFinder, ReferencesFinder nounReferencesFinder) {
	int i;

	for (i = parameter->firstWord; i < parameter->lastWord; i++)
		updateWithReferences(parameter->candidates, i, adjectiveReferencesFinder);
	updateWithReferences(parameter->candidates, parameter->lastWord, nounReferencesFinder);
}


/*----------------------------------------------------------------------*/
static void instanceMatcher(CONTEXT, Parameter *parameter) {
	Parameter *candidates = parameter->candidates;
	int i;

	if (parameter->isLiteral) {
		candidates[0].instance = instanceFromLiteral(playerWords[parameter->firstWord].code - dictionarySize);
		setEndOfArray(&candidates[1]);
	} else if (parameter->isPronoun) {
		CALL1(matchPronoun, parameter)
	} else
		matchNounPhrase(parameter, adjectiveReferencesForWord, nounReferencesForWord);

	// Ensure that every candidate have the words, even if there where no candidates
	candidates[0].firstWord = parameter->firstWord;
	candidates[0].lastWord = parameter->lastWord;
	for (i = 0; i < lengthOfParameterArray(candidates); i++) {
		candidates[i].firstWord = parameter->firstWord;
		candidates[i].lastWord = parameter->lastWord;
	}
}


/*----------------------------------------------------------------------*/
static void findCandidates(CONTEXT, Parameter parameters[], void (*instanceMatcher)(CONTEXT, Parameter *parameter)) {
	int i;

	for (i = 0; i < lengthOfParameterArray(parameters); i++) {
		parameters[i].candidates = ensureParameterArrayAllocated(parameters[i].candidates);
		CALL1(instanceMatcher, &parameters[i])

		parameters[i].candidates[0].isPronoun = parameters[i].isPronoun;
	}
}


/*----------------------------------------------------------------------*/
static void handleFailedParse(CONTEXT, ElementEntry *elms) {
	if (elms == NULL)
		error(context, M_WHAT);
	else if (elms->next == 0) { /* No verb code, verb not declared! */
		/* TODO Does this ever happen? */
		error(context, M_CANT0);
	}
}


/*----------------------------------------------------------------------*/
static void convertMultipleCandidatesToMultipleParameters(ParameterPosition parameterPositions[], Parameter multipleParameters[]) {
	int parameterIndex;
	for (parameterIndex = 0; !parameterPositions[parameterIndex].endOfList; parameterIndex++)
		if (parameterPositions[parameterIndex].explicitMultiple) {
			compressParameterArray(parameterPositions[parameterIndex].parameters);
			copyParameterArray(multipleParameters, parameterPositions[parameterIndex].parameters);
		}
}


/*----------------------------------------------------------------------*/
static ElementEntry *parseInput(CONTEXT, ParameterPosition *parameterPositions) {
	ElementEntry *element;
	SyntaxEntry *stx;

	R0FUNC1(findSyntaxTreeForVerb, stx, verbWordCode)
	R0FUNC2(parseInputAccordingToSyntax, element, stx, parameterPositions)
	R0CALL1(handleFailedParse, element)

	current.syntax = element->flags;
	current.verb = remapParameterOrder(current.syntax, parameterPositions);
	return element;
}


/*----------------------------------------------------------------------*/
static void findCandidatesForPlayerWords(CONTEXT, ParameterPosition *parameterPosition) {
	ParameterArray parameters = parameterPosition->parameters;

	if (!parameterArrayIsEmpty(parameters)) {
		if (parameters[0].isThem) {
			parameterPosition->them = TRUE;
			getPreviousMultipleParameters(parameters);
			if (lengthOfParameterArray(parameters) == 0)
				CALL1(errorWhat, parameters[0].firstWord)
			if (lengthOfParameterArray(parameters) > 1)
				parameterPosition->explicitMultiple = TRUE;
		} else if (parameterPosition->all) {
			CALL1(buildAllHere, parameters)
			if (!parameterArrayIsEmpty(parameterPosition->exceptions))
				CALL2(findCandidates, parameterPosition->exceptions, instanceMatcher)
		} else
			CALL2(findCandidates, parameters, instanceMatcher)
	}
}


/*----------------------------------------------------------------------*/
static void handleMultiplePosition(CONTEXT, ParameterPosition parameterPositions[]) {
	int multiplePosition = findMultipleParameterPosition(parameterPositions);
	if (anyAll(parameterPositions)) {
		/* If the player used ALL, try to find out what was applicable */
		CALL3(disambiguateCandidatesForPosition, parameterPositions, multiplePosition,
			parameterPositions[multiplePosition].parameters)
		
		if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0)
			CALL1(errorWhat, parameterPositions[multiplePosition].parameters[0].firstWord)

		subtractParameterArrays(parameterPositions[multiplePosition].parameters, parameterPositions[multiplePosition].exceptions);
		if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0)
			CALL1(error, M_NOT_MUCH)
	} else if (anyExplicitMultiple(parameterPositions)) {
		compressParameterArray(parameterPositions[multiplePosition].parameters);
		if (lengthOfParameterArray(parameterPositions[multiplePosition].parameters) == 0) {
			/* If there where multiple parameters but non left, exit without a */
			/* word, assuming we have already said enough */
			CALL0(abortPlayerCommand)
		}
	}
}


/*
 * Disambiguation is hard: there are a couple of different cases that
 * we want to handle: Omnipotent parameter position, multiple present
 * and non-present objects etc. The following table will show which
 * message we would like to give in the various situations.
 *
 * p = present, n = non-present, 1 = single, m = multiple
 * (1p1n = single present, single non-present)
 *
 * p, n, omni,  result,                 why?
 * -----------------------------------------------------------------
 * 0, 0, no,    errorNoSuch(w)/errorWhat(w)
 * 0, 1, no,    errorNoSuch(w)/errorWhat(w)
 * 0, m, no,    errorNoSuch(w)/errorWhat(w)
 * 1, 0, no,    ok(p)
 * 1, 1, no,    ok(p)
 * 1, m, no,    ok(p)
 * m, 0, no,    errorWhichOne(p)
 * m, 1, no,    errorWhichOne(p)    only present objects should be revealed
 * m, m, no,    errorWhichOne(p)    d:o

 * 0, 0, yes,   errorNoSuch(w)
 * 0, 1, yes,   ok(n)
 * 0, m, yes,   errorWhichOne(n)    already looking "beyond" presence, might reveal undiscovered distant objects
 * 1, 0, yes,   ok(p)
 * 1, 1, yes,   ok(p)               present objects have priority
 * 1, m, yes,   ok(p)               present objects have priority
 * m, 0, yes,   errorWhichOne(p)
 * m, 1, yes,   errorWhichOne(p)    present objects have priority, but only list present
 * m, m, yes,   errorWhichOne(p)    present objects have priority, but only list present
 */


typedef Parameter *DisambiguationHandler(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]);
typedef DisambiguationHandler *DisambiguationHandlerTable[3][3][2];

static Parameter *disambiguate00N(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	if (allCandidates[0].isPronoun)
		R0CALL1(errorWhat, allCandidates[0].firstWord)
	else
		R0CALL1(errorNoSuch, allCandidates[0]);
	return NULL;
}

static Parameter *disambiguate01N(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	if (allCandidates[0].isPronoun)
		R0CALL1(errorWhat, allCandidates[0].firstWord)
	else
		R0CALL1(errorNoSuch, allCandidates[0])
	return NULL;
}

static Parameter *disambiguate0MN(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	if (allCandidates[0].isPronoun)
		R0CALL1(errorWhat, allCandidates[0].firstWord)
	else
		R0CALL1(errorNoSuch, allCandidates[0])
	return NULL;
}

static Parameter *disambiguate10N(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	return presentCandidates;
}

static Parameter *disambiguate11N(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	return presentCandidates;
}

static Parameter *disambiguate1MN(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	return presentCandidates;
}
static Parameter *disambiguateM0N(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	R0CALL1(errorWhichOne, presentCandidates)
	return NULL;
}

static Parameter *disambiguateM1N(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	R0CALL1(errorWhichOne, presentCandidates)
	return NULL;
}

static Parameter *disambiguateMMN(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	R0CALL1(errorWhichOne, presentCandidates)
	return NULL;
}

static Parameter *disambiguate00Y(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	errorNoSuch(context, allCandidates[0]);
	return NULL;
}
static Parameter *disambiguate01Y(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	return allCandidates;
}
static Parameter *disambiguate0MY(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	R0CALL1(errorWhichOne, allCandidates)
	return NULL;
}
static Parameter *disambiguate10Y(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	return presentCandidates;
}
static Parameter *disambiguate11Y(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	return presentCandidates;
}
static Parameter *disambiguate1MY(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	return presentCandidates;
}
static Parameter *disambiguateM0Y(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	R0CALL1(errorWhichOne, presentCandidates)
	return NULL;
}
static Parameter *disambiguateM1Y(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	R0CALL1(errorWhichOne, presentCandidates)
	return NULL;
}
static Parameter *disambiguateMMY(CONTEXT, Parameter allCandidates[], Parameter presentCandidates[]) {
	R0CALL1(errorWhichOne, presentCandidates)
	return NULL;
}

static DisambiguationHandlerTable disambiguationHandlerTable = {
	{
		// Present == 0
		{
			// Distant == 0
			disambiguate00N, disambiguate00Y
		},
		{
			// Distant == 1
			disambiguate01N, disambiguate01Y
		},
		{
			// Distant == M
			disambiguate0MN, disambiguate0MY
		}
	},
	{
		//  Present == 1
		{
			// Distant == 0
			disambiguate10N, disambiguate10Y
		},
		{
			// Distant == 1
			disambiguate11N, disambiguate11Y
		},
		{
			// Distant == M
			disambiguate1MN, disambiguate1MY
		}
	},
	{
		// Present == M
		{
			// Distant == 0
			disambiguateM0N, disambiguateM0Y
		},
		{
			// Distant == 1
			disambiguateM1N, disambiguateM1Y
		},
		{
			// Distant == M
			disambiguateMMN, disambiguateMMY
		}
	}
};

/*----------------------------------------------------------------------*/
static void disambiguateCandidates(CONTEXT, Parameter *allCandidates, bool omnipotent, bool (*reachable)(int), DisambiguationHandlerTable handler) {
	static Parameter *presentCandidates = NULL;
	int present;
	int distant;
	Parameter *result;

	presentCandidates = ensureParameterArrayAllocated(presentCandidates);

	copyParameterArray(presentCandidates, allCandidates);
	filterOutNonReachable(presentCandidates, reachable);

	present = lengthOfParameterArray(presentCandidates);
	if (present > 1) present = 2; /* 2 = M */

	distant = lengthOfParameterArray(allCandidates) - present;
	if (distant > 1) distant = 2; /* 2 = M */

	FUNC2(handler[present][distant][omnipotent], result, allCandidates, presentCandidates)

	// If we returned then it's ok, use the single candidate found
	allCandidates[0] = result[0];
	setEndOfArray(&allCandidates[1]);
}


/*----------------------------------------------------------------------*/
static void disambiguate(CONTEXT, ParameterPosition parameterPositions[], ElementEntry *element) {
	/* The New Strategy! Parsing has only collected word indications,
	   not built anything, so we need to match parameters to instances here */

	int position;
	for (position = 0; !parameterPositions[position].endOfList; position++) {
		CALL1(findCandidatesForPlayerWords, &parameterPositions[position])
	}

	/* Now we have candidates for everything the player said, except
	   if he used ALL or THEM, then we have built those as parameters, or he
	   referred to the multiple parameters of the previous command
	   using 'them, if so, they too are stored as parameters */

	for (position = 0; !parameterPositions[position].endOfList; position++) {
		ParameterPosition *parameterPosition = &parameterPositions[position];
		bool omni = hasBit(parameterPosition->flags, OMNIBIT);
		if (!parameterPosition->all && !parameterPosition->them) {
			Parameter *parameters = parameterPosition->parameters;
			int p;
			for (p = 0; p < lengthOfParameterArray(parameters); p++) {
				Parameter *parameter = &parameters[p];
				Parameter *candidates = parameter->candidates;
				CALL4(disambiguateCandidates, candidates, omni, reachable, disambiguationHandlerTable)
				
				parameter->instance = candidates[0].instance;
			}
		}
		if (parameterPosition->all) {
			Parameter *exceptions = parameterPosition->exceptions;
			int p;
			for (p = 0; p < lengthOfParameterArray(exceptions); p++) {
				Parameter *parameter = &exceptions[p];
				Parameter *candidates = parameter->candidates;
				CALL4(disambiguateCandidates, candidates, omni, reachable, disambiguationHandlerTable)

				parameter->instance = candidates[0].instance;
			}
		}
	}

	CALL1(handleMultiplePosition, parameterPositions)

	/* Now perform restriction checks */
	CALL2(restrictParametersAccordingToSyntax, parameterPositions, element)
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void tryParam(CONTEXT, Parameter parameters[], Parameter multipleParameters[]) {
	ElementEntry *element;      /* Pointer to element list */
	static ParameterPosition *parameterPositions = NULL;
	if (parameterPositions != NULL)
		deallocateParameterPositions(parameterPositions);

	// TODO newParameterPositionArray()!!!! Or even reallocatePP.. or cleanPP..
	parameterPositions = (ParameterPosition *)allocate(sizeof(ParameterPosition) * (MAXPARAMS + 1));
	parameterPositions[0].endOfList = TRUE;

	FUNC1(parseInput, element, parameterPositions)
	CALL2(disambiguate, parameterPositions, element)

	// TODO: Now we need to convert back to legacy parameter and multipleParameter format
	convertPositionsToParameters(parameterPositions, parameters);
	markExplicitMultiple(parameterPositions, parameters);
	convertMultipleCandidatesToMultipleParameters(parameterPositions, multipleParameters);

	deallocateParameterPositions(parameterPositions);
	parameterPositions = NULL;
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void parseOneCommand(CONTEXT, Parameter parameters[], Parameter multipleParameters[]) {
	// ... to understand what he said
	CALL2(tryParam, parameters, multipleParameters)

	/* More on this line? */
	if (!endOfWords(currentWordIndex)) {
		if (isConjunctionWord(currentWordIndex))
			currentWordIndex++; /* If so skip the conjunction */
		else
			error(context, M_WHAT);
	}
}

/*======================================================================*/
void initParsing(void) {
	currentWordIndex = 0;
	continued = FALSE;
	ensureSpaceForPlayerWords(0);
	clearWordList(playerWords);

	pronouns = allocatePronounArray(pronouns);
	globalParameters = ensureParameterArrayAllocated(globalParameters);
	previousMultipleParameters = ensureParameterArrayAllocated(previousMultipleParameters);
}

/*----------------------------------------------------------------------*/
static int pronounWordForInstance(int instance) {
	/* Scan through the dictionary to find any pronouns that can be used
	   for this instance */
	int w;

	for (w = 0; w < dictionarySize; w++)
		if (isPronoun(w)) {
			Aword *reference = (Aword *)pointerTo(dictionary[w].pronounRefs);
			while (*reference != EOD) {
				if (*reference == (Aword)instance)
					return dictionary[w].code;
				reference++;
			}
		}
	return 0;
}

/*----------------------------------------------------------------------*/
static void addPronounForInstance(int thePronoun, int instanceCode) {
	int i;

	for (i = 0; !endOfPronouns(i); i++)
		if (pronouns[i].pronoun == thePronoun && pronouns[i].instance == instanceCode)
			// Don't add the same instance twice for the same pronoun
			return;
	pronouns[i].pronoun = thePronoun;
	pronouns[i].instance = instanceCode;
	setEndOfArray(&pronouns[i + 1]);
}

/*----------------------------------------------------------------------*/
static void notePronounsForParameters(Parameter parameters[]) {
	/* For all parameters note which ones can be referred to by a pronoun */
	Parameter *p;

	clearPronounList(pronouns);
	for (p = parameters; !isEndOfArray(p); p++) {
		int pronoun = pronounWordForInstance(p->instance);
		if (pronoun > 0)
			addPronounForInstance(pronoun, p->instance);
	}
}


/*----------------------------------------------------------------------*/
static void parseVerbCommand(CONTEXT, Parameter parameters[], Parameter multipleParameters[]) {
	verbWord = playerWords[currentWordIndex].code;
	verbWordCode = dictionary[verbWord].code;
	if (isPreBeta2(header->version))
		/* Pre-beta2 didn't generate syntax elements for verb words,
		   need to skip first word which should be the verb */
		currentWordIndex++;
	CALL2(parseOneCommand, parameters, multipleParameters)
	notePronounsForParameters(parameters);
	fail = FALSE;
}


/*----------------------------------------------------------------------*/
static void parseInstanceCommand(CONTEXT, Parameter parameters[], Parameter multipleParameters[]) {
	/* Pick up the parse tree for the syntaxes that start with an
	   instance reference and parse according to that. The
	   verbWord code is set to 0 to indicate that it is not a verb
	   but an instance that starts the command. */
	verbWordCode = 0;
	CALL2(parseOneCommand, parameters, multipleParameters)
	notePronounsForParameters(parameters);
	fail = FALSE;
}


/*======================================================================*/
void parse(CONTEXT) {
	/* longjmp's ahead so these need to survive to not leak memory */
	static Parameter *parameters = NULL;
	static Parameter *multipleParameters = NULL;
	parameters = ensureParameterArrayAllocated(parameters);
	multipleParameters = ensureParameterArrayAllocated(multipleParameters);

	if (endOfWords(currentWordIndex)) {
		currentWordIndex = 0;
		CALL0(scan)
	} else if (anyOutput) {
		para();
	}

	capitalize = TRUE;

	firstWord = currentWordIndex;
	if (isVerbWord(currentWordIndex)) {
		CALL2(parseVerbCommand, parameters, multipleParameters);
		CALL3(action, current.verb, parameters, multipleParameters)
	} else if (isDirectionWord(currentWordIndex)) {
		clearParameterArray(previousMultipleParameters);
		clearPronounList(pronouns);
		CALL0(handleDirectionalCommand)
	} else if (isInstanceReferenceWord(currentWordIndex)) {
		CALL2(parseInstanceCommand, parameters, multipleParameters)
		CALL3(action, current.verb, parameters, multipleParameters)
	} else
		CALL1(error, M_WHAT)

	if (fail)
		CALL1(error, NO_MSG)

	lastWord = currentWordIndex - 1;
	if (isConjunctionWord(lastWord))
		lastWord--;

	if (lengthOfParameterArray(parameters) > 0)
		copyParameterArray(previousMultipleParameters, multipleParameters);
	else
		clearParameterArray(previousMultipleParameters);

	freeParameterArray(parameters);
	parameters = NULL;
	freeParameterArray(multipleParameters);
	multipleParameters = NULL;
}

} // End of namespace Alan3
} // End of namespace Glk