From 87f5751fa189db6696e722f44e82cfea95b199f4 Mon Sep 17 00:00:00 2001 From: JFronny Date: Wed, 29 Jun 2022 17:46:29 +0200 Subject: [PATCH] [muscript] Enhance StringCoerce optimization --- .../compiler/expr/common/literal/DynamicLiteral.java | 2 +- .../muscript/compiler/expr/string/StringCoerce.java | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/common/literal/DynamicLiteral.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/common/literal/DynamicLiteral.java index ec3ca2d..5e41c32 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/common/literal/DynamicLiteral.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/common/literal/DynamicLiteral.java @@ -4,7 +4,7 @@ import io.gitlab.jfronny.commons.data.dynamic.Dynamic; import io.gitlab.jfronny.muscript.compiler.expr.DynamicExpr; public final class DynamicLiteral extends DynamicExpr { - private final Dynamic value; + public final Dynamic value; public DynamicLiteral(int character, Dynamic value) { super(character); diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/string/StringCoerce.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/string/StringCoerce.java index a8e16a1..f008b43 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/string/StringCoerce.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/compiler/expr/string/StringCoerce.java @@ -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); }