浏览代码

add lexing number

AbdeFaiz 5 月之前
父节点
当前提交
c9b2323e89
共有 3 个文件被更改,包括 21 次插入1 次删除
  1. 3 1
      examples/arithmetic.basic
  2. 17 0
      src/Lexer.cpp
  3. 1 0
      src/Lexer.hpp

+ 3 - 1
examples/arithmetic.basic

@@ -4,4 +4,6 @@
 <
 <=
 * / !=
-"This is a string\n it's good"
+"This is a string\n it's good"
+21324 223 "213" 324 
+2.87 8.87678898

+ 17 - 0
src/Lexer.cpp

@@ -129,6 +129,23 @@ Token Lexer::getToken()
     res = Token(tokText, TokenType::STRING);
   }
 
+  else if (isdigit(this->curChar)){
+    startPos = this->curPos;
+    while (isdigit(peek()))
+      nextChar();
+    if (peek() == '.'){
+      nextChar();
+      while (isdigit(peek()))
+        nextChar();
+      if(isalpha(peek()))
+        abort("Illegal character in number.");
+    }
+    else if(isalpha(peek()))
+      abort("Illegal character in number.");
+    tokText = this->source.substr(startPos, this->curPos - startPos + 1);
+    res = Token(tokText, TokenType::NUMBER);
+  }
+
   else if (this->curChar == '\n')
     res = Token(std::string(1, this->curChar), TokenType::NEWLINE);
 

+ 1 - 0
src/Lexer.hpp

@@ -1,4 +1,5 @@
 #include <iostream>
+#include <cctype>
 #include "Token.hpp"
 
 class Lexer{