Skip to content
This repository was archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
Project update and fixes for sonarlint.
Browse files Browse the repository at this point in the history
  • Loading branch information
andyHa committed Jul 6, 2022
1 parent ca9c634 commit 638894f
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 87 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.scireum</groupId>
<artifactId>sirius-parent</artifactId>
<version>8.0.1</version>
<version>10.1.1</version>
</parent>
<artifactId>parsii</artifactId>
<version>DEVELOPMENT-SNAPSHOT</version>
Expand Down
24 changes: 9 additions & 15 deletions src/main/java/parsii/eval/BinaryOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class BinaryOperation implements Expression {
* Enumerates the operations supported by this expression.
*/
public enum Op {
ADD(3), SUBTRACT(3), MULTIPLY(4), DIVIDE(4), MODULO(4), POWER(5), LT(2), LT_EQ(2), EQ(2), GT_EQ(2), GT(2), NEQ(2), AND(
1), OR(1);
ADD(3), SUBTRACT(3), MULTIPLY(4), DIVIDE(4), MODULO(4), POWER(5), LT(2), LT_EQ(2), EQ(2), GT_EQ(2), GT(2),
NEQ(2), AND(1), OR(1);

private final int priority;

Expand Down Expand Up @@ -189,25 +189,19 @@ private Expression trySimplifyRightSide() {
}

// We have a sub-operation with the same operator, let's see if we can pre-compute some constants
if (left.isConstant()) {
if (left.isConstant() && childOp.left.isConstant()) {
// Left side is constant, we therefore can combine constants. We can rely on the constant
// being on the left side, since we reorder commutative operations (see above)
if (childOp.left.isConstant()) {
if (op == Op.ADD) {
return new BinaryOperation(op,
new Constant(left.evaluate() + childOp.left.evaluate()),
childOp.right);
}
if (op == Op.MULTIPLY) {
return new BinaryOperation(op,
new Constant(left.evaluate() * childOp.left.evaluate()),
childOp.right);
}
if (op == Op.ADD) {
return new BinaryOperation(op, new Constant(left.evaluate() + childOp.left.evaluate()), childOp.right);
}
if (op == Op.MULTIPLY) {
return new BinaryOperation(op, new Constant(left.evaluate() * childOp.left.evaluate()), childOp.right);
}
}

if (childOp.left.isConstant()) {
// Since our left side is non constant, but the left side of the child expression is,
// Since our left side is non-constant, but the left side of the child expression is,
// we push the constant up, to support further optimizations
return new BinaryOperation(op, childOp.left, new BinaryOperation(op, left, childOp.right));
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/parsii/eval/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
* Represents a constant numeric expression.
*/
public class Constant implements Expression {
private static final long serialVersionUID = 7461494011371773146L;

private double value;
private final double value;

/**
* Used as dummy expression by the parser if an error occurs while parsing.
*/
public static final Constant EMPTY = new Constant(Double.NaN);

/**
* Creates a new constant for the given value.
* @param value the value to represent as constant
*/
public Constant(double value) {
this.value = value;
}
Expand Down
50 changes: 23 additions & 27 deletions src/main/java/parsii/eval/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
public class Parser {

private final Scope scope;
private List<ParseError> errors = new ArrayList<>();
private Tokenizer tokenizer;
private static Map<String, Function> functionTable;
private final List<ParseError> errors = new ArrayList<>();
private final Tokenizer tokenizer;
private static final Map<String, Function> functionTable;

/*
* Setup well known functions
Expand Down Expand Up @@ -255,11 +255,9 @@ protected Expression term() {
Expression right = term();
return reOrder(left, right, BinaryOperation.Op.SUBTRACT);
}
if (tokenizer.current().isNumber()) {
if (tokenizer.current().getContents().startsWith("-")) {
Expression right = term();
return reOrder(left, right, BinaryOperation.Op.ADD);
}
if (tokenizer.current().isNumber() && tokenizer.current().getContents().startsWith("-")) {
Expression right = term();
return reOrder(left, right, BinaryOperation.Op.ADD);
}

return left;
Expand Down Expand Up @@ -297,23 +295,21 @@ protected Expression product() {
* in natural order (from left to right).
*/
protected Expression reOrder(Expression left, Expression right, BinaryOperation.Op op) {
if (right instanceof BinaryOperation) {
BinaryOperation rightOp = (BinaryOperation) right;
if (!rightOp.isSealed() && rightOp.getOp().getPriority() == op.getPriority()) {
replaceLeft(rightOp, left, op);
return right;
}
if (right instanceof BinaryOperation rightOp
&& !rightOp.isSealed()
&& rightOp.getOp().getPriority() == op.getPriority()) {
replaceLeft(rightOp, left, op);
return right;
}
return new BinaryOperation(op, left, right);
}

protected void replaceLeft(BinaryOperation target, Expression newLeft, BinaryOperation.Op op) {
if (target.getLeft() instanceof BinaryOperation) {
BinaryOperation leftOp = (BinaryOperation) target.getLeft();
if (!leftOp.isSealed() && leftOp.getOp().getPriority() == op.getPriority()) {
replaceLeft(leftOp, newLeft, op);
return;
}
if (target.getLeft() instanceof BinaryOperation leftOp
&& !leftOp.isSealed()
&& leftOp.getOp().getPriority() == op.getPriority()) {
replaceLeft(leftOp, newLeft, op);
return;
}
target.setLeft(new BinaryOperation(op, newLeft, target.getLeft()));
}
Expand Down Expand Up @@ -405,23 +401,23 @@ private Expression literalAtom() {
if (tokenizer.current().isNumber()) {
double value = Double.parseDouble(tokenizer.consume().getContents());
if (tokenizer.current().is(Token.TokenType.ID)) {
String quantifier = tokenizer.current().getContents().intern();
if ("n" == quantifier) {
String quantifier = tokenizer.current().getContents();
if ("n".equals(quantifier)) {
value /= 1000000000d;
tokenizer.consume();
} else if ("u" == quantifier) {
} else if ("u".equals(quantifier)) {
value /= 1000000d;
tokenizer.consume();
} else if ("m" == quantifier) {
} else if ("m".equals(quantifier)) {
value /= 1000d;
tokenizer.consume();
} else if ("K" == quantifier || "k" == quantifier) {
} else if ("K".equals(quantifier) || "k".equals(quantifier)) {
value *= 1000d;
tokenizer.consume();
} else if ("M" == quantifier) {
} else if ("M".equals(quantifier)) {
value *= 1000000d;
tokenizer.consume();
} else if ("G" == quantifier) {
} else if ("G".equals(quantifier)) {
value *= 1000000000d;
tokenizer.consume();
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/parsii/tokenizer/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public boolean matches(TokenType type, String trigger) {
* @return <tt>true</tt> if this token was triggered by one of the given triggers, <tt>false</tt> otherwise
*/
public boolean wasTriggeredBy(String... triggers) {
return Stream.of(triggers).filter(Objects::nonNull).anyMatch(trigger -> Objects.equals(trigger, getTrigger()));
return Stream.of(triggers).filter(Objects::nonNull).anyMatch(aTrigger -> Objects.equals(aTrigger, getTrigger()));
}

/**
Expand Down
Loading

0 comments on commit 638894f

Please sign in to comment.