2022-06-07 13:36:48 +02:00
|
|
|
package io.gitlab.jfronny.muscript.compiler.expr.common.conditional;
|
|
|
|
|
2022-06-29 15:41:44 +02:00
|
|
|
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
|
2022-11-24 19:05:51 +01:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
|
|
|
|
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
|
2022-06-29 17:38:05 +02:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.expr.common.literal.BoolLiteral;
|
2022-06-07 13:36:48 +02:00
|
|
|
|
|
|
|
public class NumberConditional extends NumberExpr {
|
|
|
|
public final BoolExpr condition;
|
|
|
|
public final NumberExpr trueExpr;
|
|
|
|
public final NumberExpr falseExpr;
|
|
|
|
|
2022-06-13 13:31:54 +02:00
|
|
|
public NumberConditional(int character, BoolExpr condition, NumberExpr trueExpr, NumberExpr falseExpr) {
|
|
|
|
super(character);
|
2022-06-07 13:36:48 +02:00
|
|
|
this.condition = condition;
|
|
|
|
this.trueExpr = trueExpr;
|
|
|
|
this.falseExpr = falseExpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-13 13:31:54 +02:00
|
|
|
public Double get(Dynamic<?> dataRoot) {
|
|
|
|
return condition.get(dataRoot) ? trueExpr.get(dataRoot) : falseExpr.get(dataRoot);
|
2022-06-07 13:36:48 +02:00
|
|
|
}
|
2022-06-29 17:38:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberExpr optimize() {
|
|
|
|
BoolExpr condition = this.condition.optimize();
|
|
|
|
NumberExpr trueExpr = this.trueExpr.optimize();
|
|
|
|
NumberExpr falseExpr = this.falseExpr.optimize();
|
|
|
|
if (condition instanceof BoolLiteral literal) return literal.value ? trueExpr : falseExpr;
|
|
|
|
if (trueExpr.equals(falseExpr)) return trueExpr;
|
|
|
|
return new NumberConditional(character, condition, trueExpr, falseExpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof NumberConditional conditional
|
|
|
|
&& condition.equals(conditional.condition)
|
|
|
|
&& trueExpr.equals(conditional.trueExpr)
|
|
|
|
&& falseExpr.equals(conditional.falseExpr);
|
|
|
|
}
|
2022-06-07 13:36:48 +02:00
|
|
|
}
|