Skip to content

Commit

Permalink
feat(lexer): Impls utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jan 8, 2024
1 parent 76d8b7c commit 4450d08
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
20 changes: 14 additions & 6 deletions include/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 */
Expand Down
57 changes: 42 additions & 15 deletions src/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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

0 comments on commit 4450d08

Please sign in to comment.