]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/StringInterpolation.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / constants / StringInterpolation.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/StringInterpolation.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/StringInterpolation.java
new file mode 100644 (file)
index 0000000..55bba56
--- /dev/null
@@ -0,0 +1,64 @@
+package org.simantics.scl.compiler.constants;\r
+\r
+import org.cojen.classfile.TypeDesc;\r
+import org.simantics.scl.compiler.internal.codegen.references.Val;\r
+import org.simantics.scl.compiler.internal.codegen.utils.Constants;\r
+import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;\r
+import org.simantics.scl.compiler.types.TVar;\r
+import org.simantics.scl.compiler.types.Type;\r
+import org.simantics.scl.compiler.types.Types;\r
+\r
+public class StringInterpolation extends FunctionValue {\r
+\r
+    public static final TypeDesc STRING_BUILDER = TypeDesc.forClass(StringBuilder.class);\r
+    \r
+    private final String[] textParts;\r
+    \r
+    public StringInterpolation(Type[] parameterTypes, String[] textParts) {\r
+        super(TVar.EMPTY_ARRAY, Types.NO_EFFECTS, Types.STRING, parameterTypes);\r
+        this.textParts = textParts;\r
+    }\r
+    \r
+    public StringInterpolation(String[] textParts) {\r
+        this(stringTypeArray(textParts.length-1), textParts);\r
+    }\r
+\r
+    private static Type[] stringTypeArray(int length) {\r
+        Type[] result = new Type[length];\r
+        for(int i=0;i<length;++i)\r
+            result[i] = Types.STRING;\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public Type applyExact(MethodBuilder mb, Val[] parameters) {\r
+        mb.newObject(STRING_BUILDER);\r
+        mb.dup();\r
+        mb.invokeConstructor(STRING_BUILDER, Constants.EMPTY_TYPEDESC_ARRAY);\r
+        \r
+        for(int i=0;i<parameters.length;++i) {\r
+            String textPart = textParts[i]; \r
+            if(!textPart.isEmpty()) {\r
+                mb.loadConstant(textPart);\r
+                mb.invokeVirtual(STRING_BUILDER, "append", STRING_BUILDER, new TypeDesc[] {TypeDesc.STRING});\r
+            }\r
+            \r
+            mb.push(parameters[i], parameterTypes[i]);\r
+            mb.invokeVirtual(STRING_BUILDER, "append", STRING_BUILDER,\r
+                    new TypeDesc[] {mb.getJavaTypeTranslator().toTypeDesc(parameterTypes[i])});\r
+        }\r
+        \r
+        {\r
+            String textPart = textParts[parameters.length]; \r
+            if(!textPart.isEmpty()) {\r
+                mb.loadConstant(textPart);\r
+                mb.invokeVirtual(STRING_BUILDER, "append", STRING_BUILDER, new TypeDesc[] {TypeDesc.STRING});\r
+            }\r
+        }\r
+        \r
+        mb.invokeVirtual(STRING_BUILDER, "toString", TypeDesc.STRING, Constants.EMPTY_TYPEDESC_ARRAY);\r
+        \r
+        return Types.STRING;\r
+    }\r
+\r
+}\r