From d8607631229a6571b14205223df3d7a5ba18f762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 23 Feb 2010 19:08:54 +0100 Subject: [PATCH] Preprocessor: minor cosmetic changes related to external functions --- preprocessor/DataTree.hh | 3 ++- preprocessor/DynareBison.yy | 1 + preprocessor/ExternalFunctionsTable.cc | 10 +++---- preprocessor/ExternalFunctionsTable.hh | 37 ++++++++++---------------- preprocessor/ParsingDriver.cc | 5 ++-- preprocessor/ParsingDriver.hh | 6 ++--- 6 files changed, 27 insertions(+), 35 deletions(-) diff --git a/preprocessor/DataTree.hh b/preprocessor/DataTree.hh index 3c10cc92a..6b20e7e6c 100644 --- a/preprocessor/DataTree.hh +++ b/preprocessor/DataTree.hh @@ -181,8 +181,9 @@ public: //! Adds a model local variable with its value void AddLocalVariable(const string &name, NodeID value) throw (LocalVariableException); //! Adds an external function node - /*! \todo Use a map to share identical nodes */ NodeID AddExternalFunction(const string &function_name, const vector &arguments); + //! Adds an external function node + /*! \todo Use a map to share identical nodes */ NodeID AddExternalFunction(int symb_id, const vector &arguments); //! Adds an external function node for the first derivative of an external function NodeID AddFirstDerivExternalFunctionNode(int top_level_symb_id, const vector &arguments, int input_index); diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index ea8c2ba99..4eed5b498 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -579,6 +579,7 @@ comma_hand_side : hand_side { driver.add_external_function_arg($1); } | comma_hand_side COMMA hand_side { driver.add_external_function_arg($3); } + ; expectation_input : signed_integer | VAROBS { string *varobs = new string("varobs"); $$ = varobs; } diff --git a/preprocessor/ExternalFunctionsTable.cc b/preprocessor/ExternalFunctionsTable.cc index 9cc7c9ce3..28044a6d9 100644 --- a/preprocessor/ExternalFunctionsTable.cc +++ b/preprocessor/ExternalFunctionsTable.cc @@ -28,24 +28,24 @@ ExternalFunctionsTable::ExternalFunctionsTable() { -}; +} void -ExternalFunctionsTable::addExternalFunction(const int symb_id, const external_function_options external_function_options_arg) +ExternalFunctionsTable::addExternalFunction(int symb_id, const external_function_options &external_function_options_arg) { assert(symb_id >= 0); if (external_function_options_arg.secondDerivSymbID > eExtFunNotSet && external_function_options_arg.firstDerivSymbID == eExtFunNotSet) { - cerr << "If the second derivative is provided to the external_function() command," - << "the first derivative must also be provided." << endl; + cerr << "ERROR: If the second derivative is provided to the external_function() command," + << "the first derivative must also be provided" << endl; exit(EXIT_FAILURE); } if (external_function_options_arg.nargs <= 0) { - cerr << "The number of arguments passed to an external function must be > 0." << endl; + cerr << "ERROR: The number of arguments passed to an external function must be > 0" << endl; exit(EXIT_FAILURE); } diff --git a/preprocessor/ExternalFunctionsTable.hh b/preprocessor/ExternalFunctionsTable.hh index 867c68e58..e1d19cf91 100644 --- a/preprocessor/ExternalFunctionsTable.hh +++ b/preprocessor/ExternalFunctionsTable.hh @@ -65,64 +65,55 @@ private: public: ExternalFunctionsTable(); //! Adds an external function to the table as well as its derivative functions - void addExternalFunction(const int symb_id, const external_function_options external_function_options_arg); + void addExternalFunction(int symb_id, const external_function_options &external_function_options_arg); //! See if the function exists in the External Functions Table - inline bool exists(const int symb_id) const; + inline bool exists(int symb_id) const; //! Get the number of arguments for a given external function - inline int getNargs(const int symb_id) const throw (UnknownExternalFunctionSymbolIDException); + inline int getNargs( int symb_id) const throw (UnknownExternalFunctionSymbolIDException); //! Get the symbol_id of the first derivative function - inline int getFirstDerivSymbID(const int symb_id) const throw (UnknownExternalFunctionSymbolIDException); + inline int getFirstDerivSymbID(int symb_id) const throw (UnknownExternalFunctionSymbolIDException); //! Get the symbol_id of the second derivative function - inline int getSecondDerivSymbID(const int symb_id) const throw (UnknownExternalFunctionSymbolIDException); + inline int getSecondDerivSymbID(int symb_id) const throw (UnknownExternalFunctionSymbolIDException); //! Returns the total number of unique external functions declared or used in the .mod file - inline int get_total_number_of_unique_external_functions(); + inline int get_total_number_of_unique_external_functions() const; }; inline bool -ExternalFunctionsTable::exists(const int symb_id) const +ExternalFunctionsTable::exists(int symb_id) const { external_function_table_type::const_iterator iter = externalFunctionTable.find(symb_id); return (iter != externalFunctionTable.end()); } inline int -ExternalFunctionsTable::getNargs(const int symb_id) const throw (UnknownExternalFunctionSymbolIDException) +ExternalFunctionsTable::getNargs(int symb_id) const throw (UnknownExternalFunctionSymbolIDException) { if (exists(symb_id)) return externalFunctionTable.find(symb_id)->second.nargs; else - { - cout << "In get_nargs(): id: " << symb_id << endl; - throw UnknownExternalFunctionSymbolIDException(symb_id); - } + throw UnknownExternalFunctionSymbolIDException(symb_id); } inline int -ExternalFunctionsTable::getFirstDerivSymbID(const int symb_id) const throw (UnknownExternalFunctionSymbolIDException) +ExternalFunctionsTable::getFirstDerivSymbID(int symb_id) const throw (UnknownExternalFunctionSymbolIDException) { if (exists(symb_id)) return externalFunctionTable.find(symb_id)->second.firstDerivSymbID; else - { - cout << "In getFirstDerivSymbID(): id: " << symb_id << endl; - throw UnknownExternalFunctionSymbolIDException(symb_id); - } + throw UnknownExternalFunctionSymbolIDException(symb_id); } inline int -ExternalFunctionsTable::getSecondDerivSymbID(const int symb_id) const throw (UnknownExternalFunctionSymbolIDException) +ExternalFunctionsTable::getSecondDerivSymbID(int symb_id) const throw (UnknownExternalFunctionSymbolIDException) { if (exists(symb_id)) return externalFunctionTable.find(symb_id)->second.secondDerivSymbID; else - { - cout << "In getSecondDerivSymbID(): id: " << symb_id << endl; - throw UnknownExternalFunctionSymbolIDException(symb_id); - } + throw UnknownExternalFunctionSymbolIDException(symb_id); } inline int -ExternalFunctionsTable::get_total_number_of_unique_external_functions() +ExternalFunctionsTable::get_total_number_of_unique_external_functions() const { return externalFunctionTable.size(); } diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 088e09200..a07298bc8 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -110,7 +110,7 @@ ParsingDriver::warning(const string &m) } void -ParsingDriver::declare_symbol(const string *name, const SymbolType type, const string *tex_name) +ParsingDriver::declare_symbol(const string *name, SymbolType type, const string *tex_name) { try { @@ -1743,8 +1743,7 @@ ParsingDriver::add_model_var_or_external_function(string *function_name) { // e.g. this function has already been referenced (either ad hoc or through the external_function() statement // => check that the information matches previously declared info int symb_id = mod_file->symbol_table.getID(*function_name); - if (!mod_file->external_functions_table.exists(symb_id)) - error("In ParsingDriver::add_external_function()(1) Please report to Dynare Team."); + assert(mod_file->external_functions_table.exists(symb_id)); if ((int)(stack_external_function_args.top().size()) != mod_file->external_functions_table.getNargs(symb_id)) error("The number of arguments passed to " + *function_name + diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 7177fee0c..20e92508c 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -79,7 +79,7 @@ private: void check_symbol_existence(const string &name); //! Helper to add a symbol declaration - void declare_symbol(const string *name, const SymbolType type, const string *tex_name); + void declare_symbol(const string *name, SymbolType type, const string *tex_name); //! Creates option "optim_opt" in OptionsList if it doesn't exist, else add a comma, and adds the option name void optim_options_helper(const string &name); @@ -162,6 +162,8 @@ private: ExternalFunctionsTable::external_function_options current_external_function_options; //! reset the values for temporary storage void reset_current_external_function_options(); + //! Adds a model lagged variable to ModelTree and VariableTable + NodeID add_model_variable(int symb_id, int lag); //! The mod file representation constructed by this ParsingDriver ModFile *mod_file; @@ -226,8 +228,6 @@ public: NodeID add_inf_constant(); //! Adds a model variable to ModelTree and VariableTable NodeID add_model_variable(string *name); - //! Adds a model lagged variable to ModelTree and VariableTable - NodeID add_model_variable(int symb_id, int lag); //! Adds an Expression's variable NodeID add_expression_variable(string *name); //! Adds a "periods" statement