From 3bf7130edaea39d3b471b2f1b2702d999c14b7f7 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 10 Jul 2024 02:10:36 +0100 Subject: [PATCH] Tidy up a bit --- src/preprocesser.cpp | 32 +++++++++++++++----------------- src/preprocesser.hpp | 8 ++++---- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/preprocesser.cpp b/src/preprocesser.cpp index f641c74..0839c2e 100644 --- a/src/preprocesser.cpp +++ b/src/preprocesser.cpp @@ -101,6 +101,20 @@ namespace Preprocesser std::cout << "\t}\n"; #endif } + else if (token->type == TT::PP_REFERENCE) + { + // Reference expansion based on latest constant + const auto found = const_map.find(token->content); + if (found == const_map.end()) + return new Err{ET::UNKNOWN_NAME_IN_REFERENCE, token}; + + std::vector preprocessed; + Err *err = preprocess(found->second.body, preprocessed, new_token_bag, + const_map, file_map, depth + 1); + if (err) + return new Err{ET::IN_ERROR, token, err}; + units.push_back(Unit{token, preprocessed}); + } else if (token->type == TT::PP_USE) { // Ensure string in next token @@ -152,20 +166,6 @@ namespace Preprocesser else i += 1; } - else if (token->type == TT::PP_REFERENCE) - { - // Reference expansion based on latest constant - const auto found = const_map.find(token->content); - if (found == const_map.end()) - return new Err{ET::UNKNOWN_NAME_IN_REFERENCE, token}; - - std::vector preprocessed; - Err *err = preprocess(found->second.body, preprocessed, new_token_bag, - const_map, file_map, depth + 1); - if (err) - return new Err{ET::IN_ERROR, token, err}; - units.push_back(Unit{token, preprocessed}); - } else if (token->type == TT::PP_END) return new Err{ET::NO_CONST_AROUND, token}; else @@ -228,15 +228,13 @@ namespace Preprocesser std::string to_string(const Err &err) { std::stringstream ss; - // Reverse traversal of err linked list + // Reverse traversal of the linked list of errors std::vector errors; errors.push_back((Err *)&err); for (Err *e = err.child_error; e; e = e->child_error) errors.insert(errors.begin(), e); for (size_t depth = 0; depth < errors.size(); ++depth) { - // for (size_t i = 0; i < depth; ++i) - // ss << " "; const Err &e = *errors[depth]; ss << e.token->source_name << ":" << e.token->line << ":" << e.token->column << ": " << to_string(e.type); diff --git a/src/preprocesser.hpp b/src/preprocesser.hpp index 0fe81cc..47117d2 100644 --- a/src/preprocesser.hpp +++ b/src/preprocesser.hpp @@ -68,14 +68,14 @@ namespace Preprocesser ~Err(void); }; + Err *preprocess(std::vector tokens, std::vector &units, + std::vector &new_token_bag, Map &const_map, + Map &file_map, int depth = 0); + std::string to_string(const Unit &, int depth = 0); std::string to_string(const Err::Type &); std::string to_string(const Err &); std::ostream &operator<<(std::ostream &, const Unit &); std::ostream &operator<<(std::ostream &, const Err &); - - Err *preprocess(std::vector tokens, std::vector &units, - std::vector &new_token_bag, Map &const_map, - Map &file_map, int depth = 0); }; // namespace Preprocesser #endif