Skip to content

Commit 8ee0809

Browse files
committed
remove arithmetic unary node
1 parent a447716 commit 8ee0809

4 files changed

Lines changed: 17 additions & 63 deletions

File tree

src/main/java/com/github/sidhant92/boolparser/application/ArithmeticExpressionEvaluator.java

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
import java.util.Collection;
55
import java.util.List;
66
import java.util.Map;
7-
import java.util.Optional;
87
import java.util.stream.Collectors;
98
import com.github.sidhant92.boolparser.constant.ContainerDataType;
109
import com.github.sidhant92.boolparser.constant.DataType;
1110
import com.github.sidhant92.boolparser.constant.Operator;
1211
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
1312
import com.github.sidhant92.boolparser.domain.UnaryNode;
1413
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticNode;
15-
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticUnaryNode;
1614
import com.github.sidhant92.boolparser.domain.Node;
1715
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticFunctionNode;
1816
import com.github.sidhant92.boolparser.exception.UnsupportedToken;
@@ -54,10 +52,6 @@ private Object evaluateToken(final Node node, final Map<String, Object> data) {
5452
switch (node.getTokenType()) {
5553
case ARITHMETIC:
5654
return evaluateArithmeticToken((ArithmeticNode) node, data);
57-
case ARITHMETIC_LEAF:
58-
return evaluateUnaryNode((UnaryNode) node, data);
59-
case ARITHMETIC_UNARY:
60-
return evaluateUnaryArithmeticToken((ArithmeticUnaryNode) node, data);
6155
case ARITHMETIC_FUNCTION:
6256
return evaluateArithmeticFunctionToken((ArithmeticFunctionNode) node, data);
6357
case UNARY:
@@ -73,25 +67,6 @@ private Object evaluateUnaryToken(final UnaryNode unaryNode, final Map<String, O
7367
.orElse(unaryNode.getValue()) : unaryNode.getValue();
7468
}
7569

76-
private EvaluatedNode evaluateUnaryNode(final UnaryNode unaryNode, final Map<String, Object> data) {
77-
final Optional<Object> fetchedValue = unaryNode.getDataType() != DataType.STRING ? Optional.of(
78-
unaryNode.getValue()) : ValueUtils.getValueFromMap(unaryNode.getValue().toString(), data);
79-
return fetchedValue
80-
.map(o -> EvaluatedNode.builder().value(o).dataType(ValueUtils.getDataType(o)).build())
81-
.orElseGet(() -> EvaluatedNode.builder().value(unaryNode.getValue()).dataType(unaryNode.getDataType()).build());
82-
}
83-
84-
private Object evaluateUnaryArithmeticToken(final ArithmeticUnaryNode arithmeticUnaryNode, final Map<String, Object> data) {
85-
final Object resolvedValue = evaluateToken(arithmeticUnaryNode.getOperand(), data);
86-
if (resolvedValue instanceof EvaluatedNode) {
87-
final EvaluatedNode evaluatedNode = (EvaluatedNode) resolvedValue;
88-
return operatorService.evaluateArithmeticOperator(evaluatedNode.getValue(), evaluatedNode.getDataType(), null, null, Operator.UNARY,
89-
ContainerDataType.PRIMITIVE);
90-
}
91-
final DataType dataType = ValueUtils.getDataType(resolvedValue);
92-
return operatorService.evaluateArithmeticOperator(resolvedValue, dataType, null, null, Operator.UNARY, ContainerDataType.PRIMITIVE);
93-
}
94-
9570
private Object evaluateArithmeticFunctionToken(final ArithmeticFunctionNode arithmeticFunctionNode, final Map<String, Object> data) {
9671
final List<Object> resolvedValues = arithmeticFunctionNode.getItems()
9772
.stream()
@@ -120,6 +95,17 @@ private Object evaluateArithmeticFunctionToken(final ArithmeticFunctionNode arit
12095

12196
private Object evaluateArithmeticToken(final ArithmeticNode arithmeticNode, final Map<String, Object> data) {
12297
final Object leftValue = evaluateToken(arithmeticNode.getLeft(), data);
98+
if (arithmeticNode.getOperator().equals(Operator.UNARY)) {
99+
if (leftValue instanceof EvaluatedNode) {
100+
final EvaluatedNode leftPair = (EvaluatedNode) leftValue;
101+
return operatorService.evaluateArithmeticOperator(leftPair.getValue(), leftPair.getDataType(), null, null,
102+
arithmeticNode.getOperator(), ContainerDataType.PRIMITIVE);
103+
} else {
104+
final DataType leftDataType = ValueUtils.getDataType(leftValue);
105+
return operatorService.evaluateArithmeticOperator(leftValue, leftDataType, null, null, arithmeticNode.getOperator(),
106+
ContainerDataType.PRIMITIVE);
107+
}
108+
}
123109
final Object rightValue = evaluateToken(arithmeticNode.getRight(), data);
124110
if (leftValue instanceof EvaluatedNode && rightValue instanceof EvaluatedNode) {
125111
final EvaluatedNode leftPair = (EvaluatedNode) leftValue;

src/main/java/com/github/sidhant92/boolparser/constant/NodeType.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,5 @@ public enum NodeType {
1212
ARRAY,
1313
UNARY,
1414
ARITHMETIC,
15-
ARITHMETIC_LEAF,
16-
ARITHMETIC_UNARY,
17-
ARITHMETIC_FUNCTION,
18-
STRING
15+
ARITHMETIC_FUNCTION
1916
}

src/main/java/com/github/sidhant92/boolparser/domain/arithmetic/ArithmeticUnaryNode.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.github.sidhant92.boolparser.constant.Operator;
1616
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticFunctionNode;
1717
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticNode;
18-
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticUnaryNode;
1918
import com.github.sidhant92.boolparser.domain.ArrayNode;
2019
import com.github.sidhant92.boolparser.domain.BooleanNode;
2120
import com.github.sidhant92.boolparser.domain.InNode;
@@ -113,7 +112,7 @@ public void exitUnaryArithmeticExpression(BooleanExpressionParser.UnaryArithmeti
113112
final DataType dataType = getDataType(ctx.exp.getStart());
114113
final Object operand = ValueUtils.convertValue(ctx.exp.getText(), dataType);
115114
final UnaryNode leafNode = UnaryNode.builder().value(operand).dataType(dataType).build();
116-
currentNodes.add(ArithmeticUnaryNode.builder().operand(leafNode).build());
115+
currentNodes.add(ArithmeticNode.builder().left(leafNode).operator(Operator.UNARY).build());
117116
super.enterUnaryArithmeticExpression(ctx);
118117
}
119118

@@ -170,9 +169,6 @@ public void exitInExpression(BooleanExpressionParser.InExpressionContext ctx) {
170169
}
171170

172171
private List<Pair<DataType, Object>> getArrayElements(final List<ParseTree> trees) {
173-
trees.forEach(tr -> {
174-
System.out.println("**********" + tr.getClass());
175-
});
176172
return trees
177173
.stream()
178174
.filter(child -> child instanceof BooleanExpressionParser.TypesExpressionContext)
@@ -241,11 +237,11 @@ public void exitParse(BooleanExpressionParser.ParseContext ctx) {
241237
} else if (this.node == null && this.currentNodes.size() == 2) {
242238
final Node firstNode = currentNodes.pop();
243239
final Node secondNode = currentNodes.pop();
244-
if (firstNode instanceof ArithmeticNode && secondNode instanceof ArithmeticUnaryNode) {
245-
this.node = ArithmeticUnaryNode.builder().operand(firstNode).build();
240+
if (firstNode instanceof ArithmeticNode && secondNode instanceof ArithmeticNode && ((ArithmeticNode) secondNode).getRight() == null) {
241+
this.node = ArithmeticNode.builder().operator(Operator.UNARY).left(firstNode).build();
246242
}
247-
if (secondNode instanceof ArithmeticNode && firstNode instanceof ArithmeticUnaryNode) {
248-
this.node = ArithmeticUnaryNode.builder().operand(secondNode).build();
243+
if (secondNode instanceof ArithmeticNode && firstNode instanceof ArithmeticNode && ((ArithmeticNode) firstNode).getRight() == null) {
244+
this.node = ArithmeticNode.builder().operator(Operator.UNARY).left(secondNode).build();
249245
}
250246
}
251247
if (this.node == null && tokenCount == 1 && lastToken instanceof CommonToken) {

0 commit comments

Comments
 (0)