// Standard includes #include #include // Local includes #include "Lexer.h" /// gettok - Return the next token from standard input. namespace lexer{ std::string LexerObjects::IdentifierStr; // Filled in if tok_identifier double LexerObjects::NumVal; int gettok() { static int LastChar = ' '; // Skip any whitespace. while (isspace(LastChar)) LastChar = getchar(); if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]* LexerObjects::IdentifierStr = LastChar; while (isalnum((LastChar = getchar()))) LexerObjects::IdentifierStr += LastChar; if (LexerObjects::IdentifierStr == "def") return tok_def; if (LexerObjects::IdentifierStr == "extern") return tok_extern; if (LexerObjects::IdentifierStr == "if") return tok_if; if (LexerObjects::IdentifierStr == "then") return tok_then; if (LexerObjects::IdentifierStr == "else") return tok_else; if (LexerObjects::IdentifierStr == "for") return tok_for; if (LexerObjects::IdentifierStr == "in") return tok_in; if (LexerObjects::IdentifierStr == "binary") return tok_binary; if (LexerObjects::IdentifierStr == "unary") return tok_unary; if (LexerObjects::IdentifierStr == "var") return tok_var; return tok_identifier; } if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+ std::string NumStr; do { NumStr += LastChar; LastChar = getchar(); } while (isdigit(LastChar) || LastChar == '.'); LexerObjects::NumVal = strtod(NumStr.c_str(), nullptr); return tok_number; } if (LastChar == '#') { // Comment until end of line. do LastChar = getchar(); while (LastChar != EOF && LastChar != '\n' && LastChar != '\r'); if (LastChar != EOF) return gettok(); } // Check for end of file. Don't eat the EOF. if (LastChar == EOF) return tok_eof; // Otherwise, just return the character as its ascii value. int ThisChar = LastChar; LastChar = getchar(); return ThisChar; } }