1 package org.simantics.scl.compiler.constants;
\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
11 public class StringInterpolation extends FunctionValue {
\r
13 public static final TypeDesc STRING_BUILDER = TypeDesc.forClass(StringBuilder.class);
\r
15 private final String[] textParts;
\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
22 public StringInterpolation(String[] textParts) {
\r
23 this(stringTypeArray(textParts.length-1), textParts);
\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
34 public Type applyExact(MethodBuilder mb, Val[] parameters) {
\r
35 mb.newObject(STRING_BUILDER);
\r
37 mb.invokeConstructor(STRING_BUILDER, Constants.EMPTY_TYPEDESC_ARRAY);
\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
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
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
59 mb.invokeVirtual(STRING_BUILDER, "toString", TypeDesc.STRING, Constants.EMPTY_TYPEDESC_ARRAY);
\r
61 return Types.STRING;
\r