From b83bdd0d45223b789ec99bdc16d42fb646fdb8e2 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Mon, 15 Apr 2024 16:31:45 +0630 Subject: preprocesser function now only returns a pp_err_t We leave the parameter tokens alone, considering it constant, while the parameter vec_out is used to hold the new stream of tokens. This allows the caller to have a before and after view on the token stream and reduces the worry of double frees. --- asm/preprocesser.cpp | 16 +++++++++++----- asm/preprocesser.hpp | 3 +-- 2 files changed, 12 insertions(+), 7 deletions(-) (limited to 'asm') diff --git a/asm/preprocesser.cpp b/asm/preprocesser.cpp index 1245ff7..c0cf9e2 100644 --- a/asm/preprocesser.cpp +++ b/asm/preprocesser.cpp @@ -27,13 +27,17 @@ using std::pair, std::vector, std::make_pair, std::string, std::string_view; delete t; \ }); -pair, pp_err_t> preprocesser(vector &tokens) +pp_err_t preprocesser(const vector &tokens, + vector &vec_out) { vector use_block_tokens; pp_err_t pperr{pp_err_type_t::OK}; std::tie(use_block_tokens, pperr) = preprocess_use_blocks(tokens); if (pperr.type != pp_err_type_t::OK) - return ERR(pperr); + { + vec_out = tokens; + return pperr; + } vector const_block_tokens; std::tie(const_block_tokens, pperr) = @@ -41,12 +45,14 @@ pair, pp_err_t> preprocesser(vector &tokens) if (pperr.type != pp_err_type_t::OK) { VCLEAR(tokens); - tokens = use_block_tokens; - return ERR(pperr); + vec_out = use_block_tokens; + return pperr; } VCLEAR(use_block_tokens); - return VAL(const_block_tokens); + vec_out = const_block_tokens; + + return pp_err_t{pp_err_type_t::OK}; } pair, pp_err_t> diff --git a/asm/preprocesser.hpp b/asm/preprocesser.hpp index f4cb3bb..ed5a421 100644 --- a/asm/preprocesser.hpp +++ b/asm/preprocesser.hpp @@ -42,8 +42,7 @@ struct pp_err_t std::ostream &operator<<(std::ostream &, pp_err_t &); -std::pair, pp_err_t> -preprocesser(std::vector &); +pp_err_t preprocesser(const std::vector &, std::vector &); std::pair, pp_err_t> preprocess_const_blocks(const std::vector &); -- cgit v1.2.3-13-gbd6f