AbdeFaiz 5 miesięcy temu
rodzic
commit
8252c7b681
4 zmienionych plików z 21 dodań i 10 usunięć
  1. 1 1
      Makefile
  2. 0 9
      examples/arithmetic.basic
  3. 12 0
      examples/factorial
  4. 8 0
      src/Lexer.cpp

+ 1 - 1
Makefile

@@ -1,6 +1,6 @@
 CC = g++
 FLAGS = -Wall
-EXAMPLE = examples/arithmetic.basic
+EXAMPLE = examples/factorial
 
 all: test.out
 

+ 0 - 9
examples/arithmetic.basic

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

+ 12 - 0
examples/factorial

@@ -0,0 +1,12 @@
+PRINT "Enter a number to compute its factorial:"
+INPUT n
+PRINT ""
+
+LET result = 1
+WHILE n > 1 REPEAT
+    LET result = result * n
+    LET n = n - 1
+ENDWHILE
+
+PRINT "The factorial is:"
+PRINT result

+ 8 - 0
src/Lexer.cpp

@@ -146,6 +146,14 @@ Token Lexer::getToken()
     res = Token(tokText, TokenType::NUMBER);
   }
 
+  else if (isalpha(this->curChar)){
+    startPos = this->curPos;
+    while (isalnum(peek()))
+      nextChar();
+    tokText = this->source.substr(startPos, this->curPos - startPos + 1);
+    res = Token(tokText, checkIfKeyword(tokText));
+  }
+
   else if (this->curChar == '\n')
     res = Token(std::string(1, this->curChar), TokenType::NEWLINE);