56 lines
2.1 KiB
Java
56 lines
2.1 KiB
Java
package io.gitlab.jfronny.muscript.ast.dynamic;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.*;
|
|
import 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.*;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.additional.DFinal;
|
|
|
|
import java.io.IOException;
|
|
|
|
public class DynamicCoerce extends DynamicExpr {
|
|
public final Expr<?> inner;
|
|
|
|
public DynamicCoerce(int chStart, int chEnd, Expr<?> inner) {
|
|
super(inner.order, chStart, chEnd);
|
|
this.inner = inner;
|
|
if (!(inner instanceof DynamicExpr)
|
|
&& !(inner instanceof BoolExpr)
|
|
&& !(inner instanceof StringExpr)
|
|
&& !(inner instanceof NumberExpr)
|
|
&& !(inner instanceof NullLiteral)) {
|
|
throw new IllegalArgumentException("A DynamicCoerce can only be created with a well-defined expression type");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Dynamic<?> get(Scope dataRoot) {
|
|
if (inner instanceof DynamicExpr e) return e.get(dataRoot);
|
|
if (inner instanceof BoolExpr e) return DFinal.of(e.get(dataRoot));
|
|
if (inner instanceof StringExpr e) return DFinal.of(e.get(dataRoot));
|
|
if (inner instanceof NumberExpr e) return DFinal.of(e.get(dataRoot));
|
|
if (inner instanceof NullLiteral) return new DNull();
|
|
throw new IllegalStateException("The inner expression of DynamicCoerce should never be of undefined type");
|
|
}
|
|
|
|
@Override
|
|
public DynamicExpr optimize() {
|
|
Expr<?> inner = this.inner.optimize();
|
|
if (inner instanceof BoolUnpack unpack) return unpack.inner;
|
|
if (inner instanceof NumberUnpack unpack) return unpack.inner;
|
|
if (inner instanceof StringUnpack unpack) return unpack.inner;
|
|
return new DynamicCoerce(chStart, chEnd, inner);
|
|
}
|
|
|
|
@Override
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
inner.decompile(writer);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof DynamicCoerce coerce && inner.equals(coerce.inner);
|
|
}
|
|
}
|