Debian Codegraph @

terps/hugo/source/heparse.c

raw symbols SCIP
1 /*
2 HEPARSE.C
3
4 Input parsing functions:
5
6 Available
7 CallLibraryParse
8 FindWord
9 KillWord
10 Match routines
11 AnyObjWord
12 AddAllObjects
13 AddObj
14 AddPossibleObject
15 AdvanceGrammar
16 DomainObj
17 InList
18 ObjWord
19 ObjWordType
20 SubtractObj
21 SubtractPossibleObject
22 TryObj
23 ValidObj
24 Parse
25 ParseError
26 RemoveWord
27 ResetFindObject
28 SeparateWords
29
30 for the Hugo Engine
31
32 Copyright (c) 1995-2009 by Kent Tessman
33 */
34
35
36 #include "heheader.h"
37 #include <stdlib.h>
38
39
40 /* Function prototypes: */
41 void AddObj(int obj);
42 void AddPossibleObject(int obj, char type, unsigned int w);
43 void AdvanceGrammar(void);
44 int DomainObj(int obj);
45 int InList(int obj);
46 int MatchObject(int *wordnum);
47 int MatchWord(int *wordnum);
48 int ObjWordType(int obj, unsigned int w, int type);
49 void ResetFindObject(void);
50 void SubtractObj(int obj);
51 void SubtractPossibleObject(int obj);
52 void TryObj(int obj);
53
54 int GetVal(void); /* from heexpr.c */
55
56 #define MAXOBJLIST 32
57
58 #define STARTS_AS_NUMBER(a) (((a[0]>='0' && a[0]<='9') || a[0]=='-')?1:0)
59
60 char buffer[MAXBUFFER+MAXWORDS]; /* input buffer */
61 char errbuf[MAXBUFFER+1]; /* last invalid input */
62 char line[1025]; /* line buffer */
63
64 int words = 0; /* parsed word count */
65 char *word[MAXWORDS+1]; /* breakdown into words */
66 unsigned int wd[MAXWORDS+1]; /* " " dict. entries */
67 unsigned int parsed_number; /* needed for numbers in input */
68
69 signed char remaining = 0; /* multiple commands in input */
70 char parseerr[MAXBUFFER+1]; /* for passing to RunPrint, etc. */
71 char parsestr[MAXBUFFER+1]; /* for passing quoted string */
72 char xverb; /* flag; 0 = regular verb */
73 char starts_with_verb; /* input line; 0 = no verb word */
74 unsigned int grammaraddr; /* address in grammar */
75 char *obj_parselist = NULL; /* objects with noun/adjective */
76 int domain, odomain; /* of object(s) */
77 int objlist[MAXOBJLIST]; /* for objects of verb */
78 char objcount; /* of objlist */
79 char parse_allflag = false; /* for "all" in MatchObject() */
80 struct pobject_structure
81 pobjlist[MAXPOBJECTS]; /* for possible objects */
82 int pobjcount; /* of pobjlist */
83 int pobj; /* last remaining suspect */
84 int obj_match_state; /* see MatchCommand() for details */
85 char objword_cache[MAXWORDS]; /* for MatchWord() xobject, etc. */
86 char object_is_number; /* number used in player command */
87 unsigned int objgrammar; /* for 2nd pass */
88 int objstart; /* " " " */
89 int objfinish; /* " " " */
90 char addflag; /* true if adding to objlist[] */
91 int speaking; /* if command is addressed to obj. */
92
93 char oops[MAXBUFFER+1]; /* illegal word */
94 int oopscount = 0; /* # of corrections in a row */
95
96 char parse_called_twice;
97 char reparse_everything;
98 char punc_string[64]; /* punctuation string */
99
100 char full_buffer = false;
101 static char recursive_call = false; /* to MatchObject() */
102
103 /* Necessary for proper disambiguation when addressing a character;
104 i.e., when 'held' doesn't refer to held by the player, etc.
105 */
106 int parse_location; /* usually var[location] */
107
108
109 /* ADDALLOBJECTS
110 */
111
112 void AddAllObjects(int loc)
113 {
114 int i;
115
116 if (loc==var[player] && domain!=loc)
117 return;
118
119 /* Try to add everything in the specified domain
120 to objlist[]
121 */
122 for (i=Child(loc); i!=0; i=Sibling(i))
123 {
124 if (i==var[xobject]) continue;
125
126 TryObj(i);
127 if (domain==0)
128 {
129 if (Child(i)) AddAllObjects(i);
130 }
131 }
132 }
133
134
135 /* ADDOBJ
136
137 Adds the object <obj> to objlist[], making all related adjustments.
138 */
139
140 void AddObj(int obj)
141 {
142 int i;
143
144 for (i=0; i<objcount; i++)
145 {
146 if (objlist[i]==obj)
147 return;
148 }
149
150 objlist[(int)objcount] = obj;
151 if (++objcount> MAXOBJLIST) objcount = MAXOBJLIST;
152 }
153
154
155 /* ADDPOSSIBLEOBJECT
156
157 Adds <obj> as a contender to the possible object list, noting that
158 it was referred to as either a noun or an adjective.
159 */
160
161 void AddPossibleObject(int obj, char type, unsigned int w)
162 {
163 int i;
164
165 if (pobjcount==MAXPOBJECTS)
166 return;
167
168 for (i=0; i<pobjcount; i++)
169 {
170 /* If it is already in the list */
171 if (pobjlist[i].obj==obj)
172 {
173 /* Being referred to with a noun outweighs being
174 referred to previously with an adjective
175 */
176 if (type==(char)noun || ObjWordType(obj, w, noun))
177 pobjlist[i].type = (char)noun;
178
179 return;
180 }
181 }
182
183 /* Getting this to point is presuming that we're adding an object
184 referred to with an adjective, but check just to be sure it isn't
185 also a noun for that same object
186 */
187 if (ObjWordType(obj, w, noun)) type = (char)noun;
188
189 pobjlist[pobjcount].obj = obj;
190 pobjlist[pobjcount].type = type;
191
192 pobjcount++;
193 #ifdef DEBUG_PARSER
194 {
195 char buf[100];
196 sprintf(buf, "AddPossibleObject(%d:\"%s\")", obj, Name(obj));
197 Printout(buf);
198 }
199 #endif
200 }
201
202
203 /* ADVANCEGRAMMAR
204
205 Move the address in the grammar table past the current token.
206 */
207
208 void AdvanceGrammar(void)
209 {
210 int a;
211
212 defseg = gameseg;
213
214 switch (a = Peek(grammaraddr))
215 {
216 case FORWARD_SLASH_T:
217 case HELD_T:
218 case MULTI_T:
219 case MULTIHELD_T:
220 case ANYTHING_T:
221 case NUMBER_T:
222 case PARENT_T:
223 case NOTHELD_T:
224 case MULTINOTHELD_T:
225 case WORD_T:
226 case OBJECT_T:
227 case XOBJECT_T:
228 case STRING_T:
229 grammaraddr++;
230 break;
231
232 case ASTERISK_T:
233 case ATTR_T:
234 grammaraddr += 2;
235 break;
236
237 case DICTENTRY_T:
238 case ROUTINE_T:
239 case OBJECTNUM_T:
240 grammaraddr += 3;
241 break;
242
243 case OPEN_BRACKET_T:
244 grammaraddr +=5;
245 break;
246 }
247 }
248
249
250 /* ANYOBJWORD
251
252 For when it's only necessary to know if word[wn] is an object word for
253 any object, not a particular object. Returns 1 for an object word or -1
254 for a non-object word.
255 */
256
257 int AnyObjWord(int wn)
258 {
259 int i;
260
261 if (objword_cache[wn])
262 return objword_cache[wn];
263
264 for (i=0; i<objects; i++)
265 {
266 if (ObjWord(i, wd[wn]))
267 {
268 return (objword_cache[wn] = 1);
269 }
270 }
271
272 return (objword_cache[wn] = -1);
273 }
274
275
276 /* AVAILABLE
277
278 The non_grammar argument is true when called from a non-
279 grammar function such as RunEvents().
280 */
281
282 int Available(int obj, char non_grammar)
283 {
284 int temp_stack_depth;
285
286 if (findobjectaddr)
287 {
288 passlocal[0] = obj;
289
290 /* if anything or (Routine) grammar */
291 if ((Peek(grammaraddr)==ANYTHING_T
292 || (Peek(grammaraddr)==OPEN_BRACKET_T && Peek(grammaraddr+1)==ROUTINE_T))
293 && non_grammar==0)
294 {
295 passlocal[1] = 0;
296 }
297 else
298 {
299 if (domain > 0)
300 passlocal[1] = domain;
301 else if (speaking && non_grammar==0)
302 passlocal[1] = GrandParent(speaking);
303 /* domain of -1 is an explicit 'parent' */
304 /*
305 else if (domain==-1)
306 passlocal[1] = parse_location;
307 */
308 else
309 passlocal[1] = parse_location;
310 }
311
312 ret = 0;
313
314 PassLocals(2);
315 temp_stack_depth = stack_depth;
316
317 SetStackFrame(stack_depth, RUNROUTINE_BLOCK, 0, 0);
318
319 #if defined (DEBUGGER)
320 DebugRunRoutine((long)findobjectaddr*address_scale);
321 #else
322 RunRoutine((long)findobjectaddr*address_scale);
323 #endif
324 retflag = 0;
325 stack_depth = temp_stack_depth;
326 return ret;
327 }
328 else
329 return 1;
330 }
331
332
333 /* CALLLIBRARYPARSE */
334
335 void CallLibraryParse(void)
336 {
337 if (parseaddr)
338 {
339 #ifdef DEBUG_PARSER
340 Printout("CallLibraryParse()");
341 #endif
342 parse_called_twice = false;
343
344 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
345
346 ret = 0;
347 PassLocals(0);
348 #if defined (DEBUGGER)
349 DebugRunRoutine((long)parseaddr*address_scale);
350 #else
351 RunRoutine((long)parseaddr*address_scale);
352 #endif
353 retflag = 0;
354
355 /* Returning non-zero return calls the
356 engine's Parse routine again.
357 */
358 if (ret)
359 {
360 parse_called_twice = true;
361 Parse();
362 }
363 #ifdef DEBUG_PARSER
364 if (ret)
365 Printout("CallLibraryParse() returned true");
366 else
367 Printout("CallLibraryParse() returned false");
368 #endif
369 }
370 }
371
372
373 /* DOMAINOBJ
374
375 Takes into account the preset domain for checking an object's
376 presence; <domain> is 0, -1, or an object number..
377 */
378
379 int DomainObj(int obj)
380 {
381 int yes = false;
382
383 if (obj != var[actor])
384 {
385 switch (domain)
386 {
387 case 0:
388 case -1:
389 {
390 if (Parent(obj)==parse_location)
391 yes = true;
392 else if ((parse_allflag) && GrandParent(obj)==parse_location)
393 yes = true;
394 else
395 {
396 if (Parent(obj)==parse_location && !InList(Parent(obj)))
397 yes = true;
398 }
399
400 if (Peek(grammaraddr)==MULTINOTHELD_T)
401 {
402 if (Parent(obj)==var[actor])
403 yes = false;
404 }
405 break;
406 }
407
408 default:
409 {
410 if (Parent(obj)==domain)
411 yes = true;
412 }
413 }
414 }
415
416 return yes;
417 }
418
419
420 /* FINDWORD
421
422 Returns the dictionary address of <a>.
423 */
424
425 unsigned int FindWord(char *a)
426 {
427 unsigned int ptr = 0;
428 int i, p, alen;
429
430 if (a[0]=='\0')
431 return 0;
432
433 alen = strlen(a);
434
435 defseg = dicttable;
436
437 for (i=1; i<=dictcount; i++)
438 {
439 if (alen==(p = Peek(ptr+2)) && (unsigned char)(MEM(dicttable*16L+ptr+3)-CHAR_TRANSLATION)==(unsigned char)a[0])
440 {
441 if (!strcmp(GetString(ptr + 2), a))
442 {
443 defseg = gameseg;
444 return ptr;
445 }
446 }
447
448 ptr += (p + 1);
449 }
450
451 /* As a last resort, see if the first 6 characters of the word (if it
452 has at least six characters) match a dictionary word:
453 */
454 if (alen >= 6)
455 {
456 unsigned int possible = 0;
457 int posscount = 0;
458
459 ptr = 0;
460
461 for (i=1; i<=dictcount; i++)
462 {
463 if (alen<=(p = Peek(ptr+2)) && (unsigned char)MEM(dicttable*16L+ptr+3)-CHAR_TRANSLATION==a[0])
464 {
465 if (!strncmp(GetString(ptr + 2), a, alen))
466 {
467 /* As long as the dictionary word
468 doesn't contain a space */
469 if (!strrchr(GetString(ptr+2), ' '))
470 {
471 possible = ptr;
472 posscount++;
473 }
474 }
475 }
476 ptr += (p + 1);
477 }
478
479 if (posscount==1)
480 return possible;
481 }
482
483 defseg = gameseg;
484
485 return UNKNOWN_WORD; /* not found */
486 }
487
488
489 /* INLIST
490
491 Checks to see if <obj> is in objlist[].
492 */
493
494 int InList(int obj)
495 {
496 int i;
497
498 for (i=0; i<objcount; i++)
499 {
500 if (objlist[i]==obj)
501 return true;
502 }
503 return false;
504 }
505
506
507 /* KILLWORD
508
509 Deletes word[a].
510 */
511
512 void KillWord(int a)
513 {
514 int i;
515
516 if (a>words)
517 return;
518
519 for (i=a; i<words; i++)
520 word[i] = word[i+1];
521 word[words] = "";
522
523 RemoveWord(a);
524 words--;
525 }
526
527
528 /* MATCHCOMMAND
529
530 Here, briefly, is how MatchCommand() works:
531
532 1. Match the verb.
533
534 2. If no match, check to see if the line begins with an
535 object (character) and try to match it.
536
537 3. If found, try to match a syntax for that verb, including
538 objects, dictionary words, numbers, attributes, and
539 routines. If any objects are specified, skip over them
540 for now, marking the start and finish. This is done
541 mostly in MatchWord().
542
543 4. Match the xobject, if there is one--via MatchObject().
544
545 5. If all is well, return to match the objects that were
546 previously skipped over, loading them into objlist[].
547 Once again, this is done by MatchObject().
548
549 (The reason the objects are initially skipped is because it may
550 be necessary to know where to look for them--this may require
551 knowing what the xobject is, if the syntax is something like:
552
553 "get" <object> "from" <xobject>)
554
555 The variable <obj_match_state> is the indicator of what stage
556 object-matching is at:
557
558 obj_match_state = 0 - haven't matched anything yet
559
560 obj_match_state = 1 - xobject has been matched
561
562 obj_match_state = 2 - matching object(s), loading
563 objlist[]
564
565 obj_match_state = 5 - matching first word/name, i.e.,
566 "Bob, <do something>"
567 */
568
569 int MatchCommand()
570 {
571 int i, j, flag, a, mw = 0, gotspeaker = 0;
572 int wordnum;
573 int numverbs = 0, nextverb = 0;
574 unsigned int ptr, verbptr, nextgrammar;
575 unsigned int obj, propaddr;
576
577 #ifdef DEBUG_PARSER
578 Printout("Entering MatchCommand()");
579 #endif
580
581 odomain = 0;
582
583 /* Reset these for command-matching */
584 if (!speaking)
585 {
586 var[actor] = var[player];
587 parse_location = var[location];
588 }
589 else
590 {
591 var[actor] = speaking;
592 parse_location = GrandParent(speaking);
593 }
594
595 if (!strcmp(word[1], "~oops"))
596 {
597 strcpy(parseerr, "");
598
599 /* "oops" on its own */
600 if (words==1 || !strcmp(oops, ""))
601 {
602 ParseError(16, 0); /* "You'll have to make a mistake..." */
603 return 0;
604 }
605
606 /* trying to correct more than one word */
607 if (words > 2 || oopscount)
608 {
609 ParseError(17, 0); /* "...one word at a time..." */
610 return 0;
611 }
612
613 /* trying to correct a correction */
614 if (!strcmp(Left(errbuf, 5), "~oops"))
615 {
616 ParseError(13, 0);
617 return 0;
618 }
619
620 /* Rebuild the corrected buffer */
621 oopscount = 1;
622 strcpy(line, word[2]);
623 for (i=1; i<=(int)strlen(errbuf); i++)
624 {
625 if (!strcmp(Mid(errbuf, i, strlen(oops)), oops))
626 break;
627 }
628
629 strcpy(buffer, errbuf);
630 buffer[i-1] = '\0';
631 strcat(buffer, line);
632
633 strcat(buffer, Right(errbuf, strlen(errbuf) - i - strlen(oops) + 1));
634
635 SeparateWords();
636 if (!Parse())
637 return 0;
638
639 CallLibraryParse();
640 }
641
642 if (word[1][0]=='.') KillWord(1);
643
644
645 /*
646 * STEP 1: Match verb
647 *
648 */
649
650 ptr = 64;
651
652 MatchVerb:
653
654 #ifdef DEBUG_PARSER
655 Printout("MatchCommand(): Step 1");
656 #endif
657 if (words==0)
658 return 0;
659
660 defseg = gameseg;
661
662 grammaraddr = 0;
663 domain = 0;
664 obj_match_state = 0;
665 xverb = 0;
666 starts_with_verb = 0;
667 objcount = 0;
668 parse_allflag = false;
669 objstart = 0;
670 object_is_number = false;
671
672 for (i=1; i<MAXWORDS; i++)
673 objword_cache[i] = 0;
674
675 var[object] = 0;
676 var[xobject] = 0;
677 var[self] = 0;
678 var[verbroutine] = 0;
679
680 while ((a = Peek(ptr)) != 255)
681 {
682 defseg = gameseg;
683
684 /* verb or xverb header */
685 if (a==VERB_T || a==XVERB_T)
686 {
687 /* Skim through 1 or more verb words */
688 numverbs = Peek(ptr + 1);
689 verbptr = ptr + 2;
690 for (i=1; i<=numverbs; i++)
691 {
692 /* 0xffff signals something other than a
693 dictionary word--see BuildVerb() in hcbuild.c.
694 This will be the case, like, 1% of the time.
695 */
696
697 /* If it is a dictionary word... */
698 if (PeekWord(verbptr)!=0xffff)
699 {
700 /* If one of the verb words matches the first
701 word in the input line
702 */
703 if (wd[1]==PeekWord(verbptr))
704 {
705 grammaraddr = ptr;
706 goto GotVerb;
707 }
708
709 verbptr += 2;
710 }
711
712 /* ...otherwise assume it's an object (value) */
713 else
714 {
715 codeptr = verbptr + 1; /* skip 0xffff */
716
717 /* GetVal(), not GetValue(), since it's
718 always a simple value
719 */
720 obj = GetVal();
721
722 /* codeptr can't be >65535 on a 16-bit
723 compiler
724 */
725 verbptr = (unsigned int)codeptr;
726 propaddr = PropAddr(obj, noun, 0);
727 if (propaddr)
728 {
729 defseg = proptable;
730 a = Peek(propaddr+1); /* obj.#prop */
731 defseg = gameseg;
732
733 for (j=1; j<=a; j++)
734 {
735 if (wd[1]==(unsigned)GetProp(obj, noun, j, 0))
736 {
737 grammaraddr = ptr;
738 goto GotVerb;
739 }
740 }
741 }
742 }
743 }
744
745 /* Otherwise skip over this verb header */
746 ptr += 2 + numverbs * 2;
747 }
748
749 /* anything else */
750 else
751 ptr += Peek(ptr + 1) + 1;
752 }
753
754
755 /*
756 * STEP 2: Match object/character (if no verb match)
757 *
758 */
759 #ifdef DEBUG_PARSER
760 Printout("MatchCommand(): Step 2");
761 #endif
762 /* If we hit the end of the grammar without finding a verb match: */
763 if (Peek(ptr)==255)
764 {
765 /* If we already tried this once */
766 if (gotspeaker)
767 {
768 if (!starts_with_verb)
769 /* "Better start with a verb..." */
770 ParseError(2, 0);
771 else
772 /* "That doesn't make any sense..." */
773 ParseError(6, 0);
774 return 0;
775 }
776
777 /* See if the command begins with an object
778 (character) name:
779 */
780 flag = 0;
781 if (AnyObjWord(1)==1)
782 flag = 1;
783
784 /* No match, ergo an invalid command */
785 if (flag==0 && nextverb==true)
786 {
787 strcpy(parseerr, "");
788 ParseError(6, 0); /* "...doesn't make any sense..." */
789 return 0;
790 }
791
792 /* No provision made for addressing objects (characters) */
793 if (flag==0 || speaktoaddr==0)
794 {
795 strcpy(parseerr, "");
796 ParseError(2, 0); /* "Better start with a verb..." */
797 return 0;
798 }
799
800 /* Count how many object words there are */
801 for (i=2; i<=words; i++)
802 {
803 if (AnyObjWord(i)!=1)
804 break;
805 }
806
807 /* Try to match the first word to a valid object */
808 objfinish = i - 1;
809 obj_match_state = 5;
810 i = 1;
811 recursive_call = 0;
812
813 if (MatchObject(&i) != true)
814 return 0; /* unsuccessful */
815
816 speaking = pobj; /* successful */
817 gotspeaker = true;
818
819 /* So erase the object name from the start of the line */
820 for (i=1; i<=objfinish; i++)
821 KillWord(1);
822 if (word[1][0]=='~') KillWord(1);
823
824 /* If it's a name and that's all...*/
825 if (words==0)
826 return true;
827
828 /* ...or else proceed as usual */
829 ptr = 64;
830 goto MatchVerb;
831 }
832 else if (!gotspeaker)
833 speaking = 0;
834
835 GotVerb:
836
837 if (!speaking)
838 {
839 var[actor] = var[player];
840 parse_location = var[location];
841 }
842 else
843 {
844 var[actor] = speaking;
845 parse_location = GrandParent(speaking);
846 }
847
848 obj_match_state = 0;
849 starts_with_verb = 1;
850 strcpy(parseerr, word[1]);
851
852 if (Peek(grammaraddr)==XVERB_T) xverb = true;
853 else
854 /* clear xverb if we have specifically declared something else */
855 xverb = false;
856 grammaraddr += 2 + numverbs * 2;
857
858 /*
859 * STEP 3: Match proper grammar structure (syntax)
860 *
861 */
862 #ifdef DEBUG_PARSER
863 Printout("MatchCommand(): Step 3");
864 #endif
865
866 /*
867 * (STEP 4: We'll be matching xobject, if any, before object)
868 *
869 */
870 #ifdef DEBUG_PARSER
871 Printout("MatchCommand(): Step 4");
872 #endif
873
874 /* Loop until end of grammar table, or next verb:
875 */
876 while (Peek(grammaraddr)!=255 &&
877 Peek(grammaraddr)!=VERB_T && Peek(grammaraddr)!=XVERB_T)
878 {
879 wordnum = 1;
880
881 nextgrammar = grammaraddr + Peek(grammaraddr + 1) + 1;
882
883 /* Loop until end of table or next verb: */
884 while (Peek(grammaraddr) != 255 && Peek(grammaraddr) != VERB_T && Peek(grammaraddr) != XVERB_T)
885 {
886 mw = MatchWord(&wordnum);
887
888 if (mw==true)
889 {
890 /* end of both input and grammar */
891 if (wd[wordnum]==0 && Peek(grammaraddr)==ROUTINE_T)
892 {
893 full_buffer = (char)wordnum;
894 break;
895 }
896
897 /* end of grammar, not input */
898 if (Peek(grammaraddr)==ROUTINE_T)
899 {
900 mw = false;
901 goto NextStructure;
902 }
903 }
904 else
905 {
906 /* If error already signalled */
907 if (mw==2)
908 return 0;
909
910 /* No match, so try next structure */
911 else
912 {
913 NextStructure:
914 grammaraddr = nextgrammar;
915 var[object] = 0;
916 var[xobject] = 0;
917 var[verbroutine] = 0;
918 domain = 0;
919 odomain = 0;
920 obj_match_state = 0;
921 /* we do not want xverb cleared every line or else multi-line
922 xverb definitions only properly work for the first line
923 xverb = 0; */
924 objcount = 0;
925 objstart = 0;
926 break;
927 }
928 }
929 }
930
931 /* Matched the complete syntax of a verb */
932 if (mw==true)
933 {
934 var[verbroutine] = PeekWord(grammaraddr + 1);
935 break;
936 }
937 }
938
939 if (mw != true)
940 {
941 if (mw==false) /* mw = 2 if error already printed */
942 {
943 /* If there's more grammar to check... */
944 if (Peek(grammaraddr) != 255)
945 {
946 ptr = grammaraddr;
947 nextverb = true;
948 goto MatchVerb;
949 }
950
951 /* ...or if we reached the end without a sensible
952 syntax matched:
953 */
954 strcpy(parseerr, "");
955
956 /* "...doesn't make any sense..." */
957 ParseError(6, 0);
958 }
959 return 0;
960 }
961
962
963 /*
964 * STEP 5: Match remaining object(s), if any
965 *
966 */
967 #ifdef DEBUG_PARSER
968 Printout("MatchCommand(): Step 5");
969 #endif
970
971 /* If there are objects waiting to be loaded into objlist[] */
972 if (objstart)
973 {
974 ResetFindObject();
975
976 obj_match_state = 2;
977 recursive_call = false;
978 if (odomain) domain = odomain;
979
980 grammaraddr = objgrammar;
981 i = objstart;
982
983 /* If there was a problem matching them */
984 if (MatchObject(&i)==false)
985 return 0;
986 }
987
988 /* Got a successfully matched verb with all the requisite
989 parameters (if any).
990 */
991
992 if (words < wordnum)
993 remaining = 0;
994 else
995 remaining = (char)(words - wordnum);
996
997 #ifdef DEBUG_PARSER
998 Printout("MatchCommand(): Leaving");
999 #endif
1000 return true;
1001 }
1002
1003
1004 /* MATCHOBJECT
1005
1006 The argument is the word number we're starting matching on.
1007
1008 NOTE: recusive_call is set to 0 if this is the first call.
1009 MatchObject() sets it to 1 when calling itself when asking
1010 for clarification as to which object is meant.
1011
1012 Return -1 on a recursive call to allow parsing to continue.
1013 */
1014
1015 #define MAX_MOBJ 16 /* maximum number of matchable object words */
1016
1017 int MatchObject(int *wordnum)
1018 {
1019 char found_noun = false;
1020 int i, j, k, m, flag;
1021 int mobjs; unsigned int mobj[MAX_MOBJ];
1022 int allmatch, roomloc;
1023 int bestobj = 0;
1024 char bestavail = 0;
1025
1026 /* If this is a recursive call, we're not adding new objects */
1027 if (!recursive_call) addflag = true;
1028
1029 stack_depth = 0;
1030
1031 pobj = 0; /* possible object */
1032 objcount = 0; /* of objlist[] */
1033 pobjcount = 0; /* of pobjlist[] */
1034 mobjs = 0; /* # of previous words in phrase */
1035 bestavail = 0; /* adjective or noun */
1036
1037 #ifdef DEBUG_PARSER
1038 Printout("MatchObject(): Entering");
1039 #endif
1040 strcpy(parseerr, "");
1041
1042 do /* starting at word #a */
1043 {
1044 /* Check first to make sure it's not a housekeeping word
1045 such as "~and" or "~all".
1046 */
1047 if (word[*wordnum][0]!='~' && word[*wordnum][0]!='\0')
1048 {
1049 if (parseerr[0]!='\0') strcat(parseerr, " ");
1050 strcat(parseerr, word[*wordnum]);
1051
1052 flag = 0;
1053 for (i=0; i<objects; i++)
1054 {
1055 if (wd[*wordnum]==0)
1056 break;
1057
1058 /* Might be this object if wd[*wordnum] is an
1059 adjective or noun of object i
1060 */
1061 m = ObjWord(i, wd[*wordnum]);
1062
1063 if (m)
1064 {
1065 flag = true;
1066 allmatch = true;
1067
1068 /* check previously matched words */
1069 for (j=1; j<=mobjs; j++)
1070 {
1071 if (!ObjWord(i, mobj[j])) /* || wd[*wordnum]==mobj[j]) */
1072 allmatch = false;
1073 }
1074
1075 /* matches all previous words */
1076 if (allmatch==true)
1077 {
1078 AddPossibleObject(i, (char)m, wd[*wordnum]);
1079 pobj = i;
1080 if (bestavail==0 || bestavail>=(char)m)
1081 {
1082 if (!bestobj)
1083 bestobj = i;
1084 bestavail = (char)m;
1085 }
1086 }
1087
1088 /* doesn't match previous words */
1089 else
1090 SubtractPossibleObject(i);
1091 }
1092
1093 /* definitely not this object */
1094 else
1095 {
1096 SubtractPossibleObject(i);
1097 /* clear bestobj if i is the current best object */
1098 if (i == bestobj)
1099 bestobj = 0;
1100 }
1101 }
1102
1103
1104 /* If checking the start of an input line, i.e. for
1105 a command addressed to an object (character):
1106 */
1107 if (obj_match_state==5 && !flag) goto Clarify;
1108 }
1109
1110 else if (!strcmp(word[*wordnum], "~any"))
1111 goto NextLoop;
1112
1113 /* "~and", "~all",... */
1114 else
1115 goto Clarify;
1116
1117 /* Didn't get any suspects */
1118 if (pobjcount==0)
1119 {
1120 /* If "~and", "~all",... */
1121 if (word[*wordnum][0]=='~')
1122 {
1123 /* If checking the xobject */
1124 if (obj_match_state==1)
1125 {
1126 strcpy(parseerr, word[1]);
1127 /* "...can't use multiple objects..."
1128 (as indirect objects) */
1129 ParseError(7, 0);
1130 return 0;
1131 }
1132 goto Clarify;
1133 }
1134
1135 /* Got an unmatchable sequence of words */
1136 else
1137 {
1138 if (obj_match_state==5)
1139 {
1140 if (!starts_with_verb)
1141 /* "Better start with a verb..." */
1142 ParseError(2, 0);
1143 else
1144 /* "That doesn't make any sense..." */
1145 ParseError(6, 0);
1146 }
1147 else
1148 /* "(no such thing)..." */
1149 ParseError(5, 0);
1150 return 0;
1151 }
1152 }
1153
1154 if (word[*wordnum][0]!='~')
1155 {
1156 /* Go back for next word in this object phrase */
1157
1158 mobjs++;
1159 if (mobjs==MAX_MOBJ)
1160 {
1161 /* "(no such thing)..." */
1162 ParseError(5, 0);
1163 return 0;
1164 }
1165 mobj[mobjs] = wd[*wordnum];
1166 }
1167 else
1168 {
1169 /* Since hitting "~and" or "~all", we've obviously
1170 finished an object phrase
1171 */
1172 (*wordnum)++;
1173 goto Clarify;
1174 }
1175
1176 NextLoop:
1177 (*wordnum)++; /* next word */
1178 if ((*wordnum > words || word[*wordnum][0]=='\0')
1179 || *wordnum > objfinish)
1180 goto Clarify;
1181 }
1182 while (true); /* endless loop */
1183
1184
1185 Clarify:
1186
1187 #ifdef DEBUG_PARSER
1188 Printout("MatchObject(): Clarify");
1189 #endif
1190 /* If "~and", "~all",... */
1191 if (word[*wordnum][0]=='~')
1192 {
1193 /* If checking the xobject or addressing a command */
1194 if (obj_match_state==1 || speaking)
1195 {
1196 strcpy(parseerr, word[1]);
1197 /* "...can't use multiple objects..."
1198 (as indirect objects) */
1199 ParseError(7, 0);
1200 return 0;
1201 }
1202 }
1203
1204 if (!strcmp(word[*wordnum], "~all")) /* if "~all" is specified */
1205 {
1206 parse_allflag = true;
1207
1208 /* If one or more words were already matched, however... */
1209
1210 if (mobjs > 0)
1211 {
1212 ParseError(6, 0); /* "...doesn't make any sense..." */
1213 return 0;
1214 }
1215
1216 if (!domain) /* no particular domain object specified */
1217 roomloc = parse_location;
1218 else
1219 roomloc = domain;
1220
1221 AddAllObjects(roomloc);
1222
1223 (*wordnum)++;
1224
1225
1226 /* Done processing the object phrase yet? */
1227
1228 /* only >GET ALL EXCEPT... if we're not done the object phrase */
1229 if (*wordnum<=objfinish && strcmp(word[*wordnum], "~except"))
1230 {
1231 ParseError(6, 0); /* "Doesn't make any sense..." */
1232 return 0;
1233 }
1234
1235 if ((*wordnum > words || word[*wordnum][0]=='\0')
1236 || (obj_match_state != 1 && *wordnum >= objfinish))
1237 {
1238 if (!objcount && !speaking)
1239 {
1240 strcpy(parseerr, word[1]);
1241 ParseError(9, 0); /* "Nothing to (verb)..." */
1242 return 0;
1243 }
1244 return true;
1245 }
1246
1247 /* Go back for the next piece of the phrase */
1248 pobjcount = 0;
1249 (*wordnum)--;
1250 goto NextLoop;
1251 }
1252
1253
1254 /* If we have a possible object or set of objects, go through the
1255 disqualification process, either to sort out any confusion, or
1256 even if there's only one possible object, to make sure it's
1257 available
1258 */
1259 if (pobjcount >= 1)
1260 {
1261 bestavail = 0;
1262
1263 for (k=0; k<pobjcount; k++) /* disqualify if unavailable */
1264 {
1265 i = pobjlist[k].obj;
1266
1267 /* held or multiheld */
1268 if ((domain) && domain==var[actor])
1269 {
1270 if (Parent(i) != var[actor])
1271 {
1272 SubtractPossibleObject(i);
1273 k--;
1274 }
1275 else
1276 pobj = i;
1277 }
1278
1279 else if ((Peek(grammaraddr)==NOTHELD_T || Peek(grammaraddr)==MULTINOTHELD_T)
1280 && Parent(i)==var[actor])
1281 {
1282 SubtractPossibleObject(i);
1283 k--;
1284
1285 /* if this was the last suspect */
1286 if (pobjcount<1) /* i.e., 0 */
1287 {
1288 ParseError(11, i); /* "You don't see that..." */
1289 return 0;
1290 }
1291 }
1292
1293 /* otherwise */
1294 else if (Available(i, 0)==false) /* and obj_match_state!=5) */
1295 {
1296 SubtractPossibleObject(i);
1297 k--;
1298 }
1299 else
1300 pobj = i;
1301
1302 /* Try to determine the best available object */
1303 if ((!bestavail) || (bestavail==(char)adjective && pobjlist[k].type==(char)noun))
1304 {
1305 m = domain;
1306 /* Temporary parent domain */
1307 domain = -1;
1308 if (Available(i, 0))
1309 {
1310 bestavail = pobjlist[k].type;
1311 bestobj = i;
1312 }
1313 domain = m;
1314 }
1315
1316 /* Pick a default (poor) best match */
1317 if (!bestobj)
1318 bestobj = i;
1319 }
1320
1321
1322 /* Disqualify if an object is less exact--i.e., if the
1323 word is a noun for one object but only an adjective for
1324 another, disqualify the one for which it is an
1325 adjective.
1326 */
1327 if (pobjcount > 1)
1328 {
1329 for (i=0; i<pobjcount; i++)
1330 {
1331 if (pobjlist[i].type==(char)noun)
1332 found_noun = true;
1333 }
1334
1335 if (found_noun)
1336 {
1337 for (i=0; i<pobjcount; i++)
1338 {
1339 /* Use ObjWord()'s test to see if this word
1340 should get upgraded from an adjective to a noun
1341 */
1342 if (pobjlist[i].type==(char)adjective)
1343 {
1344 if (ObjWordType(pobjlist[i].obj, wd[*wordnum-1], noun))
1345 pobjlist[i].type = (char)noun;
1346 }
1347
1348 if (pobjlist[i].type==(char)adjective)
1349 {
1350 SubtractPossibleObject(pobjlist[i].obj);
1351 i--;
1352 }
1353
1354 if (pobjcount==1) break;
1355 }
1356 }
1357 }
1358
1359
1360 /* If we're dealing with an 'anything' object, prefer an
1361 object that's currently available to one that isn't
1362 */
1363 if (pobjcount > 1 && Peek(grammaraddr)==ANYTHING_T)
1364 {
1365 for (i=0; i<pobjcount; i++)
1366 {
1367 if (Available(pobjlist[i].obj, true))
1368 {
1369 for (j=0; j<pobjcount; j++)
1370 {
1371 if (j!=i)
1372 {
1373 if (!Available(pobjlist[j].obj, true))
1374 {
1375 SubtractPossibleObject(pobjlist[j].obj);
1376 j--; /* don't skip one */
1377 if (pobjcount<=1) break;
1378 }
1379 }
1380 }
1381 }
1382 if (pobjcount<=1) break;
1383 }
1384 }
1385
1386
1387 /* Use whatever disambiguation the FindObject routine might provide */
1388 if (pobjcount > 1 && findobjectaddr)
1389 {
1390 for (k=0; k<pobjcount; k++)
1391 {
1392 i = pobjlist[k].obj;
1393 ret = 0;
1394 passlocal[0] = i;
1395 passlocal[1] = 0;
1396 PassLocals(2);
1397
1398 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
1399 #if defined (DEBUGGER)
1400 DebugRunRoutine((long)findobjectaddr*address_scale);
1401 #else
1402 RunRoutine((long)findobjectaddr*address_scale);
1403 #endif
1404 retflag = 0;
1405
1406 if (ret==0) /* returned false */
1407 {
1408 SubtractPossibleObject(i);
1409 k--; /* so we don't skip one */
1410 if (pobjcount<=1) break;
1411 }
1412 else
1413 {
1414 bestobj = i;
1415 pobj = i;
1416 }
1417
1418 if (pobjcount==1) break;
1419 }
1420 }
1421
1422
1423 /* If this is a clarification-required call to MatchObject(),
1424 return -1 if more than one possible object is found.
1425 */
1426 if (pobjcount > 1 && recursive_call)
1427 return -1;
1428
1429
1430 /* On the other hand, if we've managed to disqualify
1431 everything:
1432 */
1433 if (pobjcount==0 && !speaking)
1434 {
1435 if (obj_match_state==5)
1436 {
1437 if (!starts_with_verb)
1438 /* "Better start with a verb..." */
1439 ParseError(2, 0);
1440 else
1441 /* "That doesn't make any sense..." */
1442 ParseError(6, 0);
1443 }
1444 else
1445 {
1446 if ((!domain) || domain != var[actor])
1447 {
1448 if (Peek(grammaraddr)==ANYTHING_T)
1449 /* "...haven't seen..." */
1450 ParseError(10, bestobj);
1451 else if (domain)
1452 /* "...don't see that there..." */
1453 ParseError(14, bestobj);
1454 else
1455 /* "...don't see any..." */
1456 ParseError(11, bestobj);
1457 }
1458 else
1459 ParseError(15, bestobj); /* "...don't have any..." */
1460 }
1461 return 0;
1462 }
1463 else if (pobjcount==0)
1464 pobj = bestobj;
1465
1466
1467 /* If, after all this, there still exists some confusion
1468 about what object is meant:
1469 */
1470 if (pobjcount > 1)
1471 {
1472 int wtemp;
1473 unsigned int wdtemp[MAXWORDS+1];
1474 int tempobjlist[MAXWORDS+1], tempobjcount;
1475 struct pobject_structure temppobjlist[MAXPOBJECTS];
1476 int tempobjfinish;
1477 int temppobjcount;
1478 int tempaddflag;
1479
1480 char tempxverb = xverb;
1481 ParseError(8, 0); /* "Which...do you mean..." */
1482 xverb = tempxverb;
1483
1484 for (i=1; i<=words; i++) /* save word arrays */
1485 wdtemp[i] = wd[i];
1486 wtemp = words;
1487 tempobjfinish = objfinish;
1488
1489 for (i=0; i<=objcount; i++) /* save object arrays */
1490 tempobjlist[i] = objlist[i];
1491 for (i=0; i<=pobjcount; i++)
1492 temppobjlist[i] = pobjlist[i];
1493
1494 tempobjcount = objcount;
1495 temppobjcount = pobjcount;
1496 tempaddflag = addflag;
1497
1498
1499 /* Get a new input and properly parse it; after
1500 all, it may be returned to MatchCommand() as
1501 a potentially valid command.
1502 */
1503 GetCommand();
1504 SeparateWords();
1505 if (words==0)
1506 {
1507 ParseError(0, 1);
1508 return 0;
1509 }
1510 if (!Parse())
1511 return 0;
1512
1513 objfinish = words;
1514
1515 /* Do we not care? i.e. is "any", "either", etc. given? */
1516 if (!strcmp(word[1], "~any") && words==1)
1517 {
1518 for (k=0; k<pobjcount; k++)
1519 {
1520 i = pobjlist[k].obj;
1521
1522 if (strcmp(Name(i), ""))
1523 {
1524 pobj = i;
1525 sprintf(line, "(%s)", Name(i));
1526 AP(line);
1527 goto RestoreTempArrays;
1528 }
1529 }
1530 }
1531
1532 /* Check to see if any object is disqualified by
1533 any one of the words.
1534 */
1535 for (i=1; i<=words; i++)
1536 {
1537 if (word[i][0]!='~')
1538 {
1539 for (j=0; j<pobjcount; j++)
1540 {
1541 if (!ObjWord(pobjlist[j].obj, wd[i]))
1542 {
1543 SubtractPossibleObject(pobjlist[j].obj);
1544 j--;
1545 }
1546 }
1547 }
1548 }
1549 if (pobjcount==1)
1550 {
1551 pobj = pobjlist[0].obj;
1552 full_buffer = false;
1553 goto RestoreTempArrays;
1554 }
1555
1556 /* Check to see if the first word is a noun
1557 or adjective for any of the possible suspect objects.
1558 */
1559 flag = 0;
1560 for (i=0; i<pobjcount; i++)
1561 {
1562 if (ObjWord(pobjlist[i].obj, wd[1]))
1563 {
1564 flag = 1;
1565 break;
1566 }
1567 }
1568 if (!strcmp(word[1], "~all"))
1569 flag = 1;
1570
1571 /* If not, tell MatchCommand() that there's a
1572 new command coming down the hopper.
1573 */
1574 if (flag==0)
1575 {
1576 full_buffer = true;
1577 return 0;
1578 }
1579
1580 /* Here, tell MatchObject()--this function--
1581 that this isn't a virgin call.
1582 */
1583 recursive_call = true;
1584 addflag = true;
1585 i = 1;
1586 j = MatchObject(&i);
1587 if (j==false) /* parsing error */
1588 {
1589 full_buffer = false;
1590 return 0;
1591 }
1592 else if (j==-1) /* multiple matches found */
1593 {
1594 /* See if a single entered word matches
1595 exactly the name of only one object
1596 */
1597 if (words==1)
1598 {
1599 flag = 0;
1600 for (j=0; j<temppobjcount; j++)
1601 {
1602 if (wd[1]==(unsigned int)GetProp(temppobjlist[j].obj, 0, 1, 0))
1603 {
1604 pobj = temppobjlist[j].obj;
1605 flag++;
1606 }
1607 }
1608 if (flag==1)
1609 {
1610 full_buffer = false;
1611 goto RestoreTempArrays;
1612 }
1613 }
1614
1615 /* Now, even if we weren't able to find a
1616 match, check to see if the last word
1617 belongs to only one of the possible
1618 suspects, especially if it's a noun.
1619 */
1620 flag = 0;
1621 bestobj = 0;
1622 i = words;
1623 while (wd[i]==0) i--;
1624 for (j=0; j<temppobjcount; j++)
1625 {
1626 if ((m = ObjWord(temppobjlist[j].obj, wd[i])) != 0)
1627 {
1628 if (!bestobj || m<=bestobj)
1629 {
1630 flag++;
1631 bestobj = m;
1632 pobj = temppobjlist[j].obj;
1633 }
1634 }
1635 }
1636 if (flag != 1)
1637 {
1638 /* "You'll have to be more specific..." */
1639 ParseError(13, 0);
1640 full_buffer = false;
1641 return 0;
1642 }
1643 full_buffer = false;
1644 }
1645
1646 RestoreTempArrays:
1647 /* Rebuild <buffer> and word[] array */
1648 strcpy(buffer, "");
1649 for (i=1; i<=wtemp; i++)
1650 {
1651 strcat(buffer, GetWord(wdtemp[i]));
1652 strcat(buffer, " ");
1653 }
1654 SeparateWords();
1655
1656 /* Restore wd[] array after SeparateWords() */
1657 for (i=1; i<=wtemp; i++)
1658 wd[i] = wdtemp[i];
1659 words = wtemp;
1660 objfinish = tempobjfinish;
1661
1662 /* Restore object lists */
1663 for (i=0; i<temppobjcount; i++)
1664 pobjlist[i] = temppobjlist[i];
1665 pobjcount = temppobjcount;
1666
1667 /* Multiple disambig. results for a non-multi verb? */
1668 i = Peek(grammaraddr);
1669 if (objcount>1 && i!=MULTI_T && i!=MULTIHELD_T && i!=MULTINOTHELD_T)
1670 {
1671 strcpy(parseerr, word[1]);
1672 /* "You can't...multiple objects." */
1673 ParseError(3, 0);
1674 return 0;
1675 }
1676
1677 /* Check objlist against original pobjlist */
1678 for (i=0; i<objcount; i++)
1679 {
1680 flag = 0;
1681 for (j=0; j<pobjcount; j++)
1682 {
1683 if (pobjlist[j].obj==objlist[i])
1684 flag = true;
1685 }
1686 if (!flag)
1687 {
1688 /* "You'll have to be more specific..." */
1689 ParseError(13, 0);
1690 full_buffer = false;
1691 return 0;
1692 }
1693 }
1694
1695 /* Check pobjlist against disambiguation objlist */
1696 for (i=0; i<pobjcount; i++)
1697 {
1698 flag = 0;
1699 for (j=0; j<objcount; j++)
1700 {
1701 if (pobjlist[i].obj==objlist[j])
1702 flag = true;
1703 }
1704 if (!flag)
1705 {
1706 SubtractPossibleObject(pobjlist[i].obj);
1707 i--;
1708 }
1709 }
1710
1711 for (i=0; i<=tempobjcount; i++)
1712 objlist[i] = tempobjlist[i];
1713 objcount = (char)tempobjcount;
1714 addflag = (char)tempaddflag;
1715
1716 if (word[*wordnum][0]=='~') (*wordnum)--;
1717 }
1718 }
1719
1720 /* Rule out "noun noun" and "noun adjective" combinations */
1721
1722 i = 0; /* count nouns */
1723 k = 10; /* best (i.e., lowest) match */
1724 for (j=1; j<=mobjs; j++)
1725 {
1726 if ((m = ObjWord(pobj, mobj[j]))==noun)
1727 {
1728 k = noun;
1729 i++;
1730 }
1731 else if (k==noun) i = 2;
1732 }
1733
1734 /* Can't use more than one noun for a given object */
1735 if (i > 1)
1736 {
1737 /* "...no such thing" */
1738 ParseError(5, 0);
1739 return 0;
1740 }
1741
1742 /* Check finally to make sure it's valid */
1743 ResetFindObject();
1744 if (!ValidObj(pobj))
1745 return false;
1746
1747 /* Finally--now add it or subtract it from the list, as
1748 appropriate:
1749 */
1750 if (addflag==true)
1751 {
1752 if (pobjcount)
1753 {
1754 for (i=0; i<pobjcount; i++)
1755 AddObj(pobjlist[i].obj);
1756 }
1757
1758 /* backup */
1759 else if (pobj != 0)
1760 AddObj(pobj);
1761 }
1762 else
1763 SubtractObj(pobj);
1764
1765 /* If the object wasn't where it was specifically claimed to be,
1766 applies to second pass through object phrase(s) after xobject
1767 is found (since <obj_match_state> is 2):
1768 */
1769 if (obj_match_state==2 && domain != 0 &&
1770 Parent(pobj) != domain && pobj != 0 && !speaking)
1771 {
1772 if (domain==var[player])
1773 ParseError(15, pobj); /* "You don't have any..." */
1774 else
1775 ParseError(14, pobj); /* "You don't see any...there..." */
1776 return 0;
1777 }
1778
1779 if (!strcmp(word[*wordnum], "~except"))
1780 {
1781 if (obj_match_state==1)
1782 {
1783 ParseError(7, 0); /* can't have multiple xobjects */
1784 return 0;
1785 }
1786 addflag = false;
1787 }
1788
1789 if ((strcmp(word[*wordnum], "~and") && strcmp(word[*wordnum], "~except"))
1790 || obj_match_state==5)
1791 {
1792 /* At the end yet? */
1793 if ((*wordnum > words || word[*wordnum][0]=='\0')
1794 || *wordnum > objfinish)
1795 {
1796 if ((objcount > 0 && pobj != 0) || recursive_call || speaking)
1797 return true;
1798 else
1799 {
1800 /* No objects found */
1801 strcpy(parseerr, word[1]);
1802 ParseError(9, 0); /* "Nothing to (verb)..." */
1803 return 0;
1804 }
1805 }
1806 }
1807
1808 /* Go back for the next object phrase */
1809 pobjcount = 0;
1810 mobjs = 0;
1811 strcpy(parseerr, "");
1812
1813 goto NextLoop;
1814 }
1815
1816
1817 /* MATCHWORD */
1818
1819 int MatchWord(int *wordnum)
1820 {
1821 char num[18];
1822 int i, p, t, flag, finish;
1823 unsigned int thissyntax, nextsyntax;
1824
1825 if (wd[*wordnum]==0)
1826 return 0;
1827
1828 switch ((t = Peek(grammaraddr)))
1829 {
1830 /* the verb ("*") */
1831 case ASTERISK_T:
1832 (*wordnum)++;
1833 AdvanceGrammar();
1834 return true;
1835
1836 /* a non-specific dictionary word */
1837 case WORD_T:
1838 if (obj_match_state==1)
1839 var[xobject] = wd[*wordnum];
1840 else
1841 {
1842 var[object] = wd[*wordnum];
1843 obj_match_state = 1;
1844 }
1845 object_is_number = true;
1846 (*wordnum)++;
1847 AdvanceGrammar();
1848 return true;
1849
1850 /* a specific dictionary entry */
1851 case DICTENTRY_T:
1852 CheckWord:
1853 /* matches word */
1854 if (wd[*wordnum]==PeekWord(grammaraddr + 1))
1855 {
1856 (*wordnum)++;
1857 AdvanceGrammar();
1858 while (Peek(grammaraddr)==9)
1859 grammaraddr += 4;
1860 return true;
1861 }
1862 else
1863 {
1864 /* if next word is a "/" */
1865 if (Peek(grammaraddr + 3)==FORWARD_SLASH_T)
1866 {
1867 AdvanceGrammar(); /* this word */
1868 AdvanceGrammar(); /* "/" */
1869 goto CheckWord;
1870 }
1871
1872 return 0;
1873 }
1874
1875 /* alternative dictionary words */
1876 case FORWARD_SLASH_T:
1877 grammaraddr++;
1878 return true;
1879
1880 /* a number */
1881 case NUMBER_T:
1882 if ((STARTS_AS_NUMBER(word[*wordnum])) &&
1883 !strcmp(itoa(atoi(word[*wordnum]), num, 10), word[*wordnum]))
1884 {
1885 if (obj_match_state==1)
1886 var[xobject] = atoi(word[*wordnum]);
1887 else
1888 {
1889 var[object] = atoi(word[*wordnum]);
1890 obj_match_state = 1;
1891 }
1892 object_is_number = true;
1893 AdvanceGrammar();
1894 (*wordnum)++;
1895 return true;
1896 }
1897 break;
1898
1899 /* a string enclosed in quotes */
1900 case STRING_T:
1901 if (parsestr[0]=='\"')
1902 {
1903 AdvanceGrammar();
1904 (*wordnum)++;
1905 return true;
1906 }
1907 else
1908 return 0;
1909
1910 default:
1911 {
1912
1913 /* Some manifestation of an object (or objects) before domain is
1914 found, since <obj_match_state> is initially set to 0:
1915 */
1916 if (obj_match_state==0)
1917 {
1918 if (Peek(grammaraddr)==HELD_T || Peek(grammaraddr)==MULTIHELD_T)
1919 odomain = var[actor];
1920
1921 obj_match_state = 1; /* since next set of object words
1922 must be the xobject */
1923 objstart = *wordnum;
1924 objgrammar = grammaraddr;
1925
1926 while (wd[*wordnum] != 0)
1927 {
1928 finish = *wordnum;
1929
1930 /* Check what's coming up in case it's a dictionary
1931 word--which would override an object phrase.
1932 */
1933 thissyntax = grammaraddr;
1934 AdvanceGrammar();
1935 nextsyntax = grammaraddr;
1936 grammaraddr = thissyntax;
1937
1938 /* dictionary word or string */
1939 CheckWordorString:
1940 p = Peek(nextsyntax);
1941 if (p==DICTENTRY_T || p==STRING_T)
1942 {
1943 if ((PeekWord(nextsyntax + 1)==wd[*wordnum]) ||
1944 (p==STRING_T && wd[*wordnum]==UNKNOWN_WORD))
1945 {
1946 grammaraddr = nextsyntax;
1947 if (*wordnum != objstart)
1948 return true;
1949 else
1950 return false;
1951 }
1952 else if (Peek(nextsyntax + 3)==FORWARD_SLASH_T)
1953 {
1954 thissyntax = grammaraddr;
1955 grammaraddr = nextsyntax + 3;
1956 AdvanceGrammar();
1957 nextsyntax = grammaraddr;
1958 grammaraddr = thissyntax;
1959
1960 goto CheckWordorString;
1961 }
1962 }
1963
1964 /* or a number */
1965 else if ((p==NUMBER_T && STARTS_AS_NUMBER(word[*wordnum])) &&
1966 !strcmp(word[*wordnum], itoa(atoi(word[*wordnum]), num, 10)))
1967 {
1968 grammaraddr = nextsyntax;
1969 if (*wordnum != objstart)
1970 return true;
1971 else
1972 return false;
1973 }
1974
1975 /* Pass over any object words--they'll be matched
1976 specifically later in MatchCommand().
1977 */
1978 flag = 0;
1979 if (AnyObjWord(*wordnum)==1)
1980 {
1981 (*wordnum)++;
1982 flag = 1;
1983 }
1984
1985 /* if "~and", "~all",... */
1986 if (word[*wordnum][0]=='~')
1987 {
1988 int multicheck = Peek(grammaraddr);
1989
1990 (*wordnum)++;
1991 flag = 1;
1992
1993 /* multi or multi(something) */
1994 if (multicheck != MULTI_T &&
1995 multicheck != MULTIHELD_T &&
1996 multicheck != MULTINOTHELD_T)
1997 {
1998 strcpy(parseerr, word[1]);
1999 /* "You can't...multiple objects." */
2000 ParseError(3, 0);
2001 return 2;
2002 }
2003 }
2004
2005 objfinish = finish;
2006
2007 if (flag==0)
2008 return 0;
2009 }
2010
2011 AdvanceGrammar();
2012 return true;
2013 }
2014
2015 /* hitting xobject */
2016
2017 else if (obj_match_state==1)
2018 {
2019 int temp_objfinish = objfinish;
2020
2021 /* If we don't know the verbroutine, try to figure it out */
2022 if (var[verbroutine]==0)
2023 {
2024 thissyntax = grammaraddr;
2025 AdvanceGrammar();
2026 nextsyntax = grammaraddr;
2027 grammaraddr = thissyntax;
2028 if (Peek(nextsyntax)==ROUTINE_T)
2029 {
2030 var[verbroutine] = PeekWord(nextsyntax+1);
2031 }
2032 }
2033
2034 p = Peek(grammaraddr);
2035
2036 /* If the xobject is specifically a parent of the
2037 object(s) to be matched later:
2038 */
2039 if (p==PARENT_T) domain = -1;
2040
2041 /* Also deal with held xobjects */
2042 t = domain;
2043 if (p==HELD_T || p==MULTIHELD_T)
2044 domain = var[actor];
2045
2046 /* Figure out where this xobject must end, as per grammar */
2047 objfinish = -1;
2048 if (*wordnum < words)
2049 {
2050 thissyntax = grammaraddr;
2051 AdvanceGrammar();
2052 nextsyntax = grammaraddr;
2053 grammaraddr = thissyntax;
2054
2055 CheckXobjectFinish:
2056 p = Peek(nextsyntax);
2057 for (i=*wordnum+1; i<=words; i++)
2058 {
2059 if (p==DICTENTRY_T || p==STRING_T)
2060 {
2061 if ((PeekWord(nextsyntax + 1)==wd[i]) ||
2062 (p==STRING_T && wd[i]==UNKNOWN_WORD))
2063 {
2064 objfinish = i-1;
2065 break;
2066 }
2067 }
2068 else if (Peek(nextsyntax + 3)==FORWARD_SLASH_T)
2069 {
2070 thissyntax = grammaraddr;
2071 grammaraddr = nextsyntax + 3;
2072 AdvanceGrammar();
2073 nextsyntax = grammaraddr;
2074 grammaraddr = thissyntax;
2075
2076 goto CheckXobjectFinish;
2077 }
2078 else if ((p==NUMBER_T && STARTS_AS_NUMBER(word[*wordnum])) &&
2079 !strcmp(word[*wordnum], itoa(atoi(word[*wordnum]), num, 10)))
2080 {
2081 objfinish = i;
2082 break;
2083 }
2084 else
2085 break;
2086 }
2087
2088 }
2089
2090 if (objfinish==-1) objfinish = words;
2091
2092 /* Regardless, try to match the xobject */
2093 recursive_call = false;
2094 if (MatchObject(&(*wordnum))==0)
2095 {
2096 objfinish = temp_objfinish;
2097
2098 return 2;
2099 }
2100 else
2101 {
2102 objfinish = temp_objfinish;
2103
2104 domain = 0;
2105 if (ValidObj(pobj)==false)
2106 return 2;
2107 domain = t;
2108
2109 if (objcount==1)
2110 var[xobject] = objlist[0];
2111 else
2112 var[xobject] = pobj;
2113 if (domain==-1) domain = var[xobject]; /* parent */
2114 obj_match_state = 2;
2115
2116 AdvanceGrammar();
2117
2118 /* Can't have multiple xobjects */
2119 if (objcount > 1)
2120 {
2121 ParseError(7, 0);
2122 return 2;
2123 }
2124 objcount = 0;
2125 return true;
2126 }
2127 }
2128 }
2129 }
2130
2131 return 0;
2132 }
2133
2134
2135 /* OBJWORDTYPE
2136
2137 Returns true if the specified object has the specified word
2138 as an adjective or noun (as specified by type).
2139 */
2140
2141 int ObjWordType(int obj, unsigned int w, int type)
2142 {
2143 int j, num;
2144 unsigned int pa;
2145
2146 pa = PropAddr(obj, type, 0);
2147 if (pa)
2148 {
2149 defseg = proptable;
2150 num = Peek(pa + 1);
2151
2152 if (num==PROP_ROUTINE)
2153 {
2154 if ((unsigned int)GetProp(obj, type, 1, false)==w)
2155 {
2156 defseg = gameseg;
2157 return true;
2158 }
2159 }
2160 else
2161 {
2162 for (j=1; j<=num; j++)
2163 {
2164 if (PeekWord(pa + j * 2)==w)
2165 {
2166 defseg = gameseg;
2167 return true;
2168 }
2169 }
2170 }
2171 }
2172
2173 defseg = gameseg;
2174
2175 return false;
2176 }
2177
2178
2179 /* OBJWORD
2180
2181 Returns <adjective> if the word at dictionary address <w> is
2182 an adjective of <obj>, or <noun> if it is a noun.
2183 */
2184
2185 int ObjWord(int obj, unsigned int w)
2186 {
2187 if ((obj_parselist) && !(obj_parselist[obj/8]&1<<(obj%8)))
2188 return 0;
2189
2190 if (ObjWordType(obj, w, adjective))
2191 return adjective;
2192
2193 if (ObjWordType(obj, w, noun))
2194 return noun;
2195
2196 return 0;
2197 }
2198
2199
2200 /* PARSE
2201
2202 Turns word[] into dictionary addresses stored in wd[]. Takes care
2203 of fingering illegal (unknown) words and doing alterations such
2204 as compounds, removals, and synonyms.
2205 */
2206
2207 int Parse()
2208 {
2209 char foundstring = 0; /* allow one unknown word/phrase */
2210 int notfound_word = 0;
2211 int i, j, k, m;
2212 char num[33];
2213 char tempword[81];
2214 unsigned int period, comma;
2215 unsigned int synptr;
2216
2217 period = FindWord(".");
2218 comma = FindWord(",");
2219
2220 strcpy(parsestr, ""); /* for storing any unknown string */
2221 parsed_number = 0; /* " " " parsed number */
2222
2223 for (i=1; i<=words; i++) /* find dictionary addresses */
2224 {
2225 if (word[i][0]=='\"' && foundstring==0)
2226 {
2227 strcpy(parsestr, word[i]);
2228 foundstring = 1;
2229 wd[i] = UNKNOWN_WORD;
2230 }
2231 else
2232 {
2233 wd[i] = FindWord(word[i]);
2234
2235 /* Numbers -32768 to 32767 are valid...*/
2236 if (!strcmp(word[i], itoa(atoi(word[i]), num, 10)))
2237 {
2238 #if !defined (MATH_16BIT)
2239 if (atoi(word[i]) > 32767 || atoi(word[i]) < -32768)
2240 goto NotinDictionary;
2241 #endif
2242 parsed_number = atoi(word[i]);
2243 if (parseerr[0]=='\0')
2244 strcpy(parseerr, word[i]);
2245 }
2246
2247 /* Otherwise it must be a dictionary entry */
2248 else
2249 {
2250 /* If it's not in the dictionary */
2251 if (wd[i]==UNKNOWN_WORD)
2252 {
2253 NotinDictionary:
2254 if (!notfound_word)
2255 {
2256 strcpy(parseerr, word[i]);
2257 strcpy(oops, word[i]);
2258
2259 notfound_word = i;
2260 }
2261 }
2262 }
2263 }
2264 }
2265
2266 /* Return here instead of immediately, so that we have a chance
2267 to load the rest of the recognized words into the word array
2268 */
2269 if (notfound_word)
2270 {
2271 i = notfound_word;
2272
2273 /* "...can't use the word..." */
2274 ParseError(1, 0);
2275 strcpy(errbuf, "");
2276 for (i=1; i<=words; i++)
2277 {
2278 strcat(errbuf, word[i]);
2279 if (i != words) strcat(errbuf, " ");
2280 }
2281
2282 return 0;
2283 }
2284
2285 wd[words+1] = 0;
2286 oopscount = 0;
2287
2288
2289 /* Do synonyms, removals, compounds, punctuation */
2290
2291 for (i=1; i<=words; i++) /* Look through words... */
2292 {
2293 synptr = 2;
2294
2295 for (j=1; j<=syncount; j++) /* ...and alterations */
2296 {
2297 defseg = syntable;
2298 if (wd[i]==PeekWord(synptr + 1))
2299 {
2300 switch (Peek(synptr))
2301 {
2302 case 0: /* synonym */
2303 {
2304 defseg = syntable;
2305 wd[i] = PeekWord(synptr + 3);
2306 m = strlen(GetWord(wd[i])) - strlen(word[i]);
2307 if (m)
2308 {
2309 if (m + (int)strlen(buffer) > 81)
2310 {strcpy(buffer, "");
2311 words = 0;
2312 ParseError(0, 0);
2313 return 0;}
2314
2315 for (k=words; k>i; k--)
2316 {
2317 strcpy(tempword, word[k]);
2318 word[k] += m;
2319 strcpy(word[k], tempword);
2320 }
2321 }
2322 strcpy(word[i], GetWord(wd[i]));
2323 i--;
2324 break;
2325 }
2326
2327 case 1: /* removal */
2328 {
2329 KillWord(i);
2330 i--;
2331 break;
2332 }
2333
2334 case 2: /* compound */
2335 {
2336 if (wd[i+1]==PeekWord(synptr+3))
2337 {
2338 memmove(word[i] + strlen(word[i]), word[i+1], strlen(word[i+1]) + 1);
2339 wd[i] = FindWord(word[i]);
2340 KillWord(i+1);
2341 }
2342 break;
2343 }
2344 }
2345 goto NextSyn;
2346 }
2347 NextSyn:
2348 synptr += 5;
2349 }
2350
2351 if (wd[i]==comma)
2352 {
2353 if (strcmp(word[i+1], "~and"))
2354 {
2355 word[i] = "~and";
2356 wd[i] = FindWord("~and");
2357 }
2358 else
2359 KillWord(i);
2360 }
2361
2362 if (wd[i]==period)
2363 {
2364 wd[i] = 0;
2365 word[i] = "";
2366 }
2367 }
2368
2369 defseg = gameseg;
2370
2371 if (strcmp(word[1], "~oops")) strcpy(oops, "");
2372
2373 if (words==0)
2374 {
2375 ParseError(0,0); /* What? */
2376 return false;
2377 }
2378
2379 return true;
2380 }
2381
2382
2383 /* PARSEERROR */
2384
2385 void ParseError(int e, int a)
2386 {
2387 int i, k, count;
2388
2389 remaining = 0;
2390 xverb = true;
2391
2392 if (e==5 && !strcmp(parseerr, "")) e = 6;
2393
2394 if (parseerroraddr)
2395 {
2396 ret = 0;
2397 passlocal[0] = e;
2398 passlocal[1] = a;
2399 PassLocals(2);
2400
2401 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
2402
2403 #if defined (DEBUGGER)
2404 DebugRunRoutine((long)parseerroraddr*address_scale);
2405 #else
2406 RunRoutine((long)parseerroraddr*address_scale);
2407 #endif
2408 stack_depth = 0;
2409 retflag = 0;
2410 if (ret)
2411 {
2412 if (ret==2) reparse_everything = true;
2413 return;
2414 }
2415 }
2416
2417 switch (e)
2418 {
2419 case 0:
2420 AP("What?");
2421 break;
2422
2423 case 1:
2424 sprintf(line, "You can't use the word \"%s\".", parseerr);
2425 AP(line);
2426 break;
2427
2428 case 2:
2429 AP("Better start with a verb.");
2430 break;
2431
2432 case 3:
2433 sprintf(line, "You can't %s multiple objects.", parseerr);
2434 AP(line);
2435 break;
2436
2437 case 4:
2438 AP("Can't do that.");
2439 break;
2440
2441 case 5:
2442 sprintf(line, "You haven't seen any \"%s\", nor are you likely to in the near future even if such a thing exists.", parseerr);
2443 AP(line);
2444 break;
2445
2446 case 6:
2447 AP("That doesn't make any sense.");
2448 break;
2449
2450 case 7:
2451 AP("You can't use multiple objects like that.");
2452 break;
2453
2454 case 8:
2455 {
2456 sprintf(line, "Which %s do you mean, ", !parse_called_twice?parseerr:"exactly");
2457 count = 1;
2458 for (k=0; k<pobjcount; k++)
2459 {
2460 i = pobjlist[k].obj;
2461
2462 if (strcmp(Name(i), ""))
2463 {
2464 if (count==pobjcount)
2465 {
2466 if (count > 2) strcat(line, ",");
2467 strcat(line, " or ");
2468 }
2469 else
2470 {
2471 if (count != 1)
2472 strcat(line, ", ");
2473 }
2474 if (GetProp(i, article, 1, 0))
2475 {
2476 char *w = GetWord(GetProp(i, article, 1, 0));
2477 /* Don't use "a" or "an" in listing */
2478 /*
2479 if (!strcmp(w, "a") || !strcmp(w, "an"))
2480 strcat(line, "the ");
2481 else
2482 sprintf(line+strlen(line), "%s ", w);
2483 */
2484 /* We'll just use "the" */
2485 if (w) strcat(line, "the ");
2486 }
2487 strcat(line, Name(i));
2488 count++;
2489 }
2490 }
2491 strcat(line, "?");
2492 AP(line);
2493 break;
2494 }
2495
2496 case 9:
2497 sprintf(line, "Nothing to %s.", parseerr);
2498 AP(line);
2499 break;
2500
2501 case 10:
2502 AP("You haven't seen anything like that.");
2503 break;
2504
2505 case 11:
2506 AP("You don't see that.");
2507 break;
2508
2509 case 12:
2510 sprintf(line, "You can't do that with the %s.", Name(a));
2511 AP(line);
2512 break;
2513
2514 case 13:
2515 AP("You'll have to be a little more specific.");
2516 break;
2517
2518 case 14:
2519 AP("You don't see that there.");
2520 break;
2521
2522 case 15:
2523 AP("You don't have that.");
2524 break;
2525
2526 case 16:
2527 AP("You'll have to make a mistake first.");
2528 break;
2529
2530 case 17:
2531 AP("You can only correct one word at a time.");
2532 break;
2533 }
2534 }
2535
2536
2537 /* REMOVEWORD
2538
2539 Deletes wd[a].
2540 */
2541
2542 void RemoveWord(int a)
2543 {
2544 if (a > words)
2545 return;
2546
2547 for (; a<words; a++)
2548 {
2549 wd[a] = wd[a + 1];
2550 objword_cache[a] = objword_cache[a + 1];
2551 }
2552 wd[words] = 0;
2553 objword_cache[words] = 0;
2554 }
2555
2556
2557 /* RESETFINDOBJECT
2558
2559 Call FindObject(0, 0) to reset library's disambiguation
2560 mechanism.
2561 */
2562
2563 void ResetFindObject(void)
2564 {
2565 if (findobjectaddr)
2566 {
2567 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
2568 PassLocals(0);
2569 #if defined (DEBUGGER)
2570 DebugRunRoutine((long)findobjectaddr*address_scale);
2571 #else
2572 RunRoutine((long)findobjectaddr*address_scale);
2573 #endif
2574 retflag = 0;
2575 }
2576 }
2577
2578
2579 /* SEPARATEWORDS
2580
2581 Splits <buffer> into the word[] array. Also does nifty things
2582 such as turning time values such as hh:mm into a single number
2583 (representing minutes from midnight).
2584 */
2585
2586 void SeparateWords(void)
2587 {
2588 char inquote = 0;
2589 char a[1025];
2590 char b[2];
2591 char w1[17], w2[17]; /* for time conversions */
2592 char temp[17];
2593 short n1, n2; /* must be 16 bits */
2594 int bloc = 0; /* buffer location */
2595 int i;
2596
2597
2598 /* First filter the line of any user-specified punctuation */
2599 do
2600 {
2601 i = strcspn(buffer, punc_string);
2602 if (buffer[i]) buffer[i] = ' ';
2603 } while (buffer[i]);
2604
2605
2606 /* Begin the word-splitting proper: */
2607
2608 words = 1; /* Setup a blank string */
2609
2610 for (i=0; i<MAXWORDS+1; i++)
2611 {
2612 word[i] = "";
2613 wd[i] = 0;
2614 }
2615 word[1] = buffer;
2616
2617 strcpy(a, buffer);
2618 strcpy(buffer, "");
2619
2620 for (i=1; i<=(int)strlen(a); i++)
2621 {
2622 if (inquote!=1 && isascii(a[i-1]))
2623 b[0] = (char)tolower(a[i-1]);
2624 else b[0] = a[i-1];
2625 b[1] = '\0';
2626
2627 if (b[0]=='\"' && inquote==1)
2628 {
2629 strcpy(buffer+bloc, b);
2630 bloc++;
2631 inquote++;
2632 }
2633
2634 if (b[0]=='\"' || ((b[0]==' ' || b[0]=='!' || b[0]=='?') && inquote!=1))
2635 {
2636 if (word[words][0]!='\0')
2637 {
2638 bloc++;
2639 if (++words > MAXWORDS) words = MAXWORDS;
2640 word[words] = buffer + bloc;
2641 strcpy(word[words], "");
2642 }
2643
2644 if (b[0]=='\"' && inquote==0)
2645 {
2646 strcpy(buffer+bloc, b);
2647 bloc++;
2648 inquote = 1;
2649 }
2650 }
2651 else
2652 {
2653 if ((b[0]=='.' || b[0]==',') && inquote!=1)
2654 {
2655 if (word[words][0]!='\0')
2656 {
2657 bloc++;
2658 if (++words > MAXWORDS) words = MAXWORDS;
2659 }
2660 word[words] = buffer + bloc;
2661 strcpy(word[words], b);
2662 bloc += strlen(b) + 1;
2663 if (++words > MAXWORDS) words = MAXWORDS;
2664 word[words] = buffer + bloc;
2665 strcpy(word[words], "");
2666 }
2667 else
2668 {
2669 strcpy(buffer+bloc, b);
2670 bloc++;
2671 }
2672 }
2673 }
2674
2675 if (!strcmp(word[words], "")) words--;
2676
2677 for (i=1; i<=words; i++)
2678 {
2679 /* Convert hours:minutes time to minutes only */
2680 if (strcspn(word[i], ":")!=strlen(word[i]) && strlen(word[i])<=5)
2681 {
2682 strcpy(w1, Left(word[i], strcspn(word[i], ":")));
2683 strcpy(w2, Right(word[i], strlen(word[i]) - strcspn(word[i], ":") - 1));
2684 n1 = (short)atoi(w1);
2685 n2 = (short)atoi(w2);
2686
2687 if (!strcmp(Left(w2, 1), "0"))
2688 strcpy(w2, Right(w2, strlen(w2) - 1));
2689
2690 /* If this is indeed a hh:mm time, write it back
2691 as the modified word, storing the original hh:mm
2692 in parse$:
2693 */
2694 if (!strcmp(w1, itoa((int)n1, temp, 10)) && !strcmp(w2, itoa((int)n2, temp, 10)) && (n1 > 0 && n1 < 25) && (n2 >= 0 && n2 < 60))
2695 {
2696 strcpy(parseerr, word[i]);
2697 itoa(n1 * 60 + n2, word[i], 10);
2698 }
2699 }
2700 }
2701 }
2702
2703
2704 /* SUBTRACTOBJ
2705
2706 Removes object <obj> from objlist[], making all related adjustments.
2707 */
2708
2709 void SubtractObj(int obj)
2710 {
2711 int i, j;
2712
2713 for (i=0; i<objcount; i++)
2714 {
2715 if (objlist[i]==obj)
2716 {
2717 for (j=i; j<objcount; j++)
2718 objlist[j] = objlist[j+1];
2719 objcount--;
2720 return;
2721 }
2722 }
2723 }
2724
2725
2726 /* SUBTRACTPOSSIBLEOBJECT
2727
2728 Removes <obj> as a possible contender for object disambiguation.
2729 */
2730
2731 void SubtractPossibleObject(int obj)
2732 {
2733 int i, j, last = 0;
2734
2735 for (i=0; i<pobjcount; i++)
2736 {
2737 if (pobjlist[i].obj==obj)
2738 {
2739 if (pobjlist[i].obj==pobj && last!=0) pobj = last;
2740
2741 for (j=i; j+1<pobjcount; j++)
2742 {
2743 pobjlist[j] = pobjlist[j+1];
2744 }
2745 pobjcount--;
2746
2747 #ifdef DEBUG_PARSER
2748 {
2749 char buf[100];
2750 sprintf(buf, "SubtractPossibleObject(%d:\"%s\")", obj, Name(obj));
2751 Printout(buf);
2752 }
2753 #endif
2754 return;
2755 }
2756 else last = pobjlist[i].obj;
2757 }
2758 }
2759
2760
2761 /* TRYOBJ
2762
2763 Called by MatchObject() to see if <obj> is available, and add it to
2764 or subtract it from objlist[] accordingly.
2765 */
2766
2767 void TryObj(int obj)
2768 {
2769 unsigned int tempdomain;
2770
2771 if ((obj_parselist) && !(obj_parselist[obj/8]&1<<(obj%8)))
2772 return;
2773
2774 if (DomainObj(obj))
2775 {
2776 tempdomain = domain;
2777 domain = 0;
2778
2779 if (Available(obj, 0) && !InList(Parent(obj)))
2780 {
2781 AddObj(obj);
2782 }
2783 else
2784 {
2785 SubtractObj(obj);
2786 }
2787
2788 domain = tempdomain;
2789 }
2790 }
2791
2792
2793 /* VALIDOBJ
2794
2795 Checks first of all to see if an object is available, then checks
2796 if it meets all the qualifications demanded by the grammar syntax.
2797 */
2798
2799 int ValidObj(int obj)
2800 {
2801 int attr, nattr = 0;
2802 unsigned int addr;
2803
2804 defseg = gameseg;
2805
2806 if (!Available(obj, 0) && !speaking &&
2807 (Peek(grammaraddr)!=OPEN_BRACKET_T ||
2808 Peek(grammaraddr+1)!=ROUTINE_T))
2809 {
2810 if (Peek(grammaraddr)==ANYTHING_T)
2811 ParseError(10, obj); /* "...haven't seen..." */
2812 else
2813 ParseError(11, obj); /* "...don't see any..." */
2814 return 0;
2815 }
2816
2817 switch (Peek(grammaraddr))
2818 {
2819 case OPEN_BRACKET_T:
2820 {
2821 if (Peek(grammaraddr+1)==ROUTINE_T)
2822 {
2823 addr = PeekWord(grammaraddr+2);
2824 ret = 0;
2825 passlocal[0] = obj;
2826 PassLocals(1);
2827
2828 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
2829
2830 #if defined (DEBUGGER)
2831 DebugRunRoutine((long)addr*address_scale);
2832 #else
2833 RunRoutine((long)addr*address_scale);
2834 #endif
2835 retflag = 0;
2836
2837 /* If the routine doesn't return true,
2838 the object doesn't qualify.
2839 */
2840 if (!ret)
2841 return (0);
2842 }
2843 else if (Peek(grammaraddr+1)==OBJECTNUM_T)
2844 {
2845 if (obj != (int)PeekWord(grammaraddr+2))
2846 {
2847 strcpy(parseerr, "");
2848 if (GetProp(obj, article, 1, 0))
2849 strcpy(parseerr, "the ");
2850 strcat(parseerr, Name(obj));
2851
2852 /* "...can't do that with..." */
2853 ParseError(12, obj);
2854 return 0;
2855 }
2856 }
2857 break;
2858 }
2859
2860 case ATTR_T:
2861 case NOT_T:
2862 {
2863 if (Peek(grammaraddr)==NOT_T) nattr = 1;
2864 attr = Peek(grammaraddr + 1 + nattr);
2865
2866 /* If the attribute match is not made,
2867 the object doesn't qualify.
2868 */
2869 if (!TestAttribute(obj, attr, nattr))
2870 {
2871 strcpy(parseerr, "");
2872 if (GetProp(obj, article, 1, 0))
2873 strcpy(parseerr, "the ");
2874 strcat(parseerr, Name(obj));
2875
2876 /* "...can't do that with..." */
2877 ParseError(12, obj);
2878 return 0;
2879 }
2880 break;
2881 }
2882 }
2883 return true;
2884 }