50 lines
1.5 KiB
Java
50 lines
1.5 KiB
Java
package io.gitlab.jfronny.muscript.ast.dynamic.unpack;
|
|
|
|
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.DynamicTypeConversionException;
|
|
import io.gitlab.jfronny.muscript.ast.BoolExpr;
|
|
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
|
|
import io.gitlab.jfronny.muscript.annotations.CanThrow;
|
|
import io.gitlab.jfronny.muscript.annotations.UncheckedDynamic;
|
|
import io.gitlab.jfronny.muscript.ast.dynamic.DynamicCoerce;
|
|
|
|
import java.io.IOException;
|
|
|
|
@CanThrow
|
|
@UncheckedDynamic
|
|
public class BoolUnpack extends BoolExpr {
|
|
public final DynamicExpr inner;
|
|
|
|
public BoolUnpack(int chStart, int chEnd, DynamicExpr inner) {
|
|
super(inner.order, chStart, chEnd);
|
|
this.inner = inner;
|
|
}
|
|
|
|
@Override
|
|
public Boolean get(Scope dataRoot) {
|
|
try {
|
|
return inner.get(dataRoot).asBool().getValue();
|
|
} catch (DynamicTypeConversionException e) {
|
|
throw e.locational(chStart, chEnd);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public BoolExpr optimize() {
|
|
DynamicExpr inner = this.inner.optimize();
|
|
if (inner instanceof DynamicCoerce coerce) return coerce.inner.asBoolExpr();
|
|
return new BoolUnpack(chStart, chEnd, inner);
|
|
}
|
|
|
|
@Override
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
inner.decompile(writer);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof BoolUnpack unpack && inner.equals(unpack.inner);
|
|
}
|
|
}
|