Skip to content

Commit

Permalink
Add support for ternary expressions
Browse files Browse the repository at this point in the history
Signed-off-by: aserkes <[email protected]>
  • Loading branch information
aserkes committed Mar 1, 2023
1 parent 85fb5f1 commit eb88933
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 173 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -91,7 +91,7 @@ public VisitResult postVisitBlock(Block block, Context ctx) {

@Override
public VisitResult visitCondition(Condition condition, Context ctx) {
if (condition.expression().eval(ctx::getValue)) {
if (condition.expression().eval(ctx::getValue).asBoolean()) {
return VisitResult.CONTINUE;
}
return VisitResult.SKIP_SUBTREE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -83,7 +83,7 @@ public <A> VisitResult accept(Visitor<A> visitor, A arg) {
*/
public static boolean filter(Node node, Function<String, Value> resolver) {
if (node instanceof Condition) {
return ((Condition) node).expression().eval(resolver);
return ((Condition) node).expression().eval(resolver).asBoolean();
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -92,7 +92,7 @@ public List<Token> tokens() {
*
* @return result
*/
public boolean eval() {
public Value eval() {
return eval(s -> null);
}

Expand All @@ -102,7 +102,7 @@ public boolean eval() {
* @param resolver variable resolver
* @return result
*/
public Value eval1(Function<String, Value> resolver) {
public Value eval(Function<String, Value> resolver) {
Deque<Value> stack = new ArrayDeque<>();
for (Token token : tokens) {
Value value;
Expand Down Expand Up @@ -134,12 +134,15 @@ public Value eval1(Function<String, Value> resolver) {
break;
case CONTAINS:
if (lastOperand.type() == ValueTypes.STRING_LIST) {
result = new TypedValue(new HashSet<>(operand2.asList()).containsAll(lastOperand.asList()), ValueTypes.BOOLEAN);
result = new TypedValue(new HashSet<>(operand2.asList()).containsAll(lastOperand.asList()),
ValueTypes.BOOLEAN);
} else {
if (operand2.type() == ValueTypes.STRING) {
result = new TypedValue(operand2.asString().contains(lastOperand.asString()), ValueTypes.BOOLEAN);
result = new TypedValue(operand2.asString().contains(lastOperand.asString()),
ValueTypes.BOOLEAN);
} else {
result = new TypedValue(operand2.asList().contains(lastOperand.asString()), ValueTypes.BOOLEAN);
result = new TypedValue(operand2.asList().contains(lastOperand.asString()),
ValueTypes.BOOLEAN);
}
}
break;
Expand All @@ -163,67 +166,6 @@ public Value eval1(Function<String, Value> resolver) {
return stack.pop();
}

/**
* Evaluate this expression.
*
* @param resolver variable resolver
* @return result
*/
public boolean eval(Function<String, Value> resolver) {
Deque<Value> stack = new ArrayDeque<>();
for (Token token : tokens) {
Value value;
if (token.operator != null) {
boolean result;
Value operand1 = stack.pop();
if (token.operator == Operator.NOT) {
result = !operand1.asBoolean();
} else {
Value operand2 = stack.pop();
switch (token.operator) {
case OR:
result = operand2.asBoolean() || operand1.asBoolean();
break;
case AND:
result = operand2.asBoolean() && operand1.asBoolean();
break;
case EQUAL:
result = Value.equals(operand2, operand1);
break;
case NOT_EQUAL:
result = !Value.equals(operand2, operand1);
break;
case CONTAINS:
if (operand1.type() == ValueTypes.STRING_LIST) {
result = new HashSet<>(operand2.asList()).containsAll(operand1.asList());
} else {
if (operand2.type() == ValueTypes.STRING) {
result = operand2.asString().contains(operand1.asString());
} else {
result = operand2.asList().contains(operand1.asString());
}
}
break;
default:
throw new IllegalStateException("Unsupported operator: " + token.operator);
}
}
value = Value.create(result);
} else if (token.operand != null) {
value = token.operand;
} else if (token.variable != null) {
value = resolver.apply(token.variable);
if (value == null) {
throw new UnresolvedVariableException(token.variable);
}
} else {
throw new IllegalStateException("Invalid token");
}
stack.push(value);
}
return stack.pop().asBoolean();
}

/**
* Unresolved variable error.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -265,7 +265,7 @@ private static boolean filter(List<Expression> filters, Map<String, String> perm
return DynamicValue.create(v);
}
return null;
});
}).asBoolean();
if (!result) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -664,7 +664,7 @@ public VisitResult visitPreset(Preset preset, Void arg) {

@Override
public VisitResult visitCondition(Condition condition, Void arg) {
if (condition.expression().eval()) {
if (condition.expression().eval().asBoolean()) {
return VisitResult.CONTINUE;
}
return VisitResult.SKIP_SUBTREE;
Expand Down
Loading

0 comments on commit eb88933

Please sign in to comment.