Browse Source

Take into account arithmetic operations

AbdeFaiz 5 months ago
parent
commit
070dec7e8a
3 changed files with 19 additions and 9 deletions
  1. 9 2
      src/Lexer.cpp
  2. 2 1
      src/Token.hpp
  3. 8 6
      src/main.cpp

+ 9 - 2
src/Lexer.cpp

@@ -63,6 +63,7 @@ Token Lexer::getToken()
     res = Token(std::string(1, this->curChar), TokenType::SLASH);
     res = Token(std::string(1, this->curChar), TokenType::SLASH);
 
 
   else if (this->curChar == '=')
   else if (this->curChar == '=')
+  {
     if (peek() == '=')
     if (peek() == '=')
     {
     {
       lastChar = this->curChar;
       lastChar = this->curChar;
@@ -73,8 +74,10 @@ Token Lexer::getToken()
     {
     {
       res = Token(std::string(1, this->curChar), TokenType::EQ);
       res = Token(std::string(1, this->curChar), TokenType::EQ);
     }
     }
+  }
 
 
-    else if (this->curChar == '>')
+  else if (this->curChar == '>')
+  {
     if (peek() == '=')
     if (peek() == '=')
     {
     {
       lastChar = this->curChar;
       lastChar = this->curChar;
@@ -85,8 +88,10 @@ Token Lexer::getToken()
     {
     {
       res = Token(std::string(1, this->curChar), TokenType::GT);
       res = Token(std::string(1, this->curChar), TokenType::GT);
     }
     }
+  }
 
 
-    else if (this->curChar == '<')
+  else if (this->curChar == '<')
+  {
     if (peek() == '=')
     if (peek() == '=')
     {
     {
       lastChar = this->curChar;
       lastChar = this->curChar;
@@ -97,6 +102,8 @@ Token Lexer::getToken()
     {
     {
       res = Token(std::string(1, this->curChar), TokenType::LT);
       res = Token(std::string(1, this->curChar), TokenType::LT);
     }
     }
+  }
 
 
+  nextChar();
   return res;
   return res;
 }
 }

+ 2 - 1
src/Token.hpp

@@ -42,9 +42,10 @@ TokenType checkIfKeyword(string &tokenText);
 
 
 class Token{
 class Token{
   string text;
   string text;
-  TokenType kind;
+  
 
 
   public:
   public:
+  TokenType kind;
   Token();
   Token();
   Token(string tokenText, TokenType tokenKind);
   Token(string tokenText, TokenType tokenKind);
 };
 };

+ 8 - 6
src/main.cpp

@@ -2,13 +2,15 @@
 #include "Lexer.hpp"
 #include "Lexer.hpp"
 
 
 
 
+std::string sample = "+-*/=+==><>=<=";
+
 int main()
 int main()
 {
 {
-  std::string if_instr = "IF";
-  std::string ident = "myvar";
-  std::cout << token_name(END_OF_FILE) << std::endl;
-  std::cout << token_name(token_type_from_name("NEWLINE")) << std::endl;
-  std::cout << token_name(checkIfKeyword(if_instr)) << std::endl;
-  std::cout << token_name(checkIfKeyword(ident)) << std::endl;
+  Lexer mylexer(sample);
+  Token t = mylexer.getToken();
+  while(t.kind != UNKNOWN){
+    std::cout << token_name(t.kind) << std::endl;
+    t = mylexer.getToken();
+  }
   return 0;
   return 0;
 }
 }