ソースを参照

code Lexer class to be completed

AbdeFaiz 5 ヶ月 前
コミット
b660fc1730
4 ファイル変更108 行追加15 行削除
  1. 94 7
      src/Lexer.cpp
  2. 8 3
      src/Lexer.hpp
  3. 3 3
      src/Token.cpp
  4. 3 2
      src/Token.hpp

+ 94 - 7
src/Lexer.cpp

@@ -1,15 +1,102 @@
 #include "Lexer.hpp"
 
-Lexer::Lexer():curChar(' '), curPos(-1){};
+Lexer::Lexer(std::string source) : source(source + '\n'), curChar(' '), curPos(-1)
+{
+  nextChar();
+};
 
-void Lexer::nextChar() {
-  return;
+void Lexer::nextChar()
+{
+  this->curPos += 1;
+  if (this->curPos >= this->source.length())
+    this->curChar = '\0';
+  else
+    this->curChar = this->source[this->curPos];
 }
 
-void Lexer::peek() {
-  return;
+char Lexer::peek()
+{
+  if (this->curPos + 1 >= this->source.length())
+    return '\0';
+  return this->source[this->curPos + 1];
 }
 
-Token Lexer::getToken() {
-  return Token("END_OF_FILE", END_OF_FILE);
+void Lexer::abort(std::string message)
+{
+  std::cout << "Lexing error. " << message << std::endl;
+  exit(1);
+}
+
+void Lexer::skipWhitespace()
+{
+  while (this->curChar == ' ' ||
+         this->curChar == '\t' ||
+         this->curChar == '\r')
+    nextChar();
+}
+
+void Lexer::skipComment()
+{
+  if (this->curChar == '#')
+    while (this->curChar != '\n')
+      nextChar();
+}
+
+Token Lexer::getToken()
+{
+  char lastChar;
+  Token res;
+
+  skipWhitespace();
+  skipComment();
+
+  if (this->curChar == '+')
+    res = Token(std::string(1, this->curChar), TokenType::PLUS);
+
+  else if (this->curChar == '-')
+    res = Token(std::string(1, this->curChar), TokenType::MINUS);
+
+  else if (this->curChar == '*')
+    res = Token(std::string(1, this->curChar), TokenType::ASTERISK);
+
+  else if (this->curChar == '/')
+    res = Token(std::string(1, this->curChar), TokenType::SLASH);
+
+  else if (this->curChar == '=')
+    if (peek() == '=')
+    {
+      lastChar = this->curChar;
+      nextChar();
+      res = Token(std::string(1, lastChar) + this->curChar, TokenType::EQEQ);
+    }
+    else
+    {
+      res = Token(std::string(1, this->curChar), TokenType::EQ);
+    }
+
+    else if (this->curChar == '>')
+    if (peek() == '=')
+    {
+      lastChar = this->curChar;
+      nextChar();
+      res = Token(std::string(1, lastChar) + this->curChar, TokenType::GTEQ);
+    }
+    else
+    {
+      res = Token(std::string(1, this->curChar), TokenType::GT);
+    }
+
+    else if (this->curChar == '<')
+    if (peek() == '=')
+    {
+      lastChar = this->curChar;
+      nextChar();
+      res = Token(std::string(1, lastChar) + this->curChar, TokenType::LTEQ);
+    }
+    else
+    {
+      res = Token(std::string(1, this->curChar), TokenType::LT);
+    }
+
+  return res;
 }

+ 8 - 3
src/Lexer.hpp

@@ -1,13 +1,18 @@
+#include <iostream>
 #include "Token.hpp"
 
 class Lexer{
+  std::string source;
   char curChar;
-  int curPos;
+  long unsigned int curPos;
 
   public:
-  Lexer();
+  Lexer(std::string source);
   void nextChar();
-  void peek();
+  char peek();
+  void abort(std::string message);
+  void skipWhitespace();
+  void skipComment();
   Token getToken();
 
 };

+ 3 - 3
src/Token.cpp

@@ -32,7 +32,7 @@ string token_name(TokenType t) {
       case LTEQ: return "LTEQ";
       case GT: return "GT";
       case GTEQ: return "GTEQ";
-      default: return "UNKOWN";
+      default: return "UNKNOWN";
   }
 }
 
@@ -66,10 +66,10 @@ TokenType token_type_from_name(const string &name) {
   if (name == "LTEQ") return LTEQ;
   if (name == "GT") return GT;
   if (name == "GTEQ") return GTEQ;
-  return UNKOWN; // Default case for unknown names
+  return UNKNOWN; // Default case for unknown names
 }
 
-
+Token::Token():text("UNKNOWN"), kind(UNKNOWN){}
 Token::Token(string tokenText, TokenType tokenKind):text(tokenText), kind(tokenKind) { }
 
 /* Checks if a string is a keyword, 

+ 3 - 2
src/Token.hpp

@@ -31,8 +31,8 @@ enum TokenType {
   LTEQ = 209,
   GT = 210,
   GTEQ = 211,
-  // UNKOWN
-  UNKOWN = 999
+  // UNKNOWN
+  UNKNOWN = 999
 };
 
 string token_name(TokenType t);
@@ -45,5 +45,6 @@ class Token{
   TokenType kind;
 
   public:
+  Token();
   Token(string tokenText, TokenType tokenKind);
 };