diff --git a/compiler/include/compiler/frontend/lexer/lexer.hpp b/compiler/include/compiler/frontend/lexer/lexer.hpp index 4b6c7a49..a48ee78f 100644 --- a/compiler/include/compiler/frontend/lexer/lexer.hpp +++ b/compiler/include/compiler/frontend/lexer/lexer.hpp @@ -1,10 +1,5 @@ #pragma once -#include -#include -#include -#include - #include "compiler/utils/error_buffer.hpp" #include "compiler/utils/source_files.hpp" @@ -13,9 +8,6 @@ namespace lexer { class Lexer { - static std::map keywords; - static std::map operators; - static TokenList processString(const utils::SourceLine &source, ErrorBuffer &errors); public: diff --git a/compiler/lib/frontend/lexer/lexer.cpp b/compiler/lib/frontend/lexer/lexer.cpp index 430dc0e0..9e46a079 100644 --- a/compiler/lib/frontend/lexer/lexer.cpp +++ b/compiler/lib/frontend/lexer/lexer.cpp @@ -1,37 +1,44 @@ #include "lexer/lexer.hpp" +#include +#include + #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 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 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 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 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(std::distance(begin_token, end_token))); } + } // namespace TokenList Lexer::process(const SourceFile &source) {