feat(furc/lexer): add integers

This commit is contained in:
2026-08-07 11:59:45 +02:00
parent 79b6b9742d
commit ecdf6944a7
3 changed files with 26 additions and 2 deletions
+2
View File
@@ -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";
}
+23 -1
View File
@@ -1,6 +1,7 @@
#include "furc/front/lexer.hpp"
#include <cctype>
#include <limits>
#include <string_view>
#include <unordered_map>
@@ -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<std::uint64_t>::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<std::string_view, token_t> s_keywords = {
+1 -1
View File
@@ -5,7 +5,7 @@
int main(void) {
furlang::arena arena;
furc::lexer lexer = { "<AK>", "func main(argc: u64) -> s32 { x: s32 = '\\\\' + 'u' - 'c' * 'd'; }" };
furc::lexer lexer = { "<AK>", "func main(argc: u64) -> s32 { x: s32 = 10 + 67 - 6 * 7; }" };
furc::parser parser = { std::move(lexer), arena };
auto program = parser.parse();