Ver código fonte

Add lexing of string

AbdeFaiz 5 meses atrás
pai
commit
8a6f86224f
3 arquivos alterados com 33 adições e 8 exclusões
  1. 2 1
      examples/arithmetic.basic
  2. 30 6
      src/Lexer.cpp
  3. 1 1
      src/Lexer.hpp

+ 2 - 1
examples/arithmetic.basic

@@ -3,4 +3,5 @@
 >=
 <
 <=
-* /
+* / !=
+"This is a string\n it's good"

+ 30 - 6
src/Lexer.cpp

@@ -1,6 +1,6 @@
 #include "Lexer.hpp"
 
-Lexer::Lexer(std::string source) : source(source + '\n'), curChar(' '), curPos(-1)
+Lexer::Lexer(std::string &source) : source(source + '\n'), curChar(' '), curPos(-1)
 {
   nextChar();
 };
@@ -23,7 +23,7 @@ char Lexer::peek()
 
 void Lexer::abort(std::string message)
 {
-  std::cout << "Lexing error. " << message << std::endl;
+  std::cout << "Lexing error: " << message << std::endl;
   exit(1);
 }
 
@@ -45,6 +45,8 @@ void Lexer::skipComment()
 Token Lexer::getToken()
 {
   char lastChar;
+  long unsigned int startPos;
+  std::string tokText;
   Token res;
 
   skipWhitespace();
@@ -104,12 +106,34 @@ Token Lexer::getToken()
     }
   }
 
+  else if (this->curChar == '!')
+  {
+    if (peek() == '=')
+    {
+      lastChar = this->curChar;
+      nextChar();
+      res = Token(std::string(1, lastChar) + this->curChar, TokenType::NOTEQ);
+    }
+    else
+    {
+      abort("Syntax Error: Expected !=, got !"+ std::string(1,peek()));
+    }
+  }
+
+  else if (this->curChar == '\"'){
+    nextChar();
+    startPos = this->curPos;
+    while (this->curChar != '\"')
+      nextChar();
+    tokText = this->source.substr(startPos, this->curPos - startPos);
+    res = Token(tokText, TokenType::STRING);
+  }
+
   else if (this->curChar == '\n')
-    res = Token(std::string(1,this->curChar), TokenType::NEWLINE);
-  
-    else if (this->curChar == '\0')
-    res = Token(std::string(1,this->curChar), TokenType::END_OF_FILE);
+    res = Token(std::string(1, this->curChar), TokenType::NEWLINE);
 
+  else if (this->curChar == '\0')
+    res = Token(std::string(1, this->curChar), TokenType::END_OF_FILE);
 
   nextChar();
   return res;

+ 1 - 1
src/Lexer.hpp

@@ -7,7 +7,7 @@ class Lexer{
   long unsigned int curPos;
 
   public:
-  Lexer(std::string source);
+  Lexer(std::string &source);
   void nextChar();
   char peek();
   void abort(std::string message);