diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-04-14 16:56:58 +0630 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-04-14 16:56:58 +0630 |
commit | 4f8f5111687355669d6df8db7a404fd9aaecacc4 (patch) | |
tree | 6ce343722e394f835ac752005e9772979b45c868 /asm | |
parent | 585aff1cbb03949613fddd60fa1fe26de64130c2 (diff) | |
download | ovm-4f8f5111687355669d6df8db7a404fd9aaecacc4.tar.gz ovm-4f8f5111687355669d6df8db7a404fd9aaecacc4.tar.bz2 ovm-4f8f5111687355669d6df8db7a404fd9aaecacc4.zip |
Implemented tokenise_literal_number (tokenise_number)
Diffstat (limited to 'asm')
-rw-r--r-- | asm/lexer.cpp | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/asm/lexer.cpp b/asm/lexer.cpp index 0024b05..189b330 100644 --- a/asm/lexer.cpp +++ b/asm/lexer.cpp @@ -16,10 +16,11 @@ #include "./lexer.hpp" -using std::string_view, std::pair, std::make_pair; +using std::string, std::string_view, std::pair, std::make_pair; -constexpr auto VALID_SYMBOL = - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.:()%#$"; +constexpr auto VALID_SYMBOL = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV" + "WXYZ0123456789-_.:()%#$", + VALID_DIGIT = "0123456789"; bool is_char_in_s(char c, const char *s) { @@ -213,3 +214,26 @@ pair<token_t, lerr_t> tokenise_symbol(string_view &source, size_t &column) column += sym.size(); return make_pair(t, lerr_t::OK); } + +token_t tokenise_literal_number(string_view &source, size_t &column) +{ + bool is_negative = false; + if (source[0] == '-') + { + is_negative = true; + source.remove_prefix(1); + } + + auto end = source.find_first_not_of(VALID_DIGIT); + if (end == string::npos) + end = source.size() - 1; + string digits{source.substr(0, end)}; + source.remove_prefix(end); + + token_t t{token_type_t::LITERAL_NUMBER, (is_negative ? "-" : "") + digits, + column}; + + column += digits.size() + (is_negative ? 1 : 0); + + return t; +} |