improved (hopefully) string back parsing and furthermore added the constant Pi for functions.
This commit is contained in:
parent
4d9732faa1
commit
b0a23afead
@ -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());
|
||||
|
@ -52,14 +52,15 @@ typedef parse_tree_match_t::tree_iterator iter_t;
|
||||
struct PFunctionGrammar : public grammar<PFunctionGrammar>
|
||||
{
|
||||
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 <typename ScannerT>
|
||||
struct definition
|
||||
@ -69,6 +70,8 @@ struct PFunctionGrammar : public grammar<PFunctionGrammar>
|
||||
// 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<PFunctionGrammar>
|
||||
;
|
||||
|
||||
factor = real
|
||||
| const_pi
|
||||
| parameter
|
||||
| map
|
||||
| function
|
||||
@ -114,6 +118,7 @@ struct PFunctionGrammar : public grammar<PFunctionGrammar>
|
||||
|
||||
// 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<PFunctionGrammar>
|
||||
rule<ScannerT, parser_context<>, parser_tag<mapID> > map;
|
||||
rule<ScannerT, parser_context<>, parser_tag<parameterID> > parameter;
|
||||
rule<ScannerT, parser_context<>, parser_tag<funLabelID> > fun_label;
|
||||
rule<ScannerT, parser_context<>, parser_tag<constPiID> > const_pi;
|
||||
rule<ScannerT, parser_context<>, parser_tag<realID> > real;
|
||||
|
||||
rule<ScannerT, parser_context<>, parser_tag<assignmentID> > const&
|
||||
|
Loading…
x
Reference in New Issue
Block a user