diff --git a/parser.src/DynareBison.yy b/parser.src/DynareBison.yy index 022924cc8..386d15adb 100644 --- a/parser.src/DynareBison.yy +++ b/parser.src/DynareBison.yy @@ -50,6 +50,9 @@ %right POWER %nonassoc FACTORIAL %token EXP LOG LOG10 LN SIN COS TAN ASIN ACOS ATAN SINH COSH TANH ASINH ACOSH ATANH SQRT +/* isn't parsed from the *.mod file, but used to distinguish EQUAL in equation and EQUAL in assignment in operation codes +*/ +%token ASSIGN %% statement_list diff --git a/parser.src/DynareMain.cc b/parser.src/DynareMain.cc index 407a250ce..225c172d9 100644 --- a/parser.src/DynareMain.cc +++ b/parser.src/DynareMain.cc @@ -14,6 +14,7 @@ using namespace std; #include "DynareParser.h" #include "DynareScanner.h" #include "OutputFile.h" +#include "Interface.h" //------------------------------------------------------------------------------ /*! main function \brief Main function of Dynare. @@ -24,6 +25,11 @@ int main(int argc, char** argv) { OutputFile output_file; ostringstream output; +#ifdef SCILAB + interfaces interface(eScilab); +#else + interfaces interface(eMatlab); +#endif int retval = 0; try { if (argc <2) @@ -67,8 +73,8 @@ int main(int argc, char** argv) p.finish(); string name = argv[1]; name.erase(name.size()-4,4); - // Opening and init main Output file (M file) - output_file.Open(name+".m"); + // Opening and init main Output file (.m or .sci file) + output_file.Open(name); // Writing remaining string output to output file output_file.Save(output); diff --git a/parser.src/DynareParser.cc b/parser.src/DynareParser.cc index 64dd252f8..bcb952706 100644 --- a/parser.src/DynareParser.cc +++ b/parser.src/DynareParser.cc @@ -96,6 +96,12 @@ dynare::Objects* dynare::parser::add_variable(Objects* var,Objects* olag) int lag = atoi((olag->symbol).c_str()); //cout << "symbol = " << olag->symbol << endl; //cout << "lag = " << lag << endl; + if ((var->type == eExogenous) && lag != 0) + { + std::cout << "Warning: exogenous variable " + << var->symbol + << " has lag " << lag << "\n"; + } if ((var->type == eEndogenous) || (var->type == eExogenous)) variable_table.AddVariable(var->symbol,lag); //cout << "add_model_token : " << var->ID << endl; @@ -638,7 +644,7 @@ dynare::Objects* dynare::parser::add_equal(Objects* arg1, Objects* arg2) dynare::Objects* dynare::parser::init_local_parameter(Objects* arg1, Objects* arg2) { - NodeID id = model_tree.AddEqual(arg1->ID, arg2->ID); + NodeID id = model_tree.AddAssign(arg1->ID, arg2->ID); return new Objects("", id, eTempResult); } diff --git a/parser.src/Makefile b/parser.src/Makefile index c22aa555d..0e4b4727a 100644 --- a/parser.src/Makefile +++ b/parser.src/Makefile @@ -1,94 +1,102 @@ -CPP = c++ -ifeq ($(DEBUG),yes) - CPPFLAGS = -mno-cygwin -ggdb -pg -Wall - FLEXFLAGS = -i -else -ifeq ($(MINGW),yes) - CPPFLAGS = -O2 - FLEXFLAGS = -i -d -else - CPPFLAGS = -mno-cygwin -O2 - FLEXFLAGS = -i -endif -endif -OBJ=\ - DynareFlex.o\ - DynareBison.o\ - ComputingTasks.o\ - DynareMain.o\ - Expression.o\ - ModelParameters.o\ - ModelTree.o\ - NumericalConstants.o\ - NumericalInitialization.o\ - OperatorTable.o\ - OutputFile.o\ - Shocks.o\ - SigmaeInitialization.o\ - SymbolTable.o\ - TmpSymbolTable.o\ - VariableTable.o\ - DynareParser.o\ - DataTree.o - - -################################################################################ -### Build ###################################################################### -################################################################################ - -all: dynare.exe - -dynare.exe: $(OBJ) - $(CPP) $(CPPFLAGS) -o dynare.exe $(OBJ);\ - cp dynare.exe ../matlab/dynare_m.exe;\ - - - -################################################################################ -### Compile #################################################################### -################################################################################ - -%.o : %.cc - $(CPP) $(CPPFLAGS) -MD -I include -c $< - @cp $*.d $*.P; \ - sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \ - -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \ - rm -f $*.d - --include $(OBJ:.o=.P) - -DynareFlex.cc: DynareFlex.ll include/DynareScanner.h - flex $(FLEXFLAGS) -oDynareFlex.cc DynareFlex.ll - -DynareBison.cc include/DynareBison.h: DynareBison.yy include/DynareParser.h - (bison -v -d -b --verbose -o DynareBison.cc DynareBison.yy; mv DynareBison.hh include/DynareBison.h) - - -################################################################################ -### Clean ###################################################################### -################################################################################ - -clean: - rm "ComputingTasks.o" \ - "DynareParser.o" \ - "DynareBison.o" \ - "DynareFlex.o" \ - "DynareMain.o" \ - "Expression.o" \ - "ModelParameters.o" \ - "ModelTree.o" \ - "NumericalConstants.o" \ - "NumericalInitialization.o" \ - "OperatorTable.o" \ - "OutputFile.o" \ - "Shocks.o" \ - "SigmaeInitialization.o" \ - "SymbolTable.o" \ - "TmpSymbolTable.o" \ - "VariableTable.o" \ - "DataTree.o" \ - "dynare.exe" \ - "DynareBison.cc" \ - "include/DynareBison.h" \ - "DynareFlex.cc" - +CPP = c++ +ifeq ($(DEBUG),yes) + CPPFLAGS = -mno-cygwin -ggdb -pg -Wall + FLEXFLAGS = -i +else +ifeq ($(MINGW),yes) + CPPFLAGS = -O2 + FLEXFLAGS = -i -d +else + CPPFLAGS = -mno-cygwin -O2 + FLEXFLAGS = -i +endif +endif +ifeq ($(PROFILE),yes) + CPPFLAGS = -mno-cygwin -O2 -pg +endif +OBJ=\ + DynareFlex.o\ + DynareBison.o\ + ComputingTasks.o\ + DynareMain.o\ + Expression.o\ + ModelParameters.o\ + ModelTree.o\ + NumericalConstants.o\ + NumericalInitialization.o\ + OperatorTable.o\ + OutputFile.o\ + Shocks.o\ + SigmaeInitialization.o\ + SymbolTable.o\ + TmpSymbolTable.o\ + VariableTable.o\ + DynareParser.o\ + DataTree.o\ + Interface.o + +################################################################################ +### Build ###################################################################### +################################################################################ + +all: dynare.exe + +dynare.exe: $(OBJ) + $(CPP) $(CPPFLAGS) -o dynare.exe $(OBJ);\ + cp dynare.exe ../matlab/dynare_m.exe;\ + +dynare_s.exe: $(OBJ) + $(CPP) $(CPPFLAGS) -Iinclude -DSCILAB -o DynareMain.o -c DynareMain.cc + $(CPP) $(CPPFLAGS) -o dynare_s.exe $(OBJ);\ + cp dynare_s.exe ../scilab/dynare_s.exe;\ + + + +################################################################################ +### Compile #################################################################### +################################################################################ + +%.o : %.cc + $(CPP) $(CPPFLAGS) -MD -I include -c $< + @cp $*.d $*.P; \ + sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \ + -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \ + rm -f $*.d + +-include $(OBJ:.o=.P) + +DynareFlex.cc: DynareFlex.ll include/DynareScanner.h + flex $(FLEXFLAGS) -oDynareFlex.cc DynareFlex.ll + +DynareBison.cc include/DynareBison.h: DynareBison.yy include/DynareParser.h + (bison -v -d -b --verbose -o DynareBison.cc DynareBison.yy; mv DynareBison.hh include/DynareBison.h) + + +################################################################################ +### Clean ###################################################################### +################################################################################ + +clean: + rm "ComputingTasks.o" \ + "DynareParser.o" \ + "DynareBison.o" \ + "DynareFlex.o" \ + "DynareMain.o" \ + "Expression.o" \ + "ModelParameters.o" \ + "ModelTree.o" \ + "NumericalConstants.o" \ + "NumericalInitialization.o" \ + "OperatorTable.o" \ + "OutputFile.o" \ + "Shocks.o" \ + "SigmaeInitialization.o" \ + "SymbolTable.o" \ + "TmpSymbolTable.o" \ + "VariableTable.o" \ + "DataTree.o" \ + "dynare.exe" \ + "DynareBison.cc" \ + "include/DynareBison.h" \ + "DynareFlex.cc" + diff --git a/parser.src/ModelTree.cc b/parser.src/ModelTree.cc index ed26792d4..55163e708 100644 --- a/parser.src/ModelTree.cc +++ b/parser.src/ModelTree.cc @@ -19,6 +19,7 @@ using namespace std; #include "NumericalConstants.h" #include "ModelTree.h" #include "ModelParameters.h" +#include "Interface.h" //------------------------------------------------------------------------------ ostringstream ModelTree::output; //------------------------------------------------------------------------------ @@ -38,7 +39,7 @@ void ModelTree::OpenMFiles(string iModelFileName1, string iModelFileName2) { if (iModelFileName1.size()) { - iModelFileName1 += ".m"; + iModelFileName1 += interfaces::function_file_extension(); mStaticModelFile.open(iModelFileName1.c_str(),ios::out|ios::binary); if (!mStaticModelFile.is_open()) { @@ -49,12 +50,15 @@ void ModelTree::OpenMFiles(string iModelFileName1, string iModelFileName2) iModelFileName1.erase(iModelFileName1.end()-2,iModelFileName1.end()); //Writing comments and function definition command mStaticModelFile << "function [residual, g1] = " << iModelFileName1 << "( y, x )\n"; - mStaticModelFile << "%\n% Status : Computes static model for Dynare\n%\n"; - mStaticModelFile << "% Warning : this file is generated automatically by Dynare\n"; - mStaticModelFile << "% from model file (.mod)\n\n"; + mStaticModelFile << interfaces::comment()+"\n"+interfaces::comment(); + mStaticModelFile << "Status : Computes static model for Dynare\n%\n"; + mStaticModelFile << interfaces::comment(); + mStaticModelFile << "Warning : this file is generated automatically by Dynare\n"; + mStaticModelFile << interfaces::comment(); + mStaticModelFile << " from model file (.mod)\n\n"; if (iModelFileName2.size() && (computeJacobian||computeJacobianExo||computeHessian)) { - iModelFileName2 += ".m"; + iModelFileName2 += interfaces::function_file_extension(); mDynamicModelFile.open(iModelFileName2.c_str(),ios::out|ios::binary); if (!mDynamicModelFile.is_open()) { @@ -64,9 +68,12 @@ void ModelTree::OpenMFiles(string iModelFileName1, string iModelFileName2) } iModelFileName2.erase(iModelFileName2.end()-2,iModelFileName2.end()); mDynamicModelFile << "function [residual, g1, g2] = " << iModelFileName2 << "(y, x)\n"; - mDynamicModelFile << "%\n% Status : Computes dynamic model for Dynare\n%\n"; - mDynamicModelFile << "%Warning : this file is generated automatically by Dynare\n"; - mDynamicModelFile << "% from model file (.mod)\n\n"; + mDynamicModelFile << interfaces::comment()+"\n"+interfaces::comment(); + mDynamicModelFile << "Status : Computes dynamic model for Dynare\n%\n"; + mDynamicModelFile << interfaces::comment(); + mDynamicModelFile << "Warning : this file is generated automatically by Dynare\n"; + mDynamicModelFile << interfaces::comment(); + mDynamicModelFile << " from model file (.mod)\n\n"; } } @@ -137,11 +144,13 @@ void ModelTree::SaveMFiles() if (mStaticModelFile.is_open()) { mStaticModelFile << StaticOutput.str(); + interfaces::function_close(); mStaticModelFile.close(); } if (mDynamicModelFile.is_open() && (computeJacobian||computeJacobianExo||computeHessian)) { mDynamicModelFile << DynamicOutput.str(); + interfaces::function_close(); mDynamicModelFile.close(); } } @@ -285,6 +294,7 @@ void ModelTree::derive(int iOrder) (*currentIT)->reference_count[0]++; } } + std::cout << "size " << EqualTokenIDs.size() << "\n"; mDerivativeIndex.resize(iOrder); // Uncomment this to print model tree data /* @@ -340,6 +350,7 @@ void ModelTree::derive(int iOrder) lArg2 = lToken->id2; lType1 = lToken->type1; lD1 = DeriveArgument(lArg1, lType1, var); + lD2 = Zero; if (lArg2 != NullID) lD2 = DeriveArgument(lArg2, eTempResult, var); // Case where token is a final argument @@ -347,9 +358,12 @@ void ModelTree::derive(int iOrder) { setDerivativeAdress(*currentIT, lD1, var); } + else if (lD1 == Zero && lD2 == Zero) + { + setDerivativeAdress(*currentIT, Zero, var); + } else { - switch (lToken->op_code) { case UMINUS: @@ -692,7 +706,7 @@ string ModelTree::setStaticModel(void) tree_it = BeginModel; for (; tree_it != mModelTree.end(); tree_it++) { - if ((*tree_it)->op_code == EQUAL) + if ((*tree_it)->op_code == EQUAL || (*tree_it)->op_code == ASSIGN ) { if ((*tree_it)->id1->type1 == eLocalParameter) { @@ -728,7 +742,9 @@ string ModelTree::setStaticModel(void) // Writing Jacobian for endogenous variables without lag for(; tree_it != mModelTree.end(); tree_it++) { - if ((*tree_it)->op_code != NoOpCode && (*tree_it)->op_code != EQUAL) + if ((*tree_it)->op_code != NoOpCode + && (*tree_it)->op_code != EQUAL + && (*tree_it)->op_code != ASSIGN) { if (optimize(*tree_it) == 1) { @@ -766,7 +782,9 @@ string ModelTree::setStaticModel(void) StaticOutput << "if M_.param_nbr > 0\n params = M_.params;\nend\n"; StaticOutput << " residual = zeros( " << ModelParameters::eq_nbr << ", 1);\n"; - StaticOutput << "\n\t%\n\t% Model equations\n\t%\n\n"; + StaticOutput << "\n\t"+interfaces::comment()+"\n\t"+interfaces::comment(); + StaticOutput << "Model equations\n\t"; + StaticOutput << interfaces::comment() + "\n\n"; StaticOutput << model_output.str(); StaticOutput << "if ~isreal(residual)\n"; StaticOutput << " residual = real(residual)+imag(residual).^2;\n"; @@ -775,7 +793,9 @@ string ModelTree::setStaticModel(void) StaticOutput << " g1 = " << "zeros(" << ModelParameters::eq_nbr << ", " << ModelParameters::endo_nbr << ");\n" ; - StaticOutput << "\n\t%\n\t% Jacobian matrix\n\t%\n\n"; + StaticOutput << "\n\t"+interfaces::comment()+"\n\t"+interfaces::comment(); + StaticOutput << "Jacobian matrix\n\t"; + StaticOutput << interfaces::comment() + "\n\n"; StaticOutput << jacobian_output.str(); StaticOutput << " if ~isreal(g1)\n"; StaticOutput << " g1 = real(g1)+2*imag(g1);\n"; @@ -838,7 +858,7 @@ string ModelTree::setDynamicModel(void) tree_it = BeginModel; for (; tree_it != mModelTree.end(); tree_it++) { - if ((*tree_it)->op_code == EQUAL) + if ((*tree_it)->op_code == EQUAL || (*tree_it)->op_code == ASSIGN) { if ((*tree_it)->id1->type1 == eLocalParameter) { @@ -873,7 +893,9 @@ string ModelTree::setDynamicModel(void) for(; tree_it != mModelTree.end(); tree_it++) { - if ((*tree_it)->op_code != NoOpCode && (*tree_it)->op_code != EQUAL) + if ((*tree_it)->op_code != NoOpCode + && (*tree_it)->op_code != EQUAL + && (*tree_it)->op_code != ASSIGN) { if (optimize(*tree_it) == 1) { @@ -893,7 +915,9 @@ string ModelTree::setDynamicModel(void) cout << "\tJacobian .. "; for(; tree_it != mModelTree.end(); tree_it++) { - if ((*tree_it)->op_code != NoOpCode && (*tree_it)->op_code != EQUAL) + if ((*tree_it)->op_code != NoOpCode + && (*tree_it)->op_code != EQUAL + && (*tree_it)->op_code != ASSIGN) { if (optimize(*tree_it) == 1) { @@ -961,7 +985,9 @@ string ModelTree::setDynamicModel(void) { DynamicOutput << "global M_ it_\n"; DynamicOutput << "if M_.param_nbr > 0\n params = M_.params;\nend\n"; - DynamicOutput << "\n\t%\n\t% Model equations\n\t%\n\n"; + DynamicOutput << "\n\t"+interfaces::comment()+"\n\t"+interfaces::comment(); + DynamicOutput << "Model equations\n\t"; + DynamicOutput << interfaces::comment() + "\n\n"; DynamicOutput << "residual = zeros(" << nrows << ", 1);\n"; DynamicOutput << model_output.str(); @@ -972,7 +998,9 @@ string ModelTree::setDynamicModel(void) // Writing initialization instruction for matrix g1 DynamicOutput << " g1 = " << "zeros(" << nrows << ", " << VariableTable::size() << ");\n" ; - DynamicOutput << "\n\t%\n\t% Jacobian matrix\n\t%\n\n"; + DynamicOutput << "\n\t"+interfaces::comment()+"\n\t"+interfaces::comment(); + DynamicOutput << "Jacobian matrix\n\t"; + DynamicOutput << interfaces::comment()+"\n\n"; DynamicOutput << jacobian_output.str(); DynamicOutput << "end\n"; } @@ -984,7 +1012,9 @@ string ModelTree::setDynamicModel(void) DynamicOutput << " g2 = " << "sparse([],[],[]," << nrows << ", " << ncols << ", " << 5*ncols << ");\n"; - DynamicOutput << "\n\t%\n\t% Hessian matrix\n\t%\n\n"; + DynamicOutput << "\n\t"+interfaces::comment()+"\n\t"+interfaces::comment(); + DynamicOutput << "Hessian matrix\n\t"; + DynamicOutput << interfaces::comment() + "\n\n"; DynamicOutput << hessian_output.str() << lsymetric.str(); DynamicOutput << "end;\n"; } diff --git a/parser.src/OutputFile.cc b/parser.src/OutputFile.cc index 12c9dded0..83aee48c1 100644 --- a/parser.src/OutputFile.cc +++ b/parser.src/OutputFile.cc @@ -11,6 +11,7 @@ using namespace std; #include "OutputFile.h" #include "SymbolTable.h" #include "ModelTree.h" +#include "Interface.h" //------------------------------------------------------------------------------ OutputFile::OutputFile() { @@ -24,71 +25,74 @@ OutputFile::~OutputFile() //------------------------------------------------------------------------------ void OutputFile::Open(string iFileName) { - if (iFileName.size()) + if (iFileName.size()) + { + string fname(iFileName); + fname += interfaces::function_file_extension(); + mOutputFile.open(fname.c_str(),ios::out|ios::binary); + if (!mOutputFile.is_open()) { - mOutputFile.open(iFileName.c_str(),ios::out|ios::binary); - if (!mOutputFile.is_open()) - { - cout << "OutputFile::Open : Error : Can't open file " << iFileName - << " for writing\n"; - exit(-1); - } - } - else - { - cout << "OutputFile::Open : Error : Missing file name\n"; - exit(-1); - } - mOutputFile << "%\n% Status : main Dynare file \n%\n"; - mOutputFile << "% Warning : this file is generated automatically by Dynare\n"; - mOutputFile << "% from model file (.mod)\n\n"; - if (clear_all) - mOutputFile << "clear all\n"; - mOutputFile << "tic;\n"; - mOutputFile << "global M_ oo_ exedet_ exdet_ recur_ recurs_ \n"; - mOutputFile << "global options_ endval_\n"; - mOutputFile << "global ys0_ recurs0_ ex0_ ct_\n"; - mOutputFile << "options_ = [];\n"; - iFileName.erase(iFileName.size()-2); - mOutputFile << "M_.fname = '" << iFileName << "';\n"; - mOutputFile << "%\n% Some global variables initialisation\n%\n"; - mOutputFile << "global_initialization;\n"; - mOutputFile << "diary off;\nwarning off;\n"; - mOutputFile << "\ndelete " << iFileName << ".log;\n"; - mOutputFile << "warning on;\nwarning backtrace;\n"; - mOutputFile << "logname_ = '" << iFileName << ".log';\n"; - mOutputFile << "diary '" << iFileName << ".log';\n"; - if (ModelTree::offset == 0) - { - mOutputFile << "if exist('" << iFileName << "_static.c', 'file') == 2,\n"; - mOutputFile << " clear " << iFileName << "_static\n"; - mOutputFile << " mex -O " << iFileName << "_static.c\n"; - mOutputFile << "end\n"; - mOutputFile << "if exist('" << iFileName << "_dynamic.c', 'file') == 2,\n"; - mOutputFile << " clear " << iFileName << "_dynamic\n"; - mOutputFile << " mex -O " << iFileName << "_dynamic.c\n"; - mOutputFile << "end\n"; - } - else - { - mOutputFile << "if exist('" << iFileName << "_static.dll', 'file') == 3,\n"; - mOutputFile << " clear " << iFileName << "_static\n"; - mOutputFile << " !del " << iFileName << "_static.dll\n"; - mOutputFile << "end\n"; - mOutputFile << "if exist('" << iFileName << "_dynamic.dll', 'file') == 3,\n"; - mOutputFile << " clear " << iFileName << "_dynamic\n"; - mOutputFile << " !del " << iFileName << "_dynamic.dll\n"; - mOutputFile << "end\n"; + cout << "OutputFile::Open : Error : Can't open file " << iFileName + << " for writing\n"; + exit(-1); } + } + else + { + cout << "OutputFile::Open : Error : Missing file name\n"; + exit(-1); + } + mOutputFile << interfaces::comment() << "\n" << interfaces::comment(); + mOutputFile << "Status : main Dynare file \n"; + mOutputFile << interfaces::comment() << "\n" << interfaces::comment(); + mOutputFile << "Warning : this file is generated automatically by Dynare\n"; + mOutputFile << interfaces::comment(); + mOutputFile << " from model file (.mod)\n\n"; + if (clear_all) + mOutputFile << "clear all\n"; + mOutputFile << "tic;\n"; + mOutputFile << "global M_ oo_ exedet_ exdet_ recur_ recurs_ \n"; + mOutputFile << "global options_ endval_\n"; + mOutputFile << "global ys0_ recurs0_ ex0_ ct_\n"; + mOutputFile << "options_ = [];\n"; + mOutputFile << "M_.fname = '" << iFileName << "';\n"; + mOutputFile << interfaces::comment() << "\n" << interfaces::comment(); + mOutputFile << "Some global variables initialisation\n"; + mOutputFile << interfaces::comment() << "\n"; + mOutputFile << "global_initialization;\n"; + mOutputFile << "diary off;\nwarning off;\n"; + mOutputFile << "\n" << interfaces::delete_file(iFileName + ".log;\n"); + mOutputFile << "warning on;\nwarning backtrace;\n"; + mOutputFile << "logname_ = '" << iFileName << ".log';\n"; + mOutputFile << "diary '" << iFileName << ".log';\n"; + if (ModelTree::offset == 0) + { + mOutputFile << "if "; + mOutputFile << interfaces::file_exist(iFileName + "_static.c)")+"\n"; + mOutputFile << " clear " << iFileName << "_static\n"; + mOutputFile << " " + interfaces::compile(iFileName +"_static.c")+"\n"; + mOutputFile << "end\n"; + mOutputFile << "if "; + mOutputFile << interfaces::file_exist(iFileName + "_dynamic.c)")+"\n"; + mOutputFile << " clear " << iFileName << "_dynamic\n"; + mOutputFile << " " + interfaces::compile(iFileName+"_dynamic.c")+"\n"; + mOutputFile << "end\n"; + } + else + { + mOutputFile << "erase_compiled_function('" + iFileName +"_static');\n"; + mOutputFile << "erase_compiled_function('" + iFileName +"_dynamic');\n"; + mOutputFile << interfaces::load_model_function_files(iFileName); + } } //------------------------------------------------------------------------------ void OutputFile::Save(ostringstream& iOutput) { - mOutputFile << SymbolTable::get(); - mOutputFile << ModelTree::get(); - mOutputFile << iOutput.str(); - mOutputFile << "\ndisp(['Total computing time : ' sec2hms(round(toc)) ]);\n"; - mOutputFile.close(); + mOutputFile << SymbolTable::get(); + mOutputFile << ModelTree::get(); + mOutputFile << iOutput.str(); + mOutputFile << "\ndisp(['Total computing time : ' sec2hms(round(toc)) ]);\n"; + mOutputFile.close(); } //------------------------------------------------------------------------------ #ifdef TEST_OUTPUTFILE @@ -100,113 +104,113 @@ void OutputFile::Save(ostringstream& iOutput) #include "TmpSymbolTable.h" int main(void) { - OutputFile outputfile; - SymbolTable st; - Expression exp; - NumericalConstants numconst; - NumericalInitialization numinit; + OutputFile outputfile; + SymbolTable st; + Expression exp; + NumericalConstants numconst; + NumericalInitialization numinit; - outputfile.Open("Test.m"); + outputfile.Open("Test.m"); - SymbolTable::AddSymbolDeclar("a",eExogenous);//0 - SymbolTable::AddSymbolDeclar("b",eParameter);//1 - SymbolTable::AddSymbolDeclar("c",eExogenous);//2 - SymbolTable::AddSymbolDeclar("d",eExogenousDet);//3 - SymbolTable::AddSymbolDeclar("x",eParameter);//3 - SymbolTable::AddSymbolDeclar("y",eExogenous);//3 + SymbolTable::AddSymbolDeclar("a",eExogenous);//0 + SymbolTable::AddSymbolDeclar("b",eParameter);//1 + SymbolTable::AddSymbolDeclar("c",eExogenous);//2 + SymbolTable::AddSymbolDeclar("d",eExogenousDet);//3 + SymbolTable::AddSymbolDeclar("x",eParameter);//3 + SymbolTable::AddSymbolDeclar("y",eExogenous);//3 - numconst.AddConstant("alpha"); - exp.AddToken(0,eExogenous,EXP); //0 - exp.AddToken(0,eParameter,0,eExogenousDet,PLUS); //1 - exp.AddToken(0,eTempResult,UMINUS); //2 - exp.AddToken(1,eExogenous,1,eTempResult,TIMES); //3 - exp.AddToken(3,eTempResult,0,eNumericalConstant,TIMES); //4 - exp.AddToken(4,eTempResult,0,eTempResult,COMMA); //5 - exp.AddToken(5,eTempResult,0,eExogenous,COMMA); //6 - exp.AddToken(6,eTempResult,"function1"); //6 - exp.set(); - //cout << exp.get(); + numconst.AddConstant("alpha"); + exp.AddToken(0,eExogenous,EXP); //0 + exp.AddToken(0,eParameter,0,eExogenousDet,PLUS); //1 + exp.AddToken(0,eTempResult,UMINUS); //2 + exp.AddToken(1,eExogenous,1,eTempResult,TIMES); //3 + exp.AddToken(3,eTempResult,0,eNumericalConstant,TIMES); //4 + exp.AddToken(4,eTempResult,0,eTempResult,COMMA); //5 + exp.AddToken(5,eTempResult,0,eExogenous,COMMA); //6 + exp.AddToken(6,eTempResult,"function1"); //6 + exp.set(); + //cout << exp.get(); - numinit.SetConstant("x","1"); - numinit.InitInitval(); - numinit.SetInit("y",exp.get()); - numinit.EndInitval(); - numinit.InitEndval(); - numinit.EndEndval(); - numinit.InitHistval(); - numinit.SetHist("y",3, exp.get()); - //cout << numinit.get(); + numinit.SetConstant("x","1"); + numinit.InitInitval(); + numinit.SetInit("y",exp.get()); + numinit.EndInitval(); + numinit.InitEndval(); + numinit.EndEndval(); + numinit.InitHistval(); + numinit.SetHist("y",3, exp.get()); + //cout << numinit.get(); - SigmaeInitialization siginit; + SigmaeInitialization siginit; - siginit.AddExpression("00"); - siginit.EndOfRow(); - siginit.AddExpression("10"); - siginit.AddExpression("11"); - siginit.EndOfRow(); - siginit.AddExpression("20"); - siginit.AddExpression("21"); - siginit.AddExpression("22"); - siginit.EndOfRow(); - siginit.AddExpression("30"); - siginit.AddExpression("31"); - siginit.AddExpression("32"); - siginit.AddExpression("33"); - siginit.EndOfRow(); - siginit.set(); + siginit.AddExpression("00"); + siginit.EndOfRow(); + siginit.AddExpression("10"); + siginit.AddExpression("11"); + siginit.EndOfRow(); + siginit.AddExpression("20"); + siginit.AddExpression("21"); + siginit.AddExpression("22"); + siginit.EndOfRow(); + siginit.AddExpression("30"); + siginit.AddExpression("31"); + siginit.AddExpression("32"); + siginit.AddExpression("33"); + siginit.EndOfRow(); + siginit.set(); - TmpSymbolTable tmp_symbol_table1, tmp_symbol_table2; + TmpSymbolTable tmp_symbol_table1, tmp_symbol_table2; - ComputingTasks computing_tasks; + ComputingTasks computing_tasks; - computing_tasks.set(); - computing_tasks.SetSteady(); - computing_tasks.SetCheck(); - computing_tasks.SetSimul(); + computing_tasks.set(); + computing_tasks.SetSteady(); + computing_tasks.SetCheck(); + computing_tasks.SetSimul(); - tmp_symbol_table1.AddTempSymbol("tmp1"); - tmp_symbol_table1.AddTempSymbol("tmp2"); - tmp_symbol_table1.AddTempSymbol("tmp3"); - tmp_symbol_table1.set("var_list_"); + tmp_symbol_table1.AddTempSymbol("tmp1"); + tmp_symbol_table1.AddTempSymbol("tmp2"); + tmp_symbol_table1.AddTempSymbol("tmp3"); + tmp_symbol_table1.set("var_list_"); - computing_tasks.SetStochSimul(tmp_symbol_table1.get()); + computing_tasks.SetStochSimul(tmp_symbol_table1.get()); - computing_tasks.SetOption("DROP", "500"); - computing_tasks.RunEstimation(); - computing_tasks.SetEstimationInit(); - computing_tasks.SetEstimation("d", "init_val", "lo_bound", "up_bound", "prior", "p1", "p2", "p3","p4"); - computing_tasks.SetEstimation("a", "c", "init_val", "lo_bound", "up_bound", "prior", "p1", "p2", "p3", "p4"); - computing_tasks.SetCalibInit(); - computing_tasks.SetCalibVariance(); - computing_tasks.SetCalibCovariance(); - computing_tasks.SetCalibAutoCorrelation(); - computing_tasks.SetCalib(); + computing_tasks.SetOption("DROP", "500"); + computing_tasks.RunEstimation(); + computing_tasks.SetEstimationInit(); + computing_tasks.SetEstimation("d", "init_val", "lo_bound", "up_bound", "prior", "p1", "p2", "p3","p4"); + computing_tasks.SetEstimation("a", "c", "init_val", "lo_bound", "up_bound", "prior", "p1", "p2", "p3", "p4"); + computing_tasks.SetCalibInit(); + computing_tasks.SetCalibVariance(); + computing_tasks.SetCalibCovariance(); + computing_tasks.SetCalibAutoCorrelation(); + computing_tasks.SetCalib(); - tmp_symbol_table1.AddTempSymbol("tmp11"); - tmp_symbol_table1.AddTempSymbol("tmp22"); - tmp_symbol_table1.AddTempSymbol("tmp33"); - tmp_symbol_table1.set("varl_list_"); - computing_tasks.SetOsr(tmp_symbol_table1.get()); + tmp_symbol_table1.AddTempSymbol("tmp11"); + tmp_symbol_table1.AddTempSymbol("tmp22"); + tmp_symbol_table1.AddTempSymbol("tmp33"); + tmp_symbol_table1.set("varl_list_"); + computing_tasks.SetOsr(tmp_symbol_table1.get()); - tmp_symbol_table1.AddTempSymbol("tmp11"); - tmp_symbol_table1.AddTempSymbol("tmp22"); - tmp_symbol_table1.AddTempSymbol("tmp33"); - tmp_symbol_table1.set("var_list_"); - tmp_symbol_table2.AddTempSymbol("tmp4"); - tmp_symbol_table2.AddTempSymbol("tmp5"); - tmp_symbol_table2.AddTempSymbol("tmp6"); - tmp_symbol_table2.set("olr_inst_"); - computing_tasks.SetOlr(tmp_symbol_table1.get(), tmp_symbol_table2.get()); + tmp_symbol_table1.AddTempSymbol("tmp11"); + tmp_symbol_table1.AddTempSymbol("tmp22"); + tmp_symbol_table1.AddTempSymbol("tmp33"); + tmp_symbol_table1.set("var_list_"); + tmp_symbol_table2.AddTempSymbol("tmp4"); + tmp_symbol_table2.AddTempSymbol("tmp5"); + tmp_symbol_table2.AddTempSymbol("tmp6"); + tmp_symbol_table2.set("olr_inst_"); + computing_tasks.SetOlr(tmp_symbol_table1.get(), tmp_symbol_table2.get()); - computing_tasks.SetOptimWeightsInit(); - computing_tasks.SetOptimWeights1(); - computing_tasks.SetOptimWeights2(); - outputfile.Save(); + computing_tasks.SetOptimWeightsInit(); + computing_tasks.SetOptimWeights1(); + computing_tasks.SetOptimWeights2(); + outputfile.Save(); } #endif //------------------------------------------------------------------------------ diff --git a/parser.src/SymbolTable.cc b/parser.src/SymbolTable.cc index 406cde019..eec7fc260 100644 --- a/parser.src/SymbolTable.cc +++ b/parser.src/SymbolTable.cc @@ -8,7 +8,7 @@ #include //------------------------------------------------------------------------------ #include "SymbolTable.h" - +#include "Interface.h" using namespace std; //------------------------------------------------------------------------------ map > SymbolTable::symboltable;// = *(new symbolmap); @@ -216,8 +216,8 @@ string SymbolTable::get() output << "M_.exo_names_tex = '" << getTexNameByID(eExogenous, 0) << "';\n"; for (int id = 1; id < ModelParameters::exo_nbr; id++) { - output << "M_.exo_names = str2mat(M_.exo_names,'" << getNameByID(eExogenous, id) << "');\n"; - output << "M_.exo_names_tex = str2mat(M_.exo_names_tex,'" << getTexNameByID(eExogenous, id) << "');\n"; + output << "M_.exo_names = " + interfaces::strvcat("M_.exo_names","'"+getNameByID(eExogenous, id)+"'") + ";\n"; + output << "M_.exo_names_tex = " + interfaces::strvcat("M_.exo_names_tex","'"+getTexNameByID(eExogenous, id)+"'") + ";\n"; } } if (ModelParameters::exo_det_nbr > 0) @@ -226,8 +226,8 @@ string SymbolTable::get() output << "lgxdet_tex_ = '" << getTexNameByID(eExogenousDet, 0) << "';\n"; for (int id = 1; id < ModelParameters::exo_det_nbr; id++) { - output << "lgxdet_ = str2mat(lgxdet_,'" << getNameByID(eExogenousDet, id) << "');\n"; - output << "lgxdet_tex_ = str2mat(lgxdet_tex_,'" << getTexNameByID(eExogenousDet, id) << "');\n"; + output << "lgxdet_ = " + interfaces::strvcat("lgxdet_","'"+getNameByID(eExogenousDet, id)+"'") + ";\n"; + output << "lgxdet_tex_ = " + interfaces::strvcat("lgxdet_tex_","'"+getTexNameByID(eExogenousDet, id)+"'") + ";\n"; } } if (ModelParameters::endo_nbr > 0) @@ -236,8 +236,8 @@ string SymbolTable::get() output << "M_.endo_names_tex = '" << getTexNameByID(eEndogenous, 0) << "';\n"; for (int id = 1; id < ModelParameters::endo_nbr; id++) { - output << "M_.endo_names = str2mat(M_.endo_names,'" << getNameByID(eEndogenous, id) << "');\n"; - output << "M_.endo_names_tex = str2mat(M_.endo_names_tex,'" << getTexNameByID(eEndogenous, id) << "');\n"; + output << "M_.endo_names = " + interfaces::strvcat("M_.endo_names","'"+getNameByID(eEndogenous, id)+"'") + ";\n"; + output << "M_.endo_names_tex = " + interfaces::strvcat("M_.endo_names_tex","'"+getTexNameByID(eEndogenous, id)+"'") + ";\n"; } } if (ModelParameters::recur_nbr > 0) @@ -246,8 +246,8 @@ string SymbolTable::get() output << "M_.recur_names_tex = '" << getTexNameByID(eRecursiveVariable, 0) << "';\n"; for (int id = 1; id < ModelParameters::recur_nbr; id++) { - output << "M_.recur_names = str2mat(M_.recur_names,'" << getNameByID(eRecursiveVariable, id) << "');\n"; - output << "M_.recur_names_tex = str2mat(M_.recur_names_tex,'" << getTexNameByID(eRecursiveVariable, id) << "');\n"; + output << "M_.recur_names = " + interfaces::strvcat("M_.recur_names","'"+getNameByID(eRecursiveVariable, id)+"'") + ";\n"; + output << "M_.recur_names_tex = " + interfaces::strvcat("M_.recur_names_tex","'"+getTexNameByID(eRecursiveVariable, id)+"'") + ";\n"; } } if (ModelParameters::parameter_nbr > 0) @@ -256,8 +256,8 @@ string SymbolTable::get() output << "M_.param_names_tex = '" << getTexNameByID(eParameter, 0) << "';\n"; for (int id = 1; id < ModelParameters::parameter_nbr; id++) { - output << "M_.param_names = str2mat(M_.param_names,'" << getNameByID(eParameter, id) << "');\n"; - output << "M_.param_names_tex = str2mat(M_.param_names_tex,'" << getTexNameByID(eParameter, id) << "');\n"; + output << "M_.param_names = " + interfaces::strvcat("M_.param_names","'"+getNameByID(eParameter, id)+"'") + ";\n"; + output << "M_.param_names_tex = " + interfaces::strvcat("M_.param_names_tex","'"+getTexNameByID(eParameter, id)+"'") + ";\n"; } } diff --git a/parser.src/TmpSymbolTable.cc b/parser.src/TmpSymbolTable.cc index c8c6fabc4..44b949dce 100644 --- a/parser.src/TmpSymbolTable.cc +++ b/parser.src/TmpSymbolTable.cc @@ -8,6 +8,7 @@ using namespace std; //------------------------------------------------------------------------------ #include "SymbolTable.h" #include "TmpSymbolTable.h" +#include "Interface.h" //------------------------------------------------------------------------------ TmpSymbolTable::TmpSymbolTable() { @@ -64,7 +65,8 @@ void TmpSymbolTable::set(string varname) { if (SymbolTable::isReferenced(*it) == eReferenced) { - output << varname << " = strvcat(" << varname << ", '" << *it << "');\n"; + output << varname << " = "; + output << interfaces::strvcat(varname,"'"+*it+"'")+";\n"; } } } diff --git a/parser.src/include/DataTree.h b/parser.src/include/DataTree.h index f6ab09463..c4ba9a513 100644 --- a/parser.src/include/DataTree.h +++ b/parser.src/include/DataTree.h @@ -117,6 +117,8 @@ class DataTree inline NodeID AddSqRt(NodeID iArg1); /*! Adds "arg1=arg2" to model tree */ inline NodeID AddEqual(NodeID iArg1, NodeID iArg2); + /*! Adds "arg1=arg2" as assignment to model tree */ + inline NodeID AddAssign(NodeID iArg1, NodeID iArg2); public : /*! Constructor */ DataTree(); @@ -752,5 +754,16 @@ inline NodeID DataTree::AddEqual(NodeID iArg1, NodeID iArg2=Zero) } return PushToken(iArg1,EQUAL,iArg2); } +inline NodeID DataTree::AddAssign(NodeID iArg1, NodeID iArg2=Zero) +{ + MToken lToken(iArg1, eTempResult, iArg2, ASSIGN); + + NodeID ID = getIDOfToken(lToken); + if (ID != NullID) + { + return ID; + } + return PushToken(iArg1,ASSIGN,iArg2); +} //------------------------------------------------------------------------------ #endif diff --git a/parser.src/include/Interface.h b/parser.src/include/Interface.h new file mode 100644 index 000000000..e15ca3402 --- /dev/null +++ b/parser.src/include/Interface.h @@ -0,0 +1,61 @@ +#include +enum eInterface{ + eMatlab = 0, + eScilab = 1 +}; + +class interfaces { + public: + static eInterface type; + interfaces(eInterface t){type = t;} + static inline void set_interface(eInterface t){type = t;} + static inline std::string comment(void){ + if (type == eMatlab) return "% "; + else if (type == eScilab) return "// "; + else return ""; + } + static inline std::string delete_file(std::string s) + { + if (type == eMatlab) + return "delete " + s; + else if (type == eScilab) + return "mdelete " + s; + else return ""; + } + static inline std::string file_exist(std::string s) + { + if (type == eMatlab) + return "exist(" + s + ")"; + else if (type == eScilab) + return "file_exist(" + s + ")"; + else return ""; + } + static inline std::string compile(std::string s) + { + if (type == eMatlab) + return "mex -O" + s + "\n"; + else return ""; + } + static inline std::string function_close(void){ + if (type == eScilab) return "endfunction\n"; + else return ""; + } + static inline std::string function_file_extension(void){ + if (type == eMatlab) return ".m"; + else if (type == eScilab) return ".sci"; + else return ""; + } + static inline std::string strvcat(std::string s1,std::string s2) + { + if (type == eMatlab) return "strvcat("+s1+","+s2+")"; + else if (type == eScilab) return "[" + s1 + ";" + s2 + "]"; + else return ""; + } + static inline std::string load_model_function_files(std::string filename) + { + if (type == eScilab) + return "getf('" + filename + "_static.sci');\ngetf('" + filename + "_dynamic.sci');\n"; + else return ""; + } +}; +