45 lines
1.6 KiB
Java
45 lines
1.6 KiB
Java
package io.gitlab.jfronny.muscript.ast.string;
|
|
|
|
import io.gitlab.jfronny.commons.StringFormatter;
|
|
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
|
import io.gitlab.jfronny.muscript.ast.*;
|
|
import io.gitlab.jfronny.muscript.ast.literal.*;
|
|
|
|
import java.io.IOException;
|
|
|
|
public class StringCoerce extends StringExpr {
|
|
private final Expr<?> inner;
|
|
|
|
public StringCoerce(int chStart, int chEnd, Expr<?> inner) {
|
|
super(inner.order, chStart, chEnd);
|
|
this.inner = inner;
|
|
}
|
|
|
|
@Override
|
|
public String get(Scope dataRoot) {
|
|
return StringFormatter.toString(inner.get(dataRoot));
|
|
}
|
|
|
|
@Override
|
|
public StringExpr optimize() {
|
|
Expr<?> inner = this.inner.optimize();
|
|
if (inner instanceof NullLiteral) return Expr.literal(chStart, chEnd, "null");
|
|
if (inner instanceof BoolLiteral expr) return Expr.literal(chStart, chEnd, StringFormatter.toString(expr.value));
|
|
if (inner instanceof DynamicLiteral<?> expr) return Expr.literal(chStart, chEnd, StringFormatter.toString(expr.value));
|
|
if (inner instanceof NumberLiteral expr) return Expr.literal(chStart, chEnd, StringFormatter.toString(expr.value));
|
|
if (inner instanceof StringExpr expr) return expr;
|
|
return new StringCoerce(chStart, chEnd, inner);
|
|
}
|
|
|
|
@Override
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
inner.decompile(writer);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof StringCoerce coerce && inner.equals(coerce.inner);
|
|
}
|
|
}
|