added debug option

This commit is contained in:
nemu 2008-02-11 07:27:01 +00:00
parent b0a23afead
commit e088b5312e
7 changed files with 192 additions and 18 deletions

View File

@ -59,7 +59,8 @@ using namespace std;
* \param param the parameter vector
* \param map the map vector
*/
PFunction::PFunction(tree_parse_info<> info, vector<double> param, vector<int> map)
PFunction::PFunction(tree_parse_info<> info, vector<double> param, vector<int> map, bool debug) :
fDebug(debug)
{
cout << endl << "in PFunction ...";
@ -71,6 +72,12 @@ PFunction::PFunction(tree_parse_info<> info, vector<double> param, vector<int> m
fValid = true;
fFuncNo = -1;
// print out ast tree
if (fDebug) {
PrintTree(info);
return;
}
// check parameter and map range
if (!CheckParameterAndMapRange()) {
fValid = false;
@ -154,7 +161,6 @@ bool PFunction::CheckParameterAndMapInTree(iter_t const& i)
fValid = false;
}
} else if (i->value.id() == PFunctionGrammar::functionID) {
cout << endl << "children = " << i->children.size() << endl;
assert(i->children.size() == 4);
// i: 'funcName', '(', 'expression', ')'
success = CheckParameterAndMapInTree(i->children.begin()+2); // thats the real stuff
@ -163,12 +169,12 @@ cout << endl << "children = " << i->children.size() << endl;
assert(i->children.size() == 1);
success = CheckParameterAndMapInTree(i->children.begin());
} else if (i->value.id() == PFunctionGrammar::termID) {
// '*'/'/' i: lhs, rhs
// '*' or '/' i: lhs, rhs
assert(i->children.size() == 2);
success = CheckParameterAndMapInTree(i->children.begin());
success = CheckParameterAndMapInTree(i->children.begin()+1);
} else if (i->value.id() == PFunctionGrammar::expressionID) {
// '+'/'-' i: lhs, rhs
// '+' or '-' i: lhs, rhs
assert(i->children.size() == 2);
success = CheckParameterAndMapInTree(i->children.begin());
success = CheckParameterAndMapInTree(i->children.begin()+1);
@ -618,3 +624,153 @@ long PFunction::EvalTreeExpression(iter_t const& i)
return 0;
}
//-------------------------------------------------------------
// PrintTree (protected)
//-------------------------------------------------------------
long PFunction::PrintTree(tree_parse_info<> info)
{
cout << endl << "********************************************";
cout << endl << ">> PrintTree";
cout << endl << "********************************************";
long value = PrintTreeExpression(info.trees.begin());
cout << endl << "********************************************";
cout << endl;
return value;
}
//-------------------------------------------------------------
// PrintTreeExpression (protected)
//-------------------------------------------------------------
long PFunction::PrintTreeExpression(iter_t const& i)
{
string str;
iter_t j;
if (i->value.id() == PFunctionGrammar::realID) {
if (i->children.size() == 0) {
str = string(i->value.begin(), i->value.end());
cout << endl << ">> " << str;
} else {
for (unsigned int k=0; k<i->children.size(); k++) {
j = i->children.begin()+k;
str = string(j->value.begin(), j->value.end());
cout << endl << "!> " << str;
}
}
} else if (i->value.id() == PFunctionGrammar::funLabelID) {
if (i->children.size() == 0) {
j = i->children.begin();
str = string(j->value.begin(), j->value.end());
cout << endl << ">> " << str;
} else {
for (unsigned int k=0; k<i->children.size(); k++) {
j = i->children.begin()+k;
str = string(j->value.begin(), j->value.end());
cout << endl << "!> " << str;
}
}
} else if (i->value.id() == PFunctionGrammar::parameterID) {
if (i->children.size() == 0) {
str = string(i->value.begin(), i->value.end());
cout << endl << ">> " << str;
} else {
for (unsigned int k=0; k<i->children.size(); k++) {
j = i->children.begin()+k;
str = string(j->value.begin(), j->value.end());
cout << endl << "!> " << str;
}
}
} else if (i->value.id() == PFunctionGrammar::mapID) {
if (i->children.size() == 0) {
str = string(i->value.begin(), i->value.end());
cout << endl << ">> " << str;
} else {
for (unsigned int k=0; k<i->children.size(); k++) {
j = i->children.begin()+k;
str = string(j->value.begin(), j->value.end());
cout << endl << "!> " << str;
}
}
} else if (i->value.id() == PFunctionGrammar::functionID) {
// i: 'funcName', '(', 'expression', ')'
if (i->children.size() == 4) {
j = i->children.begin();
str = string(j->value.begin(), j->value.end());
cout << endl << ">> " << str;
j = i->children.begin()+1;
str = string(j->value.begin(), j->value.end());
cout << endl << ">> " << str;
PrintTreeExpression(i->children.begin()+2);
j = i->children.begin()+3;
str = string(j->value.begin(), j->value.end());
cout << endl << ">> " << str;
} else {
for (unsigned int k=0; k<i->children.size(); k++) {
j = i->children.begin()+k;
str = string(j->value.begin(), j->value.end());
cout << endl << "!> " << str;
}
}
} else if (i->value.id() == PFunctionGrammar::factorID) {
// i: real | parameter | map | function | expression
if (i->children.size() == 1) {
PrintTreeExpression(i->children.begin());
} else {
for (unsigned int k=0; k<i->children.size(); k++) {
j = i->children.begin()+k;
str = string(j->value.begin(), j->value.end());
cout << endl << "!> " << str;
}
}
} else if (i->value.id() == PFunctionGrammar::termID) {
// '*' or '/' i: lhs, rhs
str = string(i->value.begin(), i->value.end());
cout << endl << ">> " << str;
if (i->children.size() == 2) {
PrintTreeExpression(i->children.begin());
PrintTreeExpression(i->children.begin()+1);
} else {
for (unsigned int k=0; k<i->children.size(); k++) {
j = i->children.begin()+k;
str = string(j->value.begin(), j->value.end());
cout << endl << "!> " << str;
}
}
} else if (i->value.id() == PFunctionGrammar::expressionID) {
// '+' or '-' i: lhs, rhs
str = string(i->value.begin(), i->value.end());
cout << endl << ">> " << str;
if (i->children.size() == 2) {
PrintTreeExpression(i->children.begin());
PrintTreeExpression(i->children.begin()+1);
} else {
for (unsigned int k=0; k<i->children.size(); k++) {
j = i->children.begin()+k;
str = string(j->value.begin(), j->value.end());
cout << endl << "!> " << str;
}
}
} else if (i->value.id() == PFunctionGrammar::assignmentID) {
// i: 'funx', '=', 'expression'
if (i->children.size() == 3) {
j = i->children.begin();
str = string(j->value.begin(), j->value.end());
cout << endl << ">> " << str;
j = i->children.begin()+1;
str = string(j->value.begin(), j->value.end());
cout << endl << ">> " << str;
PrintTreeExpression(i->children.begin()+2);
} else {
for (unsigned int k=0; k<i->children.size(); k++) {
j = i->children.begin()+k;
str = string(j->value.begin(), j->value.end());
cout << endl << "!> " << str;
}
}
} else {
cout << endl << "this point should never have been reached :-(" << endl;
assert(0);
}
return 0;
}

View File

@ -75,7 +75,7 @@ typedef struct func_tree_node {
//----------------------------------------------------------------------------
class PFunction {
public:
PFunction(tree_parse_info<> info, vector<double> param, vector<int> map);
PFunction(tree_parse_info<> info, vector<double> param, vector<int> map, bool debug);
virtual ~PFunction();
virtual bool IsValid() { return fValid; }
@ -96,12 +96,16 @@ class PFunction {
virtual long EvaluateTree(tree_parse_info<> info);
virtual long EvalTreeExpression(iter_t const& i);
virtual long PrintTree(tree_parse_info<> info);
virtual long PrintTreeExpression(iter_t const& i);
private:
tree_parse_info<> fInfo;
vector<double> fParam;
vector<int> fMap;
PFuncTreeNode fFunc;
bool fDebug;
bool fValid; ///< flag showing if the function is valid
int fFuncNo; ///< function number, i.e. FUNx with x the function number

View File

@ -109,7 +109,7 @@ struct PFunctionGrammar : public grammar<PFunctionGrammar>
| (root_node_d[ch_p('-')] >> term)
);
assignment = (fun_label >> ch_p('=') >> expression);
assignment = fun_label >> ch_p('=') >> expression;
// End grammar definition
// turn on the debugging info.

View File

@ -46,7 +46,7 @@
*
* \param fln
*/
PFunctionHandler::PFunctionHandler(char *fln)
PFunctionHandler::PFunctionHandler(char *fln, bool debug) : fDebug(debug)
{
fValid = true;
fFileName = QString(fln);
@ -186,7 +186,7 @@ cout << endl << "fLines[" << i << "] = '" << fLines[i].latin1() << "'";
if (info.full) {
cout << endl << "parse successfull ..." << endl;
PFunction func(info, fParam, fMap);
PFunction func(info, fParam, fMap, fDebug);
fFuncs.push_back(func);
} else {
cout << endl << "parse failed ... (" << i << ")" << endl;

View File

@ -44,7 +44,7 @@ using namespace std;
class PFunctionHandler
{
public:
PFunctionHandler(char *fln);
PFunctionHandler(char *fln, bool debug);
PFunctionHandler(vector<QString> lines);
virtual ~PFunctionHandler();
@ -55,6 +55,7 @@ class PFunctionHandler
virtual unsigned int GetNoOfFuncs() { return fFuncs.size(); }
private:
bool fDebug;
bool fValid;
QString fFileName;

View File

@ -17,7 +17,8 @@
PAR 1.0 2.1 3.5 -0.87 0.87
MAP 2 1 4 5
FUNCTIONS
fun0 = sin(par3/(par1+map2))
#fun0 = sin(par3/(par1+map2))
fun1 = 1.0+(((sin(par1))))
#fun0 = par1 + map3 * cos(cos(par2 - map1))
#fun8 = log(sin(par1)) + exp(-1.0*map2)
#fun1 = par1 + map1 * (0.01355+par1*(2.1 - (-2.3 / 3.4)))

View File

@ -6,10 +6,11 @@ using namespace std;
//-----------------------------------------------------
void syntax()
{
cout << endl << "spirit_fcn_test [--file <filename>] | [--help]";
cout << endl << "spirit_fcn_test [--file <filename> [--debug]] | [--help]";
cout << endl << " without arguments: interactive mode";
cout << endl << " --file <filename>: function block etc. from file";
cout << endl << " --help: this help";
cout << endl << " --help: this help";
cout << endl << " --debug: will print the ast tree only (no evaluatio)";
cout << endl << endl;
}
@ -47,26 +48,35 @@ void handle_input(vector<QString> &lines)
int main(int argc, char *argv[])
{
bool inputFile = false;
bool debug = false;
if (argc > 3) {
if (argc > 4) {
syntax();
return 0;
} else if (argc == 2) {
syntax();
return 0;
} else if (argc == 3) {
} else if (argc >= 3) {
if (strcmp(argv[1], "--file")) {
syntax();
return 0;
} else {
inputFile = true;
}
if (argc == 4) {
if (strcmp(argv[3], "--debug")) {
syntax();
return 0;
} else {
debug = true;
}
}
}
PFunctionHandler *fcnHandler = 0;
if (inputFile) {
fcnHandler = new PFunctionHandler(argv[2]);
fcnHandler = new PFunctionHandler(argv[2], debug);
} else {
vector<QString> lines;
handle_input(lines);
@ -84,9 +94,11 @@ cout << endl << "lines.size() = " << lines.size();
if (go_on) {
cout << endl << "will do the parsing ...";
if (fcnHandler->DoParse()) {
cout << endl << "will do the evaluation ...";
for (unsigned int i=0; i<fcnHandler->GetNoOfFuncs(); i++)
cout << endl << "FUN" << fcnHandler->GetFuncNo(i) << " = " << fcnHandler->Eval(fcnHandler->GetFuncNo(i));
if (!debug) {
cout << endl << "will do the evaluation ...";
for (unsigned int i=0; i<fcnHandler->GetNoOfFuncs(); i++)
cout << endl << "FUN" << fcnHandler->GetFuncNo(i) << " = " << fcnHandler->Eval(fcnHandler->GetFuncNo(i));
}
}
}