diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-13 11:11:26 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-13 11:11:26 +0530 |
commit | 99f70b817ea14012dc4139c8f753e4cccdeb3376 (patch) | |
tree | 9a961cafe5cda0dd1775a0e52ee625a9664f9134 | |
parent | 6336bd5578e3d92f1ec34d10a96542ed429fdd21 (diff) |
lexer: add number parsing
-rw-r--r-- | include/lexer.h | 2 | ||||
-rw-r--r-- | src/lexer.cc | 50 |
2 files changed, 50 insertions, 2 deletions
diff --git a/include/lexer.h b/include/lexer.h index d6936f3..8122848 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -21,10 +21,12 @@ private: bool at_end(void); char advance(void); char peek(void); + char peek_next(void); bool match(char c); private: void string(void); + void number(void); private: void add_token(token_type_e type); diff --git a/src/lexer.cc b/src/lexer.cc index c77e11d..33e364f 100644 --- a/src/lexer.cc +++ b/src/lexer.cc @@ -1,3 +1,4 @@ +#include <ctype.h> #include <lexer.h> #include <list.h> #include <stdio.h> @@ -70,6 +71,15 @@ Lexer::peek(void) return m_script[m_current]; } +char +Lexer::peek_next(void) +{ + if (m_current + 1 >= m_end) + return '\0'; + + return m_script[m_current + 1]; +} + bool Lexer::match(char c) { @@ -103,6 +113,7 @@ Lexer::string(void) char *lexeme; asprintf(&lexeme, "%.*s", token_length, m_script + m_start + 1); + Token token; token.init(STRING, lexeme, m_line); m_tokens->add(&token); @@ -112,6 +123,35 @@ Lexer::string(void) } void +Lexer::number(void) +{ + while (isdigit(peek())) { + printf("%c\n", peek()); + advance(); + } + + if (peek() == '.' && isdigit(peek_next())) { + // . + advance(); + + while (isdigit(peek())) + advance(); + } + + int token_length = m_current - m_start; + if (token_length == 0) + token_length++; + + char *lexeme; + asprintf(&lexeme, "%.*s", token_length, m_script + m_start); + + Token token; + token.init(NUMBER, lexeme, m_line); + m_tokens->add(&token); + token.clean(); +} + +void Lexer::add_token(token_type_e type) { int token_length = m_current - m_start; @@ -198,8 +238,14 @@ Lexer::scan_token(void) break; default: - printf("[-] Error: Unexpected character %c in line %lu\n", c, m_line); - m_errored = true; + if (isdigit(c)) + number(); + + else { + printf("[-] Error: Unexpected character %c in line %lu\n", c, m_line); + m_errored = true; + } + break; } } |