diff --git a/src/DataFilter.cpp b/src/DataFilter.cpp index 773568baf..433394983 100644 --- a/src/DataFilter.cpp +++ b/src/DataFilter.cpp @@ -143,6 +143,9 @@ Leaf::isDynamic(Leaf *leaf) case Leaf::Logical : if (leaf->op == 0) return leaf->isDynamic(leaf->lvalue.l); + case Leaf::UnaryOperation : + return leaf->isDynamic(leaf->lvalue.l); + break; case Leaf::Operation : case Leaf::BinaryOperation : return leaf->isDynamic(leaf->lvalue.l) || leaf->isDynamic(leaf->rvalue.l); @@ -262,6 +265,10 @@ void Leaf::color(Leaf *leaf, QTextDocument *document) return; break; + case Leaf::UnaryOperation : + leaf->color(leaf->lvalue.l, document); + return; + break; case Leaf::BinaryOperation : leaf->color(leaf->lvalue.l, document); leaf->color(leaf->rvalue.l, document); @@ -338,6 +345,9 @@ void Leaf::print(Leaf *leaf, int level) leaf->print(leaf->lvalue.l, level+1); leaf->print(leaf->rvalue.l, level+1); break; + case Leaf::UnaryOperation : qDebug()<<"uop"<op; + leaf->print(leaf->lvalue.l, level+1); + break; case Leaf::BinaryOperation : qDebug()<<"bop"<op; leaf->print(leaf->lvalue.l, level+1); leaf->print(leaf->rvalue.l, level+1); @@ -414,6 +424,7 @@ bool Leaf::isNumber(DataFilter *df, Leaf *leaf) break; case Leaf::Logical : return true; // not possible! case Leaf::Operation : return true; + case Leaf::UnaryOperation : return true; case Leaf::BinaryOperation : return true; case Leaf::Function : return true; case Leaf::Vector : @@ -594,6 +605,14 @@ void Leaf::validateFilter(DataFilter *df, Leaf *leaf) } break; + case Leaf::UnaryOperation : + { // only 1 at present, unary minus return the value * -1 + if (!Leaf::isNumber(df, leaf->lvalue.l)) { + DataFiltererrors << QString(QObject::tr("unary negation on a string!")); + leaf->inerror = true; + } + } + break; case Leaf::BinaryOperation : case Leaf::Operation : { @@ -1266,6 +1285,18 @@ Result Leaf::eval(Context *context, DataFilter *df, Leaf *leaf, RideItem *m) } break; + // + // UNARY EXPRESSION + // + + case Leaf::UnaryOperation : + { + // get result + Result lhs = eval(context, df, leaf->lvalue.l, m); + return Result(lhs.number * -1); + } + break; + // // BINARY EXPRESSION // diff --git a/src/DataFilter.h b/src/DataFilter.h index 8e23cfcee..1eedfceee 100644 --- a/src/DataFilter.h +++ b/src/DataFilter.h @@ -66,7 +66,7 @@ class Leaf { void clear(Leaf*); enum { none, Float, Integer, String, Symbol, - Logical, Operation, BinaryOperation, + Logical, Operation, BinaryOperation, UnaryOperation, Function, Conditional, Vector, Parameters } type; diff --git a/src/DataFilter.l b/src/DataFilter.l index 6dafeb981..414ce0b62 100644 --- a/src/DataFilter.l +++ b/src/DataFilter.l @@ -69,9 +69,9 @@ int DataFiltercolumn = 1; [Oo][Rr] DataFilterlval.op = OR; return OR; -[-+]?[0-9]+ return DF_INTEGER; -[-+]?[0-9]+e-[0-9]+ return DF_FLOAT; -[-+]?[0-9]+\.[-e0-9]* return DF_FLOAT; +[0-9]+ return DF_INTEGER; +[0-9]+e-[0-9]+ return DF_FLOAT; +[0-9]+\.[-e0-9]* return DF_FLOAT; \"([^\"]|\\\")*\" return DF_STRING; /* contains non-quotes or escaped-quotes */ diff --git a/src/DataFilter.y b/src/DataFilter.y index 19a74c310..dc4facfb3 100644 --- a/src/DataFilter.y +++ b/src/DataFilter.y @@ -135,7 +135,12 @@ expr : expr SUBTRACT expr { $$ = new Leaf(@1.first_column, @3.last_ $$->lvalue.l = $1; $$->op = $2; $$->rvalue.l = $3; } - + | SUBTRACT expr %prec MULTIPLY { $$ = new Leaf(@1.first_column, @2.last_column); + $$->type = Leaf::UnaryOperation; + $$->lvalue.l = $2; + $$->op = $1; + $$->rvalue.l = NULL; + } | expr '?' expr ':' expr { $$ = new Leaf(@1.first_column, @5.last_column); $$->type = Leaf::Conditional; $$->op = 0; // unused