java-commons/muscript/src/main/java/io/gitlab/jfronny/muscript/ast/conditional/UnresolvedConditional.java

107 lines
4.0 KiB
Java

package io.gitlab.jfronny.muscript.ast.conditional;
import io.gitlab.jfronny.muscript.ast.bool.Not;
import io.gitlab.jfronny.muscript.ast.dynamic.ExprGroup;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.muscript.data.dynamic.DNull;
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.ast.*;
import io.gitlab.jfronny.muscript.ast.literal.BoolLiteral;
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;
public class UnresolvedConditional extends DynamicExpr {
private final BoolExpr condition;
private final Expr<?> trueExpr;
private final Expr<?> falseExpr;
public UnresolvedConditional(CodeLocation location, BoolExpr condition, Expr<?> trueExpr, Expr<?> falseExpr) {
super(Order.Conditional, location);
this.condition = condition;
this.trueExpr = trueExpr;
this.falseExpr = falseExpr;
}
@Override
public Type getResultType() {
return trueExpr.getResultType();
}
@Override
public DynamicExpr optimize() {
BoolExpr condition = this.condition.optimize();
Expr<?> trueExpr = this.trueExpr.optimize();
Expr<?> falseExpr = this.falseExpr.optimize();
if (condition instanceof BoolLiteral literal) return literal.value ? trueExpr.asDynamicExpr() : falseExpr.asDynamicExpr();
if (trueExpr.equals(falseExpr)) return trueExpr.asDynamicExpr();
if (condition instanceof Not not) return new UnresolvedConditional(location, not.inner, falseExpr, trueExpr);
return new UnresolvedConditional(location, condition, trueExpr, falseExpr);
}
@Override
public void decompile(ExprWriter writer) throws IOException {
parenthesize(condition, writer, true);
writer.append(" ? ");
trueExpr.decompile(writer);
writer.append(" : ");
falseExpr.decompile(writer);
}
@Override
public Dynamic get(Scope dataRoot) {
// unresolved conditionals may exist as root elements in scripts/closures
// thus, this needs to be handled, but the result will always be null
if (condition.get(dataRoot)) trueExpr.get(dataRoot);
else falseExpr.get(dataRoot);
return new DNull();
}
@Override
public DynamicExpr asDynamicExpr() {
return new DynamicConditional(location, condition, trueExpr.asDynamicExpr(), falseExpr.asDynamicExpr());
}
@Override
public Stream<Expr<?>> extractSideEffects() {
return extractSideEffects(location, condition, trueExpr, falseExpr);
}
public static Stream<Expr<?>> extractSideEffects(CodeLocation location, BoolExpr condition, Expr<?> trueExpr, Expr<?> falseExpr) {
List<Expr<?>> trueSE = trueExpr.extractSideEffects().toList();
List<Expr<?>> falseSE = falseExpr.extractSideEffects().toList();
if (trueSE.isEmpty() && falseSE.isEmpty()) return condition.extractSideEffects();
return Stream.of(new UnresolvedConditional(
location,
condition,
ExprGroup.of(trueExpr.location, trueSE, false),
ExprGroup.of(falseExpr.location, falseSE, false)
));
}
@Override
public BoolExpr asBoolExpr() {
return new BoolConditional(location, condition, trueExpr.asBoolExpr(), falseExpr.asBoolExpr());
}
@Override
public StringExpr asStringExpr() {
return new StringConditional(location, condition, trueExpr.asStringExpr(), falseExpr.asStringExpr());
}
@Override
public NumberExpr asNumberExpr() {
return new NumberConditional(location, condition, trueExpr.asNumberExpr(), falseExpr.asNumberExpr());
}
@Override
public boolean equals(Object obj) {
return obj instanceof UnresolvedConditional conditional
&& condition.equals(conditional.condition)
&& trueExpr.equals(conditional.trueExpr)
&& falseExpr.equals(conditional.falseExpr);
}
}