Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local_procedure_body Parse_IF_Statement (
- alters Character_IStream& str,
- alters Tokenizing_Machine& m,
- alters Text& token_text,
- alters Integer& token_kind,
- produces Statement_Parse_1& stmnt
- )
- /*!
- requires
- str.is_open = true and
- token_text = "IF" and
- token_kind = KEYWORD and
- m.ready_to_dispense = false
- ensures
- if there exists x, y: string of character, s: STATEMENT
- (#token_text * #m.buffer * #str.content = x * y and
- STATEMENT_TO_STRING_OF_TOKENS (s) =
- REMOVE_SEPARATORS (TOKENIZE_PROGRAM_TEXT (x)))
- then
- str.is_open = true and
- str.ext_name = #str.ext_name and
- there exists z: string of character
- (#token_text * #m.buffer * #str.content =
- z * token_text * m.buffer * str.content and
- STATEMENT_TO_STRING_OF_TOKENS (stmnt) =
- REMOVE_SEPARATORS (TOKENIZE_PROGRAM_TEXT (z)) and
- m.ready_to_dispense = false)
- !*/
- {
- object Integer cond;
- object Statement_Parse_1 IF_Block;
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind); // Get the condition
- assert(token_kind == CONDITION, "Expected condition after keyword 'IF'.");
- Parse_Condition (token_text, cond); // Parse condition
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind); // Trash "THEN" token
- assert(token_text.Is_Equal_To("THEN"), "Expected keyword 'THEN' after condition.");
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind);// Get
- // next token(block)
- IF_Block.Parse_Block(str, m, token_text, token_kind);
- //Parses the IF which should then give us ELSE or END
- if (token_text == "ELSE")
- {
- object Statement_Parse_1 ELSE_Block;
- m.Get_Next_Non_Separator_Token(str, token_text, token_kind);
- ELSE_Block.Parse_Block (str, m, token_text, token_kind);//Should give us END token
- assert(token_text.Is_Equal_To("END"), "Expected keyword 'END' after IF Block.");
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind); // Trash IF token
- assert(token_text.Is_Equal_To("IF"), "Expected keyword 'IF' after 'END'.");
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind); // Get next
- // usable token
- stmnt.Compose_If_Else (cond, IF_Block, ELSE_Block); //Compose IF_ELSE
- }
- else
- {
- assert(token_text.Is_Equal_To("END"), "Expected keyword 'END' after If Block.");
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind); // Trash IF token
- assert(token_text.Is_Equal_To("IF"), "Expected keyword 'IF' after 'END'.");
- stmnt.Compose_If (cond, IF_Block); // Compose IF
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind); //Get next token
- }
- }
- local_procedure_body Parse_WHILE_Statement (
- alters Character_IStream& str,
- alters Tokenizing_Machine& m,
- alters Text& token_text,
- alters Integer& token_kind,
- produces Statement_Parse_1& stmnt
- )
- /*!
- requires
- str.is_open = true and
- token_text = "WHILE" and
- token_kind = KEYWORD and
- m.ready_to_dispense = false
- ensures
- if there exists x, y: string of character, s: STATEMENT
- (#token_text * #m.buffer * #str.content = x * y and
- STATEMENT_TO_STRING_OF_TOKENS (s) =
- REMOVE_SEPARATORS (TOKENIZE_PROGRAM_TEXT (x)))
- then
- str.is_open = true and
- str.ext_name = #str.ext_name and
- there exists z: string of character
- (#token_text * #m.buffer * #str.content =
- z * token_text * m.buffer * str.content and
- STATEMENT_TO_STRING_OF_TOKENS (stmnt) =
- REMOVE_SEPARATORS (TOKENIZE_PROGRAM_TEXT (z)) and
- m.ready_to_dispense = false)
- !*/
- {
- object Integer cond;
- object Statement_Parse_1 WHILE_Block;
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind); // Get the condition
- debug("tt: " << token_text);
- assert(token_kind == CONDITION, "Expected condition after WHILE");
- Parse_Condition (token_text, cond); // Parse condition
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind); // Trash "DO" token
- assert(token_text.Is_Equal_To("DO"), "Expected 'DO' keyword after condition");
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind);
- WHILE_Block.Parse_Block (str, m, token_text, token_kind); //Should give us END for token_text
- assert(token_text.Is_Equal_To("END"), "Expected 'END' keyword after WHILE block");
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind); // Trash WHILE token
- assert(token_text.Is_Equal_To("WHILE"), "Expected 'WHILE' keyword after 'END'");
- stmnt.Compose_While (cond, WHILE_Block);
- m.Get_Next_Non_Separator_Token (str, token_text, token_kind); //Grab next token
- }
- local_procedure_body Parse_Condition (
- preserves Text cond,
- produces Integer& value
- )
- /*!
- requires
- IS_CONDITION_NAME (cond)
- ensures
- cond = CONDITION_TO_TOKEN (value)
- !*/
- {
- object Boolean test;
- value.Clear();
- if(cond.Is_Equal_To("next-is-empty"))
- {
- value = NEXT_IS_EMPTY;
- test = true;
- }
- else if(cond.Is_Equal_To("next-is-not-empty"))
- {
- value = NEXT_IS_NOT_EMPTY;
- test = true;
- }
- else if(cond.Is_Equal_To("next-is-wall"))
- {
- value = NEXT_IS_WALL;
- test = true;
- }
- else if(cond.Is_Equal_To("next-is-not-wall"))
- {
- value = NEXT_IS_NOT_WALL;
- test = true;
- }
- else if(cond.Is_Equal_To("next-is-friend"))
- {
- value = NEXT_IS_FRIEND;
- test = true;
- }
- else if(cond.Is_Equal_To("next-is-not-friend"))
- {
- value = NEXT_IS_NOT_FRIEND;
- test = true;
- }
- else if(cond.Is_Equal_To("next-is-enemy"))
- {
- value = NEXT_IS_ENEMY;
- test = true;
- }
- else if(cond.Is_Equal_To("next-is-not-enemy"))
- {
- value = NEXT_IS_NOT_ENEMY;
- test = true;
- }
- else if(cond.Is_Equal_To("random"))
- {
- value = RANDOM;
- test = true;
- }
- else if(cond.Is_Equal_To("true"))
- {
- value = TRUE;
- test = true;
- }
- assert(test, "Expected Condition in the set of Valid Conditions");
- }
- public:
- procedure_body Parse (
- alters Character_IStream& str,
- alters Tokenizing_Machine& m,
- alters Text& token_text,
- alters Integer& token_kind
- )
- {
- if(token_kind == KEYWORD)
- {
- object Statement_Parse_1 stmnt;
- assert((token_text.Is_Equal_To("IF") or token_text.Is_Equal_To("WHILE")), "Expected IF or WHILE");
- if (token_text.Is_Equal_To("IF"))
- {
- Parse_IF_Statement(str, m, token_text, token_kind, stmnt);
- self &= stmnt;
- }
- else if (token_text.Is_Equal_To("WHILE"))
- {
- Parse_WHILE_Statement(str, m, token_text, token_kind, stmnt);
- self &= stmnt;
- }
- }
- else
- {
- debug("tt cp: " << token_text);
- assert(token_kind == IDENTIFIER, "Expected IDENTIFIER");
- self.Compose_Call(token_text);
- //Grab the next token
- m.Get_Next_Non_Separator_Token(str, token_text, token_kind);
- }
- }
- procedure_body Parse_Block (
- alters Character_IStream& str,
- alters Tokenizing_Machine& m,
- alters Text& token_text,
- alters Integer& token_kind
- )
- {
- object Integer i;
- while(not(token_text.Is_Equal_To("ELSE") and token_text.Is_Equal_To("END")))
- {
- debug("tt pb" << token_text);
- object Statement_Parse_1 s;
- s.Parse(str, m, token_text, token_kind); //Parse the statement
- self.Add_To_Block(i, s); //Add it to the block
- i++;//Increase i so we put things in in the right order
- }
- }
Add Comment
Please, Sign In to add comment