daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Jun 23rd, 2018 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local_procedure_body Parse_IF_Statement (
  2.             alters Character_IStream& str,
  3.             alters Tokenizing_Machine& m,
  4.             alters Text& token_text,
  5.             alters Integer& token_kind,
  6.             produces Statement_Parse_1& stmnt
  7.         )
  8.     /*!
  9.     requires
  10.         str.is_open = true and
  11.         token_text = "IF" and
  12.         token_kind = KEYWORD and
  13.         m.ready_to_dispense = false
  14.     ensures
  15.         if there exists x, y: string of character, s: STATEMENT
  16.            (#token_text * #m.buffer * #str.content = x * y and
  17.             STATEMENT_TO_STRING_OF_TOKENS (s) =
  18.             REMOVE_SEPARATORS (TOKENIZE_PROGRAM_TEXT (x)))
  19.         then
  20.         str.is_open = true and
  21.         str.ext_name = #str.ext_name and
  22.         there exists z: string of character
  23.             (#token_text * #m.buffer * #str.content =
  24.              z * token_text * m.buffer * str.content and
  25.              STATEMENT_TO_STRING_OF_TOKENS (stmnt) =
  26.              REMOVE_SEPARATORS (TOKENIZE_PROGRAM_TEXT (z)) and
  27.              m.ready_to_dispense = false)
  28.     !*/
  29.     {
  30.     object Integer cond;
  31.     object Statement_Parse_1 IF_Block;
  32.  
  33.     m.Get_Next_Non_Separator_Token (str, token_text, token_kind);   // Get the condition
  34.     assert(token_kind == CONDITION, "Expected condition after keyword 'IF'.");
  35.     Parse_Condition (token_text, cond);          // Parse condition
  36.     m.Get_Next_Non_Separator_Token (str, token_text, token_kind);  // Trash "THEN" token
  37.     assert(token_text.Is_Equal_To("THEN"), "Expected keyword 'THEN' after condition.");
  38.     m.Get_Next_Non_Separator_Token (str, token_text, token_kind);// Get
  39.                                 // next token(block)
  40.     IF_Block.Parse_Block(str, m, token_text, token_kind);
  41.     //Parses the IF which should then give us ELSE or END
  42.     if (token_text == "ELSE")
  43.     {
  44.         object Statement_Parse_1 ELSE_Block;
  45.         m.Get_Next_Non_Separator_Token(str, token_text, token_kind);
  46.         ELSE_Block.Parse_Block (str, m, token_text, token_kind);//Should give us END token
  47.         assert(token_text.Is_Equal_To("END"), "Expected keyword 'END' after IF Block.");
  48.         m.Get_Next_Non_Separator_Token (str, token_text, token_kind);  // Trash IF token
  49.         assert(token_text.Is_Equal_To("IF"), "Expected keyword 'IF' after 'END'.");
  50.         m.Get_Next_Non_Separator_Token (str, token_text, token_kind);  // Get next
  51.                             // usable token
  52.         stmnt.Compose_If_Else (cond, IF_Block, ELSE_Block); //Compose IF_ELSE
  53.     }
  54.     else
  55.     {
  56.         assert(token_text.Is_Equal_To("END"), "Expected keyword 'END' after If Block.");
  57.         m.Get_Next_Non_Separator_Token (str, token_text, token_kind);  // Trash IF token
  58.         assert(token_text.Is_Equal_To("IF"), "Expected keyword 'IF' after 'END'.");
  59.         stmnt.Compose_If (cond, IF_Block);          // Compose IF
  60.         m.Get_Next_Non_Separator_Token (str, token_text, token_kind); //Get next token
  61.     }
  62.     }
  63.  
  64.     local_procedure_body Parse_WHILE_Statement (
  65.             alters Character_IStream& str,
  66.             alters Tokenizing_Machine& m,
  67.             alters Text& token_text,
  68.             alters Integer& token_kind,
  69.             produces Statement_Parse_1& stmnt
  70.         )
  71.     /*!
  72.     requires
  73.         str.is_open = true and
  74.         token_text = "WHILE" and
  75.         token_kind = KEYWORD and
  76.         m.ready_to_dispense = false
  77.     ensures
  78.         if there exists x, y: string of character, s: STATEMENT
  79.            (#token_text * #m.buffer * #str.content = x * y and
  80.             STATEMENT_TO_STRING_OF_TOKENS (s) =
  81.             REMOVE_SEPARATORS (TOKENIZE_PROGRAM_TEXT (x)))
  82.         then
  83.         str.is_open = true and
  84.         str.ext_name = #str.ext_name and
  85.         there exists z: string of character
  86.             (#token_text * #m.buffer * #str.content =
  87.              z * token_text * m.buffer * str.content and
  88.              STATEMENT_TO_STRING_OF_TOKENS (stmnt) =
  89.              REMOVE_SEPARATORS (TOKENIZE_PROGRAM_TEXT (z)) and
  90.              m.ready_to_dispense = false)
  91.     !*/
  92.     {
  93.     object Integer cond;
  94.     object Statement_Parse_1 WHILE_Block;
  95.  
  96.     m.Get_Next_Non_Separator_Token (str, token_text, token_kind);   // Get the condition
  97.     debug("tt: " << token_text);
  98.     assert(token_kind == CONDITION, "Expected condition after WHILE");
  99.     Parse_Condition (token_text, cond);         // Parse condition
  100.     m.Get_Next_Non_Separator_Token (str, token_text, token_kind);   // Trash "DO" token
  101.     assert(token_text.Is_Equal_To("DO"), "Expected 'DO' keyword after condition");
  102.     m.Get_Next_Non_Separator_Token (str, token_text, token_kind);
  103.     WHILE_Block.Parse_Block (str, m, token_text, token_kind); //Should give us END for token_text
  104.     assert(token_text.Is_Equal_To("END"), "Expected 'END' keyword after WHILE block");
  105.     m.Get_Next_Non_Separator_Token (str, token_text, token_kind);   // Trash WHILE token
  106.     assert(token_text.Is_Equal_To("WHILE"), "Expected 'WHILE' keyword  after 'END'");
  107.     stmnt.Compose_While (cond, WHILE_Block);
  108.     m.Get_Next_Non_Separator_Token (str, token_text, token_kind); //Grab next token
  109.    
  110.     }
  111.  
  112.     local_procedure_body Parse_Condition (
  113.             preserves Text cond,
  114.             produces Integer& value
  115.         )
  116.     /*!
  117.     requires
  118.         IS_CONDITION_NAME (cond)
  119.     ensures
  120.         cond = CONDITION_TO_TOKEN (value)
  121.     !*/
  122.     {
  123.     object Boolean test;
  124.     value.Clear();
  125.     if(cond.Is_Equal_To("next-is-empty"))
  126.     {
  127.         value = NEXT_IS_EMPTY;
  128.         test = true;
  129.     }
  130.     else if(cond.Is_Equal_To("next-is-not-empty"))
  131.     {
  132.         value = NEXT_IS_NOT_EMPTY;
  133.         test = true;
  134.     }
  135.     else if(cond.Is_Equal_To("next-is-wall"))
  136.     {
  137.         value = NEXT_IS_WALL;
  138.         test = true;
  139.     }
  140.     else if(cond.Is_Equal_To("next-is-not-wall"))
  141.     {
  142.         value = NEXT_IS_NOT_WALL;
  143.         test = true;
  144.     }
  145.     else if(cond.Is_Equal_To("next-is-friend"))
  146.     {
  147.         value = NEXT_IS_FRIEND;
  148.         test = true;
  149.     }
  150.     else if(cond.Is_Equal_To("next-is-not-friend"))
  151.     {
  152.         value = NEXT_IS_NOT_FRIEND;
  153.         test = true;
  154.     }
  155.     else if(cond.Is_Equal_To("next-is-enemy"))
  156.     {
  157.         value = NEXT_IS_ENEMY;
  158.         test = true;
  159.     }
  160.     else if(cond.Is_Equal_To("next-is-not-enemy"))
  161.     {
  162.         value = NEXT_IS_NOT_ENEMY;
  163.         test = true;
  164.     }
  165.     else if(cond.Is_Equal_To("random"))
  166.     {
  167.         value = RANDOM;
  168.         test = true;
  169.     }
  170.     else if(cond.Is_Equal_To("true"))
  171.     {
  172.         value = TRUE;
  173.         test = true;
  174.     }
  175.     assert(test, "Expected Condition in the set of Valid Conditions");
  176.     }
  177.  
  178. public:
  179.  
  180.     procedure_body Parse (
  181.             alters Character_IStream& str,
  182.             alters Tokenizing_Machine& m,
  183.             alters Text& token_text,
  184.             alters Integer& token_kind
  185.         )
  186.     {
  187.     if(token_kind == KEYWORD)
  188.     {
  189.         object Statement_Parse_1 stmnt;
  190.         assert((token_text.Is_Equal_To("IF") or token_text.Is_Equal_To("WHILE")), "Expected IF or WHILE");
  191.         if (token_text.Is_Equal_To("IF"))
  192.         {
  193.         Parse_IF_Statement(str, m, token_text, token_kind, stmnt);
  194.         self &= stmnt;
  195.         }
  196.         else if (token_text.Is_Equal_To("WHILE"))
  197.         {
  198.         Parse_WHILE_Statement(str, m, token_text, token_kind, stmnt);
  199.         self &= stmnt;
  200.         }
  201.     }
  202.     else
  203.     {
  204.         debug("tt cp: " << token_text);
  205.         assert(token_kind == IDENTIFIER, "Expected IDENTIFIER");
  206.         self.Compose_Call(token_text);
  207.         //Grab the next token
  208.         m.Get_Next_Non_Separator_Token(str, token_text, token_kind);
  209.     }
  210.    
  211.     }
  212.  
  213.     procedure_body Parse_Block (
  214.             alters Character_IStream& str,
  215.             alters Tokenizing_Machine& m,
  216.             alters Text& token_text,
  217.             alters Integer& token_kind
  218.         )
  219.     {
  220.     object Integer i;
  221.     while(not(token_text.Is_Equal_To("ELSE") and token_text.Is_Equal_To("END")))
  222.     {
  223.         debug("tt pb" << token_text);
  224.         object Statement_Parse_1 s;
  225.         s.Parse(str, m, token_text, token_kind); //Parse the statement
  226.         self.Add_To_Block(i, s); //Add it to the block
  227.         i++;//Increase i so we put things in in the right order
  228.     }
  229.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top