[muscript] Enhance StringCoerce optimization

This commit is contained in:
Johannes Frohnmeyer 2022-06-29 17:46:29 +02:00
parent f84bdd2a02
commit 87f5751fa1
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 7 additions and 3 deletions

View File

@ -4,7 +4,7 @@ import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.DynamicExpr;
public final class DynamicLiteral<T> extends DynamicExpr {
private final Dynamic<T> value;
public final Dynamic<T> value;
public DynamicLiteral(int character, Dynamic<T> value) {
super(character);

View File

@ -2,8 +2,8 @@ package io.gitlab.jfronny.muscript.compiler.expr.string;
import io.gitlab.jfronny.commons.StringFormatter;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.Expr;
import io.gitlab.jfronny.muscript.compiler.expr.StringExpr;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.compiler.expr.common.literal.*;
public class StringCoerce extends StringExpr {
private final Expr<?> inner;
@ -21,6 +21,10 @@ public class StringCoerce extends StringExpr {
@Override
public StringExpr optimize() {
Expr<?> inner = this.inner.optimize();
if (inner instanceof NullLiteral) return Expr.literal(character, "null");
if (inner instanceof BoolLiteral expr) return Expr.literal(character, StringFormatter.toString(expr.value));
if (inner instanceof DynamicLiteral<?> expr) return Expr.literal(character, StringFormatter.toString(expr.value));
if (inner instanceof NumberLiteral expr) return Expr.literal(character, StringFormatter.toString(expr.value));
if (inner instanceof StringExpr expr) return expr;
return new StringCoerce(character, inner);
}