Preprocessor: minor cosmetic changes related to external functions

time-shift
Sébastien Villemot 2010-02-23 19:08:54 +01:00
parent 0b1888bc84
commit d860763122
6 changed files with 27 additions and 35 deletions

View File

@ -181,8 +181,9 @@ public:
//! Adds a model local variable with its value //! Adds a model local variable with its value
void AddLocalVariable(const string &name, NodeID value) throw (LocalVariableException); void AddLocalVariable(const string &name, NodeID value) throw (LocalVariableException);
//! Adds an external function node //! Adds an external function node
/*! \todo Use a map to share identical nodes */
NodeID AddExternalFunction(const string &function_name, const vector<NodeID> &arguments); NodeID AddExternalFunction(const string &function_name, const vector<NodeID> &arguments);
//! Adds an external function node
/*! \todo Use a map to share identical nodes */
NodeID AddExternalFunction(int symb_id, const vector<NodeID> &arguments); NodeID AddExternalFunction(int symb_id, const vector<NodeID> &arguments);
//! Adds an external function node for the first derivative of an external function //! Adds an external function node for the first derivative of an external function
NodeID AddFirstDerivExternalFunctionNode(int top_level_symb_id, const vector<NodeID> &arguments, int input_index); NodeID AddFirstDerivExternalFunctionNode(int top_level_symb_id, const vector<NodeID> &arguments, int input_index);

View File

@ -579,6 +579,7 @@ comma_hand_side : hand_side
{ driver.add_external_function_arg($1); } { driver.add_external_function_arg($1); }
| comma_hand_side COMMA hand_side | comma_hand_side COMMA hand_side
{ driver.add_external_function_arg($3); } { driver.add_external_function_arg($3); }
;
expectation_input : signed_integer expectation_input : signed_integer
| VAROBS { string *varobs = new string("varobs"); $$ = varobs; } | VAROBS { string *varobs = new string("varobs"); $$ = varobs; }

View File

@ -28,24 +28,24 @@
ExternalFunctionsTable::ExternalFunctionsTable() ExternalFunctionsTable::ExternalFunctionsTable()
{ {
}; }
void 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); assert(symb_id >= 0);
if (external_function_options_arg.secondDerivSymbID > eExtFunNotSet && if (external_function_options_arg.secondDerivSymbID > eExtFunNotSet &&
external_function_options_arg.firstDerivSymbID == eExtFunNotSet) external_function_options_arg.firstDerivSymbID == eExtFunNotSet)
{ {
cerr << "If the second derivative is provided to the external_function() command," cerr << "ERROR: If the second derivative is provided to the external_function() command,"
<< "the first derivative must also be provided." << endl; << "the first derivative must also be provided" << endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (external_function_options_arg.nargs <= 0) 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); exit(EXIT_FAILURE);
} }

View File

@ -65,64 +65,55 @@ private:
public: public:
ExternalFunctionsTable(); ExternalFunctionsTable();
//! Adds an external function to the table as well as its derivative functions //! 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 //! 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 //! 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 //! 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 //! 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 //! 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 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); external_function_table_type::const_iterator iter = externalFunctionTable.find(symb_id);
return (iter != externalFunctionTable.end()); return (iter != externalFunctionTable.end());
} }
inline int inline int
ExternalFunctionsTable::getNargs(const int symb_id) const throw (UnknownExternalFunctionSymbolIDException) ExternalFunctionsTable::getNargs(int symb_id) const throw (UnknownExternalFunctionSymbolIDException)
{ {
if (exists(symb_id)) if (exists(symb_id))
return externalFunctionTable.find(symb_id)->second.nargs; return externalFunctionTable.find(symb_id)->second.nargs;
else else
{ throw UnknownExternalFunctionSymbolIDException(symb_id);
cout << "In get_nargs(): id: " << symb_id << endl;
throw UnknownExternalFunctionSymbolIDException(symb_id);
}
} }
inline int inline int
ExternalFunctionsTable::getFirstDerivSymbID(const int symb_id) const throw (UnknownExternalFunctionSymbolIDException) ExternalFunctionsTable::getFirstDerivSymbID(int symb_id) const throw (UnknownExternalFunctionSymbolIDException)
{ {
if (exists(symb_id)) if (exists(symb_id))
return externalFunctionTable.find(symb_id)->second.firstDerivSymbID; return externalFunctionTable.find(symb_id)->second.firstDerivSymbID;
else else
{ throw UnknownExternalFunctionSymbolIDException(symb_id);
cout << "In getFirstDerivSymbID(): id: " << symb_id << endl;
throw UnknownExternalFunctionSymbolIDException(symb_id);
}
} }
inline int inline int
ExternalFunctionsTable::getSecondDerivSymbID(const int symb_id) const throw (UnknownExternalFunctionSymbolIDException) ExternalFunctionsTable::getSecondDerivSymbID(int symb_id) const throw (UnknownExternalFunctionSymbolIDException)
{ {
if (exists(symb_id)) if (exists(symb_id))
return externalFunctionTable.find(symb_id)->second.secondDerivSymbID; return externalFunctionTable.find(symb_id)->second.secondDerivSymbID;
else else
{ throw UnknownExternalFunctionSymbolIDException(symb_id);
cout << "In getSecondDerivSymbID(): id: " << symb_id << endl;
throw UnknownExternalFunctionSymbolIDException(symb_id);
}
} }
inline int inline int
ExternalFunctionsTable::get_total_number_of_unique_external_functions() ExternalFunctionsTable::get_total_number_of_unique_external_functions() const
{ {
return externalFunctionTable.size(); return externalFunctionTable.size();
} }

View File

@ -110,7 +110,7 @@ ParsingDriver::warning(const string &m)
} }
void 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 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 { // 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 // => check that the information matches previously declared info
int symb_id = mod_file->symbol_table.getID(*function_name); int symb_id = mod_file->symbol_table.getID(*function_name);
if (!mod_file->external_functions_table.exists(symb_id)) assert(mod_file->external_functions_table.exists(symb_id));
error("In ParsingDriver::add_external_function()(1) Please report to Dynare Team.");
if ((int)(stack_external_function_args.top().size()) != mod_file->external_functions_table.getNargs(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 + error("The number of arguments passed to " + *function_name +

View File

@ -79,7 +79,7 @@ private:
void check_symbol_existence(const string &name); void check_symbol_existence(const string &name);
//! Helper to add a symbol declaration //! 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 //! 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); void optim_options_helper(const string &name);
@ -162,6 +162,8 @@ private:
ExternalFunctionsTable::external_function_options current_external_function_options; ExternalFunctionsTable::external_function_options current_external_function_options;
//! reset the values for temporary storage //! reset the values for temporary storage
void reset_current_external_function_options(); 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 //! The mod file representation constructed by this ParsingDriver
ModFile *mod_file; ModFile *mod_file;
@ -226,8 +228,6 @@ public:
NodeID add_inf_constant(); NodeID add_inf_constant();
//! Adds a model variable to ModelTree and VariableTable //! Adds a model variable to ModelTree and VariableTable
NodeID add_model_variable(string *name); 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 //! Adds an Expression's variable
NodeID add_expression_variable(string *name); NodeID add_expression_variable(string *name);
//! Adds a "periods" statement //! Adds a "periods" statement