home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / parser.adb < prev    next >
Encoding:
Text File  |  1995-11-21  |  2.7 KB  |  82 lines

  1.  
  2. with Text_IO, Ada.Strings.Maps.Constants, Ustrings, Things, Occupants, World;
  3. use  Text_IO, Ada.Strings.Maps.Constants, Ustrings, Things, Occupants, World;
  4. use  Ada.Strings, Ada.Strings.Maps;
  5.  
  6. with Directions;
  7. use  Directions;
  8.  
  9. package body Parser is
  10.  
  11.  Spaces : constant Character_Set := To_Set(' ');
  12.  
  13.  procedure Split(Source     : in  Unbounded_String;
  14.                  First_Word : out Unbounded_String;
  15.                  Rest       : out Unbounded_String) is
  16.   First : Positive; -- Index values of first word.
  17.   Last  : Natural;
  18.  -- Puts first word of Source into First_Word, the rest of the words in Rest
  19.  -- (without leading spaces); words are separated by one or more spaces;
  20.  -- if there are no spaces, Rest returns empty.
  21.  begin
  22.   Find_Token(Source, Spaces, Outside, First, Last);
  23.   First_Word := U(Slice(Source, First, Last));
  24.   Rest       := Trim(U(Slice(Source, Last + 2, Length(Source))), Left);
  25.  end Split;
  26.  
  27.  
  28.  
  29.  procedure Execute(Command : in Unbounded_String; Quit : out Boolean) is
  30.   Trimmed_Command : Unbounded_String := Trim(Command, Both);
  31.   Verb, Arguments, First_Argument, Rest_Of_Arguments : Unbounded_String;
  32.   Direct_Object : Occupant_Access;
  33.  begin
  34.   Quit := False; -- By default assume we won't quit.
  35.   if (Empty(Trimmed_Command)) then
  36.     return;      -- Ignore blank lines.
  37.   end if;
  38.  
  39.   -- Extract Verb and First_Argument and force them to lower case.
  40.   Split(Trimmed_Command, Verb, Arguments);
  41.   Translate(Verb, Lower_Case_Map);
  42.   Split(Arguments, First_Argument, Rest_Of_Arguments);
  43.   Translate(First_Argument, Lower_Case_Map);
  44.  
  45.  
  46.   -- Try to execute "Verb".
  47.  
  48.   if    Verb = "look" then
  49.     Look(Me);
  50.   elsif Verb = "get" then
  51.     Direct_Object := Occupant_Access(Find(Me, First_Argument));
  52.     if Direct_Object /= null then
  53.       Get(Me, Direct_Object);
  54.     end if;
  55.   elsif Verb = "drop" then
  56.     Direct_Object := Occupant_Access(Find_Inside(Me, First_Argument));
  57.     if Direct_Object /= null then
  58.       Drop(Me, Direct_Object);
  59.     end if;
  60.   elsif Verb = "inventory" or Verb = "inv" then
  61.     Inventory(Me);
  62.   elsif Verb = "quit" then
  63.     Quit := True;
  64.   elsif Verb = "go" and then Is_Direction(First_Argument) then
  65.     Go(Me, To_Direction(First_Argument));
  66.     Look(Me);
  67.   elsif Is_Direction(Verb) then  -- Is the verb a direction (north, etc)?
  68.     Go(Me, To_Direction(Verb));
  69.     Look(Me);
  70.   elsif Verb = "help" then
  71.     Put_Line("Please type in one or two word commands, beginning with a verb");
  72.     Put_Line("or direction. Directions are north, south, east, west, etc.");
  73.     Put_Line("Here are some sample commands:");
  74.     Put_Line("look, get box, drop box, inventory, go west, west, w, quit.");
  75.   else
  76.    Put_Line("Sorry, I don't recognize that verb. Try 'help'.");
  77.   end if;
  78.   
  79.  end Execute;
  80. end Parser;
  81.  
  82.