Preprocessor: minor refactoring of ParsingDriver::add_model_var_or_external_function

issue#70
Sébastien Villemot 2011-01-13 19:10:16 +01:00
parent e74c283579
commit baccf9e8f5
1 changed files with 15 additions and 28 deletions

View File

@ -22,6 +22,7 @@
#include <iostream> #include <iostream>
#include <cassert> #include <cassert>
#include <sstream> #include <sstream>
#include <cmath>
#include "ParsingDriver.hh" #include "ParsingDriver.hh"
#include "Statement.hh" #include "Statement.hh"
@ -1765,68 +1766,54 @@ ParsingDriver::add_model_var_or_external_function(string *function_name, bool in
{ {
if (!in_model_block) if (!in_model_block)
{ {
if ((int)stack_external_function_args.top().size() > 0) if (stack_external_function_args.top().size() > 0)
error("A variable cannot take arguments."); error(string("Symbol ") + *function_name + string(" cannot take arguments."));
else else
return add_expression_variable(function_name); return add_expression_variable(function_name);
} }
else else
{ // e.g. model_var(lag) => ADD MODEL VARIABLE WITH LEAD (NumConstNode)/LAG (UnaryOpNode) { // e.g. model_var(lag) => ADD MODEL VARIABLE WITH LEAD (NumConstNode)/LAG (UnaryOpNode)
if ((int)stack_external_function_args.top().size() != 1) if (stack_external_function_args.top().size() != 1)
error("A model variable is being treated as if it were a function (i.e., has received more than one argument)."); error(string("Symbol ") + *function_name + string(" is being treated as if it were a function (i.e., has received more than one argument)."));
NumConstNode *numNode = dynamic_cast<NumConstNode *>(stack_external_function_args.top().front()); NumConstNode *numNode = dynamic_cast<NumConstNode *>(stack_external_function_args.top().front());
UnaryOpNode *unaryNode = dynamic_cast<UnaryOpNode *>(stack_external_function_args.top().front()); UnaryOpNode *unaryNode = dynamic_cast<UnaryOpNode *>(stack_external_function_args.top().front());
if (numNode == NULL && unaryNode == NULL) if (numNode == NULL && unaryNode == NULL)
error("A model variable is being treated as if it were a function (i.e., takes an argument that is not an integer)."); error(string("Symbol ") + *function_name + string(" is being treated as if it were a function (i.e., takes an argument that is not an integer)."));
eval_context_t ectmp; eval_context_t ectmp;
int model_var_arg; double model_var_arg;
double model_var_arg_dbl;
if (unaryNode == NULL) if (unaryNode == NULL)
{ {
try try
{ {
model_var_arg = (int)numNode->eval(ectmp); model_var_arg = numNode->eval(ectmp);
} }
catch (ExprNode::EvalException &e) catch (ExprNode::EvalException &e)
{ {
error(string("Symbol ") + *function_name + string(" is being treated as if it were a function (i.e., takes an argument that is not an integer)."));
} }
try
{
model_var_arg_dbl = numNode->eval(ectmp);
}
catch (ExprNode::EvalException &e)
{
}
} }
else else
if (unaryNode->get_op_code() != oUminus) if (unaryNode->get_op_code() != oUminus)
error("A model variable is being treated as if it were a function (i.e., takes an argument that is not an integer)."); error(string("Symbol ") + *function_name + string(" is being treated as if it were a function (i.e., takes an argument that is not an integer)."));
else else
{ {
try try
{ {
model_var_arg = (int)unaryNode->eval(ectmp); model_var_arg = unaryNode->eval(ectmp);
}
catch (ExprNode::EvalException &e)
{
}
try
{
model_var_arg_dbl = unaryNode->eval(ectmp);
} }
catch (ExprNode::EvalException &e) catch (ExprNode::EvalException &e)
{ {
error(string("Symbol ") + *function_name + string(" is being treated as if it were a function (i.e., takes an argument that is not an integer)."));
} }
} }
if ((double) model_var_arg != model_var_arg_dbl) //make 100% sure int cast didn't lose info if (model_var_arg != floor(model_var_arg))
error("A model variable is being treated as if it were a function (i.e., takes an argument that is not an integer)."); error(string("Symbol ") + *function_name + string(" is being treated as if it were a function (i.e., takes an argument that is not an integer)."));
nid = add_model_variable(mod_file->symbol_table.getID(*function_name), model_var_arg); nid = add_model_variable(mod_file->symbol_table.getID(*function_name), (int) model_var_arg);
stack_external_function_args.pop(); stack_external_function_args.pop();
delete function_name; delete function_name;
return nid; return nid;