debug print for expr
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 20 Nov 2010 16:32:21 +0000 (16:32 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 20 Nov 2010 16:32:21 +0000 (16:32 +0000)
git-svn-id: https://silmor.de/svn/softmagic/elam/trunk@637 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/elamexpression.cpp
src/elamexpression.h
tests/eval/eval.cpp

index d40a901..eb5781e 100644 (file)
@@ -277,7 +277,7 @@ Expression::Expression(Engine* parent, const QList< Token >& toks)
        }
        d->parent=parent;
        d->tokens=reduceTokens(toks);
-       qDebug()<<"expression:"<<d->tokens;
+       qDebug()<<"tokens:"<<d->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="<<ex.d->excep;
+       if(ex.d->subexpr.size()>0){
+               dbg<<",subexpressions:";
+               for(int i=0;i<ex.d->subexpr.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
index 23ad657..fb8af15 100644 (file)
@@ -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
                QList<Token>classifyTokens(QList<Token> 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
 };
 
index 65614ca..d0d000a 100644 (file)
@@ -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:"<<ex;
        QVariant v=ie.evaluate(ex);
 }