]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.scl.compiler.constants;\r
2 \r
3 import org.cojen.classfile.TypeDesc;\r
4 import org.simantics.scl.compiler.internal.codegen.references.Val;\r
5 import org.simantics.scl.compiler.internal.codegen.utils.Constants;\r
6 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;\r
7 import org.simantics.scl.compiler.types.TVar;\r
8 import org.simantics.scl.compiler.types.Type;\r
9 import org.simantics.scl.compiler.types.Types;\r
10 \r
11 public class StringInterpolation extends FunctionValue {\r
12 \r
13     public static final TypeDesc STRING_BUILDER = TypeDesc.forClass(StringBuilder.class);\r
14     \r
15     private final String[] textParts;\r
16     \r
17     public StringInterpolation(Type[] parameterTypes, String[] textParts) {\r
18         super(TVar.EMPTY_ARRAY, Types.NO_EFFECTS, Types.STRING, parameterTypes);\r
19         this.textParts = textParts;\r
20     }\r
21     \r
22     public StringInterpolation(String[] textParts) {\r
23         this(stringTypeArray(textParts.length-1), textParts);\r
24     }\r
25 \r
26     private static Type[] stringTypeArray(int length) {\r
27         Type[] result = new Type[length];\r
28         for(int i=0;i<length;++i)\r
29             result[i] = Types.STRING;\r
30         return result;\r
31     }\r
32 \r
33     @Override\r
34     public Type applyExact(MethodBuilder mb, Val[] parameters) {\r
35         mb.newObject(STRING_BUILDER);\r
36         mb.dup();\r
37         mb.invokeConstructor(STRING_BUILDER, Constants.EMPTY_TYPEDESC_ARRAY);\r
38         \r
39         for(int i=0;i<parameters.length;++i) {\r
40             String textPart = textParts[i]; \r
41             if(!textPart.isEmpty()) {\r
42                 mb.loadConstant(textPart);\r
43                 mb.invokeVirtual(STRING_BUILDER, "append", STRING_BUILDER, new TypeDesc[] {TypeDesc.STRING});\r
44             }\r
45             \r
46             mb.push(parameters[i], parameterTypes[i]);\r
47             mb.invokeVirtual(STRING_BUILDER, "append", STRING_BUILDER,\r
48                     new TypeDesc[] {mb.getJavaTypeTranslator().toTypeDesc(parameterTypes[i])});\r
49         }\r
50         \r
51         {\r
52             String textPart = textParts[parameters.length]; \r
53             if(!textPart.isEmpty()) {\r
54                 mb.loadConstant(textPart);\r
55                 mb.invokeVirtual(STRING_BUILDER, "append", STRING_BUILDER, new TypeDesc[] {TypeDesc.STRING});\r
56             }\r
57         }\r
58         \r
59         mb.invokeVirtual(STRING_BUILDER, "toString", TypeDesc.STRING, Constants.EMPTY_TYPEDESC_ARRAY);\r
60         \r
61         return Types.STRING;\r
62     }\r
63 \r
64 }\r