made the token type less repetitive

This commit is contained in:
Karma Riuk
2025-06-28 18:05:01 +02:00
parent 4364afa111
commit 81cdd0690d

View File

@ -1,60 +1,50 @@
#pragma once #pragma once
#include <array>
#include <ostream> #include <ostream>
#include <string_view>
namespace lexer {
// X-macro list of token types and their string representations
#define TOKEN_LIST \
X(ILLEGAL, "ILLEGAL") \
X(EOF_, "EOF") \
X(IDENT, "IDENT") \
X(INT, "INT") \
X(ASSIGN, "=") \
X(PLUS, "+") \
X(COMMA, ",") \
X(SEMICOLON, ";") \
X(LPAREN, "(") \
X(RPAREN, ")") \
X(LBRACE, "{") \
X(RBRACE, "}") \
X(FUNCTION, "FUNCTION") \
X(LET, "LET")
// Define the TokenType enum using the X-macro
enum class TokenType { enum class TokenType {
ILLEGAL, #define X(name, str) name,
EOF_, TOKEN_LIST
#undef X
IDENT,
INT,
ASSIGN,
PLUS,
COMMA,
SEMICOLON,
LPAREN,
RPAREN,
LBRACE,
RBRACE,
FUNCTION,
LET
}; };
inline std::ostream& operator<<(std::ostream& os, TokenType tt) { // Array mapping enum values to their string representations
switch (tt) { static constexpr std::
case TokenType::ILLEGAL: array<std::string_view, static_cast<size_t>(TokenType::LET) + 1>
return os << "ILLEGAL"; tokenTypeStrings = {
case TokenType::EOF_: #define X(name, str) str,
return os << "EOF"; TOKEN_LIST
case TokenType::IDENT: #undef X
return os << "IDENT"; };
case TokenType::INT:
return os << "INT"; // Stream insertion operator using the lookup array
case TokenType::ASSIGN: inline std::ostream& operator<<(std::ostream& os, TokenType type) {
return os << "="; auto idx = static_cast<size_t>(type);
case TokenType::PLUS: if (idx < tokenTypeStrings.size())
return os << "+"; return os << tokenTypeStrings[idx];
case TokenType::COMMA:
return os << ",";
case TokenType::SEMICOLON:
return os << ";";
case TokenType::LPAREN:
return os << "(";
case TokenType::RPAREN:
return os << ")";
case TokenType::LBRACE:
return os << "{";
case TokenType::RBRACE:
return os << "}";
case TokenType::FUNCTION:
return os << "FUNCTION";
case TokenType::LET:
return os << "LET";
default:
return os << "Unknown"; return os << "Unknown";
} }
}
} // namespace lexer