2023-01-20 18:52:57 +01:00
|
|
|
package io.gitlab.jfronny.muscript.ast.dynamic.unpack;
|
2022-06-07 13:36:48 +02:00
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
|
2023-01-20 21:05:04 +01:00
|
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
2023-01-20 18:52:57 +01:00
|
|
|
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;
|
2022-06-07 13:36:48 +02:00
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2022-06-29 15:41:44 +02:00
|
|
|
@CanThrow
|
|
|
|
@UncheckedDynamic
|
2022-06-07 13:36:48 +02:00
|
|
|
public class BoolUnpack extends BoolExpr {
|
2022-06-29 17:38:05 +02:00
|
|
|
public final DynamicExpr inner;
|
2022-06-07 13:36:48 +02:00
|
|
|
|
2023-01-20 17:47:41 +01:00
|
|
|
public BoolUnpack(int chStart, int chEnd, DynamicExpr inner) {
|
2023-03-12 15:28:44 +01:00
|
|
|
super(inner.order, chStart, chEnd);
|
2022-06-07 13:36:48 +02:00
|
|
|
this.inner = inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-01-20 21:05:04 +01:00
|
|
|
public Boolean get(Scope dataRoot) {
|
2022-06-13 13:31:54 +02:00
|
|
|
try {
|
|
|
|
return inner.get(dataRoot).asBool().getValue();
|
|
|
|
} catch (DynamicTypeConversionException e) {
|
2023-01-20 17:47:41 +01:00
|
|
|
throw e.locational(chStart, chEnd);
|
2022-06-13 13:31:54 +02:00
|
|
|
}
|
2022-06-07 13:36:48 +02:00
|
|
|
}
|
2022-06-29 17:38:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public BoolExpr optimize() {
|
|
|
|
DynamicExpr inner = this.inner.optimize();
|
|
|
|
if (inner instanceof DynamicCoerce coerce) return coerce.inner.asBoolExpr();
|
2023-01-20 17:47:41 +01:00
|
|
|
return new BoolUnpack(chStart, chEnd, inner);
|
2022-06-29 17:38:05 +02:00
|
|
|
}
|
|
|
|
|
2023-03-12 15:28:44 +01:00
|
|
|
@Override
|
|
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
|
|
inner.decompile(writer);
|
|
|
|
}
|
|
|
|
|
2022-06-29 17:38:05 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof BoolUnpack unpack && inner.equals(unpack.inner);
|
|
|
|
}
|
2022-06-07 13:36:48 +02:00
|
|
|
}
|