Add integers to lexer

Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
CHatingPython
2026-05-26 08:52:35 +02:00
committed by CHatingPython
parent cbb2efc187
commit ad84bc50bd
8 changed files with 121 additions and 60 deletions
+37 -13
View File
@@ -13,8 +13,8 @@ namespace front {
enum class token_t {
None,
Identifier,
Keyword,
String,
Keyword,
Integer,
Lparen,
Rparen,
@@ -32,8 +32,8 @@ static inline std::ostream& operator<<(std::ostream& os, token_t type) {
switch (type) {
case token_t::None: return os << "none";
case token_t::Identifier: return os << "identifier";
case token_t::Keyword: return os << "keyword";
case token_t::String: return os << "string";
case token_t::Keyword: return os << "keyword";
case token_t::Integer: return os << "integer";
case token_t::Lparen: return os << "'('";
case token_t::Rparen: return os << "')'";
@@ -52,8 +52,8 @@ static inline std::string operator+(const std::string& str, token_t type) {
switch (type) {
case token_t::None: return str + "none";
case token_t::Identifier: return str + "identifier";
case token_t::Keyword: return str + "keyword";
case token_t::String: return str + "string";
case token_t::Keyword: return str + "keyword";
case token_t::Integer: return str + "integer";
case token_t::Lparen: return str + "'('";
case token_t::Rparen: return str + "')'";
@@ -90,10 +90,28 @@ static inline std::string operator+(const std::string& str, keyword_token keywor
}
}
using integer_token = std::uint64_t;
struct token {
token_t type = token_t::None;
keyword_token keyword = keyword_token::None;
std::string_view value;
token_t type = token_t::None;
union value {
std::nullptr_t null;
std::string_view string;
keyword_token keyword;
integer_token integer;
value(std::nullptr_t value = nullptr)
: null(value) {}
value(std::string_view value)
: string(value) {}
value(keyword_token value)
: keyword(value) {}
value(integer_token value)
: integer(value) {}
} value;
token() = default;
@@ -101,15 +119,21 @@ struct token {
: type(type), value(value) {}
token(keyword_token keyword, std::string_view value = {})
: type(token_t::Keyword), keyword(keyword), value(value) {}
: type(token_t::Keyword), value(keyword) {}
token(integer_token integer)
: type(token_t::Integer), value(integer) {}
union value* operator->() { return &value; }
const union value* operator->() const { return &value; }
bool operator==(const token& rhs) const {
if (type != rhs.type) return false;
switch (type) {
case token_t::Identifier:
case token_t::String:
case token_t::Integer: return value == rhs.value;
case token_t::Keyword: return keyword == rhs.keyword;
case token_t::String: return value.string == rhs.value.string;
case token_t::Keyword: return value.keyword == rhs.value.keyword;
case token_t::Integer: return value.integer == rhs.value.integer;
case token_t::None:
case token_t::Lparen:
case token_t::Rparen:
@@ -128,9 +152,9 @@ struct token {
static inline std::ostream& operator<<(std::ostream& os, const token& token) {
switch (token.type) {
case token_t::Identifier:
case token_t::String:
case token_t::Integer: return os << token.value;
case token_t::Keyword: return os << token.keyword;
case token_t::String: return os << token.value.string;
case token_t::Keyword: return os << token.value.keyword;
case token_t::Integer: return os << token.value.integer;
default: return os << token.type;
}
}