diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-09-28 19:32:17 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2022-09-28 19:32:17 +0200 |
commit | 2c0bc69f71a422a6ac10d34b7d965d0dd835a6cc (patch) | |
tree | e461198ce42639f910f50614fbf44e51820452cf /searchlib/src/main/java/com/yahoo | |
parent | a1912b44d0b800f96b334a24ddefd0026f3af356 (diff) |
Right precedence for ^
Diffstat (limited to 'searchlib/src/main/java/com/yahoo')
-rwxr-xr-x | searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/OperationNode.java | 6 | ||||
-rw-r--r-- | searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Operator.java | 15 |
2 files changed, 10 insertions, 11 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/OperationNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/OperationNode.java index d08e2270935..0512e1dad2f 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/OperationNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/OperationNode.java @@ -68,14 +68,14 @@ public final class OperationNode extends CompositeNode { */ private boolean nonDefaultPrecedence(CompositeNode parent) { if ( parent == null) return false; - if ( ! (parent instanceof OperationNode arithmeticParent)) return false; + if ( ! (parent instanceof OperationNode operationParent)) return false; // The line below can only be correct in both only have one operator. // Getting this correct is impossible without more work. // So for now we only handle the simple case correctly, and use a safe approach by adding // extra parenthesis just in case.... - return arithmeticParent.operators.get(0).hasPrecedenceOver(this.operators.get(0)) - || ((arithmeticParent.operators.size() > 1) || (operators.size() > 1)); + return operationParent.operators.get(0).hasPrecedenceOver(this.operators.get(0)) + || ((operationParent.operators.size() > 1) || (operators.size() > 1)); } @Override diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Operator.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Operator.java index 63144f0ef4a..02af88b2c58 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Operator.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Operator.java @@ -35,27 +35,26 @@ public enum Operator { public static final List<Operator> operatorsByPrecedence = Arrays.stream(Operator.values()).toList(); private final String image; - private final boolean bindsRight; // TODO: Implement + private final boolean rightPrecedence; private final BiFunction<Value, Value, Value> function; Operator(String image, BiFunction<Value, Value, Value> function) { this(image, false, function); } - Operator(String image, boolean bindsRight, BiFunction<Value, Value, Value> function) { + Operator(String image, boolean rightPrecedence, BiFunction<Value, Value, Value> function) { this.image = image; - this.bindsRight = bindsRight; + this.rightPrecedence = rightPrecedence; this.function = function; } /** Returns true if this operator has precedence over the given operator */ - public boolean hasPrecedenceOver(Operator op) { - return operatorsByPrecedence.indexOf(this) > operatorsByPrecedence.indexOf(op); + public boolean hasPrecedenceOver(Operator other) { + if (operatorsByPrecedence.indexOf(this) == operatorsByPrecedence.indexOf(other)) + return rightPrecedence; + return operatorsByPrecedence.indexOf(this) > operatorsByPrecedence.indexOf(other); } - /** Returns true if a sequence of these operations should be evaluated from right to left rather than left to right. */ - public boolean bindsRight() { return bindsRight; } - public final Value evaluate(Value x, Value y) { return function.apply(x, y); } |