bug fix: end preprocessing with error when more than 32 nested parenthesis are encountered. closes #1201

time-shift
Houtan Bastani 2016-12-28 14:02:50 +01:00
parent 95ac8536ba
commit 9582431bd5
4 changed files with 38 additions and 1 deletions

View File

@ -2343,6 +2343,13 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
if (output_type == oMatlabDynamicModel)
{
// Check that we don't have more than 32 nested parenthesis because Matlab does not suppor this. See Issue #1201
testNestedParenthesis(model_output);
testNestedParenthesis(model_local_vars_output);
testNestedParenthesis(jacobian_output);
testNestedParenthesis(hessian_output);
testNestedParenthesis(third_derivatives_output);
DynamicOutput << "%" << endl
<< "% Model equations" << endl
<< "%" << endl

View File

@ -1292,6 +1292,28 @@ ModelTree::writeTemporaryTerms(const temporary_terms_t &tt, const temporary_term
}
}
void
ModelTree::testNestedParenthesis(const ostringstream &output) const
{
string str = output.str();
int open = 0;
for (string::iterator it = str.begin(); it != str.end(); it++)
{
if (*it == '(')
open++;
else if (*it == ')')
open--;
if (open > 32)
{
cout << "Error: A .m file created by Dynare will have more than 32 nested parenthesis. Matlab cannot support this. "
<< "Please use the use_dll option of the model block to circumnavigate this problem." << endl
<< " If you have not yet set up a compiler on your system, see the Matlab documentation for doing so." << endl
<< " For Windows, see: https://www.mathworks.com/help/matlab/matlab_external/install-mingw-support-package.html" << endl;
exit(EXIT_FAILURE);
}
}
}
void
ModelTree::compileTemporaryTerms(ostream &code_file, unsigned int &instruction_number, const temporary_terms_t &tt, map_idx_t map_idx, bool dynamic, bool steady_dynamic) const
{

View File

@ -190,7 +190,8 @@ protected:
void compileTemporaryTerms(ostream &code_file, unsigned int &instruction_number, const temporary_terms_t &tt, map_idx_t map_idx, bool dynamic, bool steady_dynamic) const;
//! Adds informations for simulation in a binary file
void Write_Inf_To_Bin_File(const string &basename, int &u_count_int, bool &file_open, bool is_two_boundaries, int block_mfs) const;
//! Checks for the number of nested parenthesis, issues error if > 32. Issue #1201
void testNestedParenthesis(const ostringstream &output) const;
//! Writes model local variables
/*! No temporary term is used in the output, so that local parameters declarations can be safely put before temporary terms declaration in the output files */
void writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const;

View File

@ -1398,6 +1398,13 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
if (output_type == oMatlabStaticModel)
{
// Check that we don't have more than 32 nested parenthesis because Matlab does not suppor this. See Issue #1201
testNestedParenthesis(model_output);
testNestedParenthesis(model_local_vars_output);
testNestedParenthesis(jacobian_output);
testNestedParenthesis(hessian_output);
testNestedParenthesis(third_derivatives_output);
StaticOutput << "residual = zeros( " << equations.size() << ", 1);" << endl << endl
<< "%" << endl
<< "% Model equations" << endl