Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Grammar::assignment(std::string idName)
- {
- /**
- * Production: <assignment> --> <id use> equal_sign <expression> semicolon
- */
- /*std::cout << "assignment" << std::endl;*/
- //DONE: Add Code Here
- std::string idUseStmt;
- if(idUse(idName,idUseStmt)){
- if(parse->curToken()
- && parse->curToken()->getSymType() == Token::SYMTYPE_EQUAL)
- {
- idUseStmt.append(" = ");
- if (parse->nextToken()){
- ASTNode *root=NULL;
- if (expression(&root) && parse->curToken() && parse->curToken()->getSymType()==Token::SYMTYPE_SEMICOLON){
- parse->evaluateASTTree(root,false,false);
- idUseStmt.append(root->value);
- idUseStmt.append(";");
- parse->getSymbolTable()->curFunction->funcStats.push(idUseStmt);
- delete root;
- parse->nextToken();
- return true;
- }
- }
- }
- }
- return false;
- }
- bool Grammar::ifStatement()
- {
- /**
- * Production: <if statement> --> if left_parenthesis <condition expression> right_parenthesis <block statements>
- */
- /*std::cout << "ifStatement" << std::endl;*/
- //DONE: Add Code Here
- ASTNode *root = NULL;
- if (parse->curToken()
- && parse->curToken()->getID() == Token::IDTYPE_RESERVEDWORD
- && !strcmp(parse->curToken()->getTokenName().c_str(), "if")
- && parse->nextToken()
- && parse->curToken()->getSymType() == Token::SYMTYPE_LEFT_PARENTHESIS
- && parse->nextToken()
- && conditionExpression(&root)
- && parse->curToken()
- && parse->curToken()->getSymType() == Token::SYMTYPE_RIGHT_PARENTHESIS)
- {
- //evaluating AST for condition syntax
- parse->evaluateASTTree(root);
- int ifLable = parse->getSymbolTable()->lableCnt++;
- int elseLable = parse->getSymbolTable()->lableCnt++;
- Util::printIfStmt(parse->getSymbolTable(), root, ifLable, elseLable);
- //evaluating parse tree done. no more need for the tree.
- delete root;
- // add the string for if label
- // std::string ifGotoLabelStmt("c");
- // ifGotoLabelStmt.append(Util::to_string(ifLable));
- // ifGotoLabelStmt.append(":;");
- // parse->getSymbolTable()->curFunction->funcStats.push(ifGotoLabelStmt); //pushes string to the file
- // go to next token
- parse->nextToken();
- if (blockStatements()) //prints block statement
- {
- //else goto c1
- std::string elseGotoLabelStmt("c");
- elseGotoLabelStmt.append(Util::to_string(elseLable));
- elseGotoLabelStmt.append(":;");
- parse->getSymbolTable()->curFunction->funcStats.push(elseGotoLabelStmt);
- return true;
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement