Skip to content
Snippets Groups Projects
Verified Commit 6f845d9e authored by Sébastien Villemot's avatar Sébastien Villemot
Browse files

Macroprocessor: minor refactoring of macro functions

– use std::pair instead of std::tuple in internal storage
– make Environment::getFunction() symmetric with Environment::getVariable()
parent c07b8c90
No related branches found
No related tags found
No related merge requests found
/* /*
* Copyright © 2019-2023 Dynare Team * Copyright © 2019-2024 Dynare Team
* *
* This file is part of Dynare. * This file is part of Dynare.
* *
...@@ -54,7 +54,7 @@ Environment::getVariable(const string& name) const ...@@ -54,7 +54,7 @@ Environment::getVariable(const string& name) const
return getGlobalEnv()->getVariable(name); return getGlobalEnv()->getVariable(name);
} }
tuple<FunctionPtr, ExpressionPtr> pair<FunctionPtr, ExpressionPtr>
Environment::getFunction(const string& name) const Environment::getFunction(const string& name) const
{ {
if (auto it = functions.find(name); it != functions.end()) if (auto it = functions.find(name); it != functions.end())
...@@ -120,11 +120,11 @@ Environment::print(ostream& output, const vector<string>& vars, const optional<i ...@@ -120,11 +120,11 @@ Environment::print(ostream& output, const vector<string>& vars, const optional<i
if (vars.empty()) if (vars.empty())
for (const auto& it : functions) for (const auto& it : functions)
printFunction(output, it.second, line, save); printFunction(output, it.first, line, save);
else else
for (const auto& it : vars) for (const auto& it : vars)
if (isFunctionDefined(it)) if (isFunctionDefined(it))
printFunction(output, functions.at(it), line, save); printFunction(output, it, line, save);
if (parent) if (parent)
parent->print(output, vars, line, save); parent->print(output, vars, line, save);
...@@ -143,20 +143,21 @@ Environment::printVariable(ostream& output, const string& name, const optional<i ...@@ -143,20 +143,21 @@ Environment::printVariable(ostream& output, const string& name, const optional<i
} }
void void
Environment::printFunction(ostream& output, const tuple<FunctionPtr, ExpressionPtr>& function, Environment::printFunction(ostream& output, const string& name, const optional<int>& line,
const optional<int>& line, bool save) const bool save) const
{ {
assert(!save || line); assert(!save || line);
auto [func_signature, func_body] = getFunction(name);
output << (save ? "options_.macrovars_line_" + to_string(*line) + ".function." : " "); output << (save ? "options_.macrovars_line_" + to_string(*line) + ".function." : " ");
if (save) if (save)
{ {
get<0>(function)->printName(output); func_signature->printName(output);
output << " = '"; output << " = '";
} }
get<0>(function)->print(output); func_signature->print(output);
output << " = "; output << " = ";
get<1>(function)->print(output); func_body->print(output);
if (save) if (save)
output << "';"; output << "';";
......
/* /*
* Copyright © 2019-2023 Dynare Team * Copyright © 2019-2024 Dynare Team
* *
* This file is part of Dynare. * This file is part of Dynare.
* *
...@@ -33,7 +33,7 @@ class Environment ...@@ -33,7 +33,7 @@ class Environment
private: private:
const Environment* parent {nullptr}; const Environment* parent {nullptr};
map<string, ExpressionPtr> variables; map<string, ExpressionPtr> variables;
map<string, tuple<FunctionPtr, ExpressionPtr>> functions; map<string, pair<FunctionPtr, ExpressionPtr>> functions;
public: public:
Environment() = default; Environment() = default;
...@@ -45,7 +45,7 @@ public: ...@@ -45,7 +45,7 @@ public:
/* The following two functions are not marked [[nodiscard]], because they are used without output /* The following two functions are not marked [[nodiscard]], because they are used without output
to check whether they return an exception or not. */ to check whether they return an exception or not. */
ExpressionPtr getVariable(const string& name) const; // NOLINT(modernize-use-nodiscard) 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; getFunction(const string& name) const;
[[nodiscard]] codes::BaseType getType(const string& name) const; [[nodiscard]] codes::BaseType getType(const string& name) const;
[[nodiscard]] bool isVariableDefined(const string& name) const noexcept; [[nodiscard]] bool isVariableDefined(const string& name) const noexcept;
...@@ -59,8 +59,8 @@ public: ...@@ -59,8 +59,8 @@ public:
bool save = false) const; bool save = false) const;
void printVariable(ostream& output, const string& name, const optional<int>& line, void printVariable(ostream& output, const string& name, const optional<int>& line,
bool save) const; bool save) const;
void printFunction(ostream& output, const tuple<FunctionPtr, ExpressionPtr>& function, void printFunction(ostream& output, const string& name, const optional<int>& line,
const optional<int>& line, bool save) const; bool save) const;
[[nodiscard]] size_t [[nodiscard]] size_t
size() const noexcept size() const noexcept
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment