From b0a23afeada13ae0b89d221b73dfd43da0da1771 Mon Sep 17 00:00:00 2001 From: nemu Date: Fri, 8 Feb 2008 07:32:55 +0000 Subject: [PATCH] improved (hopefully) string back parsing and furthermore added the constant Pi for functions. --- src/classes/PFunction.cpp | 34 ++++++++++++++++++---------------- src/include/PFunctionGrammar.h | 22 ++++++++++++++-------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/classes/PFunction.cpp b/src/classes/PFunction.cpp index 1adcaaa8..3d4a5ada 100644 --- a/src/classes/PFunction.cpp +++ b/src/classes/PFunction.cpp @@ -161,6 +161,9 @@ void PFunction::FillFuncEvalTree(iter_t const& i, PFuncTreeNode &node) node.fID = PFunctionGrammar::realID; // keep the ID node.fDvalue = dvalue; // keep the value // cout << endl << ">> realID: value = " << dvalue; + } else if (i->value.id() == PFunctionGrammar::constPiID) { // handle constant pi + node.fID = PFunctionGrammar::constPiID; // keep the ID + node.fDvalue = 3.14159265358979323846; // keep the value } else if (i->value.id() == PFunctionGrammar::parameterID) { // handle parameter number str = string(i->value.begin(), i->value.end()); // get string status = sscanf(str.c_str(), "PAR%d", &ivalue); // convert string to parameter number @@ -301,6 +304,8 @@ bool PFunction::FindAndCheckMapAndParamRange(PFuncTreeNode &node, unsigned int m { if (node.fID == PFunctionGrammar::realID) { return true; + } else if (node.fID == PFunctionGrammar::constPiID) { + return true; } else if (node.fID == PFunctionGrammar::parameterID) { if (node.fIvalue <= (int) paramSize) return true; @@ -358,6 +363,8 @@ double PFunction::EvalNode(PFuncTreeNode &node) { if (node.fID == PFunctionGrammar::realID) { return node.fDvalue; + } else if (node.fID == PFunctionGrammar::constPiID) { + return node.fDvalue; } else if (node.fID == PFunctionGrammar::parameterID) { return fParam[node.fIvalue-1]; } else if (node.fID == PFunctionGrammar::mapID) { @@ -487,6 +494,8 @@ void PFunction::EvalTreeForStringExpression(iter_t const& i) fFuncString += string(i->value.begin(), i->value.end()).c_str(); if (*i->value.begin() == '-') fFuncString += ")"; + } else if (i->value.id() == PFunctionGrammar::constPiID) { + fFuncString += "Pi"; } else if (i->value.id() == PFunctionGrammar::funLabelID) { assert(i->children.size() == 0); //SetFuncNo(i); @@ -502,53 +511,46 @@ void PFunction::EvalTreeForStringExpression(iter_t const& i) iter_t it = i->children.begin(); // funcName, '(', expression, ')' fFuncString += string(it->value.begin(), it->value.end()).c_str(); - if (termOp == 0) - fFuncString += "("; + fFuncString += "("; EvalTreeForStringExpression(i->children.begin()+2); // the real stuff - if (termOp == 0) - fFuncString += ")"; + fFuncString += ")"; } else if (i->value.id() == PFunctionGrammar::factorID) { EvalTreeForStringExpression(i->children.begin()); } else if (i->value.id() == PFunctionGrammar::termID) { + termOp++; if (*i->value.begin() == '*') { +cout << endl << ">> i->children.size() = " << i->children.size() << endl; assert(i->children.size() == 2); - termOp++; EvalTreeForStringExpression(i->children.begin()); fFuncString += " * "; EvalTreeForStringExpression(i->children.begin()+1); - termOp--; } else if (*i->value.begin() == '/') { assert(i->children.size() == 2); - termOp++; EvalTreeForStringExpression(i->children.begin()); fFuncString += " / "; EvalTreeForStringExpression(i->children.begin()+1); - termOp--; } else { assert(0); } + termOp--; } else if (i->value.id() == PFunctionGrammar::expressionID) { + if (termOp > 0) + fFuncString += "("; if (*i->value.begin() == '+') { assert(i->children.size() == 2); - if (termOp > 0) - fFuncString += "("; EvalTreeForStringExpression(i->children.begin()); fFuncString += " + "; EvalTreeForStringExpression(i->children.begin()+1); - if (termOp > 0) - fFuncString += ")"; } else if (*i->value.begin() == '-') { assert(i->children.size() == 2); - if (termOp > 0) - fFuncString += "("; EvalTreeForStringExpression(i->children.begin()); fFuncString += " - "; EvalTreeForStringExpression(i->children.begin()+1); - if (termOp > 0) - fFuncString += ")"; } else { assert(0); } + if (termOp > 0) + fFuncString += ")"; } else if (i->value.id() == PFunctionGrammar::assignmentID) { assert(i->children.size() == 3); EvalTreeForStringExpression(i->children.begin()); diff --git a/src/include/PFunctionGrammar.h b/src/include/PFunctionGrammar.h index a62bfc19..1c5f5be8 100644 --- a/src/include/PFunctionGrammar.h +++ b/src/include/PFunctionGrammar.h @@ -52,14 +52,15 @@ typedef parse_tree_match_t::tree_iterator iter_t; struct PFunctionGrammar : public grammar { static const int realID = 1; - static const int funLabelID = 2; - static const int parameterID = 3; - static const int mapID = 4; - static const int functionID = 5; - static const int factorID = 6; - static const int termID = 7; - static const int expressionID = 8; - static const int assignmentID = 9; + static const int constPiID = 2; + static const int funLabelID = 3; + static const int parameterID = 4; + static const int mapID = 5; + static const int functionID = 6; + static const int factorID = 7; + static const int termID = 8; + static const int expressionID = 9; + static const int assignmentID = 10; template struct definition @@ -69,6 +70,8 @@ struct PFunctionGrammar : public grammar // Start grammar definition real = leaf_node_d[ real_p ]; + const_pi = leaf_node_d[ str_p("PI") ]; + fun_label = leaf_node_d[ ( lexeme_d[ "FUN" >> +digit_p ] ) ]; parameter = leaf_node_d[ ( lexeme_d[ "PAR" >> +digit_p ] ) ]; @@ -93,6 +96,7 @@ struct PFunctionGrammar : public grammar ; factor = real + | const_pi | parameter | map | function @@ -114,6 +118,7 @@ struct PFunctionGrammar : public grammar // turn on the debugging info. BOOST_SPIRIT_DEBUG_RULE(real); + BOOST_SPIRIT_DEBUG_RULE(const_pi); BOOST_SPIRIT_DEBUG_RULE(fun_label); BOOST_SPIRIT_DEBUG_RULE(parameter); BOOST_SPIRIT_DEBUG_RULE(map); @@ -132,6 +137,7 @@ struct PFunctionGrammar : public grammar rule, parser_tag > map; rule, parser_tag > parameter; rule, parser_tag > fun_label; + rule, parser_tag > const_pi; rule, parser_tag > real; rule, parser_tag > const&