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 <cassert>
#include <sstream>
#include <cmath>
#include "ParsingDriver.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 ((int)stack_external_function_args.top().size() > 0)
error("A variable cannot take arguments.");
if (stack_external_function_args.top().size() > 0)
error(string("Symbol ") + *function_name + string(" cannot take arguments."));
else
return add_expression_variable(function_name);
}
else
{ // e.g. model_var(lag) => ADD MODEL VARIABLE WITH LEAD (NumConstNode)/LAG (UnaryOpNode)
if ((int)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).");
if (stack_external_function_args.top().size() != 1)
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());
UnaryOpNode *unaryNode = dynamic_cast<UnaryOpNode *>(stack_external_function_args.top().front());
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;
int model_var_arg;
double model_var_arg_dbl;
double model_var_arg;
if (unaryNode == NULL)
{
try
{
model_var_arg = (int)numNode->eval(ectmp);
model_var_arg = numNode->eval(ectmp);
}
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
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
{
try
{
model_var_arg = (int)unaryNode->eval(ectmp);
}
catch (ExprNode::EvalException &e)
{
}
try
{
model_var_arg_dbl = unaryNode->eval(ectmp);
model_var_arg = unaryNode->eval(ectmp);
}
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
error("A model variable is being treated as if it were a function (i.e., takes an argument that is not an integer).");
if (model_var_arg != floor(model_var_arg))
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();
delete function_name;
return nid;