From 6f845d9e5a12b8c19a33a26f3f589519396aab7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Mon, 8 Jul 2024 12:17:23 +0200 Subject: [PATCH] Macroprocessor: minor refactoring of macro functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit – use std::pair instead of std::tuple in internal storage – make Environment::getFunction() symmetric with Environment::getVariable() --- src/macro/Environment.cc | 19 ++++++++++--------- src/macro/Environment.hh | 10 +++++----- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/macro/Environment.cc b/src/macro/Environment.cc index 041011d2..4a0d1c0c 100644 --- a/src/macro/Environment.cc +++ b/src/macro/Environment.cc @@ -1,5 +1,5 @@ /* - * Copyright © 2019-2023 Dynare Team + * Copyright © 2019-2024 Dynare Team * * This file is part of Dynare. * @@ -54,7 +54,7 @@ Environment::getVariable(const string& name) const return getGlobalEnv()->getVariable(name); } -tuple<FunctionPtr, ExpressionPtr> +pair<FunctionPtr, ExpressionPtr> Environment::getFunction(const string& name) const { if (auto it = functions.find(name); it != functions.end()) @@ -120,11 +120,11 @@ Environment::print(ostream& output, const vector<string>& vars, const optional<i if (vars.empty()) for (const auto& it : functions) - printFunction(output, it.second, line, save); + printFunction(output, it.first, line, save); else for (const auto& it : vars) if (isFunctionDefined(it)) - printFunction(output, functions.at(it), line, save); + printFunction(output, it, line, save); if (parent) parent->print(output, vars, line, save); @@ -143,20 +143,21 @@ Environment::printVariable(ostream& output, const string& name, const optional<i } void -Environment::printFunction(ostream& output, const tuple<FunctionPtr, ExpressionPtr>& function, - const optional<int>& line, bool save) const +Environment::printFunction(ostream& output, const string& name, const optional<int>& line, + bool save) const { assert(!save || line); + auto [func_signature, func_body] = getFunction(name); output << (save ? "options_.macrovars_line_" + to_string(*line) + ".function." : " "); if (save) { - get<0>(function)->printName(output); + func_signature->printName(output); output << " = '"; } - get<0>(function)->print(output); + func_signature->print(output); output << " = "; - get<1>(function)->print(output); + func_body->print(output); if (save) output << "';"; diff --git a/src/macro/Environment.hh b/src/macro/Environment.hh index f55dd163..4a807cdd 100644 --- a/src/macro/Environment.hh +++ b/src/macro/Environment.hh @@ -1,5 +1,5 @@ /* - * Copyright © 2019-2023 Dynare Team + * Copyright © 2019-2024 Dynare Team * * This file is part of Dynare. * @@ -33,7 +33,7 @@ class Environment private: const Environment* parent {nullptr}; map<string, ExpressionPtr> variables; - map<string, tuple<FunctionPtr, ExpressionPtr>> functions; + map<string, pair<FunctionPtr, ExpressionPtr>> functions; public: Environment() = default; @@ -45,7 +45,7 @@ public: /* The following two functions are not marked [[nodiscard]], because they are used without output to check whether they return an exception or not. */ ExpressionPtr getVariable(const string& name) const; // NOLINT(modernize-use-nodiscard) - tuple<FunctionPtr, ExpressionPtr> // NOLINT(modernize-use-nodiscard) + pair<FunctionPtr, ExpressionPtr> // NOLINT(modernize-use-nodiscard) getFunction(const string& name) const; [[nodiscard]] codes::BaseType getType(const string& name) const; [[nodiscard]] bool isVariableDefined(const string& name) const noexcept; @@ -59,8 +59,8 @@ public: bool save = false) const; void printVariable(ostream& output, const string& name, const optional<int>& line, bool save) const; - void printFunction(ostream& output, const tuple<FunctionPtr, ExpressionPtr>& function, - const optional<int>& line, bool save) const; + void printFunction(ostream& output, const string& name, const optional<int>& line, + bool save) const; [[nodiscard]] size_t size() const noexcept { -- GitLab