Commit 9d3f104b authored by Houtan Bastani's avatar Houtan Bastani

fix epilogue block

parent 82b14670
Pipeline #457 passed with stage
in 1 minute and 47 seconds
......@@ -152,10 +152,7 @@ enum class SymbolType
statementDeclaredVariable = 14, //!< Local variable assigned within a Statement (see subsample statement for example)
logTrend = 15, //!< Log-trend variable
unusedEndogenous = 16,
endogenousVAR = 17, //!< Variables declared in a var_model statement
endogenousEpilogue = 18, //!< Endogenous Variables used in the epilogue block
exogenousEpilogue = 19, //!< Variables used in the epilogue block
parameterEpilogue = 20 //!< Variables used in the epilogue block
endogenousVAR = 17 //!< Variables declared in a var_model statement
};
enum ExpressionType
......
......@@ -349,7 +349,6 @@ log_trend_var_list : log_trend_var_list symbol
;
var : VAR var_list ';'
| VAR '(' EPILOGUE ')' epilogue_var_list ';'
| VAR '(' DEFLATOR EQUAL { driver.begin_trend(); } hand_side ')' nonstationary_var_list ';'
{ driver.end_nonstationary_var(false, $6); }
| VAR '(' LOG_DEFLATOR EQUAL { driver.begin_trend(); } hand_side ')' nonstationary_var_list ';'
......@@ -545,7 +544,6 @@ nonstationary_var_list : nonstationary_var_list symbol
;
varexo : VAREXO varexo_list ';'
| VAREXO '(' EPILOGUE ')' epilogue_varexo_list ';'
;
varexo_det : VAREXO_DET varexo_det_list ';';
......@@ -553,7 +551,6 @@ varexo_det : VAREXO_DET varexo_det_list ';';
predetermined_variables : PREDETERMINED_VARIABLES predetermined_variables_list ';';
parameters : PARAMETERS parameter_list ';';
| PARAMETERS '(' EPILOGUE ')' epilogue_parameter_list ';';
;
model_local_variable : MODEL_LOCAL_VARIABLE model_local_variable_list ';';
......@@ -609,32 +606,6 @@ var_list : var_list symbol
{ driver.declare_endogenous($1, $2, $3); }
;
epilogue_var_list : epilogue_var_list symbol
{ driver.declare_epilogue_endogenous($2); }
| epilogue_var_list COMMA symbol
{ driver.declare_epilogue_endogenous($3); }
| symbol
{ driver.declare_epilogue_endogenous($1); }
| epilogue_var_list symbol named_var
{ driver.declare_epilogue_endogenous($2, "", $3); }
| epilogue_var_list COMMA symbol named_var
{ driver.declare_epilogue_endogenous($3, "", $4); }
| symbol named_var
{ driver.declare_epilogue_endogenous($1, "", $2); }
| epilogue_var_list symbol TEX_NAME
{ driver.declare_epilogue_endogenous($2, $3); }
| epilogue_var_list COMMA symbol TEX_NAME
{ driver.declare_epilogue_endogenous($3, $4); }
| symbol TEX_NAME
{ driver.declare_epilogue_endogenous($1, $2); }
| epilogue_var_list symbol TEX_NAME named_var
{ driver.declare_epilogue_endogenous($2, $3, $4); }
| epilogue_var_list COMMA symbol TEX_NAME named_var
{ driver.declare_epilogue_endogenous($3, $4, $5); }
| symbol TEX_NAME named_var
{ driver.declare_epilogue_endogenous($1, $2, $3); }
;
varexo_list : varexo_list symbol
{ driver.declare_exogenous($2); }
| varexo_list COMMA symbol
......@@ -687,32 +658,6 @@ varexo_det_list : varexo_det_list symbol
{ driver.declare_exogenous_det($1, $2, $3); }
;
epilogue_varexo_list : epilogue_varexo_list symbol
{ driver.declare_epilogue_exogenous($2); }
| epilogue_varexo_list COMMA symbol
{ driver.declare_epilogue_exogenous($3); }
| symbol
{ driver.declare_epilogue_exogenous($1); }
| epilogue_varexo_list symbol named_var
{ driver.declare_epilogue_exogenous($2, "", $3); }
| epilogue_varexo_list COMMA symbol named_var
{ driver.declare_epilogue_exogenous($3, "", $4); }
| symbol named_var
{ driver.declare_epilogue_exogenous($1, "", $2); }
| epilogue_varexo_list symbol TEX_NAME
{ driver.declare_epilogue_exogenous($2, $3); }
| epilogue_varexo_list COMMA symbol TEX_NAME
{ driver.declare_epilogue_exogenous($3, $4); }
| symbol TEX_NAME
{ driver.declare_epilogue_exogenous($1, $2); }
| epilogue_varexo_list symbol TEX_NAME named_var
{ driver.declare_epilogue_exogenous($2, $3, $4); }
| epilogue_varexo_list COMMA symbol TEX_NAME named_var
{ driver.declare_epilogue_exogenous($3, $4, $5); }
| symbol TEX_NAME named_var
{ driver.declare_epilogue_exogenous($1, $2, $3); }
;
parameter_list : parameter_list symbol
{ driver.declare_parameter($2); }
| parameter_list COMMA symbol
......@@ -739,32 +684,6 @@ parameter_list : parameter_list symbol
{ driver.declare_parameter($1, $2, $3); }
;
epilogue_parameter_list : epilogue_parameter_list symbol
{ driver.declare_parameter($2); }
| epilogue_parameter_list COMMA symbol
{ driver.declare_parameter($3); }
| symbol
{ driver.declare_parameter($1); }
| epilogue_parameter_list symbol named_var
{ driver.declare_parameter($2, "", $3); }
| epilogue_parameter_list COMMA symbol named_var
{ driver.declare_parameter($3, "", $4); }
| symbol named_var
{ driver.declare_parameter($1, "", $2); }
| epilogue_parameter_list symbol TEX_NAME
{ driver.declare_parameter($2, $3); }
| epilogue_parameter_list COMMA symbol TEX_NAME
{ driver.declare_parameter($3, $4); }
| symbol TEX_NAME
{ driver.declare_parameter($1, $2); }
| epilogue_parameter_list symbol TEX_NAME named_var
{ driver.declare_parameter($2, $3, $4); }
| epilogue_parameter_list COMMA symbol TEX_NAME named_var
{ driver.declare_parameter($3, $4, $5); }
| symbol TEX_NAME named_var
{ driver.declare_parameter($1, $2, $3); }
;
predetermined_variables_list : predetermined_variables_list symbol
{ driver.add_predetermined_variable($2); }
| predetermined_variables_list COMMA symbol
......@@ -961,7 +880,7 @@ epilogue_equation_list : epilogue_equation_list epilogue_equation
| epilogue_equation
;
epilogue_equation : symbol EQUAL expression ';'
epilogue_equation : NAME EQUAL expression ';'
{ driver.add_epilogue_equal($1, $3); }
;
......
......@@ -755,9 +755,6 @@ VariableNode::prepareForDerivation()
break;
case SymbolType::externalFunction:
case SymbolType::endogenousVAR:
case SymbolType::endogenousEpilogue:
case SymbolType::exogenousEpilogue:
case SymbolType::parameterEpilogue:
cerr << "VariableNode::prepareForDerivation: impossible case" << endl;
exit(EXIT_FAILURE);
}
......@@ -791,9 +788,6 @@ VariableNode::computeDerivative(int deriv_id)
exit(EXIT_FAILURE);
case SymbolType::externalFunction:
case SymbolType::endogenousVAR:
case SymbolType::endogenousEpilogue:
case SymbolType::exogenousEpilogue:
case SymbolType::parameterEpilogue:
cerr << "VariableNode::computeDerivative: Impossible case!" << endl;
exit(EXIT_FAILURE);
}
......@@ -860,15 +854,6 @@ VariableNode::writeJsonAST(ostream &output) const
case SymbolType::endogenousVAR:
output << "endogenousVAR";
break;
case SymbolType::endogenousEpilogue:
output << "endogenousEpilogue";
break;
case SymbolType::exogenousEpilogue:
output << "exogenousEpilogue";
break;
case SymbolType::parameterEpilogue:
output << "parameterEpilogue";
break;
}
output << "\", \"lag\" : " << lag << "}";
}
......@@ -927,7 +912,6 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
switch (type)
{
case SymbolType::parameter:
case SymbolType::parameterEpilogue:
if (output_type == ExprNodeOutputType::matlabOutsideModel)
output << "M_.params" << "(" << tsid + 1 << ")";
else
......@@ -999,11 +983,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
output << LEFT_ARRAY_SUBSCRIPT(output_type) << lag << RIGHT_ARRAY_SUBSCRIPT(output_type);
break;
case ExprNodeOutputType::epilogueFile:
output << datatree.symbol_table.getName(symb_id)
<< LEFT_ARRAY_SUBSCRIPT(output_type) << "epilogue_it__";
if (lag != 0)
output << lag;
output << RIGHT_ARRAY_SUBSCRIPT(output_type);
output << "dseries__." << datatree.symbol_table.getName(symb_id);
if (lag != 0)
output << LEFT_ARRAY_SUBSCRIPT(output_type) << lag << RIGHT_ARRAY_SUBSCRIPT(output_type);
break;
default:
cerr << "VariableNode::writeOutput: should not reach this point" << endl;
......@@ -1059,11 +1041,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
output << LEFT_ARRAY_SUBSCRIPT(output_type) << lag << RIGHT_ARRAY_SUBSCRIPT(output_type);
break;
case ExprNodeOutputType::epilogueFile:
output << datatree.symbol_table.getName(symb_id)
<< LEFT_ARRAY_SUBSCRIPT(output_type) << "epilogue_it__";
output << "dseries__." << datatree.symbol_table.getName(symb_id);
if (lag != 0)
output << lag;
output << RIGHT_ARRAY_SUBSCRIPT(output_type);
output << LEFT_ARRAY_SUBSCRIPT(output_type) << lag << RIGHT_ARRAY_SUBSCRIPT(output_type);
break;
default:
cerr << "VariableNode::writeOutput: should not reach this point" << endl;
......@@ -1119,25 +1099,15 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
output << LEFT_ARRAY_SUBSCRIPT(output_type) << lag << RIGHT_ARRAY_SUBSCRIPT(output_type);
break;
case ExprNodeOutputType::epilogueFile:
output << datatree.symbol_table.getName(symb_id)
<< LEFT_ARRAY_SUBSCRIPT(output_type) << "epilogue_it__";
output << "dseries__." << datatree.symbol_table.getName(symb_id);
if (lag != 0)
output << lag;
output << RIGHT_ARRAY_SUBSCRIPT(output_type);
output << LEFT_ARRAY_SUBSCRIPT(output_type) << lag << RIGHT_ARRAY_SUBSCRIPT(output_type);
break;
default:
cerr << "VariableNode::writeOutput: should not reach this point" << endl;
exit(EXIT_FAILURE);
}
break;
case SymbolType::endogenousEpilogue:
case SymbolType::exogenousEpilogue:
output << datatree.symbol_table.getName(symb_id)
<< LEFT_ARRAY_SUBSCRIPT(output_type) << "epilogue_it__";
if (lag != 0)
output << lag;
output << RIGHT_ARRAY_SUBSCRIPT(output_type);
break;
case SymbolType::externalFunction:
case SymbolType::trend:
case SymbolType::logTrend:
......@@ -1364,9 +1334,6 @@ VariableNode::getChainRuleDerivative(int deriv_id, const map<int, expr_t> &recur
exit(EXIT_FAILURE);
case SymbolType::externalFunction:
case SymbolType::endogenousVAR:
case SymbolType::endogenousEpilogue:
case SymbolType::exogenousEpilogue:
case SymbolType::parameterEpilogue:
cerr << "VariableNode::getChainRuleDerivative: Impossible case" << endl;
exit(EXIT_FAILURE);
}
......@@ -1405,9 +1372,6 @@ VariableNode::computeXrefs(EquationInfo &ei) const
case SymbolType::unusedEndogenous:
case SymbolType::externalFunction:
case SymbolType::endogenousVAR:
case SymbolType::endogenousEpilogue:
case SymbolType::exogenousEpilogue:
case SymbolType::parameterEpilogue:
break;
}
}
......
......@@ -295,9 +295,7 @@ Epilogue::Epilogue(SymbolTable &symbol_table_arg,
}
Epilogue::Epilogue(const Epilogue &m) :
DynamicModel {m},
endogs {m.endogs},
exogs {m.exogs}
DynamicModel {m}
{
for (const auto &it : m.def_table)
def_table.push_back(make_pair(it.first, it.second->clone(*this)));
......@@ -308,9 +306,6 @@ Epilogue::operator=(const Epilogue &m)
{
DynamicModel::operator=(m);
endogs = m.endogs;
exogs = m.exogs;
def_table.clear();
for (const auto &it : m.def_table)
def_table.push_back(make_pair(it.first, it.second->clone(*this)));
......@@ -319,17 +314,9 @@ Epilogue::operator=(const Epilogue &m)
}
void
Epilogue::addDefinition(int symb_id, expr_t expr)
Epilogue::addDefinition(string varname, expr_t expr)
{
AddVariable(symb_id); // Create the variable node to be used in write method
def_table.emplace_back(symb_id, expr);
endogs.emplace(symb_id);
expr->collectVariables(SymbolType::endogenous, exogs);
expr->collectVariables(SymbolType::exogenous, exogs);
expr->collectVariables(SymbolType::endogenousEpilogue, exogs);
expr->collectVariables(SymbolType::exogenousEpilogue, exogs);
for (auto it : endogs)
exogs.erase(it);
def_table.emplace_back(varname, expr);
}
void
......@@ -338,17 +325,16 @@ Epilogue::checkPass(WarningConsolidation &warnings) const
if (def_table.size() == 0)
return;
vector<int> so_far_defined;
vector<string> so_far_defined;
for (const auto & it : def_table)
{
if (find(so_far_defined.begin(), so_far_defined.end(), it.first) != so_far_defined.end())
{
cerr << "WARNING: in the 'epilogue' block, variable '" << symbol_table.getName(it.first)
<< "' is declared twice" << endl;
exit(EXIT_FAILURE);
}
if (find(so_far_defined.begin(), so_far_defined.end(), it.first) != so_far_defined.end())
{
cerr << "WARNING: in the 'epilogue' block, variable '" << it.first
<< "' is declared twice" << endl;
exit(EXIT_FAILURE);
}
else
so_far_defined.push_back(it.first);
}
}
void
......@@ -367,46 +353,23 @@ Epilogue::writeEpilogueFile(const string &basename) const
}
ExprNodeOutputType output_type = ExprNodeOutputType::epilogueFile;
output << "function epilogue_dseries__ = epilogue(params, epilogue_dseries__)" << endl
<< "% function epilogue_dseries__ = epilogue(params, epilogue_dseries__)" << endl
<< "% Epilogue file generated by Dynare preprocessor" << endl << endl
<< "% loop range variables" << endl
<< "epilogue_loop_begin_idx__ = lastdate(epilogue_dseries__) - epilogue_dseries__.lastobservedperiod;" << endl
<< "epilogue_loop_end_idx__ = lastdate(epilogue_dseries__) - firstdate(epilogue_dseries__) + 1;" << endl << endl;
output << "% endogenous" << endl;
for (auto symb_id : endogs)
output << symbol_table.getName(symb_id) << " = epilogue_dseries__." << symbol_table.getName(symb_id) << ".data;" << endl;
output << endl
<< "% exogenous" << endl;
for (auto symb_id : exogs)
output << symbol_table.getName(symb_id) << " = epilogue_dseries__." << symbol_table.getName(symb_id) << ".data;" << endl;
output << endl
<< "for epilogue_it__ = epilogue_loop_begin_idx__:epilogue_loop_end_idx__" << endl;
output << "function dseries__ = epilogue(params, dseries__)" << endl
<< "% function dseries__ = epilogue(params, dseries__)" << endl
<< "% Epilogue file generated by Dynare preprocessor" << endl;
deriv_node_temp_terms_t tef_terms;
temporary_terms_t temporary_terms;
temporary_terms_idxs_t temporary_terms_idxs;
for (const auto & it : def_table)
if (it.second->containsExternalFunction())
{
output << " ";
it.second->writeExternalFunctionOutput(output, output_type, temporary_terms, temporary_terms_idxs, tef_terms);
}
it.second->writeExternalFunctionOutput(output, output_type, temporary_terms, temporary_terms_idxs, tef_terms);
output << endl;
for (const auto & it : def_table)
{
output << " ";
getVariable(it.first)->ExprNode::writeOutput(output, output_type);
output << " = ";
output << "dseries__." << it.first << " = ";
it.second->writeOutput(output, output_type, temporary_terms, temporary_terms_idxs, tef_terms);
output << ";" << endl;
}
output << "end" << endl << endl;
for (auto symb_id : endogs)
output << "epilogue_dseries__." << symbol_table.getName(symb_id) << " = dseries(" << symbol_table.getName(symb_id)
<< ", epilogue_dseries__.firstdate);" << endl;
output << endl
<< "end" << endl;
output << "end" << endl;
output.close();
}
......@@ -70,11 +70,7 @@ class Epilogue : public DynamicModel
{
private:
//! Associates a set of symbol IDs (the variable(s) assigned in a given statement) to an expression (their assigned value)
vector<pair<int, expr_t>> def_table;
//! List of variables found in block
set<int> endogs;
set<int> exogs;
vector<pair<string, expr_t>> def_table;
public:
Epilogue(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants_arg,
......@@ -88,7 +84,7 @@ public:
Epilogue & operator=(Epilogue &&) = delete;
//! Add an expression of the form "var = expr;"
void addDefinition(int symb_id, expr_t expr);
void addDefinition(string varname, expr_t expr);
//! Checks that no variable is declared twice
void checkPass(WarningConsolidation &warnings) const;
......
......@@ -201,14 +201,6 @@ ParsingDriver::declare_endogenous(const string &name, const string &tex_name, co
declare_symbol(name, SymbolType::endogenous, tex_name, partition_value);
}
void
ParsingDriver::declare_epilogue_endogenous(const string &name,
const string &tex_name,
const vector<pair<string, string>> &partition_value)
{
declare_symbol(name, SymbolType::endogenousEpilogue, tex_name, partition_value);
}
void
ParsingDriver::declare_var_endogenous(const string &name)
{
......@@ -238,28 +230,12 @@ ParsingDriver::declare_exogenous_det(const string &name, const string &tex_name,
declare_symbol(name, SymbolType::exogenousDet, tex_name, partition_value);
}
void
ParsingDriver::declare_epilogue_exogenous(const string &name,
const string &tex_name,
const vector<pair<string, string>> &partition_value)
{
declare_symbol(name, SymbolType::exogenousEpilogue, tex_name, partition_value);
}
void
ParsingDriver::declare_parameter(const string &name, const string &tex_name, const vector<pair<string, string>> &partition_value)
{
declare_symbol(name, SymbolType::parameter, tex_name, partition_value);
}
void
ParsingDriver::declare_epilogue_parameter(const string &name,
const string &tex_name,
const vector<pair<string, string>> &partition_value)
{
declare_symbol(name, SymbolType::parameterEpilogue, tex_name, partition_value);
}
void
ParsingDriver::declare_statement_local_variable(const string &name)
{
......@@ -859,16 +835,7 @@ ParsingDriver::end_epilogue()
void
ParsingDriver::add_epilogue_equal(const string &varname, expr_t expr)
{
int id;
try
{
id = mod_file->symbol_table.getID(varname);
}
catch (SymbolTable::UnknownSymbolNameException &e)
{
error("Variable " + varname + " used in the epilogue block but was not declared.");
}
mod_file->epilogue.addDefinition(id, expr);
mod_file->epilogue.addDefinition(varname, expr);
}
void
......
......@@ -354,18 +354,12 @@ public:
void initval_file(const string &filename);
//! Declares an endogenous variable
void declare_endogenous(const string &name, const string &tex_name = "", const vector<pair<string, string>> &partition_value = {});
//! Declares an endogenous variable in the epilogue block
void declare_epilogue_endogenous(const string &name, const string &tex_name = "", const vector<pair<string, string>> &partition_value = {});
//! Declares an exogenous variable
void declare_exogenous(const string &name, const string &tex_name = "", const vector<pair<string, string>> &partition_value = {});
//! Declares an exogenous variable in the epilogue block
void declare_epilogue_exogenous(const string &name, const string &tex_name = "", const vector<pair<string, string>> &partition_value = {});
//! Declares an exogenous deterministic variable
void declare_exogenous_det(const string &name, const string &tex_name = "", const vector<pair<string, string>> &partition_value = {});
//! Declares a parameter
void declare_parameter(const string &name, const string &tex_name = "", const vector<pair<string, string>> &partition_value = {});
//! Declare a parameter in the epilogue block
void declare_epilogue_parameter(const string &name, const string &tex_name = "", const vector<pair<string, string>> &partition_value = {});
//! Declares a VAR variable and adds to symbol_list
void declare_var_endogenous(const string &name);
//! Declares a model local variable
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment