aboutsummaryrefslogtreecommitdiff
path: root/lib/Lexer
diff options
context:
space:
mode:
authorov-4 <hausemasterstream@gmail.com>2026-01-29 10:20:58 +0000
committerov-4 <hausemasterstream@gmail.com>2026-01-29 10:20:58 +0000
commitc2a3cecc1ab2d3f27dcea40a6f0f353b0306d66a (patch)
tree3adfa6ed51620adcf6b129eade1622b01527c8a9 /lib/Lexer
parent3f48a1d3af588242cd745e344b6b0b02cfb26131 (diff)
Added INPUT, OUTPUT and DECLARE
Diffstat (limited to 'lib/Lexer')
-rw-r--r--lib/Lexer/Lexer.cc57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/Lexer/Lexer.cc b/lib/Lexer/Lexer.cc
new file mode 100644
index 0000000..b04438b
--- /dev/null
+++ b/lib/Lexer/Lexer.cc
@@ -0,0 +1,57 @@
+#include "cps/Lexer.h"
+#include <cctype>
+#include <cstdio>
+
+using namespace cps;
+
+int Lexer::gettok() {
+ static int LastChar = ' ';
+
+ while (isspace(LastChar))
+ LastChar = getchar();
+
+ if (isalpha(LastChar)) {
+ IdentifierStr = LastChar;
+ while (isalnum((LastChar = getchar())) || LastChar == '_')
+ IdentifierStr += LastChar;
+
+ if (IdentifierStr == "DECLARE") return tok_declare;
+ if (IdentifierStr == "INTEGER") return tok_integer_kw;
+ if (IdentifierStr == "INPUT") return tok_input;
+ if (IdentifierStr == "OUTPUT") return tok_output;
+ return tok_identifier;
+ }
+
+ if (isdigit(LastChar)) {
+ std::string NumStr;
+ do {
+ NumStr += LastChar;
+ LastChar = getchar();
+ } while (isdigit(LastChar));
+ NumVal = strtol(NumStr.c_str(), nullptr, 10);
+ return tok_number;
+ }
+
+ if (LastChar == '<') {
+ LastChar = getchar();
+ if (LastChar == '-') {
+ LastChar = getchar();
+ return tok_assign;
+ }
+ return '<';
+ }
+
+ // for comments
+
+ if (LastChar == ':') {
+ LastChar = getchar();
+ return tok_colon;
+ }
+
+ if (LastChar == EOF)
+ return tok_eof;
+
+ int ThisChar = LastChar;
+ LastChar = getchar();
+ return ThisChar;
+}