Fix handling of undeclared variables in the planner_objective statement

Closes: #81
(cherry picked from commit 02ee077663)
5.x
Sébastien Villemot 2021-11-19 17:32:45 +01:00
parent a2977dd07c
commit 0c1a9fa916
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
2 changed files with 20 additions and 3 deletions

View File

@ -2392,7 +2392,7 @@ PlannerObjectiveStatement::checkPass(ModFileStructure &mod_file_struct, WarningC
assert(model_tree.equation_number() == 1);
if (model_tree.exoPresentInEqs())
{
cerr << "ERROR: You cannot include exogenous variables in the planner objective. Please "
cerr << "ERROR: You cannot include exogenous variables (or variables of undeclared type) in the planner objective. Please "
<< "define an auxiliary endogenous variable like eps_aux=epsilon and use it instead "
<< "of the varexo." << endl;
exit(EXIT_FAILURE);

View File

@ -321,8 +321,9 @@ ParsingDriver::add_model_variable(const string &name)
}
catch (SymbolTable::UnknownSymbolNameException &e)
{
// Declare variable as exogenous to continue parsing
// processing will end at end of model block if nostrict option was not passed
/* Declare variable as exogenous to continue parsing. Processing will end
at end of model block (or planner_objective statement) if nostrict
option was not passed. */
declare_exogenous(name);
undeclared_model_vars.insert(name);
symb_id = mod_file->symbol_table.getID(name);
@ -785,6 +786,7 @@ ParsingDriver::end_model()
exit_after_write = true;
cerr << it.second << endl;
}
undeclared_model_variable_errors.clear();
if (exit_after_write)
exit(EXIT_FAILURE);
@ -2022,6 +2024,21 @@ ParsingDriver::end_planner_objective(expr_t expr)
mod_file->addStatement(make_unique<PlannerObjectiveStatement>(*planner_objective));
// Handle undeclared variables (see #81)
bool exit_after_write = false;
if (undeclared_model_variable_errors.size() > 0)
for (auto &it : undeclared_model_variable_errors)
if (nostrict)
warning(it.second);
else
{
exit_after_write = true;
cerr << it.second << endl;
}
undeclared_model_variable_errors.clear();
if (exit_after_write)
exit(EXIT_FAILURE);
reset_data_tree();
}