44 lines
2.1 KiB
Java
44 lines
2.1 KiB
Java
package io.gitlab.jfronny.muscript.compiler.expr.number.compare;
|
|
|
|
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
|
|
import io.gitlab.jfronny.muscript.compiler.expr.*;
|
|
import io.gitlab.jfronny.muscript.compiler.expr.bool.Not;
|
|
import io.gitlab.jfronny.muscript.compiler.expr.common.literal.NumberLiteral;
|
|
import io.gitlab.jfronny.muscript.compiler.expr.number.math.*;
|
|
|
|
public class Greater extends BoolExpr {
|
|
private final NumberExpr left;
|
|
private final NumberExpr right;
|
|
|
|
public Greater(int character, NumberExpr left, NumberExpr right) {
|
|
super(character);
|
|
this.left = left;
|
|
this.right = right;
|
|
}
|
|
|
|
@Override
|
|
public Boolean get(Dynamic<?> dataRoot) {
|
|
return left.get(dataRoot) > right.get(dataRoot);
|
|
}
|
|
|
|
@Override
|
|
public BoolExpr optimize() {
|
|
NumberExpr left = this.left.optimize();
|
|
NumberExpr right = this.right.optimize();
|
|
if (left instanceof NumberLiteral litL && right instanceof NumberLiteral litR) return Expr.literal(character, litL.value > litR.value);
|
|
if (left instanceof Divide divide) return new Greater(character, divide.dividend, new Multiply(divide.character, right, divide.divisor)).optimize();
|
|
if (left instanceof Invert invert) return new Less(character, invert.inner, new Invert(right.character, right)).optimize();
|
|
if (left instanceof Minus minus) return new Greater(character, minus.minuend, new Plus(minus.character, minus.subtrahend, right)).optimize();
|
|
// Modulo is left out because it is too complicated for this naive impl
|
|
// Multiply is left out since it would transform into a division and may be 0
|
|
if (left instanceof Plus plus) return new Greater(character, plus.augend, new Minus(plus.character, plus.addend, right)).optimize();
|
|
// Power is left out because it can't be transformed cleanly either
|
|
return new Greater(character, left, right);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof Greater greater && left.equals(greater.left) && right.equals(greater.right);
|
|
}
|
|
}
|