46 lines
1.8 KiB
Java
46 lines
1.8 KiB
Java
package io.gitlab.jfronny.muscript.compiler.expr.dynamic;
|
|
|
|
import io.gitlab.jfronny.commons.data.dynamic.*;
|
|
import io.gitlab.jfronny.muscript.compiler.expr.*;
|
|
import io.gitlab.jfronny.muscript.compiler.expr.dynamic.unpack.*;
|
|
|
|
public class DynamicCoerce extends DynamicExpr {
|
|
public final Expr<?> inner;
|
|
|
|
public DynamicCoerce(int character, Expr<?> inner) {
|
|
super(character);
|
|
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(Dynamic<?> 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(character, inner);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof DynamicCoerce coerce && inner.equals(coerce.inner);
|
|
}
|
|
}
|