Browse Source

advance in parser

AbdeFaiz 5 months ago
parent
commit
296d0cb62b
4 changed files with 96 additions and 13 deletions
  1. 5 2
      Makefile
  2. 83 4
      src/Parser.cpp
  3. 5 1
      src/Parser.hpp
  4. 3 6
      src/main.cpp

+ 5 - 2
Makefile

@@ -7,10 +7,13 @@ all: test.out
 Token.o : src/Token.cpp src/Token.hpp
 	$(CC) -c $(FLAGS) $< 
 
-Lexer.o : src/Lexer.cpp src/Lexer.hpp
+Lexer.o : src/Lexer.cpp src/Lexer.hpp Token.o
 	$(CC) -c $(FLAGS) $< 
 
-test.out: src/main.cpp Token.o Lexer.o
+Parser.o : src/Parser.cpp src/Parser.hpp Lexer.o Token.o
+	$(CC) -c $(FLAGS) $< 
+
+test.out: src/main.cpp Token.o Lexer.o Parser.o
 	$(CC) $(FLAGS) $^ -o $@
 	./$@ $(EXAMPLE)
 

+ 83 - 4
src/Parser.cpp

@@ -7,11 +7,11 @@ Parser::Parser(Lexer lexer) : lexer(lexer), curToken(Token()), peekToken(Token()
 }
 bool Parser::checkToken(TokenType kind)
 {
-  return kind = (this->curToken).get_kind();
+  return kind == (this->curToken).get_kind();
 }
 bool Parser::checkPeek(TokenType kind)
 {
-  return kind = (this->peekToken).get_kind();
+  return kind == (this->peekToken).get_kind();
 }
 void Parser::match(TokenType kind)
 {
@@ -22,19 +22,31 @@ void Parser::match(TokenType kind)
   nextToken();
 }
 
-Token Parser::nextToken()
+void Parser::nextToken()
 {
   this->curToken = this->peekToken;
   this->peekToken = (this->lexer).getToken();
 }
+
 void Parser::abort(std::string message)
 {
   std::cout << "Error: " << message << std::endl;
   exit(1);
 }
 
+bool Parser::isComparisonOperator(){
+  return (checkToken(TokenType::GT)   || 
+          checkToken(TokenType::GTEQ) ||
+          checkToken(TokenType::EQEQ) ||
+          checkToken(TokenType::NOTEQ)||
+          checkToken(TokenType::LTEQ) ||
+          checkToken(TokenType::LT)
+        );
+}
+
 void Parser::program()
 {
+  std::cout << "PROGRAM" << std::endl;
   while (checkToken(TokenType::NEWLINE)){
     nextToken();
   }
@@ -46,6 +58,7 @@ void Parser::program()
 
 void Parser::statement()
 {
+  std::cout << "STATEMENT" << std::endl;
   // "PRINT" (expression | string)
   if (checkToken(TokenType::PRINT))
   {
@@ -108,7 +121,7 @@ void Parser::statement()
     expression();
   }
   // "INPUT" ident
-  if (checkToken(TokenType::INPUT)){
+  else if (checkToken(TokenType::INPUT)){
     nextToken();
     ident();
   }
@@ -116,11 +129,77 @@ void Parser::statement()
   else{
     abort("Invalid statement at "+ (this->curToken).get_text() + "(" + token_name((this->curToken).get_kind()) + ")");
   }
+
   nl();
 }
 
+void Parser::comparison()
+{
+  std::cout << "COMPARISON" << std::endl;
+  expression();
+  if (isComparisonOperator()){
+    nextToken();
+    expression();
+  }
+  else {
+    abort("Expected comparison operator at: "+(this->curToken).get_text());
+  }
+
+  while (isComparisonOperator()){
+    nextToken();
+    expression();
+  }
+}
+
+void Parser::term(){
+  std::cout << "TERM" << std::endl;
+  unary();
+  while(checkToken(TokenType::ASTERISK) || checkToken(TokenType::SLASH)){
+    nextToken();
+    unary();
+  }
+}
+
+void Parser::unary(){
+  std::cout << "UNARY" << std::endl;
+  if(checkToken(TokenType::PLUS) || checkToken(TokenType::MINUS)){
+    nextToken();
+  }
+  primary();
+}
+
+void Parser::primary(){
+  std::cout << "PRIMARY" << " (" << (this->curToken).get_text() << ")"<< std::endl;
+  if(checkToken(TokenType::NUMBER)){
+    nextToken();
+  }
+  else if(checkToken(TokenType::IDENT)){
+    nextToken();
+  }
+  else{
+    abort("Unexpected token at "+ (this->curToken).get_text());
+  }
+}
+
+void Parser::expression()
+{
+  std::cout << "EXPRESSION" << std::endl;
+  term();
+  while(checkToken(TokenType::PLUS) || checkToken(TokenType::MINUS)){
+    nextToken();
+    term();
+  }
+}
+
+void Parser::ident()
+{
+  std::cout << "IDENT" << std::endl;
+  match(TokenType::IDENT);
+}
+
 void Parser::nl()
 {
+  std::cout << "NEWLINE" << std::endl;
   match(TokenType::NEWLINE);
   while (checkToken(TokenType::NEWLINE))
   {

+ 5 - 1
src/Parser.hpp

@@ -10,13 +10,17 @@ class Parser {
   bool checkToken(TokenType kind);
   bool checkPeek(TokenType kind);
   void match(TokenType kind);
-  Token nextToken();
+  void nextToken();
   void abort(std::string message);
+  bool isComparisonOperator();
 
   void program();
   void statement();
   void expression();
   void comparison();
+  void term();
+  void unary();
+  void primary();
   void ident();
   void nl();
 };

+ 3 - 6
src/main.cpp

@@ -1,7 +1,7 @@
 #include <iostream>
 #include <fstream>
 #include <sstream>
-#include "Lexer.hpp"
+#include "Parser.hpp"
 
 int main(int argc, char* argv[])
 {
@@ -22,10 +22,7 @@ int main(int argc, char* argv[])
   std::string source = ss.str();
 
   Lexer mylexer(source);
-  Token t = mylexer.getToken();
-  while(t.get_kind() != END_OF_FILE){
-    std::cout << token_name(t.get_kind()) << ": " << t.get_text() << std::endl;
-    t = mylexer.getToken();
-  }
+  Parser myparser(mylexer);
+  myparser.program();
   return 0;
 }