From 4450d087a4e79b76a403a58cff02ea1c26e5b42b Mon Sep 17 00:00:00 2001 From: JenChieh Date: Mon, 8 Jan 2024 04:10:22 -0800 Subject: [PATCH] feat(lexer): Impls utility functions --- include/lexer.hpp | 20 ++++++++++++----- src/lexer.cpp | 57 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 21 deletions(-) diff --git a/include/lexer.hpp b/include/lexer.hpp index b81d8ec..8efff89 100644 --- a/include/lexer.hpp +++ b/include/lexer.hpp @@ -65,12 +65,6 @@ class Lexer { char peek_next(); - bool is_binary_digit(char); - - bool is_octal_digit(char); - - bool is_underscore(char); - void skip_whitespaces(); void skip_single_line_comment(); @@ -79,6 +73,20 @@ class Lexer { TokenKind resolve_keyword_token_kind(const char *keyword); + static bool is_underscore(char); + + static bool is_binary_digit(char); + + static bool is_octal_digit(char); + + static auto hex_to_int(char c) -> int8_t; + + static std::int64_t hex_to_decimal(const std::string&); + + static std::int64_t binary_to_decimal(const std::string&); + + static std::int64_t octal_to_decimal(const std::string&); + /* Operators */ /* Setter & Getter */ diff --git a/src/lexer.cpp b/src/lexer.cpp index fc38c16..bf42dea 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -205,21 +205,6 @@ char Lexer::peek_next() return '\0'; } -bool Lexer::is_underscore(char c) -{ - return c == '_'; -} - -bool Lexer::is_binary_digit(char c) -{ - return '1' == c || '0' == c; -} - -bool Lexer::is_octal_digit(char c) -{ - return '7' >= c && c >= '0'; -} - void Lexer::skip_whitespaces() { while (is_source_available()) { @@ -296,4 +281,46 @@ bool Lexer::is_source_available() { return current_position < source_code_length; } + +bool Lexer::is_underscore(char c) +{ + return c == '_'; +} + +bool Lexer::is_binary_digit(char c) +{ + return '1' == c || '0' == c; +} + +bool Lexer::is_octal_digit(char c) +{ + return '7' >= c && c >= '0'; +} + +std::int64_t Lexer::hex_to_decimal(const std::string &hex) +{ + try { + return std::stol(hex, nullptr, 16); + } catch (...) { + return -1; + } +} + +std::int64_t Lexer::binary_to_decimal(const std::string &binary) +{ + try { + return std::stol(binary, nullptr, 2); + } catch (...) { + return -1; + } +} + +std::int64_t Lexer::octal_to_decimal(const std::string &octal) +{ + try { + return std::stol(octal, nullptr, 8); + } catch (...) { + return -1; + } +} } // namespace jayces