Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove maps of keywords and operators from Lexer class definition #114

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions compiler/include/compiler/frontend/lexer/lexer.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#pragma once

#include <list>
#include <map>
#include <string>
#include <string_view>

#include "compiler/utils/error_buffer.hpp"
#include "compiler/utils/source_files.hpp"

Expand All @@ -13,9 +8,6 @@
namespace lexer {

class Lexer {
static std::map<std::string_view, Keyword> keywords;
static std::map<std::string_view, Operator> operators;

static TokenList processString(const utils::SourceLine &source, ErrorBuffer &errors);

public:
Expand Down
53 changes: 30 additions & 23 deletions compiler/lib/frontend/lexer/lexer.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
#include "lexer/lexer.hpp"

#include <string_view>
#include <unordered_map>

#include "lexer/lexer_error.hpp"
#include "lexer/token.hpp"
#include "lexer/token_types.hpp"

using namespace lexer;
using namespace utils;

// clang-format off
std::map<std::string_view, Keyword> Lexer::keywords = {
{"bool", Keyword::Bool}, {"False", Keyword::False},
{"int", Keyword::Int}, {"float", Keyword::Float},
{"str", Keyword::Str}, {"if", Keyword::If},
{"else", Keyword::Else}, {"elif", Keyword::Elif},
{"range", Keyword::Range}, {"while", Keyword::While},
{"for", Keyword::For}, {"break", Keyword::Break},
{"import", Keyword::Import}, {"continue", Keyword::Continue},
{"def", Keyword::Definition}, {"return", Keyword::Return},
{"or", Keyword::Or}, {"and", Keyword::And},
{"not", Keyword::Not}, {"in", Keyword::In},
{"True", Keyword::True}, {"None", Keyword::None}};

std::map<std::string_view, Operator> Lexer::operators = {
{"%", Operator::Mod}, {".", Operator::Dot}, {"]", Operator::RectRightBrace},
{",", Operator::Comma}, {"=", Operator::Assign}, {"+", Operator::Add},
{"-", Operator::Sub}, {"*", Operator::Mult}, {"/", Operator::Div},
{"==", Operator::Equal}, {"!=", Operator::NotEqual}, {"<", Operator::Less},
{">", Operator::Greater}, {"<=", Operator::LessEqual}, {">=", Operator::GreaterEqual},
{"(", Operator::LeftBrace}, {")", Operator::RightBrace}, {"[", Operator::RectLeftBrace}};
// clang-format on

namespace {

std::unordered_map<std::string_view, Keyword> keywords = {
{"bool", Keyword::Bool}, {"False", Keyword::False},
{"int", Keyword::Int}, {"float", Keyword::Float},
{"str", Keyword::Str}, {"if", Keyword::If},
{"else", Keyword::Else}, {"elif", Keyword::Elif},
{"range", Keyword::Range}, {"while", Keyword::While},
{"for", Keyword::For}, {"break", Keyword::Break},
{"import", Keyword::Import}, {"continue", Keyword::Continue},
{"def", Keyword::Definition}, {"return", Keyword::Return},
{"or", Keyword::Or}, {"and", Keyword::And},
{"not", Keyword::Not}, {"in", Keyword::In},
{"True", Keyword::True}, {"None", Keyword::None},
};

std::unordered_map<std::string_view, Operator> operators = {
{"%", Operator::Mod}, {".", Operator::Dot}, {"]", Operator::RectRightBrace},
{",", Operator::Comma}, {"=", Operator::Assign}, {"+", Operator::Add},
{"-", Operator::Sub}, {"*", Operator::Mult}, {"/", Operator::Div},
{"==", Operator::Equal}, {"!=", Operator::NotEqual}, {"<", Operator::Less},
{">", Operator::Greater}, {"<=", Operator::LessEqual}, {">=", Operator::GreaterEqual},
{"(", Operator::LeftBrace}, {")", Operator::RightBrace}, {"[", Operator::RectLeftBrace},
};

inline std::string_view makeStringView(std::string::const_iterator begin_token, std::string::const_iterator end_token) {
return std::string_view(&*begin_token, static_cast<size_t>(std::distance(begin_token, end_token)));
}

} // namespace

TokenList Lexer::process(const SourceFile &source) {
Expand Down