From 52f98b09696ccfd18ec91804e1d8425168baf49e Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 20 Nov 2010 16:32:21 +0000 Subject: [PATCH] debug print for expr git-svn-id: https://silmor.de/svn/softmagic/elam/trunk@637 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- src/elamexpression.cpp | 36 +++++++++++++++++++++++++++++++++++- src/elamexpression.h | 3 +++ tests/eval/eval.cpp | 4 +++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/elamexpression.cpp b/src/elamexpression.cpp index d40a901..eb5781e 100644 --- a/src/elamexpression.cpp +++ b/src/elamexpression.cpp @@ -277,7 +277,7 @@ Expression::Expression(Engine* parent, const QList< Token >& toks) } d->parent=parent; d->tokens=reduceTokens(toks); - qDebug()<<"expression:"<tokens; + qDebug()<<"tokens:"<tokens; //check for nothing and complain if(d->tokens.size()==0){ d->type=Exception; @@ -389,6 +389,40 @@ QVariant Expression::evaluate() return QVariant(); } +void printExpression(QDebug&dbg,const Expression&ex,int level) +{ + printspaces(dbg,level); + dbg<<"Expression(type="; + switch(ex.d->type){ + case Expression::Literal:dbg<<"Literal";break; + case Expression::Variable:dbg<<"Variable";break; + case Expression::Constant:dbg<<"Constant";break; + case Expression::Function:dbg<<"Function";break; + case Expression::Parentheses:dbg<<"Parentheses";break; + case Expression::UnaryOp:dbg<<"UnaryOperator";break; + case Expression::BinaryOp:dbg<<"BinaryOperator";break; + case Expression::AssignmentOp:dbg<<"Assignment";break; + case Expression::Exception:dbg<<"Exception";break; + default:dbg<<"Unknown:"<<(int)ex.d->type;break; + } + if(ex.d->excep.errorType()!=ELAM::Exception::NoError) + dbg<<",exception="<excep; + if(ex.d->subexpr.size()>0){ + dbg<<",subexpressions:"; + for(int i=0;isubexpr.size();i++){ + dbg<<"\n"; + printExpression(dbg,ex.d->subexpr[i],level+1); + } + } + dbg<<")"; +} + +QDebug& operator<<(QDebug&dbg,const Expression&ex) +{ + dbg.nospace(); + printExpression(dbg,ex,0); + return dbg.space(); +} }; \ No newline at end of file diff --git a/src/elamexpression.h b/src/elamexpression.h index 23ad657..fb8af15 100644 --- a/src/elamexpression.h +++ b/src/elamexpression.h @@ -134,6 +134,7 @@ class Expression ///evaluates the expression and returns the result of the evaluation QVariant evaluate(); private: + friend void printExpression(QDebug&,const Expression&,int); ///scan tokens and decide what specific sub-type they are QListclassifyTokens(QList toks); /**pushes parentheses and function arguments into the sub-tokens of their parents; @@ -147,6 +148,8 @@ class Expression void functionInit(); }; +QDebug& operator<<(QDebug&,const Expression&); + //end of namespace }; diff --git a/tests/eval/eval.cpp b/tests/eval/eval.cpp index 65614ca..d0d000a 100644 --- a/tests/eval/eval.cpp +++ b/tests/eval/eval.cpp @@ -13,7 +13,9 @@ void ElamTest::evaltest() { IntEngine ie; FloatEngine::configureFloatEngine(ie); - QString ex="a=b+=345*int(3.5)+ - -(+65/(5))"; + QString exs="a=b+=345*int(3.5)+ - -(+65/(5))"; + Expression ex=ie.expression(exs); + qDebug()<<"expression:"<