Parser.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "Parser.hpp"
  2. Parser::Parser(Lexer lexer) : lexer(lexer), curToken(Token()), peekToken(Token())
  3. {
  4. nextToken();
  5. nextToken();
  6. }
  7. bool Parser::checkToken(TokenType kind)
  8. {
  9. return kind = (this->curToken).get_kind();
  10. }
  11. bool Parser::checkPeek(TokenType kind)
  12. {
  13. return kind = (this->peekToken).get_kind();
  14. }
  15. void Parser::match(TokenType kind)
  16. {
  17. if (!checkToken(kind))
  18. {
  19. abort("Expected " + token_name(kind) + ", got " + token_name((this->curToken).get_kind()));
  20. }
  21. nextToken();
  22. }
  23. Token Parser::nextToken()
  24. {
  25. this->curToken = this->peekToken;
  26. this->peekToken = (this->lexer).getToken();
  27. }
  28. void Parser::abort(std::string message)
  29. {
  30. std::cout << "Error: " << message << std::endl;
  31. exit(1);
  32. }
  33. void Parser::program()
  34. {
  35. while (checkToken(TokenType::NEWLINE)){
  36. nextToken();
  37. }
  38. while (!checkToken(TokenType::END_OF_FILE))
  39. {
  40. statement();
  41. }
  42. }
  43. void Parser::statement()
  44. {
  45. // "PRINT" (expression | string)
  46. if (checkToken(TokenType::PRINT))
  47. {
  48. nextToken();
  49. if (checkToken(TokenType::STRING))
  50. {
  51. nextToken();
  52. }
  53. else
  54. {
  55. expression();
  56. }
  57. }
  58. // "IF" comparison "THEN" nl {statement} "ENDIF"
  59. else if (checkToken(TokenType::IF))
  60. {
  61. nextToken();
  62. comparison();
  63. match(TokenType::THEN);
  64. nl();
  65. while (!checkToken(TokenType::ENDIF))
  66. {
  67. statement();
  68. }
  69. match(TokenType::ENDIF);
  70. }
  71. // "WHILE" comparison "REPEAT" nl {statement nl} "ENDWHILE"
  72. else if (checkToken(TokenType::WHILE))
  73. {
  74. nextToken();
  75. comparison();
  76. match(TokenType::REPEAT);
  77. nl();
  78. while (!checkToken(TokenType::ENDWHILE))
  79. {
  80. statement();
  81. }
  82. match(TokenType::ENDWHILE);
  83. }
  84. // "LABEL" ident
  85. else if (checkToken(TokenType::LABEL)){
  86. nextToken();
  87. ident();
  88. }
  89. // "GOTO" ident
  90. else if (checkToken(TokenType::GOTO)){
  91. nextToken();
  92. ident();
  93. }
  94. // "LET" ident "=" expression
  95. else if (checkToken(TokenType::LET)){
  96. nextToken();
  97. ident();
  98. match(TokenType::EQ);
  99. expression();
  100. }
  101. // "INPUT" ident
  102. if (checkToken(TokenType::INPUT)){
  103. nextToken();
  104. ident();
  105. }
  106. else{
  107. abort("Invalid statement at "+ (this->curToken).get_text() + "(" + token_name((this->curToken).get_kind()) + ")");
  108. }
  109. nl();
  110. }
  111. void Parser::nl()
  112. {
  113. match(TokenType::NEWLINE);
  114. while (checkToken(TokenType::NEWLINE))
  115. {
  116. nextToken();
  117. }
  118. }