Tidy up a bit

This commit is contained in:
2024-07-10 02:10:36 +01:00
parent 5b35383edf
commit 3bf7130eda
2 changed files with 19 additions and 21 deletions

View File

@@ -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<Unit> 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<Unit> 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<Err *> 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);

View File

@@ -68,14 +68,14 @@ namespace Preprocesser
~Err(void);
};
Err *preprocess(std::vector<Lexer::Token *> tokens, std::vector<Unit> &units,
std::vector<Lexer::Token *> &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<Lexer::Token *> tokens, std::vector<Unit> &units,
std::vector<Lexer::Token *> &new_token_bag, Map &const_map,
Map &file_map, int depth = 0);
}; // namespace Preprocesser
#endif