home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 875 / inform.sis / Parserm.h < prev    next >
Encoding:
Text File  |  1999-09-14  |  155.0 KB  |  4,524 lines

  1. ! ----------------------------------------------------------------------------
  2. !  PARSERM:  Core of parser.
  3. !
  4. !  Supplied for use with Inform 6                         Serial number 990428
  5. !                                                                  Release 6/9
  6. !  (c) Graham Nelson 1993, 1994, 1995, 1996, 1997, 1998, 1999
  7. !      but freely usable (see manuals)
  8. ! ----------------------------------------------------------------------------
  9. !  Inclusion of "linklpa"
  10. !                   (which defines properties and attributes)
  11. !  Global variables, constants and arrays
  12. !                1: outside of the parser
  13. !                2: used within the parser
  14. !  Inclusion of natural language definition file
  15. !                   (which creates a compass and direction-objects)
  16. !  Darkness and player objects
  17. !  Definition of grammar token numbering system used by Inform
  18. !
  19. !  The InformParser object
  20. !          keyboard reading
  21. !          level 0: outer shell, conversation, errors
  22. !                1: grammar lines
  23. !                2: tokens
  24. !                3: object lists
  25. !                4: scope and ambiguity resolving
  26. !                5: object comparisons
  27. !                6: word comparisons
  28. !                7: reading words and moving tables about
  29. !          pronoun management
  30. !
  31. !  The InformLibrary object
  32. !          main game loop
  33. !          action processing
  34. !          end of turn sequence
  35. !          scope looping, before/after sequence, sending messages out
  36. !          timers, daemons, time of day, score notification
  37. !          light and darkness
  38. !          changing player personality
  39. !          tracing code (only present if DEBUG is set)
  40. !
  41. !  Status line printing, menu display
  42. !  Printing object names with articles
  43. !  Miscellaneous utility routines
  44. !  Game banner, "version" verb, run-time errors
  45. ! ----------------------------------------------------------------------------
  46.  
  47. System_file;
  48. Constant NULL = $ffff;
  49.  
  50. IFDEF MODULE_MODE;
  51. Constant DEBUG;
  52. Constant Grammar__Version 2;
  53. Include "linklpa";
  54. ENDIF;
  55.  
  56. ! ============================================================================
  57. !   Global variables and their associated Constant and Array declarations
  58. ! ----------------------------------------------------------------------------
  59. Global location = InformLibrary;     ! Must be first global defined
  60. Global sline1;                       ! Must be second
  61. Global sline2;                       ! Must be third
  62.                                      ! (for status line display)
  63. ! ------------------------------------------------------------------------------
  64. !   Z-Machine and interpreter issues
  65. ! ------------------------------------------------------------------------------
  66. Global top_object;                   ! Largest valid number of any tree object
  67. Global standard_interpreter;         ! The version number of the Z-Machine
  68.                                      ! Standard which the interpreter claims
  69.                                      ! to support, in form (upper byte).(lower)
  70. Global undo_flag;                    ! Can the interpreter provide "undo"?
  71. Global just_undone;                  ! Can't have two successive UNDOs
  72. Global transcript_mode;              ! true when game scripting is on
  73. IFDEF DEBUG;
  74. Global xcommsdir;                    ! true if command recording is on
  75. ENDIF;
  76. ! ------------------------------------------------------------------------------
  77. !   Time and score
  78. ! (for linkage reasons, the task_* arrays are created not here but in verblib.h)
  79. ! ------------------------------------------------------------------------------
  80. Global turns = 1;                    ! Number of turns of play so far
  81. Global the_time = NULL;              ! Current time (in minutes since midnight)
  82. Global time_rate = 1;                ! How often time is updated
  83. Global time_step;                    ! By how much
  84.  
  85. #ifndef MAX_TIMERS;
  86. Constant MAX_TIMERS  32;             ! Max number timers/daemons active at once
  87. #endif;
  88. Array  the_timers  --> MAX_TIMERS;
  89. Global active_timers;                ! Number of timers/daemons actives
  90.  
  91. Global score;                        ! The current score
  92. Global last_score;                   ! Score last turn (for testing for changes)
  93. Global notify_mode = true;           ! Score notification
  94. Global places_score;                 ! Contribution to score made by visiting
  95. Global things_score;                 ! Contribution made by acquisition
  96. ! ------------------------------------------------------------------------------
  97. !   The player
  98. ! ------------------------------------------------------------------------------
  99. Global player;                       ! Which object the human is playing through
  100. Global deadflag;                     ! Normally 0, or false; 1 for dead;
  101.                                      ! 2 for victorious, and higher numbers
  102.                                      ! represent exotic forms of death
  103. ! ------------------------------------------------------------------------------
  104. !   Light and room descriptions
  105. ! ------------------------------------------------------------------------------
  106. Global lightflag = true;             ! Is there currently light to see by?
  107. Global real_location;                ! When in darkness, location = thedark
  108.                                      ! and this holds the real location
  109. Global visibility_ceiling;           ! Highest object in tree visible from
  110.                                      ! the player's point of view (usually
  111.                                      ! the room, sometimes darkness, sometimes
  112.                                      ! a closed non-transparent container).
  113.  
  114. Global lookmode = 1;                 ! 1=standard, 2=verbose, 3=brief room descs
  115. Global print_player_flag;            ! If set, print something like "(as Fred)"
  116.                                      ! in room descriptions, to reveal whom
  117.                                      ! the human is playing through
  118. Global lastdesc;                     ! Value of location at time of most recent
  119.                                      ! room description printed out
  120. ! ------------------------------------------------------------------------------
  121. !   List writing  (style bits are defined as Constants in "verblibm.h")
  122. ! ------------------------------------------------------------------------------
  123. Global c_style;                      ! Current list-writer style
  124. Global lt_value;                     ! Common value of list_together
  125. Global listing_together;             ! Object number of one member of a group
  126.                                      ! being listed together
  127. Global listing_size;                 ! Size of such a group
  128. Global wlf_indent;                   ! Current level of indentation printed by
  129.                                      ! WriteListFrom routine
  130.  
  131. Global inventory_stage = 1;          ! 1 or 2 according to the context in which
  132.                                      ! "invent" routines of objects are called
  133. Global inventory_style;              ! List-writer style currently used while
  134.                                      ! printing inventories
  135. ! ------------------------------------------------------------------------------
  136. !   Menus and printing
  137. ! ------------------------------------------------------------------------------
  138. Global pretty_flag = true;           ! Use character graphics, or plain text?
  139. Global menu_nesting;                 ! Level of nesting (0 = root menu)
  140. Global menu_item;                    ! These are used in communicating
  141. Global item_width = 8;               ! with the menu-creating routines
  142. Global item_name = "---";
  143.  
  144. Global lm_n;                         ! Parameters used by LibraryMessages
  145. Global lm_o;                         ! mechanism
  146.  
  147. IFDEF DEBUG;
  148. Global debug_flag;                   ! Bitmap of flags for tracing actions,
  149.                                      ! calls to object routines, etc.
  150. Global x_scope_count;                ! Used in printing a list of everything
  151.                                      ! in scope
  152. ENDIF;
  153. ! ------------------------------------------------------------------------------
  154. !   Action processing
  155. ! ------------------------------------------------------------------------------
  156. Global action;                       ! Action currently being asked to perform
  157. Global inp1;                         ! 0 (nothing), 1 (number) or first noun
  158. Global inp2;                         ! 0 (nothing), 1 (number) or second noun
  159. Global noun;                         ! First noun or numerical value
  160. Global second;                       ! Second noun or numerical value
  161.  
  162. Global keep_silent;                  ! If true, attempt to perform the action
  163.                                      ! silently (e.g. for implicit takes,
  164.                                      ! implicit opening of unlocked doors)
  165.  
  166. Global reason_code;                  ! Reason for calling a "life" rule
  167.                                      ! (an action or fake such as ##Kiss)
  168.  
  169. Global receive_action;               ! Either ##PutOn or ##Insert, whichever
  170.                                      ! is action being tried when an object's
  171.                                      ! "before" rule is checking "Receive"
  172. ! ==============================================================================
  173. !   Parser variables: first, for communication to the parser
  174. ! ------------------------------------------------------------------------------
  175. Global parser_trace = 0;             ! Set this to 1 to make the parser trace
  176.                                      ! tokens and lines
  177. Global parser_action;                ! For the use of the parser when calling
  178. Global parser_one;                   ! user-supplied routines
  179. Global parser_two;                   !
  180. Array  inputobjs       --> 16;       ! For parser to write its results in
  181. Global parser_inflection;            ! A property (usually "name") to find
  182.                                      ! object names in
  183. ! ------------------------------------------------------------------------------
  184. !   Parser output
  185. ! ------------------------------------------------------------------------------
  186. Global actor;                        ! Person asked to do something
  187. Global actors_location;              ! Like location, but for the actor
  188. Global meta;                         ! Verb is a meta-command (such as "save")
  189.  
  190. Array  multiple_object --> 64;       ! List of multiple parameters
  191. Global multiflag;                    ! Multiple-object flag
  192. Global toomany_flag;                 ! Flag for "multiple match too large"
  193.                                      ! (e.g. if "take all" took over 100 things)
  194.  
  195. Global special_word;                 ! Dictionary address for "special" token
  196. Global special_number;               ! Number typed for "special" token
  197. Global parsed_number;                ! For user-supplied parsing routines
  198. Global consult_from;                 ! Word that a "consult"átopic starts on
  199. Global consult_words;                ! ...and number of words in topic
  200. ! ------------------------------------------------------------------------------
  201. !   Implicit taking
  202. ! ------------------------------------------------------------------------------
  203. Global notheld_mode;                 ! To do with implicit taking
  204. Global onotheld_mode;                !     "old copy of notheld_mode", ditto
  205. Global not_holding;                  ! Object to be automatically taken as an
  206.                                      ! implicit command
  207. Array  kept_results --> 16;          ! Delayed command (while the take happens)
  208. ! ------------------------------------------------------------------------------
  209. !   Error numbers when parsing a grammar line
  210. ! ------------------------------------------------------------------------------
  211. Global etype;                        ! Error number on current line
  212. Global best_etype;                   ! Preferred error number so far
  213. Global nextbest_etype;               ! Preferred one, if ASKSCOPE_PE disallowed
  214.  
  215. Constant STUCK_PE     = 1;
  216. Constant UPTO_PE      = 2;
  217. Constant NUMBER_PE    = 3;
  218. Constant CANTSEE_PE   = 4;
  219. Constant TOOLIT_PE    = 5;
  220. Constant NOTHELD_PE   = 6;
  221. Constant MULTI_PE     = 7;
  222. Constant MMULTI_PE    = 8;
  223. Constant VAGUE_PE     = 9;
  224. Constant EXCEPT_PE    = 10;
  225. Constant ANIMA_PE     = 11;
  226. Constant VERB_PE      = 12;
  227. Constant SCENERY_PE   = 13;
  228. Constant ITGONE_PE    = 14;
  229. Constant JUNKAFTER_PE = 15;
  230. Constant TOOFEW_PE    = 16;
  231. Constant NOTHING_PE   = 17;
  232. Constant ASKSCOPE_PE  = 18;
  233. ! ------------------------------------------------------------------------------
  234. !   Pattern-matching against a single grammar line
  235. ! ------------------------------------------------------------------------------
  236. Array pattern --> 32;                ! For the current pattern match
  237. Global pcount;                       ! and a marker within it
  238. Array pattern2 --> 32;               ! And another, which stores the best match
  239. Global pcount2;                      ! so far
  240. Constant PATTERN_NULL = $ffff;       ! Entry for a token producing no text
  241.  
  242. Array  line_ttype-->32;              ! For storing an analysed grammar line
  243. Array  line_tdata-->32;
  244. Array  line_token-->32;
  245.  
  246. Global parameters;                   ! Parameters (objects) entered so far
  247. Global nsns;                         ! Number of special_numbers entered so far
  248. Global special_number1;              ! First number, if one was typed
  249. Global special_number2;              ! Second number, if two were typed
  250. ! ------------------------------------------------------------------------------
  251. !   Inferences and looking ahead
  252. ! ------------------------------------------------------------------------------
  253. Global params_wanted;                ! Number of parameters needed
  254.                                      ! (which may change in parsing)
  255.  
  256. Global inferfrom;                    ! The point from which the rest of the
  257.                                      ! command must be inferred
  258. Global inferword;                    ! And the preposition inferred
  259. Global dont_infer;                   ! Another dull flag
  260.  
  261. Global action_to_be;                 ! (If the current line were accepted.)
  262. Global action_reversed;              ! (Parameters would be reversed in order.)
  263. Global advance_warning;              ! What a later-named thing will be
  264. ! ------------------------------------------------------------------------------
  265. !   At the level of individual tokens now
  266. ! ------------------------------------------------------------------------------
  267. Global found_ttype;                  ! Used to break up tokens into type
  268. Global found_tdata;                  ! and data (by AnalyseToken)
  269. Global token_filter;                 ! For noun filtering by user routines
  270.  
  271. Global length_of_noun;               ! Set by NounDomain to no of words in noun
  272. Constant REPARSE_CODE = 10000;       ! Signals "reparse the text" as a reply
  273.                                      ! from NounDomain
  274.  
  275. Global lookahead;                    ! The token after the one now being matched
  276.  
  277. Global multi_mode;                   ! Multiple mode
  278. Global multi_wanted;                 ! Number of things needed in multitude
  279. Global multi_had;                    ! Number of things actually found
  280. Global multi_context;                ! What token the multi-obj was accepted for
  281.  
  282. Global indef_mode;                   ! "Indefinite" mode - ie, "take a brick"
  283.                                      ! is in this mode
  284. Global indef_type;                   ! Bit-map holding types of specification
  285. Global indef_wanted;                 ! Number of items wanted (100 for all)
  286. Global indef_guess_p;                ! Plural-guessing flag
  287. Global indef_owner;                  ! Object which must hold these items
  288. Global indef_cases;                  ! Possible gender and numbers of them
  289. Global indef_possambig;              ! Has a possibly dangerous assumption
  290.                                      ! been made about meaning of a descriptor?
  291. Global indef_nspec_at;               ! Word at which a number like "two" was
  292.                                      ! parsed (for backtracking)
  293. Global allow_plurals;                ! Whether plurals presently allowed or not
  294.  
  295. Global take_all_rule;                ! Slightly different rules apply to
  296.                                      ! "take all" than other uses of multiple
  297.                                      ! objects, to make adjudication produce
  298.                                      ! more pragmatically useful results
  299.                                      ! (Not a flag: possible values 0, 1, 2)
  300.  
  301. Global dict_flags_of_noun;           ! Of the noun currently being parsed
  302.                                      ! (a bitmap in #dict_par1 format)
  303. Global pronoun_word;                 ! Records which pronoun ("it", "them", ...)
  304.                                      ! caused an error
  305. Global pronoun_obj;                  ! And what obj it was thought to refer to
  306. Global pronoun__word;                ! Saved value
  307. Global pronoun__obj;                 ! Saved value
  308. ! ------------------------------------------------------------------------------
  309. !   Searching through scope and parsing "scope=Routine" grammar tokens
  310. ! ------------------------------------------------------------------------------
  311. Constant PARSING_REASON       = 0;   ! Possible reasons for searching scope
  312. Constant TALKING_REASON       = 1;
  313. Constant EACH_TURN_REASON     = 2;
  314. Constant REACT_BEFORE_REASON  = 3;
  315. Constant REACT_AFTER_REASON   = 4;
  316. Constant LOOPOVERSCOPE_REASON = 5;
  317. Constant TESTSCOPE_REASON     = 6;
  318.  
  319. Global scope_reason = PARSING_REASON; ! Current reason for searching scope
  320.  
  321. Global scope_token;                  ! For "scope=Routine" grammar tokens
  322. Global scope_error;
  323. Global scope_stage;                  ! 1, 2 then 3
  324.  
  325. Global ats_flag = 0;                 ! For AddToScope routines
  326. Global ats_hls;                      !
  327.  
  328. Global placed_in_flag;               ! To do with PlaceInScope
  329.  
  330. ! ------------------------------------------------------------------------------
  331. !   The match list of candidate objects for a given token
  332. ! ------------------------------------------------------------------------------
  333. Constant MATCH_LIST_SIZE = 128;
  334. Array  match_list    --> 64;         ! An array of matched objects so far
  335. Array  match_classes --> 64;         ! An array of equivalence classes for them
  336. Array  match_scores --> 64;          ! An array of match scores for them
  337. Global number_matched;               ! How many items in it?  (0 means none)
  338. Global number_of_classes;            ! How many equivalence classes?
  339. Global match_length;                 ! How many words long are these matches?
  340. Global match_from;                   ! At what word of the input do they begin?
  341. ! ------------------------------------------------------------------------------
  342. !   Low level textual manipulation
  343. ! ------------------------------------------------------------------------------
  344. Array  buffer    -> 121;             ! Buffer for parsing main line of input
  345. Array  parse     -> 65;              ! Parse table mirroring it
  346. Array  buffer2   -> 121;             ! Buffers for supplementary questions
  347. Array  parse2    -> 65;              !
  348. Array  buffer3   -> 121;             ! Buffer retaining input for "again"
  349.  
  350. Constant comma_word = 'comma,';      ! An "untypeable word" used to substitute
  351.                                      ! for commas in parse buffers
  352.  
  353. Global wn;                           ! Word number within "parse" (from 1)
  354. Global num_words;                    ! Number of words typed
  355. Global verb_word;                    ! Verb word (eg, take in "take all" or
  356.                                      ! "dwarf, take all") - address in dict
  357. Global verb_wordnum;                 ! its number in typing order (eg, 1 or 3)
  358. Global usual_grammar_after;          ! Point from which usual grammar is parsed
  359.                                      ! (it may vary from the above if user's
  360.                                      ! routines match multi-word verbs)
  361.  
  362. Global oops_from;                    ! The "first mistake" word number
  363. Global saved_oops;                   ! Used in working this out
  364. Array  oops_workspace -> 64;         ! Used temporarily by "oops" routine
  365.  
  366. Global held_back_mode;               ! Flag: is there some input from last time
  367. Global hb_wn;                        ! left over?  (And a save value for wn.)
  368.                                      ! (Used for full stops and "then".)
  369. ! ----------------------------------------------------------------------------
  370. Array PowersOfTwo_TB                 ! Used in converting case numbers to
  371.   --> $$100000000000                 ! case bitmaps
  372.       $$010000000000
  373.       $$001000000000
  374.       $$000100000000
  375.       $$000010000000
  376.       $$000001000000
  377.       $$000000100000
  378.       $$000000010000
  379.       $$000000001000
  380.       $$000000000100
  381.       $$000000000010
  382.       $$000000000001;
  383. ! ============================================================================
  384.  
  385.  
  386. ! ============================================================================
  387. !  Constants, and one variable, needed for the language definition file
  388. ! ----------------------------------------------------------------------------
  389. Constant POSSESS_PK  = $100;
  390. Constant DEFART_PK   = $101;
  391. Constant INDEFART_PK = $102;
  392. Global short_name_case;
  393. ! ----------------------------------------------------------------------------
  394. Include "language__";                !  The natural language definition,
  395.                                      !  whose filename is taken from the ICL
  396.                                      !  language_name variable
  397. ! ----------------------------------------------------------------------------
  398. #ifndef LanguageCases;
  399. Constant LanguageCases = 1;
  400. #endif;
  401. ! ------------------------------------------------------------------------------
  402. !   Pronouns support for the cruder (library 6/2 and earlier) version:
  403. !   only needed in English
  404. ! ------------------------------------------------------------------------------
  405. #ifdef EnglishNaturalLanguage;
  406. Global itobj = NULL;                 ! The object which is currently "it"
  407. Global himobj = NULL;                ! The object which is currently "him"
  408. Global herobj = NULL;                ! The object which is currently "her"
  409.  
  410. Global old_itobj = NULL;             ! The object which is currently "it"
  411. Global old_himobj = NULL;            ! The object which is currently "him"
  412. Global old_herobj = NULL;            ! The object which is currently "her"
  413. #endif;
  414. ! ============================================================================
  415.  
  416.  
  417. ! ============================================================================
  418. ! "Darkness" is not really a place: but it has to be an object so that the
  419. !  location-name on the status line can be "Darkness".
  420. ! ----------------------------------------------------------------------------
  421. Object thedark "(darkness object)"
  422.   with initial 0,
  423.        short_name DARKNESS__TX,
  424.        description
  425.        [;  return L__M(##Miscellany, 17);
  426.        ];
  427. Object selfobj "(self object)"
  428.   with short_name
  429.        [;  return L__M(##Miscellany, 18);
  430.        ],
  431.        description
  432.        [;  return L__M(##Miscellany, 19);
  433.        ],
  434.        before NULL,   after NULL,    life NULL,    each_turn NULL,
  435.        time_out NULL, describe NULL,
  436.        capacity 100, parse_name 0,
  437.        orders 0, number 0,
  438.   has  concealed animate proper transparent;
  439.  
  440. ! ============================================================================
  441. !  The definition of the token-numbering system used by Inform.
  442. ! ----------------------------------------------------------------------------
  443.  
  444. Constant ILLEGAL_TT        = 0;      ! Types of grammar token: illegal
  445. Constant ELEMENTARY_TT     = 1;      !     (one of those below)
  446. Constant PREPOSITION_TT    = 2;      !     e.g. 'into'
  447. Constant ROUTINE_FILTER_TT = 3;      !     e.g. noun=CagedCreature
  448. Constant ATTR_FILTER_TT    = 4;      !     e.g. edible
  449. Constant SCOPE_TT          = 5;      !     e.g. scope=Spells
  450. Constant GPR_TT            = 6;      !     a general parsing routine
  451.  
  452. Constant NOUN_TOKEN        = 0;      ! The elementary grammar tokens, and
  453. Constant HELD_TOKEN        = 1;      ! the numbers compiled by Inform to
  454. Constant MULTI_TOKEN       = 2;      ! encode them
  455. Constant MULTIHELD_TOKEN   = 3;
  456. Constant MULTIEXCEPT_TOKEN = 4;
  457. Constant MULTIINSIDE_TOKEN = 5;
  458. Constant CREATURE_TOKEN    = 6;
  459. Constant SPECIAL_TOKEN     = 7;
  460. Constant NUMBER_TOKEN      = 8;
  461. Constant TOPIC_TOKEN       = 9;
  462.  
  463.  
  464. Constant GPR_FAIL          = -1;     ! Return values from General Parsing
  465. Constant GPR_PREPOSITION   = 0;      ! Routines
  466. Constant GPR_NUMBER        = 1;
  467. Constant GPR_MULTIPLE      = 2;
  468. Constant GPR_REPARSE       = REPARSE_CODE;
  469. Constant GPR_NOUN          = $ff00;
  470. Constant GPR_HELD          = $ff01;
  471. Constant GPR_MULTI         = $ff02;
  472. Constant GPR_MULTIHELD     = $ff03;
  473. Constant GPR_MULTIEXCEPT   = $ff04;
  474. Constant GPR_MULTIINSIDE   = $ff05;
  475. Constant GPR_CREATURE      = $ff06;
  476.  
  477. Constant ENDIT_TOKEN       = 15;     ! Value used to mean "end of grammar line"
  478.  
  479. #Iftrue Grammar__Version == 1;
  480. [ AnalyseToken token m;
  481.  
  482.     found_tdata = token;
  483.  
  484.     if (token < 0)   { found_ttype = ILLEGAL_TT; return; }
  485.     if (token <= 8)  { found_ttype = ELEMENTARY_TT; return; }
  486.     if (token < 15)  { found_ttype = ILLEGAL_TT; return; }
  487.     if (token == 15) { found_ttype = ELEMENTARY_TT; return; }
  488.     if (token < 48)  { found_ttype = ROUTINE_FILTER_TT;
  489.                        found_tdata = token - 16;
  490.                        return; }
  491.     if (token < 80)  { found_ttype = GPR_TT;
  492.                        found_tdata = #preactions_table-->(token-48);
  493.                        return; }
  494.     if (token < 128) { found_ttype = SCOPE_TT;
  495.                        found_tdata = #preactions_table-->(token-80);
  496.                        return; }
  497.     if (token < 180) { found_ttype = ATTR_FILTER_TT;
  498.                        found_tdata = token - 128;
  499.                        return; }
  500.  
  501.     found_ttype = PREPOSITION_TT;
  502.     m=#adjectives_table;
  503.     for (::)
  504.     {   if (token==m-->1) { found_tdata = m-->0; return; }
  505.         m=m+4;
  506.     }
  507.     m=#adjectives_table; RunTimeError(1);
  508.     found_tdata = m;
  509. ];
  510. [ UnpackGrammarLine line_address i m;
  511.   for (i = 0 : i < 32 : i++)
  512.   {   line_token-->i = ENDIT_TOKEN;
  513.       line_ttype-->i = ELEMENTARY_TT;
  514.       line_tdata-->i = ENDIT_TOKEN;
  515.   }
  516.   for (i = 0: i <= 5 :i++)
  517.   {   line_token-->i = line_address->(i+1);
  518.       AnalyseToken(line_token-->i);
  519.       if ((found_ttype == ELEMENTARY_TT) && (found_tdata == NOUN_TOKEN)
  520.           && (m == line_address->0))
  521.       {   line_token-->i = ENDIT_TOKEN;
  522.           break;
  523.       }
  524.       line_ttype-->i = found_ttype;
  525.       line_tdata-->i = found_tdata;
  526.       if (found_ttype ~= PREPOSITION_TT) m++;
  527.   }
  528.   action_to_be = line_address->7;
  529.   action_reversed = false;
  530.   params_wanted = line_address->0;
  531.   return line_address + 8;
  532. ];
  533. #Ifnot;
  534. [ AnalyseToken token;
  535.  
  536.     if (token == ENDIT_TOKEN)
  537.     {   found_ttype = ELEMENTARY_TT;
  538.         found_tdata = ENDIT_TOKEN;
  539.         return;
  540.     }
  541.  
  542.     found_ttype = (token->0) & $$1111;
  543.     found_tdata = (token+1)-->0;
  544. ];
  545. [ UnpackGrammarLine line_address i;
  546.   for (i = 0 : i < 32 : i++)
  547.   {   line_token-->i = ENDIT_TOKEN;
  548.       line_ttype-->i = ELEMENTARY_TT;
  549.       line_tdata-->i = ENDIT_TOKEN;
  550.   }
  551.   action_to_be = 256*(line_address->0) + line_address->1;
  552.   action_reversed = ((action_to_be & $400) ~= 0);
  553.   action_to_be = action_to_be & $3ff;
  554.   line_address--;
  555.   params_wanted = 0;
  556.   for (i=0::i++)
  557.   {   line_address = line_address + 3;
  558.       if (line_address->0 == ENDIT_TOKEN) break;
  559.       line_token-->i = line_address;
  560.       AnalyseToken(line_address);
  561.       if (found_ttype ~= PREPOSITION_TT) params_wanted++;
  562.       line_ttype-->i = found_ttype;
  563.       line_tdata-->i = found_tdata;
  564.   }
  565.   return line_address + 1;
  566. ];
  567. #Endif;
  568.  
  569. !  To protect against a bug in early versions of the "Zip" interpreter:
  570.  
  571. [ Tokenise__ b p; b->(2 + b->1) = 0; @tokenise b p; ];
  572.  
  573. ! ============================================================================
  574. !  The InformParser object abstracts the front end of the parser.
  575. !
  576. !  InformParser.parse_input(results)
  577. !  returns only when a sensible request has been made, and puts into the
  578. !  "results" buffer:
  579. !
  580. !  --> 0 = The action number
  581. !  --> 1 = Number of parameters
  582. !  --> 2, 3, ... = The parameters (object numbers), but
  583. !                  0 means "put the multiple object list here"
  584. !                  1 means "put one of the special numbers here"
  585. !
  586. ! ----------------------------------------------------------------------------
  587.  
  588. Object InformParser "(Inform Parser)"
  589.   with parse_input
  590.        [ results; Parser__parse(results);
  591.        ], has proper;
  592.  
  593. ! ----------------------------------------------------------------------------
  594. !  The Keyboard routine actually receives the player's words,
  595. !  putting the words in "a_buffer" and their dictionary addresses in
  596. !  "a_table".  It is assumed that the table is the same one on each
  597. !  (standard) call.
  598. !
  599. !  It can also be used by miscellaneous routines in the game to ask
  600. !  yes-no questions and the like, without invoking the rest of the parser.
  601. !
  602. !  Return the number of words typed
  603. ! ----------------------------------------------------------------------------
  604.  
  605. [ KeyboardPrimitive  a_buffer a_table;
  606.   read a_buffer a_table;
  607. ];
  608. [ Keyboard  a_buffer a_table  nw i w w2 x1 x2;
  609.  
  610.     DisplayStatus();
  611.     .FreshInput;
  612.  
  613. !  Save the start of the buffer, in case "oops" needs to restore it
  614. !  to the previous time's buffer
  615.  
  616.     for (i=0:i<64:i++) oops_workspace->i = a_buffer->i;
  617.  
  618. !  In case of an array entry corruption that shouldn't happen, but would be
  619. !  disastrous if it did:
  620.  
  621.    a_buffer->0 = 120;
  622.    a_table->0 = 15;  ! Allow to split input into this many words
  623.  
  624. !  Print the prompt, and read in the words and dictionary addresses
  625.  
  626.     L__M(##Prompt);
  627.     AfterPrompt();
  628.     #IFV5; DrawStatusLine(); #ENDIF;
  629.     KeyboardPrimitive(a_buffer, a_table);
  630.     nw=a_table->1;
  631.  
  632. !  If the line was blank, get a fresh line
  633.     if (nw == 0)
  634.     { L__M(##Miscellany,10); jump FreshInput; }
  635.  
  636. !  Unless the opening word was "oops", return
  637.  
  638.     w=a_table-->1;
  639.     if (w == OOPS1__WD or OOPS2__WD or OOPS3__WD) jump DoOops;
  640.  
  641. #IFV5;
  642. !  Undo handling
  643.  
  644.     if ((w == UNDO1__WD or UNDO2__WD or UNDO3__WD) && (parse->1==1))
  645.     {   if (turns==1)
  646.         {   L__M(##Miscellany,11); jump FreshInput;
  647.         }
  648.         if (undo_flag==0)
  649.         {   L__M(##Miscellany,6); jump FreshInput;
  650.         }
  651.         if (undo_flag==1) jump UndoFailed;
  652.         if (just_undone==1)
  653.         {   L__M(##Miscellany,12); jump FreshInput;
  654.         }
  655.         @restore_undo i;
  656.         if (i==0)
  657.         {   .UndoFailed;
  658.             L__M(##Miscellany,7);
  659.         }
  660.         jump FreshInput;
  661.     }
  662.     @save_undo i;
  663.     just_undone=0;
  664.     undo_flag=2;
  665.     if (i==-1) undo_flag=0;
  666.     if (i==0) undo_flag=1;
  667.     if (i==2)
  668.     {   style bold;
  669.         print (name) location, "^";
  670.         style roman;
  671.         L__M(##Miscellany,13);
  672.         just_undone=1;
  673.         jump FreshInput;
  674.     }
  675. #ENDIF;
  676.  
  677.     return nw;
  678.  
  679.     .DoOops;
  680.     if (oops_from == 0)
  681.     {   L__M(##Miscellany,14); jump FreshInput; }
  682.     if (nw == 1)
  683.     {   L__M(##Miscellany,15); jump FreshInput; }
  684.     if (nw > 2)
  685.     {   L__M(##Miscellany,16); jump FreshInput; }
  686.  
  687. !  So now we know: there was a previous mistake, and the player has
  688. !  attempted to correct a single word of it.
  689.  
  690.     for (i=0:i<=120:i++) buffer2->i = a_buffer->i;
  691.     x1 = a_table->9; ! Start of word following "oops"
  692.     x2 = a_table->8; ! Length of word following "oops"
  693.  
  694. !  Repair the buffer to the text that was in it before the "oops"
  695. !  was typed:
  696.  
  697.     for (i=0:i<64:i++) a_buffer->i = oops_workspace->i;
  698.     Tokenise__(a_buffer,a_table);
  699.  
  700. !  Work out the position in the buffer of the word to be corrected:
  701.  
  702.     w = a_table->(4*oops_from + 1); ! Start of word to go
  703.     w2 = a_table->(4*oops_from);    ! Length of word to go
  704.  
  705. !  Write spaces over the word to be corrected:
  706.  
  707.     for (i=0:i<w2:i++) a_buffer->(i+w) = ' ';
  708.  
  709.     if (w2 < x2)
  710.     {   ! If the replacement is longer than the original, move up...
  711.  
  712.         for (i=120:i>=w+x2:i--)
  713.             a_buffer->i = a_buffer->(i-x2+w2);
  714.  
  715.         ! ...increasing buffer size accordingly.
  716.  
  717.         a_buffer->1 = (a_buffer->1) + (x2-w2);
  718.     }
  719.  
  720. !  Write the correction in:
  721.  
  722.     for (i=0:i<x2:i++) a_buffer->(i+w) = buffer2->(i+x1);
  723.  
  724.     Tokenise__(a_buffer,a_table);
  725.     nw=a_table->1;
  726.  
  727.     return nw;
  728. ];
  729.  
  730. ! ----------------------------------------------------------------------------
  731. !  To simplify the picture a little, a rough map of the main routine:
  732. !
  733. !  (A)    Get the input, do "oops" and "again"
  734. !  (B)    Is it a direction, and so an implicit "go"?  If so go to (K)
  735. !  (C)    Is anyone being addressed?
  736. !  (D)    Get the verb: try all the syntax lines for that verb
  737. !  (E)    Break down a syntax line into analysed tokens
  738. !  (F)    Look ahead for advance warning for multiexcept/multiinside
  739. !  (G)    Parse each token in turn (calling ParseToken to do most of the work)
  740. !  (H)    Cheaply parse otherwise unrecognised conversation and return
  741. !  (I)    Print best possible error message
  742. !  (J)    Retry the whole lot
  743. !  (K)    Last thing: check for "then" and further instructions(s), return.
  744. !
  745. !  The strategic points (A) to (K) are marked in the commentary.
  746. !
  747. !  Note that there are three different places where a return can happen.
  748. ! ----------------------------------------------------------------------------
  749.  
  750. [ Parser__parse  results   syntax line num_lines line_address i j k
  751.                            token l m;
  752.  
  753. !  **** (A) ****
  754.  
  755. !  Firstly, in "not held" mode, we still have a command left over from last
  756. !  time (eg, the user typed "eat biscuit", which was parsed as "take biscuit"
  757. !  last time, with "eat biscuit" tucked away until now).  So we return that.
  758.  
  759.     if (notheld_mode==1)
  760.     {   for (i=0:i<8:i++) results-->i=kept_results-->i;
  761.         notheld_mode=0; rtrue;
  762.     }
  763.  
  764.     if (held_back_mode==1)
  765.     {   held_back_mode=0;
  766.         Tokenise__(buffer,parse);
  767.         jump ReParse;
  768.     }
  769.  
  770.   .ReType;
  771.  
  772.     Keyboard(buffer,parse);
  773.  
  774.   .ReParse;
  775.  
  776.     parser_inflection = name;
  777.  
  778. !  Initially assume the command is aimed at the player, and the verb
  779. !  is the first word
  780.  
  781.     num_words=parse->1;
  782.     wn=1;
  783. #ifdef LanguageToInformese;
  784.     LanguageToInformese();
  785. #ifv5;
  786. !   Re-tokenise:
  787.     Tokenise__(buffer,parse);
  788. #endif;
  789. #endif;
  790.  
  791.     BeforeParsing();
  792.     num_words=parse->1;
  793.  
  794.     k=0;
  795. #ifdef DEBUG;
  796.     if (parser_trace>=2)
  797.     {   print "[ ";
  798.         for (i=0:i<num_words:i++)
  799.         {   j=parse-->(i*2 + 1);
  800.             k=WordAddress(i+1);
  801.             l=WordLength(i+1);
  802.             print "~"; for (m=0:m<l:m++) print (char) k->m; print "~ ";
  803.  
  804.             if (j == 0) print "?";
  805.             else
  806.             {   if (UnsignedCompare(j, 0-->4)>=0
  807.                     && UnsignedCompare(j, 0-->2)<0) print (address) j;
  808.                 else print j;
  809.             }
  810.             if (i ~= num_words-1) print " / ";
  811.         }
  812.         print " ]^";
  813.     }
  814. #endif;
  815.     verb_wordnum=1;
  816.     actor=player;
  817.     actors_location = ScopeCeiling(player);
  818.     usual_grammar_after = 0;
  819.  
  820.   .AlmostReParse;
  821.  
  822.     scope_token = 0;
  823.     action_to_be = NULL;
  824.  
  825. !  Begin from what we currently think is the verb word
  826.  
  827.   .BeginCommand;
  828.     wn=verb_wordnum;
  829.     verb_word = NextWordStopped();
  830.  
  831. !  If there's no input here, we must have something like
  832. !  "person,".
  833.  
  834.     if (verb_word==-1)
  835.     {   best_etype = STUCK_PE; jump GiveError; }
  836.  
  837. !  Now try for "again" or "g", which are special cases:
  838. !  don't allow "again" if nothing has previously been typed;
  839. !  simply copy the previous text across
  840.  
  841.     if (verb_word==AGAIN2__WD or AGAIN3__WD) verb_word=AGAIN1__WD;
  842.     if (verb_word==AGAIN1__WD)
  843.     {   if (actor~=player)
  844.         {   L__M(##Miscellany,20); jump ReType; }
  845.         if (buffer3->1==0)
  846.         {   L__M(##Miscellany,21); jump ReType; }
  847.         for (i=0:i<120:i++) buffer->i=buffer3->i;
  848.         jump ReParse;
  849.     }
  850.  
  851. !  Save the present input in case of an "again" next time
  852.  
  853.     if (verb_word~=AGAIN1__WD)
  854.         for (i=0:i<120:i++) buffer3->i=buffer->i;
  855.  
  856.     if (usual_grammar_after==0)
  857.     {   i = RunRoutines(actor, grammar);
  858.         #ifdef DEBUG;
  859.         if (parser_trace>=2 && actor.grammar~=0 or NULL)
  860.             print " [Grammar property returned ", i, "]^";
  861.         #endif;
  862.         if (i<0) { usual_grammar_after = verb_wordnum; i=-i; }
  863.         if (i==1)
  864.         {   results-->0 = action;
  865.             results-->1 = noun;
  866.             results-->2 = second;
  867.             rtrue;
  868.         }
  869.         if (i~=0) { verb_word = i; wn--; verb_wordnum--; }
  870.         else
  871.         {   wn = verb_wordnum; verb_word=NextWord();
  872.         }
  873.     }
  874.     else usual_grammar_after=0;
  875.  
  876. !  **** (B) ****
  877.  
  878.     #ifdef LanguageIsVerb;
  879.     if (verb_word==0)
  880.     {   i = wn; verb_word=LanguageIsVerb(buffer, parse, verb_wordnum);
  881.         wn = i;
  882.     }
  883.     #endif;
  884.  
  885. !  If the first word is not listed as a verb, it must be a direction
  886. !  or the name of someone to talk to
  887.  
  888.     if (verb_word==0 || ((verb_word->#dict_par1) & 1) == 0)
  889.     {   
  890.  
  891. !  So is the first word an object contained in the special object "compass"
  892. !  (i.e., a direction)?  This needs use of NounDomain, a routine which
  893. !  does the object matching, returning the object number, or 0 if none found,
  894. !  or REPARSE_CODE if it has restructured the parse table so the whole parse
  895. !  must be begun again...
  896.  
  897.         wn=verb_wordnum; indef_mode = false; token_filter = 0;
  898.         l=NounDomain(compass,0,0); if (l==REPARSE_CODE) jump ReParse;
  899.  
  900. !  If it is a direction, send back the results:
  901. !  action=GoSub, no of arguments=1, argument 1=the direction.
  902.  
  903.         if (l~=0)
  904.         {   results-->0 = ##Go;
  905.             results-->1 = 1;
  906.             results-->2 = l;
  907.             jump LookForMore;
  908.         }
  909.  
  910. !  **** (C) ****
  911.  
  912. !  Only check for a comma (a "someone, do something" command) if we are
  913. !  not already in the middle of one.  (This simplification stops us from
  914. !  worrying about "robot, wizard, you are an idiot", telling the robot to
  915. !  tell the wizard that she is an idiot.)
  916.  
  917.         if (actor==player)
  918.         {   for (j=2:j<=num_words:j++)
  919.             {   i=NextWord(); if (i==comma_word) jump Conversation;
  920.             }
  921.  
  922.             verb_word=UnknownVerb(verb_word);
  923.             if (verb_word~=0) jump VerbAccepted;
  924.         }
  925.  
  926.         best_etype=VERB_PE; jump GiveError;
  927.  
  928. !  NextWord nudges the word number wn on by one each time, so we've now
  929. !  advanced past a comma.  (A comma is a word all on its own in the table.)
  930.  
  931.       .Conversation;
  932.         j=wn-1;
  933.         if (j==1) { L__M(##Miscellany,22); jump ReType; }
  934.  
  935. !  Use NounDomain (in the context of "animate creature") to see if the
  936. !  words make sense as the name of someone held or nearby
  937.  
  938.         wn=1; lookahead=HELD_TOKEN;
  939.         scope_reason = TALKING_REASON;
  940.         l=NounDomain(player,actors_location,6);
  941.         scope_reason = PARSING_REASON;
  942.         if (l==REPARSE_CODE) jump ReParse;
  943.  
  944.         if (l==0) { L__M(##Miscellany,23); jump ReType; }
  945.  
  946. !  The object addressed must at least be "talkable" if not actually "animate"
  947. !  (the distinction allows, for instance, a microphone to be spoken to,
  948. !  without the parser thinking that the microphone is human).
  949.  
  950.         if (l hasnt animate && l hasnt talkable)
  951.         {   L__M(##Miscellany, 24, l); jump ReType; }
  952.  
  953. !  Check that there aren't any mystery words between the end of the person's
  954. !  name and the comma (eg, throw out "dwarf sdfgsdgs, go north").
  955.  
  956.         if (wn~=j)
  957.         {   L__M(##Miscellany, 25); jump ReType; }
  958.  
  959. !  The player has now successfully named someone.  Adjust "him", "her", "it":
  960.  
  961.         PronounNotice(l);
  962.  
  963. !  Set the global variable "actor", adjust the number of the first word,
  964. !  and begin parsing again from there.
  965.  
  966.         verb_wordnum=j+1;
  967.  
  968. !  Stop things like "me, again":
  969.  
  970.         if (l == player)
  971.         {   wn = verb_wordnum;
  972.             if (NextWordStopped() == AGAIN1__WD or AGAIN2__WD or AGAIN3__WD)
  973.             {   L__M(##Miscellany,20); jump ReType;
  974.             }
  975.         }
  976.  
  977.         actor=l;
  978.         actors_location=ScopeCeiling(l);
  979.         #ifdef DEBUG;
  980.         if (parser_trace>=1)
  981.             print "[Actor is ", (the) actor, " in ",
  982.                 (name) actors_location, "]^";
  983.         #endif;
  984.         jump BeginCommand;
  985.     }
  986.  
  987. !  **** (D) ****
  988.  
  989.    .VerbAccepted;
  990.  
  991. !  We now definitely have a verb, not a direction, whether we got here by the
  992. !  "take ..." or "person, take ..." method.  Get the meta flag for this verb:
  993.  
  994.     meta=((verb_word->#dict_par1) & 2)/2;
  995.  
  996. !  You can't order other people to "full score" for you, and so on...
  997.  
  998.     if (meta==1 && actor~=player)
  999.     {   best_etype=VERB_PE; meta=0; jump GiveError; }
  1000.  
  1001. !  Now let i be the corresponding verb number, stored in the dictionary entry
  1002. !  (in a peculiar 255-n fashion for traditional Infocom reasons)...
  1003.  
  1004.     i=$ff-(verb_word->#dict_par2);
  1005.  
  1006. !  ...then look up the i-th entry in the verb table, whose address is at word
  1007. !  7 in the Z-machine (in the header), so as to get the address of the syntax
  1008. !  table for the given verb...
  1009.  
  1010.     syntax=(0-->7)-->i;
  1011.  
  1012. !  ...and then see how many lines (ie, different patterns corresponding to the
  1013. !  same verb) are stored in the parse table...
  1014.  
  1015.     num_lines=(syntax->0)-1;
  1016.  
  1017. !  ...and now go through them all, one by one.
  1018. !  To prevent pronoun_word 0 being misunderstood,
  1019.  
  1020.    pronoun_word=NULL; pronoun_obj=NULL;
  1021.  
  1022.    #ifdef DEBUG;
  1023.    if (parser_trace>=1)
  1024.    {    print "[Parsing for the verb '", (address) verb_word,
  1025.               "' (", num_lines+1, " lines)]^";
  1026.    }
  1027.    #endif;
  1028.  
  1029.    best_etype=STUCK_PE; nextbest_etype=STUCK_PE;
  1030.  
  1031. !  "best_etype" is the current failure-to-match error - it is by default
  1032. !  the least informative one, "don't understand that sentence".
  1033. !  "nextbest_etype" remembers the best alternative to having to ask a
  1034. !  scope token for an error message (i.e., the best not counting ASKSCOPE_PE).
  1035.  
  1036.  
  1037. !  **** (E) ****
  1038.  
  1039.     line_address = syntax + 1;
  1040.  
  1041.     for (line=0:line<=num_lines:line++)
  1042.     {   
  1043.         for (i = 0 : i < 32 : i++)
  1044.         {   line_token-->i = ENDIT_TOKEN;
  1045.             line_ttype-->i = ELEMENTARY_TT;
  1046.             line_tdata-->i = ENDIT_TOKEN;
  1047.         }
  1048.  
  1049. !  Unpack the syntax line from Inform format into three arrays; ensure that
  1050. !  the sequence of tokens ends in an ENDIT_TOKEN.
  1051.  
  1052.         line_address = UnpackGrammarLine(line_address);
  1053.             
  1054.         #ifdef DEBUG;
  1055.         if (parser_trace >= 1)
  1056.         {   if (parser_trace >= 2) new_line;
  1057.             print "[line ", line; DebugGrammarLine();
  1058.             print "]^";
  1059.         }
  1060.         #endif;
  1061.  
  1062. !  We aren't in "not holding" or inferring modes, and haven't entered
  1063. !  any parameters on the line yet, or any special numbers; the multiple
  1064. !  object is still empty.
  1065.  
  1066.         not_holding=0;
  1067.         inferfrom=0;
  1068.         parameters=0;
  1069.         nsns=0; special_word=0; special_number=0;
  1070.         multiple_object-->0 = 0;
  1071.         multi_context = 0;
  1072.         etype=STUCK_PE;
  1073.  
  1074. !  Put the word marker back to just after the verb
  1075.  
  1076.         wn=verb_wordnum+1;
  1077.  
  1078. !  **** (F) ****
  1079. !  There are two special cases where parsing a token now has to be
  1080. !  affected by the result of parsing another token later, and these
  1081. !  two cases (multiexcept and multiinside tokens) are helped by a quick
  1082. !  look ahead, to work out the future token now.  We can only carry this
  1083. !  out in the simple (but by far the most common) case:
  1084. !
  1085. !      multiexcept <one or more prepositions> noun
  1086. !
  1087. !  and similarly for multiinside.
  1088.  
  1089.         advance_warning = NULL; indef_mode = false;
  1090.         for (i=0,m=false,pcount=0:line_token-->pcount ~= ENDIT_TOKEN:pcount++)
  1091.         {   scope_token = 0;
  1092.  
  1093.             if (line_ttype-->pcount ~= PREPOSITION_TT) i++;
  1094.  
  1095.             if (line_ttype-->pcount == ELEMENTARY_TT)
  1096.             {   if (line_tdata-->pcount == MULTI_TOKEN) m=true;
  1097.                 if (line_tdata-->pcount
  1098.                     == MULTIEXCEPT_TOKEN or MULTIINSIDE_TOKEN  && i==1)
  1099.                 {   !   First non-preposition is "multiexcept" or
  1100.                     !   "multiinside", so look ahead.
  1101.  
  1102.                     #ifdef DEBUG;
  1103.                     if (parser_trace>=2) print " [Trying look-ahead]^";
  1104.                     #endif;
  1105.  
  1106.                     !   We need this to be followed by 1 or more prepositions.
  1107.  
  1108.                     pcount++;
  1109.                     if (line_ttype-->pcount == PREPOSITION_TT)
  1110.                     {   while (line_ttype-->pcount == PREPOSITION_TT)
  1111.                             pcount++;
  1112.  
  1113.                         if ((line_ttype-->pcount == ELEMENTARY_TT)
  1114.                             && (line_tdata-->pcount == NOUN_TOKEN))
  1115.                         {
  1116.                             !  Advance past the last preposition
  1117.  
  1118.                             while (wn <= num_words)
  1119.                             {   if (NextWord() == line_tdata-->(pcount-1))
  1120.                                 {   l = NounDomain(actors_location, actor,
  1121.                                             NOUN_TOKEN);
  1122.                                     #ifdef DEBUG;
  1123.                                     if (parser_trace>=2)
  1124.                                     {   print " [Advanced to ~noun~ token: ";
  1125.                                         if (l==REPARSE_CODE)
  1126.                                             print "re-parse request]^";
  1127.                                         if (l==1) print "but multiple found]^";
  1128.                                         if (l==0) print "error ", etype, "]^";
  1129.                                         if (l>=2) print (the) l, "]^";
  1130.                                     }
  1131.                                     #endif;
  1132.                                     if (l==REPARSE_CODE) jump ReParse;
  1133.                                     if (l>=2) advance_warning = l;
  1134.                                 }
  1135.                             }
  1136.                         }
  1137.                     }
  1138.                     break;
  1139.                 }
  1140.             }
  1141.         }
  1142.  
  1143. !  Slightly different line-parsing rules will apply to "take multi", to
  1144. !  prevent "take all" behaving correctly but misleadingly when there's
  1145. !  nothing to take.
  1146.  
  1147.         take_all_rule = 0;
  1148.         if (m && params_wanted==1 && action_to_be==##Take)
  1149.             take_all_rule = 1;
  1150.  
  1151. !  And now start again, properly, forearmed or not as the case may be.
  1152. !  As a precaution, we clear all the variables again (they may have been
  1153. !  disturbed by the call to NounDomain, which may have called outside
  1154. !  code, which may have done anything!).
  1155.  
  1156.         not_holding=0;
  1157.         inferfrom=0;
  1158.         parameters=0;
  1159.         nsns=0; special_word=0; special_number=0;
  1160.         multiple_object-->0 = 0;
  1161.         etype=STUCK_PE;
  1162.         wn=verb_wordnum+1;
  1163.  
  1164. !  **** (G) ****
  1165. !  "Pattern" gradually accumulates what has been recognised so far,
  1166. !  so that it may be reprinted by the parser later on
  1167.  
  1168.         for (pcount=1::pcount++)
  1169.         {   pattern-->pcount = PATTERN_NULL; scope_token=0;
  1170.  
  1171.             token = line_token-->(pcount-1);
  1172.             lookahead = line_token-->pcount;
  1173.  
  1174.             #ifdef DEBUG;
  1175.             if (parser_trace >= 2)
  1176.                print " [line ", line, " token ", pcount, " word ", wn, " : ",
  1177.                      (DebugToken) token, "]^";
  1178.             #endif;
  1179.  
  1180.             if (token ~= ENDIT_TOKEN)
  1181.             {   scope_reason = PARSING_REASON;
  1182.                 AnalyseToken(token);
  1183.                 l = ParseToken(found_ttype, found_tdata, pcount-1, token);
  1184.                 while (l<-200) l = ParseToken(ELEMENTARY_TT, l + 256);
  1185.                 scope_reason = PARSING_REASON;
  1186.  
  1187.                 if (l==GPR_PREPOSITION)
  1188.                 {   if (found_ttype~=PREPOSITION_TT
  1189.                         && (found_ttype~=ELEMENTARY_TT
  1190.                             || found_tdata~=TOPIC_TOKEN)) params_wanted--;
  1191.                     l = true;
  1192.                 }
  1193.                 else
  1194.                 if (l<0) l = false;
  1195.                 else
  1196.                 if (l~=GPR_REPARSE)
  1197.                 {   if (l==GPR_NUMBER)
  1198.                     {   if (nsns==0) special_number1=parsed_number;
  1199.                         else special_number2=parsed_number;
  1200.                         nsns++; l = 1;
  1201.                     }
  1202.                     if (l==GPR_MULTIPLE) l = 0;
  1203.                     results-->(parameters+2) = l;
  1204.                     parameters++;
  1205.                     pattern-->pcount = l;
  1206.                     l = true;
  1207.                 }
  1208.  
  1209.                 #ifdef DEBUG;
  1210.                 if (parser_trace >= 3)
  1211.                 {   print "  [token resulted in ";
  1212.                     if (l==REPARSE_CODE) print "re-parse request]^";
  1213.                     if (l==0) print "failure with error type ", etype, "]^";
  1214.                     if (l==1) print "success]^";
  1215.                 }
  1216.                 #endif;
  1217.  
  1218.                 if (l==REPARSE_CODE) jump ReParse;
  1219.                 if (l==false) break;
  1220.             }
  1221.             else
  1222.             {
  1223.  
  1224. !  If the player has entered enough already but there's still
  1225. !  text to wade through: store the pattern away so as to be able to produce
  1226. !  a decent error message if this turns out to be the best we ever manage,
  1227. !  and in the mean time give up on this line
  1228.  
  1229. !  However, if the superfluous text begins with a comma or "then" then
  1230. !  take that to be the start of another instruction
  1231.  
  1232.                 if (wn <= num_words)
  1233.                 {   l=NextWord();
  1234.                     if (l==THEN1__WD or THEN2__WD or THEN3__WD or comma_word)
  1235.                     {   held_back_mode=1; hb_wn=wn-1; }
  1236.                     else
  1237.                     {   for (m=0:m<32:m++) pattern2-->m=pattern-->m;
  1238.                         pcount2=pcount;
  1239.                         etype=UPTO_PE; break;
  1240.                     }
  1241.                 }
  1242.  
  1243. !  Now, we may need to revise the multiple object because of the single one
  1244. !  we now know (but didn't when the list was drawn up).
  1245.  
  1246.                 if (parameters>=1 && results-->2 == 0)
  1247.                 {   l=ReviseMulti(results-->3);
  1248.                     if (l~=0) { etype=l; break; }
  1249.                 }
  1250.                 if (parameters>=2 && results-->3 == 0)
  1251.                 {   l=ReviseMulti(results-->2);
  1252.                     if (l~=0) { etype=l; break; }
  1253.                 }
  1254.  
  1255. !  To trap the case of "take all" inferring only "yourself" when absolutely
  1256. !  nothing else is in the vicinity...
  1257.  
  1258.                 if (take_all_rule==2 && results-->2 == actor)
  1259.                 {   best_etype = NOTHING_PE; jump GiveError;
  1260.                 }
  1261.  
  1262.                 #ifdef DEBUG;
  1263.                 if (parser_trace>=1)
  1264.                     print "[Line successfully parsed]^";
  1265.                 #endif;
  1266.  
  1267. !  The line has successfully matched the text.  Declare the input error-free...
  1268.  
  1269.                 oops_from = 0;
  1270.  
  1271. !  ...explain any inferences made (using the pattern)...
  1272.  
  1273.                 if (inferfrom~=0)
  1274.                 {   print "("; PrintCommand(inferfrom); print ")^";
  1275.                 }
  1276.  
  1277. !  ...copy the action number, and the number of parameters...
  1278.  
  1279.                 results-->0 = action_to_be;
  1280.                 results-->1 = parameters;
  1281.  
  1282. !  ...reverse first and second parameters if need be...
  1283.  
  1284.                 if (action_reversed && parameters==2)
  1285.                 {   i = results-->2; results-->2 = results-->3;
  1286.                     results-->3 = i;
  1287.                     if (nsns == 2)
  1288.                     {   i = special_number1; special_number1=special_number2;
  1289.                         special_number2=i;
  1290.                     }
  1291.                 }
  1292.  
  1293. !  ...and to reset "it"-style objects to the first of these parameters, if
  1294. !  there is one (and it really is an object)...
  1295.  
  1296.                 if (parameters > 0 && results-->2 >= 2)
  1297.                     PronounNotice(results-->2);
  1298.  
  1299. !  ...and worry about the case where an object was allowed as a parameter
  1300. !  even though the player wasn't holding it and should have been: in this
  1301. !  event, keep the results for next time round, go into "not holding" mode,
  1302. !  and for now tell the player what's happening and return a "take" request
  1303. !  instead...
  1304.  
  1305.                 if (not_holding~=0 && actor==player)
  1306.                 {   notheld_mode=1;
  1307.                     for (i=0:i<8:i++) kept_results-->i = results-->i;
  1308.                     results-->0 = ##Take;
  1309.                     results-->1 = 1;
  1310.                     results-->2 = not_holding;
  1311.                     L__M(##Miscellany, 26, not_holding);
  1312.                 }
  1313.  
  1314. !  (Notice that implicit takes are only generated for the player, and not
  1315. !  for other actors.  This avoids entirely logical, but misleading, text
  1316. !  being printed.)
  1317.  
  1318. !  ...and return from the parser altogether, having successfully matched
  1319. !  a line.
  1320.  
  1321.                 if (held_back_mode==1) { wn=hb_wn; jump LookForMore; }
  1322.                 rtrue;
  1323.             }
  1324.         }
  1325.  
  1326. !  The line has failed to match.
  1327. !  We continue the outer "for" loop, trying the next line in the grammar.
  1328.  
  1329.         if (etype>best_etype) best_etype=etype;
  1330.         if (etype~=ASKSCOPE_PE && etype>nextbest_etype) nextbest_etype=etype;
  1331.  
  1332. !  ...unless the line was something like "take all" which failed because
  1333. !  nothing matched the "all", in which case we stop and give an error now.
  1334.  
  1335.         if (take_all_rule == 2 && etype==NOTHING_PE) break;
  1336.    }
  1337.  
  1338. !  The grammar is exhausted: every line has failed to match.
  1339.  
  1340. !  **** (H) ****
  1341.  
  1342.   .GiveError;
  1343.         etype=best_etype;
  1344.  
  1345. !  Errors are handled differently depending on who was talking.
  1346.  
  1347. !  If the command was addressed to somebody else (eg, "dwarf, sfgh") then
  1348. !  it is taken as conversation which the parser has no business in disallowing.
  1349.  
  1350.     if (actor~=player)
  1351.     {   if (usual_grammar_after>0)
  1352.         {   verb_wordnum = usual_grammar_after;
  1353.             jump AlmostReParse;
  1354.         }
  1355.         wn=verb_wordnum;
  1356.         special_word=NextWord();
  1357.         if (special_word==comma_word)
  1358.         {   special_word=NextWord();
  1359.             verb_wordnum++;
  1360.         }
  1361.         special_number=TryNumber(verb_wordnum);
  1362.         results-->0=##NotUnderstood;
  1363.         results-->1=2;
  1364.         results-->2=1; special_number1=special_word;
  1365.         results-->3=actor;
  1366.         consult_from = verb_wordnum; consult_words = num_words-consult_from+1;
  1367.         rtrue;
  1368.     }
  1369.  
  1370. !  **** (I) ****
  1371.  
  1372. !  If the player was the actor (eg, in "take dfghh") the error must be printed,
  1373. !  and fresh input called for.  In three cases the oops word must be jiggled.
  1374.  
  1375.     if (ParserError(etype)~=0) jump ReType;
  1376.     pronoun_word = pronoun__word; pronoun_obj = pronoun__obj;
  1377.  
  1378.     if (etype==STUCK_PE)   { L__M(##Miscellany, 27); oops_from=1; }
  1379.     if (etype==UPTO_PE)    { L__M(##Miscellany, 28);
  1380.                              for (m=0:m<32:m++) pattern-->m = pattern2-->m;
  1381.                              pcount=pcount2; PrintCommand(0); print ".^";
  1382.                            }
  1383.     if (etype==NUMBER_PE)  L__M(##Miscellany, 29);
  1384.     if (etype==CANTSEE_PE) { L__M(##Miscellany, 30); oops_from=saved_oops; }
  1385.     if (etype==TOOLIT_PE)  L__M(##Miscellany, 31);
  1386.     if (etype==NOTHELD_PE) { L__M(##Miscellany, 32); oops_from=saved_oops; }
  1387.     if (etype==MULTI_PE)   L__M(##Miscellany, 33);
  1388.     if (etype==MMULTI_PE)  L__M(##Miscellany, 34);
  1389.     if (etype==VAGUE_PE)   L__M(##Miscellany, 35);
  1390.     if (etype==EXCEPT_PE)  L__M(##Miscellany, 36);
  1391.     if (etype==ANIMA_PE)   L__M(##Miscellany, 37);
  1392.     if (etype==VERB_PE)    L__M(##Miscellany, 38);
  1393.     if (etype==SCENERY_PE) L__M(##Miscellany, 39);
  1394.     if (etype==ITGONE_PE)
  1395.     {   if (pronoun_obj == NULL) L__M(##Miscellany, 35);
  1396.                             else L__M(##Miscellany, 40);
  1397.     }
  1398.     if (etype==JUNKAFTER_PE) L__M(##Miscellany, 41);
  1399.     if (etype==TOOFEW_PE)  L__M(##Miscellany, 42, multi_had);
  1400.     if (etype==NOTHING_PE) { if (multi_wanted==100) L__M(##Miscellany, 43);
  1401.                              else L__M(##Miscellany, 44);  }
  1402.  
  1403.     if (etype==ASKSCOPE_PE)
  1404.     {   scope_stage=3;
  1405.         if (indirect(scope_error)==-1)
  1406.         {   best_etype=nextbest_etype; jump GiveError;  }
  1407.     }
  1408.  
  1409. !  **** (J) ****
  1410.  
  1411. !  And go (almost) right back to square one...
  1412.  
  1413.     jump ReType;
  1414.  
  1415. !  ...being careful not to go all the way back, to avoid infinite repetition
  1416. !  of a deferred command causing an error.
  1417.  
  1418.  
  1419. !  **** (K) ****
  1420.  
  1421. !  At this point, the return value is all prepared, and we are only looking
  1422. !  to see if there is a "then" followed by subsequent instruction(s).
  1423.     
  1424.    .LookForMore;
  1425.  
  1426.    if (wn>num_words) rtrue;
  1427.  
  1428.    i=NextWord();
  1429.    if (i==THEN1__WD or THEN2__WD or THEN3__WD or comma_word)
  1430.    {   if (wn>num_words)
  1431.        {   held_back_mode = false; return; }
  1432.        i = WordAddress(verb_wordnum);
  1433.        j = WordAddress(wn);
  1434.        for (:i<j:i++) i->0 = ' ';
  1435.        i = NextWord();
  1436.        if (i==AGAIN1__WD or AGAIN2__WD or AGAIN3__WD)
  1437.        {   !   Delete the words "then again" from the again buffer,
  1438.            !   in which we have just realised that it must occur:
  1439.            !   prevents an infinite loop on "i. again"
  1440.  
  1441.            i = WordAddress(wn-2)-buffer;
  1442.            if (wn > num_words) j = 119; else j = WordAddress(wn)-buffer;
  1443.            for (:i<j:i++) buffer3->i = ' ';
  1444.        }
  1445.        Tokenise__(buffer,parse); held_back_mode = true; return;
  1446.    }
  1447.    best_etype=UPTO_PE; jump GiveError;
  1448. ];
  1449.  
  1450. [ ScopeCeiling person act;
  1451.   act = parent(person); if (act == 0) return person;
  1452.   if (person == player && location == thedark) return thedark;
  1453.   while (parent(act)~=0
  1454.          && (act has transparent || act has supporter
  1455.              || (act has container && act has open)))
  1456.       act = parent(act);
  1457.   return act;
  1458. ];
  1459.  
  1460. ! ----------------------------------------------------------------------------
  1461. !  Descriptors()
  1462. !
  1463. !  Handles descriptive words like "my", "his", "another" and so on.
  1464. !  Skips "the", and leaves wn pointing to the first misunderstood word.
  1465. !
  1466. !  Allowed to set up for a plural only if allow_p is set
  1467. !
  1468. !  Returns error number, or 0 if no error occurred
  1469. ! ----------------------------------------------------------------------------
  1470.  
  1471. Constant OTHER_BIT  =   1;     !  These will be used in Adjudicate()
  1472. Constant MY_BIT     =   2;     !  to disambiguate choices
  1473. Constant THAT_BIT   =   4;
  1474. Constant PLURAL_BIT =   8;
  1475. Constant LIT_BIT    =  16;
  1476. Constant UNLIT_BIT  =  32;
  1477.  
  1478. [ ResetDescriptors;
  1479.    indef_mode=0; indef_type=0; indef_wanted=0; indef_guess_p=0;
  1480.    indef_possambig = false;
  1481.    indef_owner = nothing;
  1482.    indef_cases = $$111111111111;
  1483.    indef_nspec_at = 0;
  1484. ];
  1485.  
  1486. [ Descriptors allow_multiple  o x flag cto type n;
  1487.  
  1488.    ResetDescriptors();
  1489.    if (wn > num_words) return 0;
  1490.  
  1491.    for (flag=true:flag:)
  1492.    {   o=NextWordStopped(); flag=false;
  1493.  
  1494.        for (x=1:x<=LanguageDescriptors-->0:x=x+4)
  1495.            if (o == LanguageDescriptors-->x)
  1496.            {   flag = true;
  1497.                type = LanguageDescriptors-->(x+2);
  1498.                if (type ~= DEFART_PK) indef_mode = true;
  1499.                indef_possambig = true;
  1500.                indef_cases = indef_cases & (LanguageDescriptors-->(x+1));
  1501.  
  1502.                if (type == POSSESS_PK)
  1503.                {   cto = LanguageDescriptors-->(x+3);
  1504.                    switch(cto)
  1505.                    {  0: indef_type = indef_type | MY_BIT;
  1506.                       1: indef_type = indef_type | THAT_BIT;
  1507.                       default: indef_owner = PronounValue(cto);
  1508.                         if (indef_owner == NULL) indef_owner = InformParser;
  1509.                    }
  1510.                }
  1511.  
  1512.                if (type == light)
  1513.                    indef_type = indef_type | LIT_BIT;
  1514.                if (type == -light)
  1515.                    indef_type = indef_type | UNLIT_BIT;
  1516.            }
  1517.  
  1518.        if (o==OTHER1__WD or OTHER2__WD or OTHER3__WD)
  1519.                             { indef_mode=1; flag=1;
  1520.                               indef_type = indef_type | OTHER_BIT; }
  1521.        if (o==ALL1__WD or ALL2__WD or ALL3__WD or ALL4__WD or ALL5__WD)
  1522.                             { indef_mode=1; flag=1; indef_wanted=100;
  1523.                               if (take_all_rule == 1)
  1524.                                   take_all_rule = 2;
  1525.                               indef_type = indef_type | PLURAL_BIT; }
  1526.        if (allow_plurals && allow_multiple)
  1527.        {   n=NumberWord(o);
  1528.            if (n==1)        { indef_mode=1; flag=1; }
  1529.            if (n>1)         { indef_guess_p=1;
  1530.                               indef_mode=1; flag=1; indef_wanted=n;
  1531.                               indef_nspec_at=wn-1;
  1532.                               indef_type = indef_type | PLURAL_BIT; }
  1533.        }
  1534.        if (flag==1
  1535.            && NextWordStopped() ~= OF1__WD or OF2__WD or OF3__WD or OF4__WD)
  1536.            wn--;  ! Skip 'of' after these
  1537.    }
  1538.    wn--;
  1539.    if ((indef_wanted > 0) && (~~allow_multiple)) return MULTI_PE;
  1540.    return 0;
  1541. ];
  1542.  
  1543. ! ----------------------------------------------------------------------------
  1544. !  CreatureTest: Will this person do for a "creature" token?
  1545. ! ----------------------------------------------------------------------------
  1546.  
  1547. [ CreatureTest obj;
  1548.   if (obj has animate) rtrue;
  1549.   if (obj hasnt talkable) rfalse;
  1550.   if (action_to_be == ##Ask or ##Answer or ##Tell or ##AskFor) rtrue;
  1551.   rfalse;
  1552. ];
  1553.  
  1554. [ PrepositionChain wd index;
  1555.   if (line_tdata-->index == wd) return wd;
  1556.   if ((line_token-->index)->0 & $20 == 0) return -1;
  1557.   do
  1558.   {   if (line_tdata-->index == wd) return wd;
  1559.       index++;
  1560.   }
  1561.   until ((line_token-->index == ENDIT_TOKEN)
  1562.          || (((line_token-->index)->0 & $10) == 0));
  1563.   return -1;
  1564. ];
  1565.  
  1566. ! ----------------------------------------------------------------------------
  1567. !  ParseToken(type, data):
  1568. !      Parses the given token, from the current word number wn, with exactly
  1569. !      the specification of a general parsing routine.
  1570. !      (Except that for "topic" tokens and prepositions, you need to supply
  1571. !      a position in a valid grammar line as third argument.)
  1572. !
  1573. !  Returns:
  1574. !    GPR_REPARSE  for "reconstructed input, please re-parse from scratch"
  1575. !    GPR_PREPOSITION  for "token accepted with no result"
  1576. !    $ff00 + x    for "please parse ParseToken(ELEMENTARY_TT, x) instead"
  1577. !    0            for "token accepted, result is the multiple object list"
  1578. !    1            for "token accepted, result is the number in parsed_number"
  1579. !    object num   for "token accepted with this object as result"
  1580. !    -1           for "token rejected"
  1581. !
  1582. !  (A)            Analyse the token; handle all tokens not involving
  1583. !                 object lists and break down others into elementary tokens
  1584. !  (B)            Begin parsing an object list
  1585. !  (C)            Parse descriptors (articles, pronouns, etc.) in the list
  1586. !  (D)            Parse an object name
  1587. !  (E)            Parse connectives ("and", "but", etc.) and go back to (C)
  1588. !  (F)            Return the conclusion of parsing an object list
  1589. ! ----------------------------------------------------------------------------
  1590.  
  1591. [ ParseToken given_ttype given_tdata token_n
  1592.              token l o i j k and_parity single_object desc_wn many_flag
  1593.              token_allows_multiple;
  1594.  
  1595. !  **** (A) ****
  1596.  
  1597.    token_filter = 0;
  1598.    parser_inflection = name;
  1599.  
  1600.    switch(given_ttype)
  1601.    {   ELEMENTARY_TT:
  1602.            switch(given_tdata)
  1603.            {   SPECIAL_TOKEN:
  1604.                    l=TryNumber(wn);
  1605.                    special_word=NextWord();
  1606.                    #ifdef DEBUG;
  1607.                    if (l~=-1000)
  1608.                        if (parser_trace>=3)
  1609.                            print "  [Read special as the number ", l, "]^";
  1610.                    #endif;
  1611.                    if (l==-1000)
  1612.                    {   #ifdef DEBUG;
  1613.                        if (parser_trace>=3)
  1614.                          print "  [Read special word at word number ", wn, "]^";
  1615.                        #endif;
  1616.                        l = special_word;
  1617.                    }
  1618.                    parsed_number = l; return 1;
  1619.  
  1620.                NUMBER_TOKEN:
  1621.                    l=TryNumber(wn++);
  1622.                    if (l==-1000) { etype=NUMBER_PE; rfalse; }
  1623.                    #ifdef DEBUG;
  1624.                    if (parser_trace>=3) print "  [Read number as ", l, "]^";
  1625.                    #endif;
  1626.                    parsed_number = l; return 1;
  1627.  
  1628.                CREATURE_TOKEN:
  1629.                    if (action_to_be==##Answer or ##Ask or ##AskFor or ##Tell)
  1630.                        scope_reason = TALKING_REASON;
  1631.  
  1632.                TOPIC_TOKEN:
  1633.                    consult_from = wn;
  1634.                    if ((line_ttype-->(token_n+1) ~= PREPOSITION_TT)
  1635.                        && (line_token-->(token_n+1) ~= ENDIT_TOKEN))
  1636.                        RunTimeError(13);
  1637.                    do o=NextWordStopped();
  1638.                    until (o==-1 || PrepositionChain(o, token_n+1) ~= -1);
  1639.                    wn--;
  1640.                    consult_words = wn-consult_from;
  1641.                    if (consult_words==0) return GPR_PREPOSITION;
  1642.                    if (action_to_be==##Ask or ##Answer or ##Tell)
  1643.                    {   o=wn; wn=consult_from; parsed_number=NextWord();
  1644.                        #IFDEF EnglishNaturalLanguage;
  1645.                        if (parsed_number=='the' && consult_words>1)
  1646.                            parsed_number=NextWord();
  1647.                        #ENDIF;
  1648.                        wn=o; return 1;
  1649.                    }
  1650.                    return GPR_PREPOSITION;
  1651.            }
  1652.  
  1653.        PREPOSITION_TT:
  1654.            #Iffalse Grammar__Version==1;
  1655. !  Is it an unnecessary alternative preposition, when a previous choice
  1656. !  has already been matched?
  1657.            if ((token->0) & $10) return GPR_PREPOSITION;
  1658.            #Endif;
  1659.  
  1660. !  If we've run out of the player's input, but still have parameters to
  1661. !  specify, we go into "infer" mode, remembering where we are and the
  1662. !  preposition we are inferring...
  1663.  
  1664.            if (wn > num_words)
  1665.            {   if (inferfrom==0 && parameters<params_wanted)
  1666.                {   inferfrom = pcount; inferword = token;
  1667.                    pattern-->pcount = REPARSE_CODE + Dword__No(given_tdata);
  1668.                }
  1669.  
  1670. !  If we are not inferring, then the line is wrong...
  1671.  
  1672.                if (inferfrom==0) return -1;
  1673.  
  1674. !  If not, then the line is right but we mark in the preposition...
  1675.  
  1676.                pattern-->pcount = REPARSE_CODE + Dword__No(given_tdata);
  1677.                return GPR_PREPOSITION;
  1678.            }
  1679.  
  1680.            o = NextWord();
  1681.  
  1682.            pattern-->pcount = REPARSE_CODE + Dword__No(o);
  1683.  
  1684. !  Whereas, if the player has typed something here, see if it is the
  1685. !  required preposition... if it's wrong, the line must be wrong,
  1686. !  but if it's right, the token is passed (jump to finish this token).
  1687.  
  1688.            if (o == given_tdata) return GPR_PREPOSITION;
  1689.            #Iffalse Grammar__Version==1;
  1690.            if (PrepositionChain(o, token_n) ~= -1)
  1691.                return GPR_PREPOSITION;
  1692.            #Endif;
  1693.            return -1;
  1694.  
  1695.        GPR_TT:
  1696.            l=indirect(given_tdata);
  1697.            #ifdef DEBUG;
  1698.            if (parser_trace>=3)
  1699.                print "  [Outside parsing routine returned ", l, "]^";
  1700.            #endif;
  1701.            return l;
  1702.  
  1703.        SCOPE_TT:
  1704.            scope_token = given_tdata;
  1705.            scope_stage = 1;
  1706.            l = indirect(scope_token);
  1707.            #ifdef DEBUG;
  1708.            if (parser_trace>=3)
  1709.                print "  [Scope routine returned multiple-flag of ", l, "]^";
  1710.            #endif;
  1711.            if (l==1) given_tdata = MULTI_TOKEN; else given_tdata = NOUN_TOKEN;
  1712.  
  1713.        ATTR_FILTER_TT:
  1714.            token_filter = 1 + given_tdata;
  1715.            given_tdata = NOUN_TOKEN;
  1716.  
  1717.        ROUTINE_FILTER_TT:
  1718.            token_filter = given_tdata;
  1719.            given_tdata = NOUN_TOKEN;
  1720.    }
  1721.  
  1722.    token = given_tdata;
  1723.  
  1724. !  **** (B) ****
  1725.  
  1726. !  There are now three possible ways we can be here:
  1727. !      parsing an elementary token other than "special" or "number";
  1728. !      parsing a scope token;
  1729. !      parsing a noun-filter token (either by routine or attribute).
  1730. !
  1731. !  In each case, token holds the type of elementary parse to
  1732. !  perform in matching one or more objects, and
  1733. !  token_filter is 0 (default), an attribute + 1 for an attribute filter
  1734. !  or a routine address for a routine filter.
  1735.  
  1736.    token_allows_multiple = false;
  1737.    if (token == MULTI_TOKEN or MULTIHELD_TOKEN or MULTIEXCEPT_TOKEN
  1738.                 or MULTIINSIDE_TOKEN) token_allows_multiple = true;
  1739.  
  1740.    many_flag = false; and_parity = true; dont_infer = false;
  1741.  
  1742. !  **** (C) ****
  1743. !  We expect to find a list of objects next in what the player's typed.
  1744.  
  1745.   .ObjectList;
  1746.  
  1747.    #ifdef DEBUG;
  1748.    if (parser_trace>=3) print "  [Object list from word ", wn, "]^";
  1749.    #endif;
  1750.  
  1751. !  Take an advance look at the next word: if it's "it" or "them", and these
  1752. !  are unset, set the appropriate error number and give up on the line
  1753. !  (if not, these are still parsed in the usual way - it is not assumed
  1754. !  that they still refer to something in scope)
  1755.  
  1756.     o=NextWord(); wn--;
  1757.  
  1758.     pronoun_word = NULL; pronoun_obj = NULL;
  1759.     l = PronounValue(o);
  1760.     if (l ~= 0)
  1761.     {   pronoun_word = o; pronoun_obj = l;
  1762.         if (l == NULL)
  1763.         {   !   Don't assume this is a use of an unset pronoun until the
  1764.             !   descriptors have been checked, because it might be an
  1765.             !   article (or some such) instead
  1766.  
  1767.             for (l=1:l<=LanguageDescriptors-->0:l=l+4)
  1768.                 if (o == LanguageDescriptors-->l) jump AssumeDescriptor;
  1769.             pronoun__word=pronoun_word; pronoun__obj=pronoun_obj;
  1770.             etype=VAGUE_PE; return 0;
  1771.         }
  1772.     }
  1773.  
  1774.     .AssumeDescriptor;
  1775.  
  1776.     if (o==ME1__WD or ME2__WD or ME3__WD)
  1777.     {   pronoun_word = o; pronoun_obj = player;
  1778.     }
  1779.  
  1780.     allow_plurals = true; desc_wn = wn;
  1781.  
  1782.     .TryAgain;
  1783. !   First, we parse any descriptive words (like "the", "five" or "every"):
  1784.     l = Descriptors(token_allows_multiple); if (l~=0) { etype=l; return 0; }
  1785.  
  1786.     .TryAgain2;
  1787.  
  1788. !  **** (D) ****
  1789.  
  1790. !  This is an actual specified object, and is therefore where a typing error
  1791. !  is most likely to occur, so we set:
  1792.  
  1793.     oops_from = wn;
  1794.  
  1795. !  So, two cases.  Case 1: token not equal to "held" (so, no implicit takes)
  1796. !  but we may well be dealing with multiple objects
  1797.  
  1798. !  In either case below we use NounDomain, giving it the token number as
  1799. !  context, and two places to look: among the actor's possessions, and in the
  1800. !  present location.  (Note that the order depends on which is likeliest.)
  1801.  
  1802.     if (token ~= HELD_TOKEN)
  1803.     {   i=multiple_object-->0;
  1804.         #ifdef DEBUG;
  1805.         if (parser_trace>=3)
  1806.             print "  [Calling NounDomain on location and actor]^";
  1807.         #endif;
  1808.         l=NounDomain(actors_location, actor, token);
  1809.         if (l==REPARSE_CODE) return l;                  ! Reparse after Q&A
  1810.         if (l==0) {   if (indef_possambig)
  1811.                       {   ResetDescriptors(); wn = desc_wn; jump TryAgain2; }
  1812.                       etype=CantSee(); jump FailToken; } ! Choose best error
  1813.  
  1814.         #ifdef DEBUG;
  1815.         if (parser_trace>=3)
  1816.         {   if (l>1)
  1817.                 print "  [ND returned ", (the) l, "]^";
  1818.             else
  1819.             {   print "  [ND appended to the multiple object list:^";
  1820.                 k=multiple_object-->0;
  1821.                 for (j=i+1:j<=k:j++)
  1822.                     print "  Entry ", j, ": ", (The) multiple_object-->j,
  1823.                           " (", multiple_object-->j, ")^";
  1824.                 print "  List now has size ", k, "]^";
  1825.             }
  1826.         }
  1827.         #endif;
  1828.  
  1829.         if (l==1)
  1830.         {   if (~~many_flag)
  1831.             {   many_flag = true;
  1832.             }
  1833.             else                                  ! Merge with earlier ones
  1834.             {   k=multiple_object-->0;            ! (with either parity)
  1835.                 multiple_object-->0 = i;
  1836.                 for (j=i+1:j<=k:j++)
  1837.                 {   if (and_parity) MultiAdd(multiple_object-->j);
  1838.                     else MultiSub(multiple_object-->j);
  1839.                 }
  1840.                 #ifdef DEBUG;
  1841.                 if (parser_trace>=3)
  1842.                     print "  [Merging ", k-i, " new objects to the ",
  1843.                         i, " old ones]^";
  1844.                 #endif;
  1845.             }
  1846.         }
  1847.         else
  1848.         {   ! A single object was indeed found
  1849.  
  1850.             if (match_length == 0 && indef_possambig)
  1851.             {   !   So the answer had to be inferred from no textual data,
  1852.                 !   and we know that there was an ambiguity in the descriptor
  1853.                 !   stage (such as a word which could be a pronoun being
  1854.                 !   parsed as an article or possessive).  It's worth having
  1855.                 !   another go.
  1856.  
  1857.                 ResetDescriptors(); wn = desc_wn; jump TryAgain2;
  1858.             }
  1859.         
  1860.             if (token==CREATURE_TOKEN && CreatureTest(l)==0)
  1861.             {   etype=ANIMA_PE; jump FailToken; } !  Animation is required
  1862.  
  1863.             if (~~many_flag)
  1864.                 single_object = l;
  1865.             else
  1866.             {   if (and_parity) MultiAdd(l); else MultiSub(l);
  1867.                 #ifdef DEBUG;
  1868.                 if (parser_trace>=3)
  1869.                     print "  [Combining ", (the) l, " with list]^";
  1870.                 #endif;
  1871.             }
  1872.         }
  1873.     }
  1874.  
  1875. !  Case 2: token is "held" (which fortunately can't take multiple objects)
  1876. !  and may generate an implicit take
  1877.  
  1878.     else
  1879.  
  1880.     {   l=NounDomain(actor,actors_location,token);       ! Same as above...
  1881.         if (l==REPARSE_CODE) return l;
  1882.         if (l==0)
  1883.         {   if (indef_possambig)
  1884.             {   ResetDescriptors(); wn = desc_wn; jump TryAgain2; }
  1885.             etype=CantSee(); return l;                   ! Choose best error
  1886.         }
  1887.  
  1888. !  ...until it produces something not held by the actor.  Then an implicit
  1889. !  take must be tried.  If this is already happening anyway, things are too
  1890. !  confused and we have to give up (but saving the oops marker so as to get
  1891. !  it on the right word afterwards).
  1892. !  The point of this last rule is that a sequence like
  1893. !
  1894. !      > read newspaper
  1895. !      (taking the newspaper first)
  1896. !      The dwarf unexpectedly prevents you from taking the newspaper!
  1897. !
  1898. !  should not be allowed to go into an infinite repeat - read becomes
  1899. !  take then read, but take has no effect, so read becomes take then read...
  1900. !  Anyway for now all we do is record the number of the object to take.
  1901.  
  1902.         o=parent(l);
  1903.         if (o~=actor)
  1904.         {   if (notheld_mode==1)
  1905.             {   saved_oops=oops_from; etype=NOTHELD_PE; jump FailToken;
  1906.             }
  1907.             not_holding = l;
  1908.             #ifdef DEBUG;
  1909.             if (parser_trace>=3)
  1910.                 print "  [Allowing object ", (the) l, " for now]^";
  1911.             #endif;
  1912.         }
  1913.         single_object = l;
  1914.     }
  1915.  
  1916. !  The following moves the word marker to just past the named object...
  1917.  
  1918.     wn = oops_from + match_length;
  1919.  
  1920. !  **** (E) ****
  1921.  
  1922. !  Object(s) specified now: is that the end of the list, or have we reached
  1923. !  "and", "but" and so on?  If so, create a multiple-object list if we
  1924. !  haven't already (and are allowed to).
  1925.  
  1926.     .NextInList;
  1927.  
  1928.     o=NextWord();
  1929.  
  1930.     if (o==AND1__WD or AND2__WD or AND3__WD or BUT1__WD or BUT2__WD or BUT3__WD
  1931.            or comma_word)
  1932.     {
  1933.         #ifdef DEBUG;
  1934.         if (parser_trace>=3) print "  [Read connective '", (address) o, "']^";
  1935.         #endif;
  1936.  
  1937.         if (~~token_allows_multiple)
  1938.         {   etype=MULTI_PE; jump FailToken;
  1939.         }
  1940.  
  1941.         if (o==BUT1__WD or BUT2__WD or BUT3__WD) and_parity = 1-and_parity;
  1942.  
  1943.         if (~~many_flag)
  1944.         {   multiple_object-->0 = 1;
  1945.             multiple_object-->1 = single_object;
  1946.             many_flag = true;
  1947.             #ifdef DEBUG;
  1948.             if (parser_trace>=3)
  1949.                 print "  [Making new list from ", (the) single_object, "]^";
  1950.             #endif;
  1951.         }
  1952.         dont_infer = true; inferfrom=0;           ! Don't print (inferences)
  1953.         jump ObjectList;                          ! And back around
  1954.     }
  1955.  
  1956.     wn--;   ! Word marker back to first not-understood word
  1957.  
  1958. !  **** (F) ****
  1959.  
  1960. !  Happy or unhappy endings:
  1961.  
  1962.     .PassToken;
  1963.  
  1964.     if (many_flag)
  1965.     {   single_object = GPR_MULTIPLE;
  1966.         multi_context = token;
  1967.     }
  1968.     else
  1969.     {   if (indef_mode==1 && indef_type & PLURAL_BIT ~= 0)
  1970.         {   if (indef_wanted<100 && indef_wanted>1)
  1971.             {   multi_had=1; multi_wanted=indef_wanted;
  1972.                 etype=TOOFEW_PE;
  1973.                 jump FailToken;
  1974.             }
  1975.         }
  1976.     }
  1977.     return single_object;
  1978.  
  1979.     .FailToken;
  1980.  
  1981. !  If we were only guessing about it being a plural, try again but only
  1982. !  allowing singulars (so that words like "six" are not swallowed up as
  1983. !  Descriptors)
  1984.  
  1985.     if (allow_plurals && indef_guess_p==1)
  1986.     {   allow_plurals=false; wn=desc_wn; jump TryAgain;
  1987.     }
  1988.     return -1;
  1989. ];
  1990.  
  1991. ! ----------------------------------------------------------------------------
  1992. !  NounDomain does the most substantial part of parsing an object name.
  1993. !
  1994. !  It is given two "domains" - usually a location and then the actor who is
  1995. !  looking - and a context (i.e. token type), and returns:
  1996. !
  1997. !   0    if no match at all could be made,
  1998. !   1    if a multiple object was made,
  1999. !   k    if object k was the one decided upon,
  2000. !   REPARSE_CODE if it asked a question of the player and consequently rewrote
  2001. !        the player's input, so that the whole parser should start again
  2002. !        on the rewritten input.
  2003. !
  2004. !   In the case when it returns 1<k<REPARSE_CODE, it also sets the variable
  2005. !   length_of_noun to the number of words in the input text matched to the
  2006. !   noun.
  2007. !   In the case k=1, the multiple objects are added to multiple_object by
  2008. !   hand (not by MultiAdd, because we want to allow duplicates).
  2009. ! ----------------------------------------------------------------------------
  2010.  
  2011. [ NounDomain domain1 domain2 context    first_word i j k l
  2012.                                         answer_words marker;
  2013.  
  2014. #ifdef DEBUG;
  2015.   if (parser_trace>=4)
  2016.   {   print "   [NounDomain called at word ", wn, "^";
  2017.       print "   ";
  2018.       if (indef_mode)
  2019.       {   print "seeking indefinite object: ";
  2020.           if (indef_type & OTHER_BIT)  print "other ";
  2021.           if (indef_type & MY_BIT)     print "my ";
  2022.           if (indef_type & THAT_BIT)   print "that ";
  2023.           if (indef_type & PLURAL_BIT) print "plural ";
  2024.           if (indef_type & LIT_BIT)    print "lit ";
  2025.           if (indef_type & UNLIT_BIT)  print "unlit ";
  2026.           if (indef_owner ~= 0) print "owner:", (name) indef_owner;
  2027.           new_line;
  2028.           print "   number wanted: ";
  2029.           if (indef_wanted == 100) print "all"; else print indef_wanted;
  2030.           new_line;
  2031.           print "   most likely GNAs of names: ", indef_cases, "^";
  2032.       }
  2033.       else print "seeking definite object^";
  2034.   }
  2035. #endif;
  2036.  
  2037.   match_length=0; number_matched=0; match_from=wn; placed_in_flag=0;
  2038.  
  2039.   SearchScope(domain1, domain2, context);
  2040.  
  2041. #ifdef DEBUG;
  2042.   if (parser_trace>=4) print "   [ND made ", number_matched, " matches]^";
  2043. #endif;
  2044.  
  2045.   wn=match_from+match_length;
  2046.  
  2047. !  If nothing worked at all, leave with the word marker skipped past the
  2048. !  first unmatched word...
  2049.  
  2050.   if (number_matched==0) { wn++; rfalse; }
  2051.  
  2052. !  Suppose that there really were some words being parsed (i.e., we did
  2053. !  not just infer).  If so, and if there was only one match, it must be
  2054. !  right and we return it...
  2055.  
  2056.   if (match_from <= num_words)
  2057.   {   if (number_matched==1) { i=match_list-->0; return i; }
  2058.  
  2059. !  ...now suppose that there was more typing to come, i.e. suppose that
  2060. !  the user entered something beyond this noun.  If nothing ought to follow,
  2061. !  then there must be a mistake, (unless what does follow is just a full
  2062. !  stop, and or comma)
  2063.  
  2064.       if (wn<=num_words)
  2065.       {   i=NextWord(); wn--;
  2066.           if (i ~=  AND1__WD or AND2__WD or AND3__WD or comma_word
  2067.                  or THEN1__WD or THEN2__WD or THEN3__WD
  2068.                  or BUT1__WD or BUT2__WD or BUT3__WD)
  2069.           {   if (lookahead==ENDIT_TOKEN) rfalse;
  2070.           }
  2071.       }
  2072.   }
  2073.  
  2074. !  Now look for a good choice, if there's more than one choice...
  2075.  
  2076.   number_of_classes=0;
  2077.   
  2078.   if (number_matched==1) i=match_list-->0;
  2079.   if (number_matched>1)
  2080.   {   i=Adjudicate(context);
  2081.       if (i==-1) rfalse;
  2082.       if (i==1) rtrue;       !  Adjudicate has made a multiple
  2083.                              !  object, and we pass it on
  2084.   }
  2085.  
  2086. !  If i is non-zero here, one of two things is happening: either
  2087. !  (a) an inference has been successfully made that object i is
  2088. !      the intended one from the user's specification, or
  2089. !  (b) the user finished typing some time ago, but we've decided
  2090. !      on i because it's the only possible choice.
  2091. !  In either case we have to keep the pattern up to date,
  2092. !  note that an inference has been made and return.
  2093. !  (Except, we don't note which of a pile of identical objects.)
  2094.  
  2095.   if (i~=0)
  2096.   {   if (dont_infer) return i;
  2097.       if (inferfrom==0) inferfrom=pcount;
  2098.       pattern-->pcount = i;
  2099.       return i;
  2100.   }
  2101.  
  2102. !  If we get here, there was no obvious choice of object to make.  If in
  2103. !  fact we've already gone past the end of the player's typing (which
  2104. !  means the match list must contain every object in scope, regardless
  2105. !  of its name), then it's foolish to give an enormous list to choose
  2106. !  from - instead we go and ask a more suitable question...
  2107.  
  2108.   if (match_from > num_words) jump Incomplete;
  2109.  
  2110. !  Now we print up the question, using the equivalence classes as worked
  2111. !  out by Adjudicate() so as not to repeat ourselves on plural objects...
  2112.  
  2113.   if (context==CREATURE_TOKEN)
  2114.       L__M(##Miscellany, 45); else L__M(##Miscellany, 46);
  2115.  
  2116.   j=number_of_classes; marker=0;
  2117.   for (i=1:i<=number_of_classes:i++)
  2118.   {   
  2119.       while (((match_classes-->marker) ~= i)
  2120.              && ((match_classes-->marker) ~= -i)) marker++;
  2121.       k=match_list-->marker;
  2122.  
  2123.       if (match_classes-->marker > 0) print (the) k; else print (a) k;
  2124.  
  2125.       if (i<j-1)  print ", ";
  2126.       if (i==j-1) print (string) OR__TX;
  2127.   }
  2128.   print "?^";
  2129.  
  2130. !  ...and get an answer:
  2131.  
  2132.   .WhichOne;
  2133.   for (i=2:i<120:i++) buffer2->i=' ';
  2134.   answer_words=Keyboard(buffer2, parse2);
  2135.  
  2136.   first_word=(parse2-->1);
  2137.  
  2138. !  Take care of "all", because that does something too clever here to do
  2139. !  later on:
  2140.  
  2141.   if (first_word == ALL1__WD or ALL2__WD or ALL3__WD or ALL4__WD or ALL5__WD)
  2142.   {   
  2143.       if (context == MULTI_TOKEN or MULTIHELD_TOKEN or MULTIEXCEPT_TOKEN
  2144.                      or MULTIINSIDE_TOKEN)
  2145.       {   l=multiple_object-->0;
  2146.           for (i=0:i<number_matched && l+i<63:i++)
  2147.           {   k=match_list-->i;
  2148.               multiple_object-->(i+1+l) = k;
  2149.           }
  2150.           multiple_object-->0 = i+l;
  2151.           rtrue;
  2152.       }
  2153.       L__M(##Miscellany, 47);
  2154.       jump WhichOne;
  2155.   }
  2156.  
  2157. !  If the first word of the reply can be interpreted as a verb, then
  2158. !  assume that the player has ignored the question and given a new
  2159. !  command altogether.
  2160. !  (This is one time when it's convenient that the directions are
  2161. !  not themselves verbs - thus, "north" as a reply to "Which, the north
  2162. !  or south door" is not treated as a fresh command but as an answer.)
  2163.  
  2164.   #ifdef LanguageIsVerb;
  2165.   if (first_word==0)
  2166.   {   j = wn; first_word=LanguageIsVerb(buffer2, parse2, 1); wn = j;
  2167.   }
  2168.   #endif;
  2169.   if (first_word ~= 0)
  2170.   {   j=first_word->#dict_par1;
  2171.       if ((0~=j&1) && (first_word ~= 'long' or 'short' or 'normal'
  2172.                                      or 'brief' or 'full' or 'verbose'))
  2173.       {   CopyBuffer(buffer, buffer2);
  2174.           return REPARSE_CODE;
  2175.       }
  2176.   }
  2177.  
  2178. !  Now we insert the answer into the original typed command, as
  2179. !  words additionally describing the same object
  2180. !  (eg, > take red button
  2181. !       Which one, ...
  2182. !       > music
  2183. !  becomes "take music red button".  The parser will thus have three
  2184. !  words to work from next time, not two.)
  2185.  
  2186.   k = WordAddress(match_from) - buffer; l=buffer2->1+1;
  2187.   for (j=buffer + buffer->0 - 1: j>= buffer+k+l: j--)
  2188.       j->0 = 0->(j-l);
  2189.   for (i=0:i<l:i++) buffer->(k+i) = buffer2->(2+i);
  2190.   buffer->(k+l-1) = ' ';
  2191.   buffer->1 = buffer->1 + l;
  2192.   if (buffer->1 >= (buffer->0 - 1)) buffer->1 = buffer->0;
  2193.  
  2194. !  Having reconstructed the input, we warn the parser accordingly
  2195. !  and get out.
  2196.  
  2197.   return REPARSE_CODE;
  2198.  
  2199. !  Now we come to the question asked when the input has run out
  2200. !  and can't easily be guessed (eg, the player typed "take" and there
  2201. !  were plenty of things which might have been meant).
  2202.  
  2203.   .Incomplete;
  2204.  
  2205.   if (context==CREATURE_TOKEN)
  2206.       L__M(##Miscellany, 48); else L__M(##Miscellany, 49);
  2207.  
  2208.   for (i=2:i<120:i++) buffer2->i=' ';
  2209.   answer_words=Keyboard(buffer2, parse2);
  2210.  
  2211.   first_word=(parse2-->1);
  2212.   #ifdef LanguageIsVerb;
  2213.   if (first_word==0)
  2214.   {   j = wn; first_word=LanguageIsVerb(buffer2, parse2, 1); wn = j;
  2215.   }
  2216.   #endif;
  2217.  
  2218. !  Once again, if the reply looks like a command, give it to the
  2219. !  parser to get on with and forget about the question...
  2220.  
  2221.   if (first_word ~= 0)
  2222.   {   j=first_word->#dict_par1;
  2223.       if (0~=j&1)
  2224.       {   CopyBuffer(buffer, buffer2);
  2225.           return REPARSE_CODE;
  2226.       }
  2227.   }
  2228.  
  2229. !  ...but if we have a genuine answer, then:
  2230. !
  2231. !  (1) we must glue in text suitable for anything that's been inferred.
  2232.  
  2233.   if (inferfrom ~= 0)
  2234.   {   for (j = inferfrom: j<pcount: j++)
  2235.       {   if (pattern-->j == PATTERN_NULL) continue;
  2236.           i=2+buffer->1; (buffer->1)++; buffer->(i++) = ' ';
  2237.     
  2238.           if (parser_trace >= 5)
  2239.           print "[Gluing in inference with pattern code ", pattern-->j, "]^";
  2240.  
  2241.           parse2-->1 = 0;
  2242.  
  2243.           ! An inferred object.  Best we can do is glue in a pronoun.
  2244.           ! (This is imperfect, but it's very seldom needed anyway.)
  2245.     
  2246.           if (pattern-->j >= 2 && pattern-->j < REPARSE_CODE)
  2247.           {   PronounNotice(pattern-->j);
  2248.               for (k=1: k<=LanguagePronouns-->0: k=k+3)
  2249.                   if (pattern-->j == LanguagePronouns-->(k+2))
  2250.                   {   parse2-->1 = LanguagePronouns-->k;
  2251.                       if (parser_trace >= 5)
  2252.                       print "[Using pronoun '", (address) parse2-->1, "']^";
  2253.                       break;
  2254.                   }
  2255.           }
  2256.           else
  2257.           {   ! An inferred preposition.
  2258.               parse2-->1 = No__Dword(pattern-->j - REPARSE_CODE);
  2259.               if (parser_trace >= 5)
  2260.                   print "[Using preposition '", (address) parse2-->1, "']^";
  2261.           }
  2262.     
  2263.           ! parse2-->1 now holds the dictionary address of the word to glue in.
  2264.     
  2265.           if (parse2-->1 ~= 0)
  2266.           {   k = buffer + i;
  2267.               @output_stream 3 k;
  2268.               print (address) parse2-->1;
  2269.               @output_stream -3;
  2270.               k = k-->0;
  2271.               for (l=i:l<i+k:l++) buffer->l = buffer->(l+2);
  2272.               i = i + k; buffer->1 = i-2;
  2273.           }
  2274.       }
  2275.   }
  2276.  
  2277. !  (2) we must glue the newly-typed text onto the end.
  2278.  
  2279.   i=2+buffer->1; (buffer->1)++; buffer->(i++) = ' ';
  2280.   for (j=0: j<buffer2->1: i++, j++)
  2281.   {   buffer->i = buffer2->(j+2);
  2282.       (buffer->1)++;
  2283.       if (buffer->1 == 120) break;
  2284.   }    
  2285.  
  2286. !  (3) we fill up the buffer with spaces, which is unnecessary, but may
  2287. !      help incorrectly-written interpreters to cope.
  2288.  
  2289.   for (:i<120:i++) buffer->i = ' ';
  2290.  
  2291.   return REPARSE_CODE;
  2292. ];
  2293.  
  2294. ! ----------------------------------------------------------------------------
  2295. !  The Adjudicate routine tries to see if there is an obvious choice, when
  2296. !  faced with a list of objects (the match_list) each of which matches the
  2297. !  player's specification equally well.
  2298. !
  2299. !  To do this it makes use of the context (the token type being worked on).
  2300. !  It counts up the number of obvious choices for the given context
  2301. !  (all to do with where a candidate is, except for 6 (animate) which is to
  2302. !  do with whether it is animate or not);
  2303. !
  2304. !  if only one obvious choice is found, that is returned;
  2305. !
  2306. !  if we are in indefinite mode (don't care which) one of the obvious choices
  2307. !    is returned, or if there is no obvious choice then an unobvious one is
  2308. !    made;
  2309. !
  2310. !  at this stage, we work out whether the objects are distinguishable from
  2311. !    each other or not: if they are all indistinguishable from each other,
  2312. !    then choose one, it doesn't matter which;
  2313. !
  2314. !  otherwise, 0 (meaning, unable to decide) is returned (but remember that
  2315. !    the equivalence classes we've just worked out will be needed by other
  2316. !    routines to clear up this mess, so we can't economise on working them
  2317. !    out).
  2318. !
  2319. !  Returns -1 if an error occurred
  2320. ! ----------------------------------------------------------------------------
  2321.  
  2322. [ Adjudicate context i j k good_ones last n ultimate flag offset;
  2323.  
  2324. #ifdef DEBUG;
  2325.   if (parser_trace>=4)
  2326.   {   print "   [Adjudicating match list of size ", number_matched,
  2327.           " in context ", context, "^";
  2328.       print "   ";
  2329.       if (indef_mode)
  2330.       {   print "indefinite type: ";
  2331.           if (indef_type & OTHER_BIT)  print "other ";
  2332.           if (indef_type & MY_BIT)     print "my ";
  2333.           if (indef_type & THAT_BIT)   print "that ";
  2334.           if (indef_type & PLURAL_BIT) print "plural ";
  2335.           if (indef_type & LIT_BIT)    print "lit ";
  2336.           if (indef_type & UNLIT_BIT)  print "unlit ";
  2337.           if (indef_owner ~= 0) print "owner:", (name) indef_owner;
  2338.           new_line;
  2339.           print "   number wanted: ";
  2340.           if (indef_wanted == 100) print "all"; else print indef_wanted;
  2341.           new_line;
  2342.           print "   most likely GNAs of names: ", indef_cases, "^";
  2343.       }
  2344.       else print "definite object^";
  2345.   }
  2346. #endif;
  2347.  
  2348.   j=number_matched-1; good_ones=0; last=match_list-->0;
  2349.   for (i=0:i<=j:i++)
  2350.   {   n=match_list-->i;
  2351.       match_scores-->i = good_ones;
  2352.  
  2353.       ultimate=ScopeCeiling(n);
  2354.  
  2355. !      if (context==NOUN_TOKEN && ultimate==ScopeCeiling(actor)
  2356. !          && n~=actor && n hasnt concealed && n hasnt scenery &&
  2357. !          (token_filter==0 || UserFilter(n)==1)) { good_ones++; last=n; }
  2358.       if (context==HELD_TOKEN && parent(n)==actor)
  2359.       {   good_ones++; last=n; }
  2360.       if (context==MULTI_TOKEN && ultimate==ScopeCeiling(actor)
  2361.           && n~=actor && n hasnt concealed && n hasnt scenery) 
  2362.       {   good_ones++; last=n; }
  2363.       if (context==MULTIHELD_TOKEN && parent(n)==actor)
  2364.       {   good_ones++; last=n; }
  2365.  
  2366.       if (context==MULTIEXCEPT_TOKEN or MULTIINSIDE_TOKEN)
  2367.       {   if (advance_warning==-1)
  2368.           {   if (context==MULTIEXCEPT_TOKEN)
  2369.               {   good_ones++; last=n;
  2370.               }
  2371.               if (context==MULTIINSIDE_TOKEN)
  2372.               {   if (parent(n)~=actor) { good_ones++; last=n; }
  2373.               }
  2374.           }
  2375.           else
  2376.           {   if (context==MULTIEXCEPT_TOKEN && n~=advance_warning)
  2377.               {   good_ones++; last=n; }
  2378.               if (context==MULTIINSIDE_TOKEN && n in advance_warning)
  2379.               {   good_ones++; last=n; }
  2380.           }
  2381.       }
  2382.       if (context==CREATURE_TOKEN && CreatureTest(n)==1)
  2383.       {   good_ones++; last=n; }
  2384.  
  2385.       match_scores-->i = 1000*(good_ones - match_scores-->i);
  2386.   }
  2387.   if (good_ones==1) return last;
  2388.  
  2389.   ! If there is ambiguity about what was typed, but it definitely wasn't
  2390.   ! animate as required, then return anything; higher up in the parser
  2391.   ! a suitable error will be given.  (This prevents a question being asked.)
  2392.   !
  2393.   if (context==CREATURE_TOKEN && good_ones==0) return match_list-->0;
  2394.  
  2395.   if (indef_mode==0) indef_type=0;
  2396.  
  2397.   ScoreMatchL();
  2398.   if (number_matched == 0) return -1;
  2399.  
  2400.   if (indef_mode == 0)
  2401.   {   !  Is there now a single highest-scoring object?
  2402.       i = SingleBestGuess();
  2403.       if (i >= 0)
  2404.       {   
  2405. #ifdef DEBUG;
  2406.           if (parser_trace>=4)
  2407.               print "   Single best-scoring object returned.]^";
  2408. #endif;
  2409.           return i;
  2410.       }
  2411.   }
  2412.  
  2413.   if (indef_mode==1 && indef_type & PLURAL_BIT ~= 0)
  2414.   {   if (context ~= MULTI_TOKEN or MULTIHELD_TOKEN or MULTIEXCEPT_TOKEN
  2415.                      or MULTIINSIDE_TOKEN)
  2416.       {   etype=MULTI_PE; return -1; }
  2417.       i=0; offset=multiple_object-->0;
  2418.       for (j=BestGuess():j~=-1 && i<indef_wanted
  2419.            && i+offset<63:j=BestGuess())
  2420.       {   flag=0;
  2421.           if (j hasnt concealed && j hasnt worn) flag=1;
  2422.           if (context==MULTIHELD_TOKEN or MULTIEXCEPT_TOKEN
  2423.               && parent(j)~=actor) flag=0;
  2424.           k=ChooseObjects(j,flag);
  2425.           if (k==1) flag=1; else { if (k==2) flag=0; }
  2426.           if (flag==1)
  2427.           {   i++; multiple_object-->(i+offset) = j;
  2428. #ifdef DEBUG;
  2429.               if (parser_trace>=4) print "   Accepting it^";
  2430. #endif;
  2431.           }
  2432.           else
  2433.           {   i=i;
  2434. #ifdef DEBUG;
  2435.               if (parser_trace>=4) print "   Rejecting it^";
  2436. #endif;
  2437.           }
  2438.       }
  2439.       if (i<indef_wanted && indef_wanted<100)
  2440.       {   etype=TOOFEW_PE; multi_wanted=indef_wanted;
  2441.           multi_had=i;
  2442.           return -1;
  2443.       }
  2444.       multiple_object-->0 = i+offset;
  2445.       multi_context=context;
  2446. #ifdef DEBUG;
  2447.       if (parser_trace>=4)
  2448.           print "   Made multiple object of size ", i, "]^";
  2449. #endif;
  2450.       return 1;
  2451.   }
  2452.  
  2453.   for (i=0:i<number_matched:i++) match_classes-->i=0;
  2454.  
  2455.   n=1;
  2456.   for (i=0:i<number_matched:i++)
  2457.       if (match_classes-->i==0)
  2458.       {   match_classes-->i=n++; flag=0;
  2459.           for (j=i+1:j<number_matched:j++)
  2460.               if (match_classes-->j==0
  2461.                   && Identical(match_list-->i, match_list-->j)==1)
  2462.               {   flag=1;
  2463.                   match_classes-->j=match_classes-->i;
  2464.               }
  2465.           if (flag==1) match_classes-->i = 1-n;
  2466.       }
  2467.   n--; number_of_classes = n;
  2468.  
  2469. #ifdef DEBUG;
  2470.   if (parser_trace>=4)
  2471.   {   print "   Grouped into ", n, " possibilities by name:^";
  2472.       for (i=0:i<number_matched:i++)
  2473.           if (match_classes-->i > 0)
  2474.               print "   ", (The) match_list-->i,
  2475.                   " (", match_list-->i, ")  ---  group ",
  2476.                   match_classes-->i, "^";
  2477.   }
  2478. #endif;
  2479.  
  2480.   if (indef_mode == 0)
  2481.   {   if (n > 1)
  2482.       {   k = -1;
  2483.           for (i=0:i<number_matched:i++)
  2484.           {   if (match_scores-->i > k)
  2485.               {   k = match_scores-->i;
  2486.                   j = match_classes-->i; j=j*j;
  2487.                   flag = 0;
  2488.               }
  2489.               else
  2490.               if (match_scores-->i == k)
  2491.               {   if ((match_classes-->i) * (match_classes-->i) ~= j)
  2492.                       flag = 1;
  2493.               }
  2494.           }
  2495.           if (flag)
  2496.           {
  2497. #ifdef DEBUG;
  2498.               if (parser_trace>=4)
  2499.                   print "   Unable to choose best group, so ask player.]^";
  2500. #endif;
  2501.               return 0;
  2502.           }
  2503. #ifdef DEBUG;
  2504.           if (parser_trace>=4)
  2505.               print "   Best choices are all from the same group.^";
  2506. #endif;          
  2507.       }
  2508.   }
  2509.  
  2510. !  When the player is really vague, or there's a single collection of
  2511. !  indistinguishable objects to choose from, choose the one the player
  2512. !  most recently acquired, or if the player has none of them, then
  2513. !  the one most recently put where it is.
  2514.  
  2515.   if (n==1) dont_infer = true;
  2516.   return BestGuess();
  2517. ];
  2518.  
  2519. ! ----------------------------------------------------------------------------
  2520. !  ReviseMulti  revises the multiple object which already exists, in the
  2521. !    light of information which has come along since then (i.e., the second
  2522. !    parameter).  It returns a parser error number, or else 0 if all is well.
  2523. !    This only ever throws things out, never adds new ones.
  2524. ! ----------------------------------------------------------------------------
  2525.  
  2526. [ ReviseMulti second_p  i low;
  2527.  
  2528. #ifdef DEBUG;
  2529.   if (parser_trace>=4)
  2530.       print "   Revising multiple object list of size ", multiple_object-->0,
  2531.             " with 2nd ", (name) second_p, "^";
  2532. #endif;
  2533.  
  2534.   if (multi_context==MULTIEXCEPT_TOKEN or MULTIINSIDE_TOKEN)
  2535.   {   for (i=1, low=0:i<=multiple_object-->0:i++)
  2536.       {   if ( (multi_context==MULTIEXCEPT_TOKEN
  2537.                 && multiple_object-->i ~= second_p)
  2538.                || (multi_context==MULTIINSIDE_TOKEN
  2539.                    && multiple_object-->i in second_p))
  2540.           {   low++; multiple_object-->low = multiple_object-->i;
  2541.           }
  2542.       }
  2543.       multiple_object-->0 = low;
  2544.   }
  2545.  
  2546.   if (multi_context==MULTI_TOKEN && action_to_be == ##Take)
  2547.   {   for (i=1, low=0:i<=multiple_object-->0:i++)
  2548.           if (ScopeCeiling(multiple_object-->i)==ScopeCeiling(actor))
  2549.               low++;
  2550. #ifdef DEBUG;
  2551.       if (parser_trace>=4)
  2552.           print "   Token 2 plural case: number with actor ", low, "^";
  2553. #endif;
  2554.       if (take_all_rule==2 || low>0)
  2555.       {   for (i=1, low=0:i<=multiple_object-->0:i++)
  2556.           {   if (ScopeCeiling(multiple_object-->i)==ScopeCeiling(actor))
  2557.               {   low++; multiple_object-->low = multiple_object-->i;
  2558.               }
  2559.           }
  2560.           multiple_object-->0 = low;
  2561.       }
  2562.   }
  2563.  
  2564.   i=multiple_object-->0;
  2565. #ifdef DEBUG;
  2566.   if (parser_trace>=4)
  2567.       print "   Done: new size ", i, "^";
  2568. #endif;
  2569.   if (i==0) return NOTHING_PE;
  2570.   return 0;
  2571. ];
  2572.  
  2573. ! ----------------------------------------------------------------------------
  2574. !  ScoreMatchL  scores the match list for quality in terms of what the
  2575. !  player has vaguely asked for.  Points are awarded for conforming with
  2576. !  requirements like "my", and so on.  Remove from the match list any
  2577. !  entries which fail the basic requirements of the descriptors.
  2578. ! ----------------------------------------------------------------------------
  2579.  
  2580. [ ScoreMatchL  its_owner its_score obj i j threshold a_s l_s;
  2581.  
  2582. !  if (indef_type & OTHER_BIT ~= 0) threshold=45;
  2583.   if (indef_type & MY_BIT ~= 0)    threshold=threshold+45;
  2584.   if (indef_type & THAT_BIT ~= 0)  threshold=threshold+45;
  2585.   if (indef_type & LIT_BIT ~= 0)   threshold=threshold+45;
  2586.   if (indef_type & UNLIT_BIT ~= 0) threshold=threshold+45;
  2587.   if (indef_owner ~= nothing)      threshold=threshold+45;
  2588.  
  2589. #ifdef DEBUG;
  2590.   if (parser_trace>=4) print "   Scoring match list: indef mode ", indef_mode,
  2591.       " type ", indef_type,
  2592.       ", threshold ", threshold, ":^";
  2593. #endif;
  2594.  
  2595.   a_s = 30; l_s = 20;
  2596.   if (action_to_be == ##Take or ##Remove) { a_s=20; l_s=30; }
  2597.  
  2598.   for (i=0:i<number_matched:i++)
  2599.   {   obj = match_list-->i; its_owner = parent(obj); its_score=0;
  2600.  
  2601. !      if (indef_type & OTHER_BIT ~=0
  2602. !          &&  obj~=itobj or himobj or herobj)
  2603. !          its_score=its_score+45;
  2604.       if (indef_type & MY_BIT ~=0  &&  its_owner==actor)
  2605.           its_score=its_score+45;
  2606.       if (indef_type & THAT_BIT ~=0  &&  its_owner==actors_location)
  2607.           its_score=its_score+45;
  2608.       if (indef_type & LIT_BIT ~=0  &&  obj has light)
  2609.           its_score=its_score+45;
  2610.       if (indef_type & UNLIT_BIT ~=0  &&  obj hasnt light)
  2611.           its_score=its_score+45;
  2612.       if (indef_owner~=0 && its_owner == indef_owner)
  2613.           its_score=its_score+45;
  2614.  
  2615.       if (its_score < threshold)
  2616.       {
  2617. #ifdef DEBUG;
  2618.           if (parser_trace >= 4)
  2619.               print "   ", (The) match_list-->i,
  2620.                     " (", match_list-->i, ") in ", (the) its_owner,
  2621.                     " is rejected (doesn't match descriptors)^";
  2622. #endif;
  2623.           match_list-->i=-1;
  2624.       }
  2625.       else
  2626.       {   !   Now forget all of that: the object passes descriptors
  2627.           !   and we can score it properly.
  2628.  
  2629.           its_score = 0;
  2630.           if (obj hasnt concealed) its_score=50;
  2631.  
  2632.           if (its_owner==actor)   its_score = its_score+a_s;
  2633.           else
  2634.           if (its_owner==actors_location) its_score = its_score+l_s;
  2635.           else
  2636.           if (its_score==0 && its_owner~=compass) its_score = its_score+10;
  2637.  
  2638.           !   The entry point "ChooseObjects" can contribute 0 to 9 points
  2639.           !   to refine the score
  2640.           its_score = its_score + ChooseObjects(obj,2);
  2641.  
  2642.           !   A small bonus for having the correct GNA,
  2643.           !   for sorting out ambiguous articles and the like.
  2644.  
  2645.           its_score = 2*its_score;
  2646.           if (indef_cases & (PowersOfTwo_TB-->(GetGNAOfObject(obj))))
  2647.               its_score++;
  2648.  
  2649.           match_scores-->i = match_scores-->i + its_score;
  2650. #ifdef DEBUG;
  2651.           if (parser_trace >= 4)
  2652.               print "     ", (The) match_list-->i,
  2653.                     " (", match_list-->i, ") in ", (the) its_owner,
  2654.                     " : ", match_scores-->i, " points^";
  2655. #endif;
  2656.       }
  2657.   }
  2658.  
  2659.   for (i=0:i<number_matched:i++)
  2660.   {   while (match_list-->i == -1)
  2661.       {   if (i == number_matched-1) { number_matched--; break; }
  2662.           for (j=i:j<number_matched:j++)
  2663.           {   match_list-->j = match_list-->(j+1);
  2664.               match_scores-->j = match_scores-->(j+1);              
  2665.           }
  2666.           number_matched--;
  2667.       }
  2668.   }
  2669. ];
  2670.  
  2671. ! ----------------------------------------------------------------------------
  2672. !  BestGuess makes the best guess it can out of the match list, assuming that
  2673. !  everything in the match list is textually as good as everything else;
  2674. !  however it ignores items marked as -1, and so marks anything it chooses.
  2675. !  It returns -1 if there are no possible choices.
  2676. ! ----------------------------------------------------------------------------
  2677.  
  2678. [ BestGuess  earliest its_score best i;
  2679.  
  2680.   earliest=0; best=-1;
  2681.   for (i=0:i<number_matched:i++)
  2682.   {   if (match_list-->i >= 0)
  2683.       {   its_score=match_scores-->i;
  2684.           if (its_score>best) { best=its_score; earliest=i; }
  2685.       }
  2686.   }
  2687. #ifdef DEBUG;
  2688.   if (parser_trace>=4)
  2689.   {   if (best<0)
  2690.           print "   Best guess ran out of choices^";
  2691.       else
  2692.           print "   Best guess ", (the) match_list-->earliest,
  2693.                 " (", match_list-->earliest, ")^";
  2694.   }
  2695. #endif;
  2696.   if (best<0) return -1;
  2697.   i=match_list-->earliest;
  2698.   match_list-->earliest=-1;
  2699.   return i;
  2700. ];
  2701.  
  2702. ! ----------------------------------------------------------------------------
  2703. !  SingleBestGuess returns the highest-scoring object in the match list
  2704. !  if it is the clear winner, or returns -1 if there is no clear winner
  2705. ! ----------------------------------------------------------------------------
  2706.  
  2707. [ SingleBestGuess  earliest its_score best i;
  2708.  
  2709.   earliest=-1; best=-1000;
  2710.   for (i=0:i<number_matched:i++)
  2711.   {   its_score=match_scores-->i;
  2712.       if (its_score==best) { earliest = -1; }
  2713.       if (its_score>best) { best=its_score; earliest=match_list-->i; }
  2714.   }
  2715.   return earliest;
  2716. ];
  2717.  
  2718. ! ----------------------------------------------------------------------------
  2719. !  Identical decides whether or not two objects can be distinguished from
  2720. !  each other by anything the player can type.  If not, it returns true.
  2721. ! ----------------------------------------------------------------------------
  2722.  
  2723. [ Identical o1 o2 p1 p2 n1 n2 i j flag;
  2724.  
  2725.   if (o1==o2) rtrue;  ! This should never happen, but to be on the safe side
  2726.   if (o1==0 || o2==0) rfalse;  ! Similarly
  2727.   if (parent(o1)==compass || parent(o2)==compass) rfalse; ! Saves time
  2728.  
  2729. !  What complicates things is that o1 or o2 might have a parsing routine,
  2730. !  so the parser can't know from here whether they are or aren't the same.
  2731. !  If they have different parsing routines, we simply assume they're
  2732. !  different.  If they have the same routine (which they probably got from
  2733. !  a class definition) then the decision process is as follows:
  2734. !
  2735. !     the routine is called (with self being o1, not that it matters)
  2736. !       with noun and second being set to o1 and o2, and action being set
  2737. !       to the fake action TheSame.  If it returns -1, they are found
  2738. !       identical; if -2, different; and if >=0, then the usual method
  2739. !       is used instead.
  2740.  
  2741.   if (o1.parse_name~=0 || o2.parse_name~=0)
  2742.   {   if (o1.parse_name ~= o2.parse_name) rfalse;
  2743.       parser_action=##TheSame; parser_one=o1; parser_two=o2;
  2744.       j=wn; i=RunRoutines(o1,parse_name); wn=j;
  2745.       if (i==-1) rtrue; if (i==-2) rfalse;
  2746.   }
  2747.  
  2748. !  This is the default algorithm: do they have the same words in their
  2749. !  "name" (i.e. property no. 1) properties.  (Note that the following allows
  2750. !  for repeated words and words in different orders.)
  2751.  
  2752.   p1 = o1.&1; n1 = (o1.#1)/2;
  2753.   p2 = o2.&1; n2 = (o2.#1)/2;
  2754.  
  2755. !  for (i=0:i<n1:i++) { print (address) p1-->i, " "; } new_line;
  2756. !  for (i=0:i<n2:i++) { print (address) p2-->i, " "; } new_line;
  2757.  
  2758.   for (i=0:i<n1:i++)
  2759.   {   flag=0;
  2760.       for (j=0:j<n2:j++)
  2761.           if (p1-->i == p2-->j) flag=1;
  2762.       if (flag==0) rfalse;
  2763.   }
  2764.  
  2765.   for (j=0:j<n2:j++)
  2766.   {   flag=0;
  2767.       for (i=0:i<n1:i++)
  2768.           if (p1-->i == p2-->j) flag=1;
  2769.       if (flag==0) rfalse;
  2770.   }
  2771.  
  2772. !  print "Which are identical!^";
  2773.   rtrue;
  2774. ];
  2775.  
  2776. ! ----------------------------------------------------------------------------
  2777. !  PrintCommand reconstructs the command as it presently reads, from
  2778. !  the pattern which has been built up
  2779. !
  2780. !  If from is 0, it starts with the verb: then it goes through the pattern.
  2781. !  The other parameter is "emptyf" - a flag: if 0, it goes up to pcount:
  2782. !  if 1, it goes up to pcount-1.
  2783. !
  2784. !  Note that verbs and prepositions are printed out of the dictionary:
  2785. !  and that since the dictionary may only preserve the first six characters
  2786. !  of a word (in a V3 game), we have to hand-code the longer words needed.
  2787. !
  2788. !  (Recall that pattern entries are 0 for "multiple object", 1 for "special
  2789. !  word", 2 to REPARSE_CODE-1 are object numbers and REPARSE_CODE+n means the
  2790. !  preposition n)
  2791. ! ----------------------------------------------------------------------------
  2792.  
  2793. [ PrintCommand from i k spacing_flag;
  2794.  
  2795.   if (from==0)
  2796.   {   i=verb_word;
  2797.       if (LanguageVerb(i) == 0)
  2798.           if (PrintVerb(i) == 0)
  2799.               print (address) i;
  2800.       from++; spacing_flag = true;
  2801.   }
  2802.  
  2803.   for (k=from:k<pcount:k++)
  2804.   {   i=pattern-->k;
  2805.       if (i == PATTERN_NULL) continue;
  2806.       if (spacing_flag) print (char) ' ';
  2807.       if (i==0) { print (string) THOSET__TX; jump TokenPrinted; }
  2808.       if (i==1) { print (string) THAT__TX; jump TokenPrinted; }
  2809.       if (i>=REPARSE_CODE) print (address) No__Dword(i-REPARSE_CODE);
  2810.       else print (the) i;
  2811.       .TokenPrinted;
  2812.       spacing_flag = true;
  2813.   }
  2814. ];
  2815.  
  2816. ! ----------------------------------------------------------------------------
  2817. !  The CantSee routine returns a good error number for the situation where
  2818. !  the last word looked at didn't seem to refer to any object in context.
  2819. !
  2820. !  The idea is that: if the actor is in a location (but not inside something
  2821. !  like, for instance, a tank which is in that location) then an attempt to
  2822. !  refer to one of the words listed as meaningful-but-irrelevant there
  2823. !  will cause "you don't need to refer to that in this game" rather than
  2824. !  "no such thing" or "what's 'it'?".
  2825. !  (The advantage of not having looked at "irrelevant" local nouns until now
  2826. !  is that it stops them from clogging up the ambiguity-resolving process.
  2827. !  Thus game objects always triumph over scenery.)
  2828. ! ----------------------------------------------------------------------------
  2829.  
  2830. [ CantSee  i w e;
  2831.     saved_oops=oops_from;
  2832.  
  2833.     if (scope_token~=0) { scope_error = scope_token; return ASKSCOPE_PE; }
  2834.  
  2835.     wn--; w=NextWord();
  2836.     e=CANTSEE_PE;
  2837.     if (w==pronoun_word)
  2838.     {   pronoun__word=pronoun_word; pronoun__obj=pronoun_obj;
  2839.         e=ITGONE_PE;
  2840.     }
  2841.     i=actor; while (parent(i) ~= 0) i = parent(i);
  2842.     if (i has visited && Refers(i,wn-1)==1) e=SCENERY_PE;
  2843.     if (etype>e) return etype;
  2844.     return e;
  2845. ];
  2846.  
  2847. ! ----------------------------------------------------------------------------
  2848. !  The MultiAdd routine adds object "o" to the multiple-object-list.
  2849. !
  2850. !  This is only allowed to hold 63 objects at most, at which point it ignores
  2851. !  any new entries (and sets a global flag so that a warning may later be
  2852. !  printed if need be).
  2853. ! ----------------------------------------------------------------------------
  2854.  
  2855. [ MultiAdd o i j;
  2856.   i=multiple_object-->0;
  2857.   if (i==63) { toomany_flag=1; rtrue; }
  2858.   for (j=1:j<=i:j++)
  2859.       if (o==multiple_object-->j) 
  2860.           rtrue;
  2861.   i++;
  2862.   multiple_object-->i = o;
  2863.   multiple_object-->0 = i;
  2864. ];
  2865.  
  2866. ! ----------------------------------------------------------------------------
  2867. !  The MultiSub routine deletes object "o" from the multiple-object-list.
  2868. !
  2869. !  It returns 0 if the object was there in the first place, and 9 (because
  2870. !  this is the appropriate error number in Parser()) if it wasn't.
  2871. ! ----------------------------------------------------------------------------
  2872.  
  2873. [ MultiSub o i j k et;
  2874.   i=multiple_object-->0; et=0;
  2875.   for (j=1:j<=i:j++)
  2876.       if (o==multiple_object-->j)
  2877.       {   for (k=j:k<=i:k++)
  2878.               multiple_object-->k = multiple_object-->(k+1);
  2879.           multiple_object-->0 = --i;
  2880.           return et;
  2881.       }
  2882.   et=9; return et;
  2883. ];
  2884.  
  2885. ! ----------------------------------------------------------------------------
  2886. !  The MultiFilter routine goes through the multiple-object-list and throws
  2887. !  out anything without the given attribute "attr" set.
  2888. ! ----------------------------------------------------------------------------
  2889.  
  2890. [ MultiFilter attr  i j o;
  2891.   .MFiltl;
  2892.   i=multiple_object-->0;
  2893.   for (j=1:j<=i:j++)
  2894.   {   o=multiple_object-->j;
  2895.       if (o hasnt attr) { MultiSub(o); jump Mfiltl; }
  2896.   }
  2897. ];
  2898.  
  2899. ! ----------------------------------------------------------------------------
  2900. !  The UserFilter routine consults the user's filter (or checks on attribute)
  2901. !  to see what already-accepted nouns are acceptable
  2902. ! ----------------------------------------------------------------------------
  2903.  
  2904. [ UserFilter obj;
  2905.  
  2906.   if (token_filter > 0 && token_filter < 49)
  2907.   {   if (obj has (token_filter-1)) rtrue;
  2908.       rfalse;
  2909.   }
  2910.   noun = obj;
  2911.   return indirect(token_filter);
  2912. ];
  2913.  
  2914. ! ----------------------------------------------------------------------------
  2915. !  MoveWord copies word at2 from parse buffer b2 to word at1 in "parse"
  2916. !  (the main parse buffer)
  2917. ! ----------------------------------------------------------------------------
  2918.  
  2919. [ MoveWord at1 b2 at2 x y;
  2920.   x=at1*2-1; y=at2*2-1;
  2921.   parse-->x++ = b2-->y++;
  2922.   parse-->x = b2-->y;
  2923. ];
  2924.  
  2925. ! ----------------------------------------------------------------------------
  2926. !  SearchScope  domain1 domain2 context
  2927. !
  2928. !  Works out what objects are in scope (possibly asking an outside routine),
  2929. !  but does not look at anything the player has typed.
  2930. ! ----------------------------------------------------------------------------
  2931.  
  2932. [ SearchScope domain1 domain2 context i;
  2933.  
  2934.   i=0;
  2935. !  Everything is in scope to the debugging commands
  2936.  
  2937. #ifdef DEBUG;
  2938.   if (scope_reason==PARSING_REASON
  2939.       && verb_word == 'purloin' or 'tree' or 'abstract'
  2940.                        or 'gonear' or 'scope' or 'showobj')
  2941.   {   for (i=selfobj:i<=top_object:i++)
  2942.           if (i ofclass Object && (parent(i)==0 || parent(i) ofclass Object))
  2943.               PlaceInScope(i);
  2944.       rtrue;
  2945.   }
  2946. #endif;
  2947.  
  2948. !  First, a scope token gets priority here:
  2949.  
  2950.   if (scope_token ~= 0)
  2951.   {   scope_stage=2;
  2952.       if (indirect(scope_token)~=0) rtrue;
  2953.   }
  2954.  
  2955. !  Next, call any user-supplied routine adding things to the scope,
  2956. !  which may circumvent the usual routines altogether if they return true:
  2957.  
  2958.   if (actor==domain1 or domain2 && InScope(actor)~=0) rtrue;
  2959.  
  2960. !  Pick up everything in the location except the actor's possessions;
  2961. !  then go through those.  (This ensures the actor's possessions are in
  2962. !  scope even in Darkness.)
  2963.  
  2964.   if (context==MULTIINSIDE_TOKEN && advance_warning ~= -1)
  2965.   {   if (IsSeeThrough(advance_warning)==1)
  2966.           ScopeWithin(advance_warning, 0, context);
  2967.   }
  2968.   else
  2969.   {   if (domain1~=0 && domain1 has supporter or container)
  2970.           ScopeWithin_O(domain1, domain1, context);
  2971.       ScopeWithin(domain1, domain2, context);
  2972.       if (domain2~=0 && domain2 has supporter or container)
  2973.           ScopeWithin_O(domain2, domain2, context);
  2974.       ScopeWithin(domain2, 0, context);
  2975.   }
  2976.  
  2977. !  A special rule applies:
  2978. !  in Darkness as in light, the actor is always in scope to himself.
  2979.  
  2980.   if (thedark == domain1 or domain2)
  2981.   {   ScopeWithin_O(actor, actor, context);
  2982.       if (parent(actor) has supporter or container)
  2983.           ScopeWithin_O(parent(actor), parent(actor), context);
  2984.   }
  2985. ];
  2986.  
  2987. ! ----------------------------------------------------------------------------
  2988. !  IsSeeThrough is used at various places: roughly speaking, it determines
  2989. !  whether o being in scope means that the contents of o are in scope.
  2990. ! ----------------------------------------------------------------------------
  2991.  
  2992. [ IsSeeThrough o;
  2993.   if (o has supporter
  2994.       || (o has transparent)
  2995.       || (o has container && o has open))
  2996.       rtrue;
  2997.   rfalse;
  2998. ];
  2999.  
  3000. ! ----------------------------------------------------------------------------
  3001. !  PlaceInScope is provided for routines outside the library, and is not
  3002. !  called within the parser (except for debugging purposes).
  3003. ! ----------------------------------------------------------------------------
  3004.  
  3005. [ PlaceInScope thing;
  3006.    if (scope_reason~=PARSING_REASON or TALKING_REASON)
  3007.    {   DoScopeAction(thing); rtrue; }
  3008.    wn=match_from; TryGivenObject(thing); placed_in_flag=1;
  3009. ];
  3010.  
  3011. ! ----------------------------------------------------------------------------
  3012. !  DoScopeAction
  3013. ! ----------------------------------------------------------------------------
  3014.  
  3015. [ DoScopeAction thing s p1;
  3016.   s = scope_reason; p1=parser_one;
  3017. #ifdef DEBUG;
  3018.   if (parser_trace>=6)
  3019.   {   print "[DSA on ", (the) thing, " with reason = ", scope_reason,
  3020.       " p1 = ", parser_one, " p2 = ", parser_two, "]^";
  3021.   }
  3022. #endif;
  3023.   switch(scope_reason)
  3024.   {   REACT_BEFORE_REASON:
  3025.           if (thing.react_before==0 or NULL) return;
  3026. #ifdef DEBUG;
  3027.           if (parser_trace>=2)
  3028.           {   print "[Considering react_before for ", (the) thing, "]^"; }
  3029. #endif;
  3030.           if (parser_one==0) parser_one = RunRoutines(thing,react_before);
  3031.       REACT_AFTER_REASON:
  3032.           if (thing.react_after==0 or NULL) return;
  3033. #ifdef DEBUG;
  3034.           if (parser_trace>=2)
  3035.           {   print "[Considering react_after for ", (the) thing, "]^"; }
  3036. #endif;
  3037.           if (parser_one==0) parser_one = RunRoutines(thing,react_after);
  3038.       EACH_TURN_REASON:
  3039.           if (thing.&each_turn==0) return;
  3040. #ifdef DEBUG;
  3041.           if (parser_trace>=2)
  3042.           {   print "[Considering each_turn for ", (the) thing, "]^"; }
  3043. #endif;
  3044.           PrintOrRun(thing, each_turn);
  3045.       TESTSCOPE_REASON:
  3046.           if (thing==parser_one) parser_two = 1;
  3047.       LOOPOVERSCOPE_REASON:
  3048.           indirect(parser_one,thing); parser_one=p1;
  3049.   }
  3050.   scope_reason = s;
  3051. ];
  3052.  
  3053. ! ----------------------------------------------------------------------------
  3054. !  ScopeWithin looks for objects in the domain which make textual sense
  3055. !  and puts them in the match list.  (However, it does not recurse through
  3056. !  the second argument.)
  3057. ! ----------------------------------------------------------------------------
  3058.  
  3059. [ ScopeWithin domain nosearch context x y;
  3060.  
  3061.    if (domain==0) rtrue;
  3062.  
  3063. !  Special rule: the directions (interpreted as the 12 walls of a room) are
  3064. !  always in context.  (So, e.g., "examine north wall" is always legal.)
  3065. !  (Unless we're parsing something like "all", because it would just slow
  3066. !  things down then, or unless the context is "creature".)
  3067.  
  3068.    if (indef_mode==0 && domain==actors_location
  3069.        && scope_reason==PARSING_REASON && context~=CREATURE_TOKEN)
  3070.            ScopeWithin(compass);
  3071.  
  3072. !  Look through the objects in the domain, avoiding "objectloop" in case
  3073. !  movements occur, e.g. when trying each_turn.
  3074.  
  3075.    x = child(domain);
  3076.    while (x ~= 0)
  3077.    {   y = sibling(x);
  3078.        ScopeWithin_O(x, nosearch, context);
  3079.        x = y;
  3080.    }
  3081. ];
  3082.  
  3083. [ ScopeWithin_O domain nosearch context i ad n;
  3084.  
  3085. !  multiexcept doesn't have second parameter in scope
  3086.    if (context==MULTIEXCEPT_TOKEN && domain==advance_warning) jump DontAccept;
  3087.  
  3088. !  If the scope reason is unusual, don't parse.
  3089.  
  3090.       if (scope_reason~=PARSING_REASON or TALKING_REASON)
  3091.       {   DoScopeAction(domain); jump DontAccept; }
  3092.  
  3093. !  "it" or "them" matches to the it-object only.  (Note that (1) this means
  3094. !  that "it" will only be understood if the object in question is still
  3095. !  in context, and (2) only one match can ever be made in this case.)
  3096.  
  3097.       if (match_from <= num_words)  ! If there's any text to match, that is
  3098.       {   wn=match_from;
  3099.           i=NounWord();
  3100.           if (i==1 && player==domain)  MakeMatch(domain, 1);
  3101.  
  3102.           if (i>=2 && i<128 && (LanguagePronouns-->i == domain))
  3103.               MakeMatch(domain, 1);
  3104.       }
  3105.  
  3106. !  Construing the current word as the start of a noun, can it refer to the
  3107. !  object?
  3108.  
  3109.       wn = match_from;
  3110.       if (TryGivenObject(domain) > 0)
  3111.           if (indef_nspec_at>0 && match_from~=indef_nspec_at)
  3112.           {   !  This case arises if the player has typed a number in
  3113.               !  which is hypothetically an indefinite descriptor:
  3114.               !  e.g. "take two clubs".  We have just checked the object
  3115.               !  against the word "clubs", in the hope of eventually finding
  3116.               !  two such objects.  But we also backtrack and check it
  3117.               !  against the words "two clubs", in case it turns out to
  3118.               !  be the 2 of Clubs from a pack of cards, say.  If it does
  3119.               !  match against "two clubs", we tear up our original
  3120.               !  assumption about the meaning of "two" and lapse back into
  3121.               !  definite mode.
  3122.           
  3123.               wn = indef_nspec_at;
  3124.               if (TryGivenObject(domain) > 0)
  3125.               {   match_from = indef_nspec_at;
  3126.                   ResetDescriptors();                  
  3127.               }
  3128.               wn = match_from;
  3129.           }
  3130.  
  3131.       .DontAccept;
  3132.  
  3133. !  Shall we consider the possessions of the current object, as well?
  3134. !  Only if it's a container (so, for instance, if a dwarf carries a
  3135. !  sword, then "drop sword" will not be accepted, but "dwarf, drop sword"
  3136. !  will).
  3137. !  Also, only if there are such possessions.
  3138. !
  3139. !  Notice that the parser can see "into" anything flagged as
  3140. !  transparent - such as a dwarf whose sword you can get at.
  3141.  
  3142.       if (child(domain)~=0 && domain ~= nosearch && IsSeeThrough(domain)==1)
  3143.           ScopeWithin(domain,nosearch,context);
  3144.  
  3145. !  Drag any extras into context
  3146.  
  3147.    ad = domain.&add_to_scope;
  3148.    if (ad ~= 0)
  3149.    {   if (UnsignedCompare(ad-->0,top_object) > 0)
  3150.        {   ats_flag = 2+context;
  3151.            RunRoutines(domain, add_to_scope);
  3152.            ats_flag = 0;
  3153.        }
  3154.        else
  3155.        {   n=domain.#add_to_scope;
  3156.            for (i=0:(2*i)<n:i++)
  3157.                ScopeWithin_O(ad-->i,0,context);
  3158.        }
  3159.    }
  3160. ];
  3161.  
  3162. [ AddToScope obj;
  3163.    if (ats_flag>=2)
  3164.        ScopeWithin_O(obj,0,ats_flag-2);
  3165.    if (ats_flag==1)
  3166.    {   if  (HasLightSource(obj)==1) ats_hls = 1;
  3167.    }
  3168. ];
  3169.  
  3170. ! ----------------------------------------------------------------------------
  3171. !  MakeMatch looks at how good a match is.  If it's the best so far, then
  3172. !  wipe out all the previous matches and start a new list with this one.
  3173. !  If it's only as good as the best so far, add it to the list.
  3174. !  If it's worse, ignore it altogether.
  3175. !
  3176. !  The idea is that "red panic button" is better than "red button" or "panic".
  3177. !
  3178. !  number_matched (the number of words matched) is set to the current level
  3179. !  of quality.
  3180. !
  3181. !  We never match anything twice, and keep at most 64 equally good items.
  3182. ! ----------------------------------------------------------------------------
  3183.  
  3184. [ MakeMatch obj quality i;
  3185. #ifdef DEBUG;
  3186.    if (parser_trace>=6) print "    Match with quality ",quality,"^";
  3187. #endif;
  3188.    if (token_filter~=0 && UserFilter(obj)==0)
  3189.    {   #ifdef DEBUG;
  3190.        if (parser_trace>=6)
  3191.        {   print "    Match filtered out: token filter ", token_filter, "^";
  3192.        }
  3193.        #endif;
  3194.        rtrue;
  3195.    }
  3196.    if (quality < match_length) rtrue;
  3197.    if (quality > match_length) { match_length=quality; number_matched=0; }
  3198.    else
  3199.    {   if (2*number_matched>=MATCH_LIST_SIZE) rtrue;
  3200.        for (i=0:i<number_matched:i++)
  3201.            if (match_list-->i==obj) rtrue;
  3202.    }
  3203.    match_list-->number_matched++ = obj;
  3204. #ifdef DEBUG;
  3205.    if (parser_trace>=6) print "    Match added to list^";
  3206. #endif;
  3207. ];
  3208.  
  3209. ! ----------------------------------------------------------------------------
  3210. !  TryGivenObject tries to match as many words as possible in what has been
  3211. !  typed to the given object, obj.  If it manages any words matched at all,
  3212. !  it calls MakeMatch to say so, then returns the number of words (or 1
  3213. !  if it was a match because of inadequate input).
  3214. ! ----------------------------------------------------------------------------
  3215.  
  3216. [ TryGivenObject obj threshold k w j;
  3217.  
  3218. #ifdef DEBUG;
  3219.    if (parser_trace>=5)
  3220.        print "    Trying ", (the) obj, " (", obj, ") at word ", wn, "^";
  3221. #endif;
  3222.  
  3223.    dict_flags_of_noun = 0;
  3224.  
  3225. !  If input has run out then always match, with only quality 0 (this saves
  3226. !  time).
  3227.  
  3228.    if (wn > num_words)
  3229.    {   if (indef_mode ~= 0)
  3230.            dict_flags_of_noun = $$01110000;  ! Reject "plural" bit
  3231.        MakeMatch(obj,0);
  3232.        #ifdef DEBUG;
  3233.        if (parser_trace>=5)
  3234.        print "    Matched (0)^";
  3235.        #endif;
  3236.        return 1;
  3237.    }
  3238.  
  3239. !  Ask the object to parse itself if necessary, sitting up and taking notice
  3240. !  if it says the plural was used:
  3241.  
  3242.    if (obj.parse_name~=0)
  3243.    {   parser_action = NULL; j=wn;
  3244.        k=RunRoutines(obj,parse_name);
  3245.        if (k>0)
  3246.        {   wn=j+k;
  3247.            .MMbyPN;
  3248.  
  3249.            if (parser_action == ##PluralFound)
  3250.                dict_flags_of_noun = dict_flags_of_noun | 4;
  3251.  
  3252.            if (dict_flags_of_noun & 4)
  3253.            {   if (~~allow_plurals) k=0;
  3254.                else
  3255.                {   if (indef_mode==0)
  3256.                    {   indef_mode=1; indef_type=0; indef_wanted=0; }
  3257.                    indef_type = indef_type | PLURAL_BIT;
  3258.                    if (indef_wanted==0) indef_wanted=100;
  3259.                }
  3260.            }
  3261.  
  3262.            #ifdef DEBUG;
  3263.                if (parser_trace>=5)
  3264.                {   print "    Matched (", k, ")^";
  3265.                }
  3266.            #endif;
  3267.            MakeMatch(obj,k);
  3268.            return k;
  3269.        }
  3270.        if (k==0) jump NoWordsMatch;
  3271.    }
  3272.  
  3273. !  The default algorithm is simply to count up how many words pass the
  3274. !  Refers test:
  3275.  
  3276.    parser_action = NULL;
  3277.  
  3278.    w = NounWord();
  3279.  
  3280.    if (w==1 && player==obj) { k=1; jump MMbyPN; }
  3281.  
  3282.    if (w>=2 && w<128 && (LanguagePronouns-->w == obj))
  3283.    {   k=1; jump MMbyPN; }
  3284.  
  3285.    j=--wn;
  3286.    threshold = ParseNoun(obj);
  3287. #ifdef DEBUG;
  3288.    if (threshold>=0 && parser_trace>=5)
  3289.        print "    ParseNoun returned ", threshold, "^";
  3290. #endif;
  3291.    if (threshold<0) wn++;
  3292.    if (threshold>0) { k=threshold; jump MMbyPN; }
  3293.  
  3294.    if (threshold==0 || Refers(obj,wn-1)==0)
  3295.    {   .NoWordsMatch;
  3296.        if (indef_mode~=0)
  3297.        {   k=0; parser_action=NULL; jump MMbyPN;
  3298.        }
  3299.        rfalse;
  3300.    }
  3301.  
  3302.    if (threshold<0)
  3303.    {   threshold=1;
  3304.        dict_flags_of_noun = (w->#dict_par1) & $$01110100;
  3305.        w = NextWord();
  3306.        while (Refers(obj, wn-1))
  3307.        {   threshold++;
  3308.            if (w)
  3309.                dict_flags_of_noun = dict_flags_of_noun
  3310.                                     | ((w->#dict_par1) & $$01110100);
  3311.            w = NextWord();
  3312.        }
  3313.    }
  3314.  
  3315.    k = threshold; jump MMbyPN;
  3316. ];
  3317.  
  3318. ! ----------------------------------------------------------------------------
  3319. !  Refers works out whether the word at number wnum can refer to the object
  3320. !  obj, returning true or false.  The standard method is to see if the
  3321. !  word is listed under "name" for the object, but this is more complex
  3322. !  in languages other than English.
  3323. ! ----------------------------------------------------------------------------
  3324.  
  3325. [ Refers obj wnum   wd k l m;
  3326.     if (obj==0) rfalse;
  3327.  
  3328.     #ifdef LanguageRefers;
  3329.     k = LanguageRefers(obj,wnum); if (k>=0) return k;
  3330.     #endif;
  3331.  
  3332.     k = wn; wn = wnum; wd = NextWordStopped(); wn = k;
  3333.  
  3334.     if (parser_inflection >= 256)
  3335.     {   k = indirect(parser_inflection, obj, wd);
  3336.         if (k>=0) return k;
  3337.         m = -k;
  3338.     } else m = parser_inflection;
  3339.     k=obj.&m; l=(obj.#m)/2-1;
  3340.     for (m=0:m<=l:m++)
  3341.         if (wd==k-->m) rtrue;
  3342.     rfalse;
  3343. ];
  3344.  
  3345. [ WordInProperty wd obj prop k l m;
  3346.     k=obj.∝ l=(obj.#prop)/2-1;
  3347.     for (m=0:m<=l:m++)
  3348.         if (wd==k-->m) rtrue;
  3349.     rfalse;
  3350. ];
  3351.  
  3352. [ DictionaryLookup b l i;
  3353.   for (i=0:i<l:i++) buffer2->(2+i) = b->i;
  3354.   buffer2->1 = l;
  3355.   Tokenise__(buffer2,parse2);
  3356.   return parse2-->1;
  3357. ];
  3358.  
  3359. ! ----------------------------------------------------------------------------
  3360. !  NounWord (which takes no arguments) returns:
  3361. !
  3362. !   0  if the next word is unrecognised or does not carry the "noun" bit in
  3363. !      its dictionary entry,
  3364. !   1  if a word meaning "me",
  3365. !   the index in the pronoun table (plus 2) of the value field of a pronoun,
  3366. !      if the word is a pronoun,
  3367. !   the address in the dictionary if it is a recognised noun.
  3368. !
  3369. !  The "current word" marker moves on one.
  3370. ! ----------------------------------------------------------------------------
  3371.  
  3372. [ NounWord i j s;
  3373.    i=NextWord();
  3374.    if (i==0) rfalse;
  3375.    if (i==ME1__WD or ME2__WD or ME3__WD) return 1;
  3376.    s = LanguagePronouns-->0;
  3377.    for (j=1 : j<=s : j=j+3)
  3378.        if (i == LanguagePronouns-->j)
  3379.            return j+2;
  3380.    if ((i->#dict_par1)&128 == 0) rfalse;
  3381.    return i;
  3382. ];
  3383.  
  3384. ! ----------------------------------------------------------------------------
  3385. !  NextWord (which takes no arguments) returns:
  3386. !
  3387. !  0            if the next word is unrecognised,
  3388. !  comma_word   if it is a comma character
  3389. !               (which is treated oddly by the Z-machine, hence the code)
  3390. !  or the dictionary address if it is recognised.
  3391. !  The "current word" marker is moved on.
  3392. !
  3393. !  NextWordStopped does the same, but returns -1 when input has run out
  3394. ! ----------------------------------------------------------------------------
  3395.  
  3396. [ NextWord i j k;
  3397.    if (wn > parse->1) { wn++; rfalse; }
  3398.    i=wn*2-1; wn++;
  3399.    j=parse-->i;
  3400.    if (j==0)
  3401.    {   k=wn*4-3; i=buffer->(parse->k);
  3402.        if (i==',') j=comma_word;
  3403.        if (i=='.') j=THEN1__WD;
  3404.    }
  3405.    return j;
  3406. ];   
  3407.  
  3408. [ NextWordStopped;
  3409.    if (wn > parse->1) { wn++; return -1; }
  3410.    return NextWord();
  3411. ];
  3412.  
  3413. [ WordAddress wordnum;
  3414.    return buffer + parse->(wordnum*4+1);
  3415. ];
  3416.  
  3417. [ WordLength wordnum;
  3418.    return parse->(wordnum*4);
  3419. ];
  3420.  
  3421. ! ----------------------------------------------------------------------------
  3422. !  TryNumber is the only routine which really does any character-level
  3423. !  parsing, since that's normally left to the Z-machine.
  3424. !  It takes word number "wordnum" and tries to parse it as an (unsigned)
  3425. !  decimal number, returning
  3426. !
  3427. !  -1000                if it is not a number
  3428. !  the number           if it has between 1 and 4 digits
  3429. !  10000                if it has 5 or more digits.
  3430. !
  3431. !  (The danger of allowing 5 digits is that Z-machine integers are only
  3432. !  16 bits long, and anyway this isn't meant to be perfect.)
  3433. !
  3434. !  Using NumberWord, it also catches "one" up to "twenty".
  3435. !
  3436. !  Note that a game can provide a ParseNumber routine which takes priority,
  3437. !  to enable parsing of odder numbers ("x45y12", say).
  3438. ! ----------------------------------------------------------------------------
  3439.  
  3440. [ TryNumber wordnum   i j c num len mul tot d digit;
  3441.  
  3442.    i=wn; wn=wordnum; j=NextWord(); wn=i;
  3443.    j=NumberWord(j); if (j>=1) return j;
  3444.  
  3445.    i=wordnum*4+1; j=parse->i; num=j+buffer; len=parse->(i-1);
  3446.  
  3447.    tot=ParseNumber(num, len);  if (tot~=0) return tot;
  3448.  
  3449.    if (len>=4) mul=1000;
  3450.    if (len==3) mul=100;
  3451.    if (len==2) mul=10;
  3452.    if (len==1) mul=1;
  3453.  
  3454.    tot=0; c=0; len=len-1;
  3455.  
  3456.    for (c=0:c<=len:c++)
  3457.    {   digit=num->c;
  3458.        if (digit=='0') { d=0; jump digok; }
  3459.        if (digit=='1') { d=1; jump digok; }
  3460.        if (digit=='2') { d=2; jump digok; }
  3461.        if (digit=='3') { d=3; jump digok; }
  3462.        if (digit=='4') { d=4; jump digok; }
  3463.        if (digit=='5') { d=5; jump digok; }
  3464.        if (digit=='6') { d=6; jump digok; }
  3465.        if (digit=='7') { d=7; jump digok; }
  3466.        if (digit=='8') { d=8; jump digok; }
  3467.        if (digit=='9') { d=9; jump digok; }
  3468.        return -1000;
  3469.      .digok;
  3470.        tot=tot+mul*d; mul=mul/10;
  3471.    }
  3472.    if (len>3) tot=10000;
  3473.    return tot;
  3474. ];
  3475.  
  3476. ! ----------------------------------------------------------------------------
  3477. !  GetGender returns 0 if the given animate object is female, and 1 if male
  3478. !  (not all games will want such a simple decision function!)
  3479. ! ----------------------------------------------------------------------------
  3480.  
  3481. [ GetGender person;
  3482.    if (person hasnt female) rtrue;
  3483.    rfalse;
  3484. ];
  3485.  
  3486. [ GetGNAOfObject obj case gender;
  3487.    if (obj hasnt animate) case = 6;
  3488.    if (obj has male) gender = male;
  3489.    if (obj has female) gender = female;
  3490.    if (obj has neuter) gender = neuter;
  3491.    if (gender == 0)
  3492.    {   if (case == 0) gender = LanguageAnimateGender;
  3493.        else gender = LanguageInanimateGender;
  3494.    }
  3495.    if (gender == female) case = case + 1;
  3496.    if (gender == neuter) case = case + 2;
  3497.    if (obj has pluralname) case = case + 3;
  3498.    return case;
  3499. ];
  3500.  
  3501. ! ----------------------------------------------------------------------------
  3502. !  Converting between dictionary addresses and entry numbers
  3503. ! ----------------------------------------------------------------------------
  3504.  
  3505. [ Dword__No w; return (w-(0-->4 + 7))/9; ];
  3506. [ No__Dword n; return 0-->4 + 7 + 9*n; ];
  3507.  
  3508. ! ----------------------------------------------------------------------------
  3509. !  For copying buffers
  3510. ! ----------------------------------------------------------------------------
  3511.  
  3512. [ CopyBuffer bto bfrom i size;
  3513.    size=bto->0;
  3514.    for (i=1:i<=size:i++) bto->i=bfrom->i;
  3515. ];
  3516.  
  3517. ! ----------------------------------------------------------------------------
  3518. !  Provided for use by language definition files
  3519. ! ----------------------------------------------------------------------------
  3520.  
  3521. [ LTI_Insert i ch  y;
  3522.  
  3523.   !   Insert character ch into buffer at point i.
  3524.  
  3525.   !   Being careful not to let the buffer possibly overflow:
  3526.  
  3527.       y = buffer->1;
  3528.       if (y > buffer->0) y = buffer->0;
  3529.  
  3530.   !   Move the subsequent text along one character:
  3531.  
  3532.       for (y=y+2: y>i : y--) buffer->y = buffer->(y-1);
  3533.  
  3534.       buffer->i = ch;
  3535.  
  3536.   !   And the text is now one character longer:
  3537.       if (buffer->1 < buffer->0)
  3538.           (buffer->1)++;
  3539. ];
  3540.  
  3541. ! ============================================================================
  3542.  
  3543. [ PronounsSub x y c d;
  3544.  
  3545.   L__M(##Pronouns, 1);
  3546.  
  3547.   c = (LanguagePronouns-->0)/3;
  3548.   if (player ~= selfobj) c++;
  3549.  
  3550.   if (c==0) return L__M(##Pronouns, 4);
  3551.  
  3552.   for (x = 1, d = 0 : x <= LanguagePronouns-->0: x = x+3)
  3553.   {   print "~", (address) LanguagePronouns-->x, "~ ";
  3554.       y = LanguagePronouns-->(x+2);
  3555.       if (y == NULL) L__M(##Pronouns, 3);
  3556.       else { L__M(##Pronouns, 2); print (the) y; }
  3557.       d++;
  3558.       if (d < c-1) print ", ";
  3559.       if (d == c-1) print (string) AND__TX;
  3560.   }
  3561.   if (player ~= selfobj)
  3562.   {   print "~", (address) ME1__WD, "~ "; L__M(##Pronouns, 2);
  3563.       c = player; player = selfobj;
  3564.       print (the) c; player = c;
  3565.   }
  3566.   ".";
  3567. ];
  3568.  
  3569. [ SetPronoun dword value x;
  3570.   for (x = 1 : x <= LanguagePronouns-->0: x = x+3)
  3571.       if (LanguagePronouns-->x == dword)
  3572.       {   LanguagePronouns-->(x+2) = value; return;
  3573.       }
  3574.   RunTimeError(14);
  3575. ];
  3576.  
  3577. [ PronounValue dword x;
  3578.   for (x = 1 : x <= LanguagePronouns-->0: x = x+3)
  3579.       if (LanguagePronouns-->x == dword)
  3580.           return LanguagePronouns-->(x+2);
  3581.   return 0;
  3582. ];
  3583.  
  3584. [ ResetVagueWords obj; PronounNotice(obj); ];
  3585.  
  3586. #ifdef EnglishNaturalLanguage;
  3587. [ PronounOldEnglish;
  3588.    if (itobj ~= old_itobj)   SetPronoun('it', itobj);
  3589.    if (himobj ~= old_himobj) SetPronoun('him', himobj);
  3590.    if (herobj ~= old_herobj) SetPronoun('her', herobj);
  3591.    old_itobj = itobj; old_himobj = himobj; old_herobj = herobj;
  3592. ];
  3593. #endif;
  3594.  
  3595. [ PronounNotice obj x bm;
  3596.  
  3597.    if (obj == player) return;
  3598.  
  3599.    #ifdef EnglishNaturalLanguage;
  3600.    PronounOldEnglish();
  3601.    #endif;
  3602.  
  3603.    bm = PowersOfTwo_TB-->(GetGNAOfObject(obj));
  3604.  
  3605.    for (x = 1 : x <= LanguagePronouns-->0: x = x+3)
  3606.        if (bm & (LanguagePronouns-->(x+1)) ~= 0)
  3607.            LanguagePronouns-->(x+2) = obj;
  3608.  
  3609.    #ifdef EnglishNaturalLanguage;
  3610.    itobj  = PronounValue('it');  old_itobj  = itobj;
  3611.    himobj = PronounValue('him'); old_himobj = himobj;
  3612.    herobj = PronounValue('her'); old_herobj = herobj;
  3613.    #endif;
  3614. ];
  3615.  
  3616. ! ============================================================================
  3617. !  End of the parser proper: the remaining routines are its front end.
  3618. ! ----------------------------------------------------------------------------
  3619.  
  3620. Object InformLibrary "(Inform Library)"
  3621.   with play
  3622.        [ i j k l;
  3623.        standard_interpreter = $32-->0;
  3624.        transcript_mode = ((0-->8) & 1);
  3625.        ChangeDefault(cant_go, CANTGO__TX);
  3626.  
  3627.        buffer->0 = 120;
  3628.        buffer2->0 = 120;
  3629.        buffer3->0 = 120;
  3630.        parse->0 = 64;
  3631.        parse2->0 = 64;
  3632.        
  3633.        real_location = thedark;
  3634.        player = selfobj; actor = player;
  3635.     
  3636.        top_object = #largest_object-255;
  3637.        selfobj.capacity = MAX_CARRIED;
  3638.        #ifdef LanguageInitialise;
  3639.        LanguageInitialise();
  3640.        #endif;
  3641.        new_line;
  3642.        j=Initialise();
  3643.        last_score = score;
  3644.        move player to location;
  3645.        while (parent(location)~=0) location=parent(location);
  3646.        real_location = location;
  3647.        objectloop (i in player) give i moved ~concealed;
  3648.     
  3649.        if (j~=2) Banner();
  3650.  
  3651.        MoveFloatingObjects();
  3652.        lightflag=OffersLight(parent(player));
  3653.        if (lightflag==0) { real_location=location; location=thedark; }
  3654.        <Look>;
  3655.     
  3656.        for (i=1:i<=100:i++) j=random(i);
  3657.  
  3658.        #ifdef EnglishNaturalLanguage;
  3659.        old_itobj = itobj; old_himobj = himobj; old_herobj = herobj;
  3660.        #endif;
  3661.     
  3662.        while (~~deadflag)
  3663.        {   
  3664.            #ifdef EnglishNaturalLanguage;
  3665.                PronounOldEnglish();
  3666.                old_itobj = PronounValue('it');
  3667.                old_himobj = PronounValue('him');
  3668.                old_herobj = PronounValue('her');
  3669.            #endif;
  3670.  
  3671.            .very__late__error;
  3672.  
  3673.            if (score ~= last_score)
  3674.            {   if (notify_mode==1) NotifyTheScore(); last_score=score; }
  3675.  
  3676.            .late__error;
  3677.  
  3678.            inputobjs-->0 = 0; inputobjs-->1 = 0;
  3679.            inputobjs-->2 = 0; inputobjs-->3 = 0; meta=false;
  3680.     
  3681.            !  The Parser writes its results into inputobjs and meta,
  3682.            !  a flag indicating a "meta-verb".  This can only be set for
  3683.            !  commands by the player, not for orders to others.
  3684.     
  3685.            InformParser.parse_input(inputobjs);
  3686.     
  3687.            action=inputobjs-->0;
  3688.  
  3689.            !  --------------------------------------------------------------
  3690.  
  3691.            !  Reverse "give fred biscuit" into "give biscuit to fred"
  3692.     
  3693.            if (action==##GiveR or ##ShowR)
  3694.            {   i=inputobjs-->2; inputobjs-->2=inputobjs-->3; inputobjs-->3=i;
  3695.                if (action==##GiveR) action=##Give; else action=##Show;
  3696.            }
  3697.     
  3698.            !  Convert "P, tell me about X" to "ask P about X"
  3699.     
  3700.            if (action==##Tell && inputobjs-->2==player && actor~=player)
  3701.            {   inputobjs-->2=actor; actor=player; action=##Ask;
  3702.            }
  3703.     
  3704.            !  Convert "ask P for X" to "P, give X to me"
  3705.     
  3706.            if (action==##AskFor && inputobjs-->2~=player && actor==player)
  3707.            {   actor=inputobjs-->2; inputobjs-->2=inputobjs-->3;
  3708.                inputobjs-->3=player; action=##Give;
  3709.            }
  3710.     
  3711.            !  For old, obsolete code: special_word contains the topic word
  3712.            !  in conversation
  3713.     
  3714.            if (action==##Ask or ##Tell or ##Answer)
  3715.                special_word = special_number1;
  3716.  
  3717.            !  --------------------------------------------------------------
  3718.     
  3719.            multiflag=false; onotheld_mode=notheld_mode; notheld_mode=false;
  3720.            !  For implicit taking and multiple object detection
  3721.     
  3722.           .begin__action;
  3723.            inp1 = 0; inp2 = 0; i=inputobjs-->1;
  3724.            if (i>=1) inp1=inputobjs-->2;
  3725.            if (i>=2) inp2=inputobjs-->3;
  3726.     
  3727.            !  inp1 and inp2 hold: object numbers, or 0 for "multiple object",
  3728.            !  or 1 for "a number or dictionary address"
  3729.     
  3730.            if (inp1 == 1) noun = special_number1; else noun = inp1;
  3731.            if (inp2 == 1)
  3732.            {   if (inp1 == 1) second = special_number2;
  3733.                else second = special_number1;
  3734.            } else second = inp2;
  3735.  
  3736.            !  --------------------------------------------------------------
  3737.     
  3738.            if (actor~=player)
  3739.            {   
  3740.            !  The player's "orders" property can refuse to allow conversation
  3741.            !  here, by returning true.  If not, the order is sent to the
  3742.            !  other person's "orders" property.  If that also returns false,
  3743.            !  then: if it was a misunderstood command anyway, it is converted
  3744.            !  to an Answer action (thus "floyd, grrr" ends up as
  3745.            !  "say grrr to floyd").  If it was a good command, it is finally
  3746.            !  offered to the Order: part of the other person's "life"
  3747.            !  property, the old-fashioned way of dealing with conversation.
  3748.     
  3749.                j=RunRoutines(player,orders);
  3750.                if (j==0)
  3751.                {   j=RunRoutines(actor,orders);
  3752.                    if (j==0)
  3753.                    {   if (action==##NotUnderstood)
  3754.                        {   inputobjs-->3=actor; actor=player; action=##Answer;
  3755.                            jump begin__action;
  3756.                        }
  3757.                        if (RunLife(actor,##Order)==0) L__M(##Order,1,actor);
  3758.                    }
  3759.                }
  3760.                jump turn__end;
  3761.            }
  3762.  
  3763.            !  --------------------------------------------------------------
  3764.            !  Generate the action...
  3765.  
  3766.            if ((i==0)
  3767.                || (i==1 && inp1 ~= 0)
  3768.                || (i==2 && inp1 ~= 0 && inp2 ~= 0))
  3769.            {   self.begin_action(action, noun, second, 0);
  3770.                jump turn__end;
  3771.            }
  3772.  
  3773.            !  ...unless a multiple object must be substituted.  First:
  3774.            !  (a) check the multiple list isn't empty;
  3775.            !  (b) warn the player if it has been cut short because too long;
  3776.            !  (c) generate a sequence of actions from the list
  3777.            !      (stopping in the event of death or movement away).
  3778.  
  3779.            multiflag = true;
  3780.            j=multiple_object-->0;
  3781.            if (j==0) { L__M(##Miscellany,2); jump late__error; }
  3782.            if (toomany_flag)
  3783.            {   toomany_flag = false; L__M(##Miscellany,1); }
  3784.            i=location;
  3785.            for (k=1:k<=j:k++)
  3786.            {   if (deadflag) break;
  3787.                if (location ~= i)
  3788.                {   L__M(##Miscellany, 51);
  3789.                    break;
  3790.                }
  3791.                l = multiple_object-->k;
  3792.                PronounNotice(l);
  3793.                print (name) l, ": ";
  3794.                if (inp1 == 0)
  3795.                {   inp1 = l; self.begin_action(action, l, second, 0); inp1 = 0;
  3796.                }
  3797.                else
  3798.                {   inp2 = l; self.begin_action(action, noun, l, 0); inp2 = 0;
  3799.                }
  3800.            }
  3801.  
  3802.            !  --------------------------------------------------------------
  3803.     
  3804.            .turn__end;
  3805.     
  3806.            !  No time passes if either (i) the verb was meta, or
  3807.            !  (ii) we've only had the implicit take before the "real"
  3808.            !  action to follow.
  3809.     
  3810.            if (notheld_mode==1) { NoteObjectAcquisitions(); continue; }
  3811.            if (meta) continue;
  3812.            if (~~deadflag) self.end_turn_sequence();
  3813.        }
  3814.  
  3815.            if (deadflag~=2) AfterLife();
  3816.            if (deadflag==0) jump very__late__error;
  3817.     
  3818.            print "^^    ";
  3819.            #IFV5; style bold; #ENDIF;
  3820.            print "***";
  3821.            if (deadflag==1) L__M(##Miscellany,3);
  3822.            if (deadflag==2) L__M(##Miscellany,4);
  3823.            if (deadflag>2)  { print " "; DeathMessage(); print " "; }
  3824.            print "***";
  3825.            #IFV5; style roman; #ENDIF;
  3826.            print "^^^";
  3827.            ScoreSub();
  3828.            DisplayStatus();
  3829.            AfterGameOver();
  3830.        ],
  3831.  
  3832.        end_turn_sequence
  3833.        [ i j;
  3834.  
  3835.            turns++;
  3836.            if (the_time~=NULL)
  3837.            {   if (time_rate>=0) the_time=the_time+time_rate;
  3838.                else
  3839.                {   time_step--;
  3840.                    if (time_step==0)
  3841.                    {   the_time++;
  3842.                        time_step = -time_rate;
  3843.                    }
  3844.                }
  3845.                the_time=the_time % 1440;
  3846.            }
  3847.  
  3848.            #IFDEF DEBUG;
  3849.            if (debug_flag & 4 ~= 0)
  3850.            {   for (i=0: i<active_timers: i++)
  3851.                {   j=the_timers-->i;
  3852.                    if (j~=0)
  3853.                    {   print (name) (j&$7fff), ": ";
  3854.                        if (j & $8000) print "daemon";
  3855.                        else
  3856.                        {   print "timer with ",
  3857.                                  j.time_left, " turns to go"; }
  3858.                        new_line;
  3859.                    }
  3860.                }
  3861.            }
  3862.            #ENDIF;
  3863.  
  3864.            for (i=0: i<active_timers: i++)
  3865.            {   if (deadflag) return;
  3866.                j=the_timers-->i;
  3867.                if (j~=0)
  3868.                {   if (j & $8000) RunRoutines(j&$7fff,daemon);
  3869.                    else
  3870.                    {   if (j.time_left==0)
  3871.                        {   StopTimer(j);
  3872.                            RunRoutines(j,time_out);
  3873.                        }
  3874.                        else
  3875.                            j.time_left=j.time_left-1;
  3876.                    }
  3877.                }
  3878.            }
  3879.            if (deadflag) return;
  3880.  
  3881.            scope_reason=EACH_TURN_REASON; verb_word=0;
  3882.            DoScopeAction(location);
  3883.            SearchScope(ScopeCeiling(player), player, 0);
  3884.            scope_reason=PARSING_REASON;
  3885.  
  3886.            if (deadflag) return;
  3887.  
  3888.            TimePasses();
  3889.  
  3890.            if (deadflag) return;
  3891.  
  3892.            AdjustLight();
  3893.  
  3894.            if (deadflag) return;
  3895.  
  3896.            NoteObjectAcquisitions();
  3897.        ],
  3898.  
  3899.        begin_action
  3900.        [ a n s source   sa sn ss;
  3901.            sa = action; sn = noun; ss = second;
  3902.            action = a; noun = n; second = s;
  3903.            #IFDEF DEBUG;
  3904.            if (debug_flag & 2 ~= 0) TraceAction(source);
  3905.            #IFNOT;
  3906.            source = 0;
  3907.            #ENDIF;
  3908.            #IFTRUE Grammar__Version == 1;
  3909.            if ((meta || BeforeRoutines()==false) && action<256)
  3910.                ActionPrimitive();
  3911.            #IFNOT;
  3912.            if ((meta || BeforeRoutines()==false) && action<4096)
  3913.                ActionPrimitive();
  3914.            #ENDIF;
  3915.            action = sa; noun = sn; second = ss;
  3916.        ],
  3917.   has  proper;
  3918.  
  3919. [ ActionPrimitive;
  3920.   indirect(#actions_table-->action);
  3921. ];
  3922.        
  3923. [ AfterGameOver i;
  3924.    .RRQPL;
  3925.    L__M(##Miscellany,5);
  3926.    .RRQL;
  3927.    print "> ";
  3928.    #IFV3; read buffer parse; #ENDIF;
  3929.    temp_global=0;
  3930.    #IFV5; read buffer parse DrawStatusLine; #ENDIF;
  3931.    i=parse-->1;
  3932.    if (i==QUIT1__WD or QUIT2__WD) quit;
  3933.    if (i==RESTART__WD)      @restart;
  3934.    if (i==RESTORE__WD)      { RestoreSub(); jump RRQPL; }
  3935.    if (i==FULLSCORE1__WD or FULLSCORE2__WD && TASKS_PROVIDED==0)
  3936.    {   new_line; FullScoreSub(); jump RRQPL; }
  3937.    if (deadflag==2 && i==AMUSING__WD && AMUSING_PROVIDED==0)
  3938.    {   new_line; Amusing(); jump RRQPL; }
  3939.    #IFV5;
  3940.    if (i==UNDO1__WD or UNDO2__WD or UNDO3__WD)
  3941.    {   if (undo_flag==0)
  3942.        {   L__M(##Miscellany,6);
  3943.            jump RRQPL;
  3944.        }
  3945.        if (undo_flag==1) jump UndoFailed2;
  3946.        @restore_undo i;
  3947.        if (i==0)
  3948.        {   .UndoFailed2; L__M(##Miscellany,7);
  3949.        }
  3950.        jump RRQPL;
  3951.    }
  3952.    #ENDIF;
  3953.    L__M(##Miscellany,8);
  3954.    jump RRQL;
  3955. ];
  3956.  
  3957. [ R_Process a i j s1 s2;
  3958.    s1 = inp1; s2 = inp2;
  3959.    inp1 = i; inp2 = j; InformLibrary.begin_action(a, i, j, 1);
  3960.    inp1 = s1; inp2 = s2;
  3961. ];
  3962.  
  3963. [ NoteObjectAcquisitions i;
  3964.   objectloop (i in player && i hasnt moved)
  3965.   {   give i moved;
  3966.       if (i has scored)
  3967.       {   score = score + OBJECT_SCORE;
  3968.           things_score = things_score + OBJECT_SCORE;
  3969.       }
  3970.   }
  3971. ];
  3972.  
  3973. ! ----------------------------------------------------------------------------
  3974.  
  3975. [ TestScope obj act a al sr x y;
  3976.   x=parser_one; y=parser_two;
  3977.   parser_one=obj; parser_two=0; a=actor; al=actors_location;
  3978.   sr=scope_reason; scope_reason=TESTSCOPE_REASON;
  3979.   if (act==0) actor=player; else actor=act;
  3980.   actors_location=ScopeCeiling(actor);
  3981.   SearchScope(actors_location,actor,0); scope_reason=sr; actor=a;
  3982.   actors_location=al; parser_one=x; x=parser_two; parser_two=y;
  3983.   return x;
  3984. ];
  3985.  
  3986. [ LoopOverScope routine act x y a al;
  3987.   x = parser_one; y=scope_reason; a=actor; al=actors_location;
  3988.   parser_one=routine; if (act==0) actor=player; else actor=act;
  3989.   actors_location=ScopeCeiling(actor);
  3990.   scope_reason=LOOPOVERSCOPE_REASON;
  3991.   SearchScope(actors_location,actor,0);
  3992.   parser_one=x; scope_reason=y; actor=a; actors_location=al;
  3993. ];
  3994.  
  3995. [ BeforeRoutines;
  3996.   if (GamePreRoutine()~=0) rtrue;
  3997.   if (RunRoutines(player,orders)~=0) rtrue;
  3998.   if (location~=0 && RunRoutines(location,before)~=0) rtrue;
  3999.   scope_reason=REACT_BEFORE_REASON; parser_one=0;
  4000.   SearchScope(ScopeCeiling(player),player,0); scope_reason=PARSING_REASON;
  4001.   if (parser_one~=0) rtrue;
  4002.   if (inp1>1 && RunRoutines(inp1,before)~=0) rtrue;
  4003.   rfalse;
  4004. ];
  4005.  
  4006. [ AfterRoutines;
  4007.   scope_reason=REACT_AFTER_REASON; parser_one=0;
  4008.   SearchScope(ScopeCeiling(player),player,0); scope_reason=PARSING_REASON;
  4009.   if (parser_one~=0) rtrue;
  4010.   if (location~=0 && RunRoutines(location,after)~=0) rtrue;
  4011.   if (inp1>1 && RunRoutines(inp1,after)~=0) rtrue;
  4012.   return GamePostRoutine();
  4013. ];
  4014.  
  4015. [ RunLife a j;
  4016. #IFDEF DEBUG;
  4017.    if (debug_flag & 2 ~= 0) TraceAction(2, j);
  4018. #ENDIF;
  4019.    reason_code = j; return RunRoutines(a,life);
  4020. ];
  4021.  
  4022. [ ZRegion addr;
  4023.   switch(metaclass(addr))       ! Left over from Inform 5
  4024.   {   nothing: return 0;
  4025.       Object, Class: return 1;
  4026.       Routine: return 2;
  4027.       String: return 3;
  4028.   }
  4029. ];
  4030.  
  4031. [ PrintOrRun obj prop flag;
  4032.   if (obj.#prop > 2) return RunRoutines(obj,prop);
  4033.   if (obj.prop==NULL) rfalse;
  4034.   switch(metaclass(obj.prop))
  4035.   {   Class, Object, nothing: return RunTimeError(2,obj,prop);
  4036.       String: print (string) obj.prop; if (flag==0) new_line; rtrue;
  4037.       Routine: return RunRoutines(obj,prop);
  4038.   }
  4039. ];
  4040.  
  4041. [ ValueOrRun obj prop;
  4042.   if (obj.prop < 256) return obj.prop;
  4043.   return RunRoutines(obj, prop);
  4044. ];
  4045.  
  4046. [ RunRoutines obj prop;
  4047.    if (obj == thedark
  4048.        && prop ~= initial or short_name or description) obj=real_location;
  4049.    if (obj.&prop == 0) rfalse;
  4050.    return obj.prop();
  4051. ];
  4052.  
  4053. [ ChangeDefault prop val a b;
  4054.    ! Use assembly-language here because -S compilation won't allow this:
  4055.    @loadw 0 5 -> a;
  4056.    b = prop-1;
  4057.    @storew a b val;
  4058. ];
  4059.  
  4060. ! ----------------------------------------------------------------------------
  4061.  
  4062. [ StartTimer obj timer i;
  4063.    for (i=0:i<active_timers:i++)
  4064.        if (the_timers-->i==obj) rfalse;
  4065.    for (i=0:i<active_timers:i++)
  4066.        if (the_timers-->i==0) jump FoundTSlot;
  4067.    i=active_timers++;
  4068.    if (i >= MAX_TIMERS) RunTimeError(4);
  4069.    .FoundTSlot;
  4070.    if (obj.&time_left==0) RunTimeError(5,obj);
  4071.    the_timers-->i=obj; obj.time_left=timer;
  4072. ];
  4073.  
  4074. [ StopTimer obj i;
  4075.    for (i=0:i<active_timers:i++)
  4076.        if (the_timers-->i==obj) jump FoundTSlot2;
  4077.    rfalse;
  4078.    .FoundTSlot2;
  4079.    if (obj.&time_left==0) RunTimeError(5,obj);
  4080.    the_timers-->i=0; obj.time_left=0;
  4081. ];
  4082.  
  4083. [ StartDaemon obj i;
  4084.    for (i=0:i<active_timers:i++)
  4085.        if (the_timers-->i == $8000 + obj)
  4086.            rfalse;
  4087.    for (i=0:i<active_timers:i++)
  4088.        if (the_timers-->i==0) jump FoundTSlot3;
  4089.    i=active_timers++;
  4090.    if (i >= MAX_TIMERS) RunTimeError(4);
  4091.    .FoundTSlot3;
  4092.    the_timers-->i = $8000 + obj;
  4093. ];
  4094.  
  4095. [ StopDaemon obj i;
  4096.    for (i=0:i<active_timers:i++)
  4097.        if (the_timers-->i == $8000 + obj) jump FoundTSlot4;
  4098.    rfalse;
  4099.    .FoundTSlot4;
  4100.    the_timers-->i=0;
  4101. ];
  4102.  
  4103. ! ----------------------------------------------------------------------------
  4104.  
  4105. [ DisplayStatus;
  4106.    if (the_time==NULL)
  4107.    {   sline1=score; sline2=turns; }
  4108.    else
  4109.    {   sline1=the_time/60; sline2=the_time%60; }
  4110. ];
  4111.  
  4112. [ SetTime t s;
  4113.    the_time=t; time_rate=s; time_step=0;
  4114.    if (s<0) time_step=0-s;
  4115. ];
  4116.  
  4117. [ NotifyTheScore;
  4118.    print "^[";  L__M(##Miscellany, 50, score-last_score);  print ".]^";
  4119. ];
  4120.  
  4121. ! ----------------------------------------------------------------------------
  4122.  
  4123. [ AdjustLight flag i;
  4124.    i=lightflag;
  4125.    lightflag=OffersLight(parent(player));
  4126.  
  4127.    if (i==0 && lightflag==1)
  4128.    {   location=real_location; if (flag==0) <Look>;
  4129.    }
  4130.  
  4131.    if (i==1 && lightflag==0)
  4132.    {   real_location=location; location=thedark;
  4133.        if (flag==0) { NoteArrival();
  4134.                       return L__M(##Miscellany, 9); }
  4135.    }
  4136.    if (i==0 && lightflag==0) location=thedark;
  4137. ];
  4138.  
  4139. [ OffersLight i j;
  4140.    if (i==0) rfalse;
  4141.    if (i has light) rtrue;
  4142.    objectloop (j in i)
  4143.        if (HasLightSource(j)==1) rtrue;
  4144.    if (i has container)
  4145.    {   if (i has open || i has transparent)
  4146.            return OffersLight(parent(i));
  4147.    }
  4148.    else
  4149.    {   if (i has enterable || i has transparent || i has supporter)
  4150.            return OffersLight(parent(i));
  4151.    }
  4152.    rfalse;
  4153. ];
  4154.  
  4155. [ HidesLightSource obj;
  4156.     if (obj == player) rfalse;
  4157.     if (obj has transparent or supporter) rfalse;
  4158.     if (obj has container) return obj hasnt open;
  4159.     return obj hasnt enterable;
  4160. ];
  4161.  
  4162. [ HasLightSource i j ad;
  4163.    if (i==0) rfalse;
  4164.    if (i has light) rtrue;
  4165.    if (i has enterable || IsSeeThrough(i)==1)
  4166.        if (~~(HidesLightSource(i)))
  4167.            objectloop (j in i)
  4168.                if (HasLightSource(j)==1) rtrue;
  4169.    ad = i.&add_to_scope;
  4170.    if (parent(i)~=0 && ad ~= 0)
  4171.    {   if (metaclass(ad-->0) == Routine)
  4172.        {   ats_hls = 0; ats_flag = 1;
  4173.            RunRoutines(i, add_to_scope);
  4174.            ats_flag = 0; if (ats_hls == 1) rtrue;
  4175.        }
  4176.        else
  4177.        {   for (j=0:(2*j)<i.#add_to_scope:j++)
  4178.                if (HasLightSource(ad-->j)==1) rtrue;
  4179.        }
  4180.    }
  4181.    rfalse;
  4182. ];
  4183.  
  4184. [ ChangePlayer obj flag i;
  4185. !  if (obj.&number==0) return RunTimeError(7,obj);
  4186.   if (actor==player) actor=obj;
  4187.   give player ~transparent ~concealed;
  4188.   i=obj; while(parent(i)~=0) { if (i has animate) give i transparent;
  4189.                                i=parent(i); }
  4190.   if (player==selfobj) player.short_name=FORMER__TX;
  4191.  
  4192.   player=obj;
  4193.  
  4194.   if (player==selfobj) player.short_name=NULL;
  4195.   give player transparent concealed animate proper;
  4196.   i=player; while(parent(i)~=0) i=parent(i); location=i;
  4197.   real_location=location;
  4198.   MoveFloatingObjects();
  4199.   lightflag=OffersLight(parent(player));
  4200.   if (lightflag==0) location=thedark;
  4201.   print_player_flag=flag;
  4202. ];
  4203.  
  4204. ! ----------------------------------------------------------------------------
  4205.  
  4206. #IFDEF DEBUG;
  4207. [ DebugParameter w x n l;
  4208.   x=0-->4; x=x+(x->0)+1; l=x->0; n=(x+1)-->0; x=w-(x+3);
  4209.   print w;
  4210.   if (w>=1 && w<=top_object) print " (", (name) w, ")";
  4211.   if (x%l==0 && (x/l)<n) print " ('", (address) w, "')";
  4212. ];
  4213. [ DebugAction a anames;
  4214. #iftrue Grammar__Version==1;
  4215.   if (a>=256) { print "<fake action ", a-256, ">"; return; }
  4216. #ifnot;
  4217.   if (a>=4096) { print "<fake action ", a-4096, ">"; return; }
  4218. #endif;
  4219.   anames = #identifiers_table;
  4220.   anames = anames + 2*(anames-->0) + 2*48;
  4221.   print (string) anames-->a;
  4222. ];
  4223. [ DebugAttribute a anames;
  4224.   if (a<0 || a>=48) print "<invalid attribute ", a, ">";
  4225.   else
  4226.   {   anames = #identifiers_table; anames = anames + 2*(anames-->0);
  4227.       print (string) anames-->a;
  4228.   }
  4229. ];
  4230. [ TraceAction source ar;
  4231.   if (source<2) print "[ Action ", (DebugAction) action;
  4232.   else
  4233.   {   if (ar==##Order)
  4234.           print "[ Order to ", (name) actor, ": ", (DebugAction) action;
  4235.       else
  4236.           print "[ Life rule ", (DebugAction) ar;
  4237.   }
  4238.   if (noun~=0)   print " with noun ", (DebugParameter) noun;
  4239.   if (second~=0) print " and second ", (DebugParameter) second;
  4240.   if (source==0) print " ";
  4241.   if (source==1) print " (from < > statement) ";
  4242.   print "]^";
  4243. ];
  4244. [ DebugToken token;
  4245.   AnalyseToken(token);
  4246.   switch(found_ttype)
  4247.   {   ILLEGAL_TT: print "<illegal token number ", token, ">";
  4248.       ELEMENTARY_TT:
  4249.       switch(found_tdata)
  4250.       {   NOUN_TOKEN:        print "noun";
  4251.           HELD_TOKEN:        print "held";
  4252.           MULTI_TOKEN:       print "multi";
  4253.           MULTIHELD_TOKEN:   print "multiheld";
  4254.           MULTIEXCEPT_TOKEN: print "multiexcept";
  4255.           MULTIINSIDE_TOKEN: print "multiinside";
  4256.           CREATURE_TOKEN:    print "creature";
  4257.           SPECIAL_TOKEN:     print "special";
  4258.           NUMBER_TOKEN:      print "number";
  4259.           TOPIC_TOKEN:       print "topic";
  4260.           ENDIT_TOKEN:       print "END";
  4261.       }
  4262.       PREPOSITION_TT:
  4263.           print "'", (address) found_tdata, "'";
  4264.       ROUTINE_FILTER_TT:
  4265.       #ifdef INFIX; print "noun=", (InfixPrintPA) found_tdata;
  4266.       #ifnot; print "noun=Routine(", found_tdata, ")"; #endif;
  4267.       ATTR_FILTER_TT:
  4268.           print (DebugAttribute) found_tdata;
  4269.       SCOPE_TT:
  4270.       #ifdef INFIX; print "scope=", (InfixPrintPA) found_tdata;
  4271.       #ifnot; print "scope=Routine(", found_tdata, ")"; #endif;
  4272.       GPR_TT:
  4273.       #ifdef INFIX; print (InfixPrintPA) found_tdata;
  4274.       #ifnot; print "Routine(", found_tdata, ")"; #endif;
  4275.   }
  4276. ];
  4277. [ DebugGrammarLine pcount;
  4278.   print " * ";
  4279.   for (:line_token-->pcount ~= ENDIT_TOKEN:pcount++)
  4280.   {   if ((line_token-->pcount)->0 & $10) print "/ ";
  4281.       print (DebugToken) line_token-->pcount, " ";
  4282.   }
  4283.   print "-> ", (DebugAction) action_to_be;
  4284.   if (action_reversed) print " reverse";
  4285. ];
  4286. [ ShowVerbSub address lines da meta i j;
  4287.     if (((noun->#dict_par1) & 1) == 0)
  4288.       "Try typing ~showverb~ and then the name of a verb.";
  4289.     meta=((noun->#dict_par1) & 2)/2;
  4290.     i = $ff-(noun->#dict_par2);
  4291.     address = (0-->7)-->i;
  4292.     lines = address->0;
  4293.     address++;
  4294.     print "Verb ";
  4295.     if (meta) print "meta ";
  4296.     da = 0-->4;
  4297.     for (j=0:j < (da+5)-->0:j++)
  4298.         if (da->(j*9 + 14) == $ff-i)
  4299.             print "'", (address) (da + 9*j + 7), "' ";
  4300.     new_line;
  4301.     if (lines == 0) "has no grammar lines.";
  4302.     for (:lines > 0:lines--)
  4303.     {   address = UnpackGrammarLine(address);
  4304.         print "    "; DebugGrammarLine(); new_line;
  4305.     }
  4306. ];
  4307. [ ShowobjSub c f l a n x;
  4308.    if (noun==0) noun=location;
  4309.    objectloop (c ofclass Class) if (noun ofclass c) { f++; l=c; }
  4310.    if (f == 1) print (name) l, " ~"; else print "Object ~";
  4311.    print (name) noun, "~ (", noun, ")";
  4312.    if (parent(noun)~=0) print " in ~", (name) parent(noun), "~";
  4313.    new_line;
  4314.    if (f > 1)
  4315.    {   print "  class ";
  4316.        objectloop (c ofclass Class) if (noun ofclass c) print (name) c, " ";
  4317.        new_line;
  4318.    }
  4319.    for (a=0,f=0:a<48:a++) if (noun has a) f=1;
  4320.    if (f)
  4321.    {   print "  has ";
  4322.        for (a=0:a<48:a++) if (noun has a) print (DebugAttribute) a, " ";
  4323.        new_line;
  4324.    }
  4325.    if (noun ofclass Class) return;
  4326.  
  4327.    f=0; l = #identifiers_table-->0;
  4328.    for (a=1:a<=l:a++)
  4329.    {   if ((a~=2 or 3) && noun.&a)
  4330.        {   if (f==0) { print "  with "; f=1; }
  4331.            print (property) a;
  4332.            n = noun.#a;
  4333.            for (c=0:2*c<n:c++)
  4334.            {   print " ";
  4335.                x = (noun.&a)-->c;
  4336.                if (a==name) print "'", (address) x, "'";
  4337.                else
  4338.                {   if (a==number or capacity or time_left)
  4339.                        print x;
  4340.                    else
  4341.                    {   switch(x)
  4342.                        {   NULL: print "NULL";
  4343.                            0: print "0";
  4344.                            1: print "1";
  4345.                            default:
  4346.                            switch(metaclass(x))
  4347.                            {   Class, Object: print (name) x;
  4348.                                String: print "~", (string) x, "~";
  4349.                                Routine: print "[...]";
  4350.                            }
  4351.                            print " (", x, ")";
  4352.                        }
  4353.                    }
  4354.                }
  4355.            }
  4356.            print ",^       ";
  4357.        }
  4358.    }
  4359. !   if (f==1) new_line;
  4360. ];
  4361. #ENDIF;
  4362.  
  4363. ! ----------------------------------------------------------------------------
  4364. !  Except in Version 3, the DrawStatusLine routine does just that: this is
  4365. !  provided explicitly so that it can be Replace'd to change the style, and
  4366. !  as written it emulates the ordinary Standard game status line, which is
  4367. !  drawn in hardware
  4368. ! ----------------------------------------------------------------------------
  4369.  
  4370. #IFV5;
  4371. [ DrawStatusLine width posa posb;
  4372.    @split_window 1; @set_window 1; @set_cursor 1 1; style reverse;
  4373.    width = 0->33; posa = width-26; posb = width-13;
  4374.    spaces width;
  4375.    @set_cursor 1 2;
  4376.    if (location == thedark) print (name) location;
  4377.    else
  4378.    {   FindVisibilityLevels();
  4379.        if (visibility_ceiling == location)
  4380.            print (name) location;
  4381.        else print (The) visibility_ceiling;
  4382.    }
  4383.    if ((0->1)&2 == 0)
  4384.    {   if (width > 76)
  4385.        {   @set_cursor 1 posa; print (string) SCORE__TX, sline1;
  4386.            @set_cursor 1 posb; print (string) MOVES__TX, sline2;
  4387.        }
  4388.        if (width > 63 && width <= 76)
  4389.        {   @set_cursor 1 posb; print sline1, "/", sline2;
  4390.        }
  4391.    }
  4392.    else
  4393.    {   @set_cursor 1 posa;
  4394.        print (string) TIME__TX;
  4395.        LanguageTimeOfDay(sline1, sline2);
  4396.    }
  4397.    @set_cursor 1 1; style roman; @set_window 0;
  4398. ];
  4399. #ENDIF;
  4400.  
  4401. #ifv5;
  4402. Array StorageForShortName --> 161;
  4403. #endif;
  4404.  
  4405. [ PrefaceByArticle o acode pluralise  i artform findout;
  4406.  
  4407.    if (o provides articles)
  4408.    {   print (string) (o.&articles)-->(acode+short_name_case*LanguageCases),
  4409.            " ";
  4410.        if (pluralise) return;
  4411.        print (PSN__) o; return;
  4412.    }
  4413.  
  4414.    i = GetGNAOfObject(o);
  4415.    if (pluralise)
  4416.    {   if (i<3 || (i>=6 && i<9)) i = i + 3;
  4417.    }
  4418.    i = LanguageGNAsToArticles-->i;
  4419.  
  4420.    artform = LanguageArticles
  4421.              + 6*LanguageContractionForms*(short_name_case + i*LanguageCases);
  4422.  
  4423. #iftrue LanguageContractionForms == 2;
  4424.    if (artform-->acode ~= artform-->(acode+3)) findout = true;
  4425. #endif;
  4426. #iftrue LanguageContractionForms == 3;
  4427.    if (artform-->acode ~= artform-->(acode+3)) findout = true;
  4428.    if (artform-->(acode+3) ~= artform-->(acode+6)) findout = true;
  4429. #endif;
  4430. #iftrue LanguageContractionForms == 4;
  4431.    if (artform-->acode ~= artform-->(acode+3)) findout = true;
  4432.    if (artform-->(acode+3) ~= artform-->(acode+6)) findout = true;
  4433.    if (artform-->(acode+6) ~= artform-->(acode+9)) findout = true;
  4434. #endif;
  4435. #iftrue LanguageContractionForms > 4;
  4436.    findout = true;
  4437. #endif;
  4438.    if (standard_interpreter ~= 0 && findout)
  4439.    {   StorageForShortName-->0 = 160;
  4440.        @output_stream 3 StorageForShortName;
  4441.        if (pluralise) print (number) pluralise; else print (PSN__) o;
  4442.        @output_stream -3;
  4443.        acode = acode + 3*LanguageContraction(StorageForShortName + 2);
  4444.    }
  4445.  
  4446.    print (string) artform-->acode;
  4447.    if (pluralise) return;
  4448.    print (PSN__) o;
  4449. ];
  4450.  
  4451. [ PSN__ o;
  4452.    if (o==0) { print (string) NOTHING__TX; rtrue; }
  4453.    switch(metaclass(o))
  4454.    {   Routine: print "<routine ", o, ">"; rtrue;
  4455.        String:  print "<string ~", (string) o, "~>"; rtrue;
  4456.        nothing: print "<illegal object number ", o, ">"; rtrue;
  4457.    }
  4458.    if (o==player) { print (string) YOURSELF__TX; rtrue; }
  4459.    #ifdef LanguagePrintShortName;
  4460.    if (LanguagePrintShortName(o)) rtrue;
  4461.    #endif;
  4462.    if (indef_mode && o.&short_name_indef~=0
  4463.        && PrintOrRun(o, short_name_indef, 1)~=0) rtrue;
  4464.    if (o.&short_name~=0 && PrintOrRun(o,short_name,1)~=0) rtrue;
  4465.    @print_obj o;
  4466. ];
  4467.  
  4468. [ Indefart o i;
  4469.    i = indef_mode; indef_mode = true;
  4470.    if (o has proper) { indef_mode = NULL; print (PSN__) o; return; }
  4471.    if (o provides article)
  4472.    {   PrintOrRun(o,article,1); print " ", (PSN__) o; indef_mode = i; return;
  4473.    }
  4474.    PrefaceByArticle(o, 2); indef_mode = i;
  4475. ];
  4476. [ Defart o i;
  4477.    i = indef_mode; indef_mode = false;
  4478.    if (o has proper)
  4479.    { indef_mode = NULL; print (PSN__) o; indef_mode = i; return; }
  4480.    PrefaceByArticle(o, 1); indef_mode = i;
  4481. ];
  4482. [ CDefart o i;
  4483.    i = indef_mode; indef_mode = false;
  4484.    if (o has proper)
  4485.    { indef_mode = NULL; print (PSN__) o; indef_mode = i; return; }
  4486.    PrefaceByArticle(o, 0); indef_mode = i;
  4487. ];
  4488.  
  4489. [ PrintShortName o i;
  4490.    i = indef_mode; indef_mode = NULL;
  4491.    PSN__(o); indef_mode = i;
  4492. ];
  4493.  
  4494. [ EnglishNumber n; LanguageNumber(n); ];
  4495.  
  4496. [ NumberWord o i n;
  4497.   n = LanguageNumbers-->0;
  4498.   for (i=1:i<=n:i=i+2)
  4499.       if (o == LanguageNumbers-->i)
  4500.           return LanguageNumbers-->(i+1);
  4501.   return 0;
  4502. ];
  4503.  
  4504. [ RandomEntry tab;
  4505.   if (tab-->0==0) return RunTimeError(8);
  4506.   return tab-->(random(tab-->0));
  4507. ];
  4508.  
  4509. ! ----------------------------------------------------------------------------
  4510. !  Useful routine: unsigned comparison (for addresses in Z-machine)
  4511. !    Returns 1 if x>y, 0 if x=y, -1 if x<y
  4512. ! ----------------------------------------------------------------------------
  4513.  
  4514. [ UnsignedCompare x y u v;
  4515.   if (x==y) return 0;
  4516.   if (x<0 && y>=0) return 1;
  4517.   if (x>=0 && y<0) return -1;
  4518.   u = x&$7fff; v= y&$7fff;
  4519.   if (u>v) return 1;
  4520.   return -1;
  4521. ];
  4522.  
  4523. ! ----------------------------------------------------------------------------
  4524.