v4 parser:

* fixed operators precedence (in particular for relational operators)
* various minor cleanups


git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1483 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
sebastien 2007-12-07 16:02:55 +00:00
parent ddc81ce534
commit 10bdf16a2a
7 changed files with 52 additions and 137 deletions

View File

@ -303,18 +303,6 @@ MarkowitzStatement::writeOutput(ostream &output, const string &basename) const
output << "options_.markowitz = " << markowitz << ";" << endl;
}
Simulation_MethodStatement::Simulation_MethodStatement(double simulation_method_arg) : simulation_method(simulation_method_arg)
{
}
void
Simulation_MethodStatement::writeOutput(ostream &output, const string &basename) const
{
output << "options_.simulation_method = " << simulation_method << ";" << endl;
}
DsampleStatement::DsampleStatement(int val1_arg) : val1(val1_arg), val2(-1)
{
}

View File

@ -129,7 +129,6 @@ NumConstNode::eval(const eval_context_type &eval_context) const throw (EvalExcep
return(datatree.num_constants.getDouble(id));
}
/*New*/
void
NumConstNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const
{
@ -151,7 +150,6 @@ NumConstNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType ou
CompileCode.write(reinterpret_cast<char *>(&vard),sizeof(vard));
/*}*/
}
/*EndNew*/
void
@ -248,12 +246,6 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
output << "T" << idx << "(it_)";
else
output << "T" << idx;
/*if (output_type != oCDynamicModelSparseDLL)
output << "T" << idx;
else if (output_type == oMatlabDynamicModelSparse)
output << "T" << idx << "(it_)";
else
output << "T" << idx << "[it_]";*/
return;
}
@ -411,7 +403,6 @@ VariableNode::eval(const eval_context_type &eval_context) const throw (EvalExcep
return it->second;
}
/*New*/
void
VariableNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const
{
@ -471,7 +462,6 @@ VariableNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType ou
exit(-1);
}
}
/*EndNew*/
void
VariableNode::collectEndogenous(NodeID &Id)
@ -709,12 +699,6 @@ UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
output << "T" << idx << "(it_)";
else
output << "T" << idx;
/*if (output_type != oCDynamicModelSparseDLL)
output << "T" << idx;
else if (output_type == oMatlabDynamicModelSparse)
output << "T" << idx << "(it_)";
else
output << "T" << idx << "[it_]";*/
return;
}
@ -722,9 +706,6 @@ UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
if (op_code == oUminus)
output << "(";
switch(op_code)
{
case oUminus:
@ -857,7 +838,6 @@ UnaryOpNode::eval(const eval_context_type &eval_context) const throw (EvalExcept
return eval_opcode(op_code, v);
}
/*New*/
void
UnaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const
{
@ -874,7 +854,6 @@ UnaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType out
UnaryOpcode op_codel=op_code;
CompileCode.write(reinterpret_cast<char *>(&op_codel), sizeof(op_codel));
}
/*EndNEw*/
void
UnaryOpNode::collectEndogenous(NodeID &Id)
@ -982,28 +961,31 @@ BinaryOpNode::precedence(ExprNodeOutputType output_type, const temporary_terms_t
switch(op_code)
{
case oEqual:
return 0;
case oEqualEqual:
case oDifferent:
return 1;
case oLessEqual:
case oGreaterEqual:
case oLess:
case oGreater:
case oMin:
case oMax:
return 0;
case oEqual:
return 2;
case oPlus:
case oMinus:
return 1;
return 3;
case oTimes:
case oDivide:
return 2;
return 4;
case oPower:
if (!OFFSET(output_type))
// In C, power operator is of the form pow(a, b)
return 100;
else
return 4;
return 5;
case oMin:
case oMax:
return 100;
}
cerr << "Impossible case!" << endl;
exit(-1);
@ -1143,27 +1125,27 @@ BinaryOpNode::eval_opcode(double v1, BinaryOpcode op_code, double v2) throw (Eva
case oPower:
return(pow(v1, v2));
case oMax:
if(v1<v2)
return( v2);
if (v1 < v2)
return v2;
else
return( v1);
return v1;
case oMin:
if(v1>v2)
return( v2);
if (v1 > v2)
return v2;
else
return( v1);
return v1;
case oLess:
return( v1< v2 ? 1.0 : 0.0);
return (v1 < v2);
case oGreater:
return( v1> v2 ? 1.0 : 0.0);
return (v1 > v2);
case oLessEqual:
return( v1<= v2 ? 1.0 : 0.0);
return (v1 <= v2);
case oGreaterEqual:
return( v1>= v2 ? 1.0 : 0.0);
return (v1 >= v2);
case oEqualEqual:
return( v1== v2 ? 1.0 : 0.0);
return (v1 == v2);
case oDifferent:
return( v1!= v2 ? 1.0 : 0.0);
return (v1 != v2);
case oEqual:
throw EvalException();
}
@ -1180,7 +1162,6 @@ BinaryOpNode::eval(const eval_context_type &eval_context) const throw (EvalExcep
return eval_opcode(v1, op_code, v2);
}
/*New*/
void
BinaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const
{
@ -1199,7 +1180,6 @@ BinaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType ou
BinaryOpcode op_codel=op_code;
CompileCode.write(reinterpret_cast<char *>(&op_codel),sizeof(op_codel));
}
/*EndNew*/
void
BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
@ -1215,30 +1195,25 @@ BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
output << "T" << idx << "(it_)";
else
output << "T" << idx;
/*if (output_type != oCDynamicModelSparseDLL)
output << "T" << idx;
else if (output_type == oMatlabDynamicModelSparse)
output << "T" << idx << "(it_)";
else
output << "T" << idx << "[it_]";*/
return;
}
// Treat special case of power operator in C
// Treat special case of power operator in C, and case of max and min operators
if ((op_code == oPower && !OFFSET(output_type)) || op_code == oMax || op_code == oMin )
{
switch (op_code)
{
case oPower:
output << "pow(";
break;
case oMax:
output << "max(";
break;
case oMin:
output << "min(";
break;
default:;
case oPower:
output << "pow(";
break;
case oMax:
output << "max(";
break;
case oMin:
output << "min(";
break;
default:
;
}
arg1->writeOutput(output, output_type, temporary_terms);
output << ",";
@ -1300,7 +1275,7 @@ BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
output << "==";
break;
case oDifferent:
if(OFFSET(output_type))
if (OFFSET(output_type))
output << "~=";
else
output << "!=";
@ -1308,7 +1283,8 @@ BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
case oEqual:
output << "=";
break;
default:;
default:
;
}
close_parenthesis = false;
@ -1354,7 +1330,8 @@ TrinaryOpNode::TrinaryOpNode(DataTree &datatree_arg, const NodeID arg1_arg,
datatree.trinary_op_node_map[make_pair(make_pair(make_pair(arg1, arg2), arg3), op_code)] = this;
// Non-null derivatives are the union of those of the arguments
// Compute set union of arg1->non_null_derivatives and arg2->non_null_derivatives
// Compute set union of arg{1,2,3}->non_null_derivatives
set<int> non_null_derivatives_tmp;
set_union(arg1->non_null_derivatives.begin(),
arg1->non_null_derivatives.end(),
arg2->non_null_derivatives.begin(),
@ -1433,7 +1410,7 @@ TrinaryOpNode::precedence(ExprNodeOutputType output_type, const temporary_terms_
switch(op_code)
{
case oNormcdf:
return 100;
return 100;
}
cerr << "Impossible case!" << endl;
exit(-1);
@ -1546,9 +1523,9 @@ TrinaryOpNode::eval(const eval_context_type &eval_context) const throw (EvalExce
return eval_opcode(v1, op_code, v2, v3);
}
/*New*/
void
TrinaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const
TrinaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type,
const temporary_terms_type &temporary_terms, map_idx_type map_idx) const
{
// If current node is a temporary term
temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<TrinaryOpNode *>(this));
@ -1566,13 +1543,11 @@ TrinaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType o
TrinaryOpcode op_codel=op_code;
CompileCode.write(reinterpret_cast<char *>(&op_codel),sizeof(op_codel));
}
/*EndNew*/
void
TrinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_type &temporary_terms) const
{
if (!OFFSET(output_type))
{
cerr << "TrinaryOpNode not implemented for C output" << endl;

View File

@ -200,15 +200,6 @@ ParsingDriver::markowitz(string *markowitz)
delete markowitz;
}
void
ParsingDriver::simulation_method(string *simulation_method)
{
double simulation_method_val = atof(simulation_method->c_str());
mod_file->addStatement(new Simulation_MethodStatement(simulation_method_val ));
delete simulation_method;
}
void
ParsingDriver::dsample(string *arg1)
{
@ -533,12 +524,9 @@ ParsingDriver::add_value(NodeID value)
void
ParsingDriver::add_value(string *p1)
{
//int p1_val = atoi(p1->c_str());
det_shocks_values.push_back(add_constant(p1));
//delete p1;
}
void
ParsingDriver::do_sigma_e()
{
@ -572,22 +560,14 @@ ParsingDriver::add_to_row(NodeID v)
sigmae_row.push_back(v);
}
void
ParsingDriver::steady()
{
if (/*mod_file->model_tree.mode == eSparseDLLMode || */mod_file->model_tree.mode == eSparseMode)
{
mod_file->addStatement(new SteadySparseStatement(options_list));
options_list.clear();
}
if (mod_file->model_tree.mode == eSparseMode)
mod_file->addStatement(new SteadySparseStatement(options_list));
else
{
mod_file->addStatement(new SteadyStatement(options_list));
options_list.clear();
}
/*mod_file->addStatement(new SteadyStatement(options_list));
options_list.clear();*/
mod_file->addStatement(new SteadyStatement(options_list));
options_list.clear();
}
void
@ -1145,9 +1125,6 @@ ParsingDriver::add_different(NodeID arg1, NodeID arg2)
return data_tree->AddDifferent(arg1, arg2);
}
NodeID
ParsingDriver::add_power(NodeID arg1, NodeID arg2)
{

View File

@ -119,15 +119,6 @@ public:
virtual void writeOutput(ostream &output, const string &basename) const;
};
class Simulation_MethodStatement : public Statement
{
private:
const int simulation_method;
public:
Simulation_MethodStatement(double simulation_method_arg) ;
void writeOutput(ostream &output, const string &basename) const;
};
class MarkowitzStatement : public Statement
{
private:

View File

@ -14,6 +14,8 @@ using namespace std;
#include "VariableTable.hh"
#include "ExprNode.hh"
#define CONSTANTS_PRECISION 16
class DataTree
{
friend class ExprNode;
@ -151,7 +153,7 @@ DataTree::AddPossiblyNegativeConstant(double v)
neg = true;
}
ostringstream ost;
ost << setprecision(16) << v;
ost << setprecision(CONSTANTS_PRECISION) << v;
NodeID cnode = AddNumConstant(ost.str());

View File

@ -140,10 +140,7 @@ public:
};
virtual double eval(const eval_context_type &eval_context) const throw (EvalException) = 0;
/*New*/
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const = 0;
/*EndNew*/
};
//! Object used to compare two nodes (using their indexes)
@ -168,9 +165,7 @@ public:
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const;
virtual void collectEndogenous(NodeID &Id);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
/*New*/
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
/*EndNew*/
};
//! Symbol or variable node
@ -189,9 +184,7 @@ public:
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms = temporary_terms_type()) const;
virtual void collectEndogenous(NodeID &Id);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
/*New*/
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
/*EndNew*/
};
enum UnaryOpcode
@ -238,9 +231,7 @@ public:
virtual void collectEndogenous(NodeID &Id);
static double eval_opcode(UnaryOpcode op_code, double v) throw (EvalException);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
/*New*/
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
/*EndNew*/
};
enum BinaryOpcode
@ -285,9 +276,7 @@ public:
virtual void collectEndogenous(NodeID &Id);
static double eval_opcode(double v1, BinaryOpcode op_code, double v2) throw (EvalException);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
/*New*/
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
/*EndNew*/
};
enum TrinaryOpcode
@ -304,7 +293,6 @@ private:
const TrinaryOpcode op_code;
virtual NodeID computeDerivative(int varID);
virtual int cost(const temporary_terms_type &temporary_terms, bool is_matlab) const;
set<int> non_null_derivatives_tmp;
public:
TrinaryOpNode(DataTree &datatree_arg, const NodeID arg1_arg,
TrinaryOpcode op_code_arg, const NodeID arg2_arg, const NodeID arg3_arg);
@ -320,9 +308,7 @@ public:
virtual void collectEndogenous(NodeID &Id);
static double eval_opcode(double v1, TrinaryOpcode op_code, double v2, double v3) throw (EvalException);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
/*New*/
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
/*EndNew*/
};
//! Unknown function node
@ -346,9 +332,7 @@ public:
map_idx_type &map_idx) const;
virtual void collectEndogenous(NodeID &Id);
virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
/*New*/
virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
/*EndNew*/
};
typedef struct IM_compact

View File

@ -184,8 +184,6 @@ public:
//! Adds a "cutoff" statement
void cutoff(string *cutoff);
//! Adds a weight of the "markowitz" criteria statement
void simulation_method(string *simulation_method);
//! Adds a deterministic simulation method statement
void markowitz(string *markowitz);
//! Adds a "dsample" statement
void dsample(string *arg1);
@ -378,11 +376,11 @@ public:
NodeID add_atanh(NodeID arg1);
//! Writes token "sqrt(arg1)" to model tree
NodeID add_sqrt(NodeID arg1);
//! Writes token "max(arg1,arg2)" to model tree
//! Writes token "max(arg1,arg2)" to model tree
NodeID add_max(NodeID arg1, NodeID arg2);
//! Writes token "min(arg1,arg2)" to model tree
//! Writes token "min(arg1,arg2)" to model tree
NodeID add_min(NodeID arg1, NodeID arg2);
//! Writes token "normcdf(arg1,arg2,arg3)" to model tree
//! Writes token "normcdf(arg1,arg2,arg3)" to model tree
NodeID add_normcdf(NodeID arg1, NodeID arg2, NodeID arg3);
//! Adds an unknwon function argument
void add_unknown_function_arg(NodeID arg);