From c2a3cecc1ab2d3f27dcea40a6f0f353b0306d66a Mon Sep 17 00:00:00 2001 From: ov-4 Date: Thu, 29 Jan 2026 10:20:58 +0000 Subject: Added INPUT, OUTPUT and DECLARE --- lib/Lexer/Lexer.cc | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lib/Lexer/Lexer.cc (limited to 'lib/Lexer') 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 +#include + +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; +} -- cgit v1.3.1-4-g156e