Skip to content

Commit a702f71

Browse files
committed
simplify condition
1 parent a5f4269 commit a702f71

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/main/java/com/github/sidhant92/boolparser/parser/antlr/BooleanFilterListener.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,22 @@ private ArithmeticFunctionNode mapArithmeticFunctionExpressionContext(BooleanExp
155155

156156
private ComparisonNode mapComparatorExpressionContext(BooleanExpressionParser.ComparatorExpressionContext ctx) {
157157
final Operator operator = Operator.getOperatorFromSymbol(ctx.op.getText()).orElse(Operator.EQUALS);
158-
if (!(ctx.right instanceof BooleanExpressionParser.TypesExpressionContext) && !currentNodes.isEmpty()) {
159-
final Node value = currentNodes.pop();
160-
return new ComparisonNode(mapContextToNode(ctx.left), value, operator, DataType.INTEGER);
161-
} else {
162-
if (ctx.left instanceof BooleanExpressionParser.ParentExpressionContext && !currentNodes.isEmpty()) {
158+
159+
if (!currentNodes.isEmpty() && ((ctx.right instanceof BooleanExpressionParser.ParentExpressionContext || ctx.left instanceof BooleanExpressionParser.ParentExpressionContext) || !(currentNodes.peek() instanceof ComparisonNode || currentNodes.peek() instanceof BooleanNode))) {
160+
if (ctx.left instanceof BooleanExpressionParser.TypesExpressionContext) {
161+
final DataType dataType = getDataType(ctx.left.getStart());
162+
final Node value = mapContextToNode(ctx.left);
163+
return new ComparisonNode(value, currentNodes.pop(), operator, dataType);
164+
} else if (ctx.right instanceof BooleanExpressionParser.TypesExpressionContext) {
163165
final DataType dataType = getDataType(ctx.right.getStart());
164166
final Node value = mapContextToNode(ctx.right);
165167
return new ComparisonNode(currentNodes.pop(), value, operator, dataType);
166168
}
167-
final DataType dataType = getDataType(ctx.right.getStart());
168-
final Node value = mapContextToNode(ctx.right);
169-
return new ComparisonNode(mapContextToNode(ctx.left), value, operator, dataType);
170169
}
170+
171+
final DataType dataType = getDataType(ctx.right.getStart());
172+
final Node value = mapContextToNode(ctx.right);
173+
return new ComparisonNode(mapContextToNode(ctx.left), value, operator, dataType);
171174
}
172175

173176
private ArithmeticNode mapArithmeticExpressionContext(BooleanExpressionParser.ArithmeticExpressionContext ctx) {

src/test/java/com/github/sidhant92/boolparser/application/BooleanExpressionEvaluatorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,16 @@ public void testComparisonWithArithmeticFalseCondition1() {
614614
assertFalse(booleanOptional.get());
615615
}
616616

617+
@Test
618+
public void testBooleanComparison() {
619+
final Map<String, Object> data = new HashMap<>();
620+
data.put("age", true);
621+
data.put("a", 20);
622+
final Try<Boolean> booleanOptional = booleanExpressionEvaluator.evaluate("age = (5 > 3)", data);
623+
assertTrue(booleanOptional.isSuccess());
624+
assertTrue(booleanOptional.get());
625+
}
626+
617627
@Test
618628
public void testNegativeComparison() {
619629
final Map<String, Object> data = new HashMap<>();

0 commit comments

Comments
 (0)