implemented lexer for a more complex subset of the

monkey language
This commit is contained in:
Karma Riuk
2025-06-30 00:12:28 +02:00
parent 69217fdf90
commit dec93f8272
4 changed files with 60 additions and 5 deletions

View File

@@ -1,5 +1,8 @@
#include "type.hpp"
#include <array>
#include <unordered_map>
namespace token {
// Array mapping enum values to their string representations
@@ -18,4 +21,17 @@ namespace token {
return os << "Unknown";
}
static std::unordered_map<std::string, type> keywords{
{"fn", type::FUNCTION},
{"let", type::LET},
};
type lookup_identifier(std::string ident) {
try {
return keywords.at(ident);
} catch (const std::out_of_range&) {
return type::IDENTIFIER;
}
}
} // namespace token

View File

@@ -1,8 +1,6 @@
#pragma once
#include <array>
#include <ostream>
#include <string_view>
namespace token {
@@ -30,5 +28,6 @@ namespace token {
#undef X
};
std::ostream& operator<<(std::ostream& os, type type);
std::ostream& operator<<(std::ostream&, type);
type lookup_identifier(std::string);
} // namespace token