48 lines
2.0 KiB
Java
48 lines
2.0 KiB
Java
package io.gitlab.jfronny.muscript.ast.compare;
|
|
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
|
import io.gitlab.jfronny.muscript.ast.*;
|
|
import io.gitlab.jfronny.muscript.ast.literal.NumberLiteral;
|
|
import io.gitlab.jfronny.muscript.ast.math.*;
|
|
|
|
public class Less extends BoolExpr {
|
|
private final NumberExpr left;
|
|
private final NumberExpr right;
|
|
|
|
public Less(int chStart, int chEnd, NumberExpr left, NumberExpr right) {
|
|
super(chStart, chEnd);
|
|
this.left = left;
|
|
this.right = right;
|
|
}
|
|
|
|
@Override
|
|
public Boolean get(Scope 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(chStart, chEnd, litL.value < litR.value);
|
|
if (left instanceof Divide divide)
|
|
return new Less(chStart, chEnd, divide.dividend, new Multiply(divide.chStart, divide.chEnd, divide.divisor, right)).optimize();
|
|
if (left instanceof Invert invert)
|
|
return new Greater(chStart, chEnd, invert.inner, new Invert(right.chStart, right.chEnd, right)).optimize();
|
|
if (left instanceof Minus minus)
|
|
return new Less(chStart, chEnd, minus.minuend, new Plus(minus.chStart, minus.chEnd, 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 Less(chStart, chEnd, plus.augend, new Minus(plus.chStart, plus.chEnd, plus.addend, right)).optimize();
|
|
// Power is left out because it can't be transformed cleanly either
|
|
return new Less(chStart, chEnd, left, right);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof Less less && left.equals(less.left) && right.equals(less.right);
|
|
}
|
|
}
|