2022-06-07 13:36:48 +02:00
|
|
|
package io.gitlab.jfronny.muscript.compiler.expr.string;
|
|
|
|
|
2022-06-29 15:41:44 +02:00
|
|
|
import io.gitlab.jfronny.commons.StringFormatter;
|
|
|
|
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
|
2022-06-29 17:46:29 +02:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.expr.*;
|
|
|
|
import io.gitlab.jfronny.muscript.compiler.expr.common.literal.*;
|
2022-06-07 13:36:48 +02:00
|
|
|
|
|
|
|
public class StringCoerce extends StringExpr {
|
|
|
|
private final Expr<?> inner;
|
|
|
|
|
2023-01-20 17:47:41 +01:00
|
|
|
public StringCoerce(int chStart, int chEnd, Expr<?> inner) {
|
|
|
|
super(chStart, chEnd);
|
2022-06-07 13:36:48 +02:00
|
|
|
this.inner = inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-13 13:31:54 +02:00
|
|
|
public String get(Dynamic<?> dataRoot) {
|
|
|
|
return StringFormatter.toString(inner.get(dataRoot));
|
2022-06-07 13:36:48 +02:00
|
|
|
}
|
2022-06-29 17:38:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public StringExpr optimize() {
|
|
|
|
Expr<?> inner = this.inner.optimize();
|
2023-01-20 17:47:41 +01:00
|
|
|
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));
|
2022-06-29 17:38:05 +02:00
|
|
|
if (inner instanceof StringExpr expr) return expr;
|
2023-01-20 17:47:41 +01:00
|
|
|
return new StringCoerce(chStart, chEnd, inner);
|
2022-06-29 17:38:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof StringCoerce coerce && inner.equals(coerce.inner);
|
|
|
|
}
|
2022-06-07 13:36:48 +02:00
|
|
|
}
|