added the current character to the lexer struct

for cleaner structure
This commit is contained in:
Karma Riuk
2025-07-01 18:59:43 +02:00
parent 69bee723a2
commit e773cb649f
2 changed files with 9 additions and 9 deletions

View File

@@ -8,7 +8,6 @@
namespace lexer { namespace lexer {
token::token lexer::next_token() { token::token lexer::next_token() {
char c;
if (!(input >> c)) if (!(input >> c))
return {token::type::END_OF_FILE, ""}; return {token::type::END_OF_FILE, ""};
@@ -47,14 +46,14 @@ namespace lexer {
return {token::type::RBRACE, c}; return {token::type::RBRACE, c};
default: default:
if (is_letter(c)) { if (is_letter(c)) {
std::string identifier_or_keyword = read_string(c); std::string identifier_or_keyword = read_string();
return { return {
token::lookup_identifier(identifier_or_keyword), token::lookup_identifier(identifier_or_keyword),
identifier_or_keyword identifier_or_keyword
}; };
} }
if (std::isdigit(c)) if (std::isdigit(c))
return {token::type::INT, read_int(c)}; return {token::type::INT, read_int()};
return {token::type::ILLEGAL, c}; return {token::type::ILLEGAL, c};
} }
@@ -64,17 +63,17 @@ namespace lexer {
return c == '_' || std::isalpha(static_cast<unsigned char>(c)); return c == '_' || std::isalpha(static_cast<unsigned char>(c));
} }
std::string lexer::read_string(char first_char) { std::string lexer::read_string() {
std::string result; std::string result;
result.push_back(first_char); result.push_back(c);
for (char c = input.peek(); is_letter(c); c = input.peek()) for (char c = input.peek(); is_letter(c); c = input.peek())
result.push_back(input.get()); result.push_back(input.get());
return result; return result;
} }
std::string lexer::read_int(char first_digit) { std::string lexer::read_int() {
std::string result; std::string result;
result.push_back(first_digit); result.push_back(c);
for (char c = input.peek(); std::isdigit(c); c = input.peek()) for (char c = input.peek(); std::isdigit(c); c = input.peek())
result.push_back(input.get()); result.push_back(input.get());
return result; return result;

View File

@@ -5,12 +5,13 @@
namespace lexer { namespace lexer {
struct lexer { struct lexer {
std::istream& input; std::istream& input;
char c;
token::token next_token(); token::token next_token();
private: private:
bool is_letter(char); bool is_letter(char);
std::string read_string(char); std::string read_string();
std::string read_int(char); std::string read_int();
}; };
} // namespace lexer } // namespace lexer