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.StringExpr;
|
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 StringConditional extends StringExpr {
|
|
|
|
public final BoolExpr condition;
|
|
|
|
public final StringExpr trueExpr;
|
|
|
|
public final StringExpr falseExpr;
|
|
|
|
|
2023-01-20 17:47:41 +01:00
|
|
|
public StringConditional(int chStart, int chEnd, BoolExpr condition, StringExpr trueExpr, StringExpr falseExpr) {
|
|
|
|
super(chStart, chEnd);
|
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 String 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 StringExpr optimize() {
|
|
|
|
BoolExpr condition = this.condition.optimize();
|
|
|
|
StringExpr trueExpr = this.trueExpr.optimize();
|
|
|
|
StringExpr falseExpr = this.falseExpr.optimize();
|
|
|
|
if (condition instanceof BoolLiteral literal) return literal.value ? trueExpr : falseExpr;
|
|
|
|
if (trueExpr.equals(falseExpr)) return trueExpr;
|
2023-01-20 17:47:41 +01:00
|
|
|
return new StringConditional(chStart, chEnd, condition, trueExpr, falseExpr);
|
2022-06-29 17:38:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof StringConditional conditional
|
|
|
|
&& condition.equals(conditional.condition)
|
|
|
|
&& trueExpr.equals(conditional.trueExpr)
|
|
|
|
&& falseExpr.equals(conditional.falseExpr);
|
|
|
|
}
|
2022-06-07 13:36:48 +02:00
|
|
|
}
|