From ecdf6944a7939569faf161c704574158a94022b2 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Fri, 7 Aug 2026 11:59:45 +0200 Subject: [PATCH] feat(furc/lexer): add integers --- furc/include/furc/front/token.hpp | 2 ++ furc/src/front/lexer.cpp | 24 +++++++++++++++++++++++- furc/src/main.cpp | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/furc/include/furc/front/token.hpp b/furc/include/furc/front/token.hpp index 79350e0..39382db 100644 --- a/furc/include/furc/front/token.hpp +++ b/furc/include/furc/front/token.hpp @@ -93,6 +93,7 @@ struct token { // Errors: UnexpectedCharacter, UnexpectedEOF, + InvalidInteger, EndOfFile, } type; union value { @@ -198,6 +199,7 @@ struct token { case token::U64: return os << "u64"; case token::UnexpectedCharacter: return os << "Unexpected character `" << token.value.character << "`"; case token::UnexpectedEOF: return os << "Unexpected End Of File"; + case token::InvalidInteger: return os << "Invalid Integer"; case token::EndOfFile: return os << "End Of File"; } diff --git a/furc/src/front/lexer.cpp b/furc/src/front/lexer.cpp index 6e9756c..0b11fbb 100644 --- a/furc/src/front/lexer.cpp +++ b/furc/src/front/lexer.cpp @@ -1,6 +1,7 @@ #include "furc/front/lexer.hpp" #include +#include #include #include @@ -19,7 +20,28 @@ token lexer::next_token() { auto loc = location(); - if (std::isdigit(get()) != 0) {} + if (std::isdigit(get()) != 0) { + std::uint64_t value = get() - '0'; + next(); + while (m_cursor < m_content.size() && std::isdigit(get()) != 0) { + static constexpr std::uint64_t MAX = std::numeric_limits::max(); + static constexpr std::uint64_t MAX_MULTS = MAX / 10; + static constexpr std::uint64_t LAST_DIGIT = MAX % 10; + + if (value > MAX_MULTS) { + return { location(), token::InvalidInteger }; + } + std::uint64_t digit = get() - '0'; + if (value == MAX_MULTS && digit > LAST_DIGIT) { + return { location(), token::InvalidInteger }; + } + + value *= 10; + value += digit; + next(); + } + return { loc, value }; + } if (std::isalnum(get()) != 0 || get() == '_') { static std::unordered_map s_keywords = { diff --git a/furc/src/main.cpp b/furc/src/main.cpp index c2c4bd8..cb0cf8f 100644 --- a/furc/src/main.cpp +++ b/furc/src/main.cpp @@ -5,7 +5,7 @@ int main(void) { furlang::arena arena; - furc::lexer lexer = { "", "func main(argc: u64) -> s32 { x: s32 = '\\\\' + 'u' - 'c' * 'd'; }" }; + furc::lexer lexer = { "", "func main(argc: u64) -> s32 { x: s32 = 10 + 67 - 6 * 7; }" }; furc::parser parser = { std::move(lexer), arena }; auto program = parser.parse();