feat(furc/lexer): add integers
This commit is contained in:
@@ -93,6 +93,7 @@ struct token {
|
|||||||
// Errors:
|
// Errors:
|
||||||
UnexpectedCharacter,
|
UnexpectedCharacter,
|
||||||
UnexpectedEOF,
|
UnexpectedEOF,
|
||||||
|
InvalidInteger,
|
||||||
EndOfFile,
|
EndOfFile,
|
||||||
} type;
|
} type;
|
||||||
union value {
|
union value {
|
||||||
@@ -198,6 +199,7 @@ struct token {
|
|||||||
case token::U64: return os << "u64";
|
case token::U64: return os << "u64";
|
||||||
case token::UnexpectedCharacter: return os << "Unexpected character `" << token.value.character << "`";
|
case token::UnexpectedCharacter: return os << "Unexpected character `" << token.value.character << "`";
|
||||||
case token::UnexpectedEOF: return os << "Unexpected End Of File";
|
case token::UnexpectedEOF: return os << "Unexpected End Of File";
|
||||||
|
case token::InvalidInteger: return os << "Invalid Integer";
|
||||||
case token::EndOfFile: return os << "End Of File";
|
case token::EndOfFile: return os << "End Of File";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "furc/front/lexer.hpp"
|
#include "furc/front/lexer.hpp"
|
||||||
|
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
#include <limits>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
@@ -19,7 +20,28 @@ token lexer::next_token() {
|
|||||||
|
|
||||||
auto loc = location();
|
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() == '_') {
|
if (std::isalnum(get()) != 0 || get() == '_') {
|
||||||
static std::unordered_map<std::string_view, token_t> s_keywords = {
|
static std::unordered_map<std::string_view, token_t> s_keywords = {
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
int main(void) {
|
int main(void) {
|
||||||
furlang::arena arena;
|
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 };
|
furc::parser parser = { std::move(lexer), arena };
|
||||||
|
|
||||||
auto program = parser.parse();
|
auto program = parser.parse();
|
||||||
|
|||||||
Reference in New Issue
Block a user