Fixup unary '-'

.. so date-9 is not treated as "date" "-9" but
   instead as "date" "-" "9"
This commit is contained in:
Mark Liversedge
2015-08-17 15:21:24 +01:00
parent 6088c21a32
commit e1f2c1b48b
4 changed files with 41 additions and 5 deletions

View File

@@ -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"<<leaf->op;
leaf->print(leaf->lvalue.l, level+1);
break;
case Leaf::BinaryOperation : qDebug()<<"bop"<<leaf->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
//

View File

@@ -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;

View File

@@ -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 */

View File

@@ -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