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
Branches
Tags
No related merge requests found
/*
* 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 << "';";
......
/*
* 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
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment