Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. bool Grammar::assignment(std::string idName)
  2. {
  3. /**
  4. * Production: <assignment> --> <id use> equal_sign <expression> semicolon
  5. */
  6. /*std::cout << "assignment" << std::endl;*/
  7. //DONE: Add Code Here
  8. std::string idUseStmt;
  9. if(idUse(idName,idUseStmt)){
  10. if(parse->curToken()
  11. && parse->curToken()->getSymType() == Token::SYMTYPE_EQUAL)
  12. {
  13. idUseStmt.append(" = ");
  14. if (parse->nextToken()){
  15. ASTNode *root=NULL;
  16. if (expression(&root) && parse->curToken() && parse->curToken()->getSymType()==Token::SYMTYPE_SEMICOLON){
  17. parse->evaluateASTTree(root,false,false);
  18. idUseStmt.append(root->value);
  19. idUseStmt.append(";");
  20. parse->getSymbolTable()->curFunction->funcStats.push(idUseStmt);
  21. delete root;
  22. parse->nextToken();
  23. return true;
  24. }
  25. }
  26. }
  27. }
  28. return false;
  29. }
  30.  
  31.  
  32. bool Grammar::ifStatement()
  33. {
  34. /**
  35. * Production: <if statement> --> if left_parenthesis <condition expression> right_parenthesis <block statements>
  36. */
  37. /*std::cout << "ifStatement" << std::endl;*/
  38. //DONE: Add Code Here
  39. ASTNode *root = NULL;
  40. if (parse->curToken()
  41. && parse->curToken()->getID() == Token::IDTYPE_RESERVEDWORD
  42. && !strcmp(parse->curToken()->getTokenName().c_str(), "if")
  43. && parse->nextToken()
  44. && parse->curToken()->getSymType() == Token::SYMTYPE_LEFT_PARENTHESIS
  45. && parse->nextToken()
  46. && conditionExpression(&root)
  47. && parse->curToken()
  48. && parse->curToken()->getSymType() == Token::SYMTYPE_RIGHT_PARENTHESIS)
  49. {
  50. //evaluating AST for condition syntax
  51. parse->evaluateASTTree(root);
  52. int ifLable = parse->getSymbolTable()->lableCnt++;
  53. int elseLable = parse->getSymbolTable()->lableCnt++;
  54. Util::printIfStmt(parse->getSymbolTable(), root, ifLable, elseLable);
  55.  
  56. //evaluating parse tree done. no more need for the tree.
  57. delete root;
  58.  
  59. // add the string for if label
  60. // std::string ifGotoLabelStmt("c");
  61. // ifGotoLabelStmt.append(Util::to_string(ifLable));
  62. // ifGotoLabelStmt.append(":;");
  63. // parse->getSymbolTable()->curFunction->funcStats.push(ifGotoLabelStmt); //pushes string to the file
  64.  
  65. // go to next token
  66. parse->nextToken();
  67. if (blockStatements()) //prints block statement
  68. {
  69. //else goto c1
  70. std::string elseGotoLabelStmt("c");
  71. elseGotoLabelStmt.append(Util::to_string(elseLable));
  72. elseGotoLabelStmt.append(":;");
  73. parse->getSymbolTable()->curFunction->funcStats.push(elseGotoLabelStmt);
  74. return true;
  75. }
  76. }
  77. return false;
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement