aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/backends/csharp/primitives/CsharpPrimitiveMethods.java
blob: 3029a60aa2eb45e082b68a072e1c2914f6e434e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package org.zwobble.couscous.backends.csharp.primitives;

import com.google.common.collect.ImmutableMap;
import org.zwobble.couscous.ast.ExpressionNode;
import org.zwobble.couscous.ast.MethodCallNode;
import org.zwobble.couscous.ast.Operator;
import org.zwobble.couscous.ast.Receiver;
import org.zwobble.couscous.ast.visitors.NodeTransformer;
import org.zwobble.couscous.types.ScalarType;
import org.zwobble.couscous.types.Types;
import org.zwobble.couscous.values.InternalCouscousValue;

import java.util.Map;
import java.util.Optional;

import static org.zwobble.couscous.ast.FieldAccessNode.fieldAccess;
import static org.zwobble.couscous.ast.MethodCallNode.methodCall;
import static org.zwobble.couscous.ast.MethodCallNode.staticMethodCall;
import static org.zwobble.couscous.ast.OperationNode.operation;
import static org.zwobble.couscous.ast.Operations.boxInt;
import static org.zwobble.couscous.ast.Operations.integerSubtract;
import static org.zwobble.couscous.types.Types.erasure;
import static org.zwobble.couscous.util.Casts.tryCast;
import static org.zwobble.couscous.util.ExtraLists.list;

public class CsharpPrimitiveMethods {
    public static final NodeTransformer TRANSFORMER = NodeTransformer.builder()
        .transformExpression(CsharpPrimitiveMethods::transformExpression)
        .build();


    private static Optional<ExpressionNode> transformExpression(ExpressionNode expression) {
        return tryCast(MethodCallNode.class, expression)
            .flatMap(CsharpPrimitiveMethods::transformMethodCall);
    }

    private static Optional<ExpressionNode> transformMethodCall(MethodCallNode call) {
        return call.getReceiver().accept(new Receiver.Mapper<Optional<ExpressionNode>>() {
            @Override
            public Optional<ExpressionNode> visit(ExpressionNode receiver) {
                return CsharpPrimitiveMethods.getPrimitiveMethod(erasure(receiver.getType()), call.getMethodName())
                    .map(method -> new PrimitiveInstanceMethodCall(method, receiver, call.getArguments()));
            }

            @Override
            public Optional<ExpressionNode> visit(ScalarType receiver) {
                return CsharpPrimitiveMethods.getPrimitiveStaticMethod(receiver, call.getMethodName())
                    .map(method -> new PrimitiveStaticMethodCall(method, call.getArguments()));
            }
        });
    }

    private static final Map<String, PrimitiveInstanceMethod> BOOLEAN_METHODS;

    static {
        ImmutableMap.Builder<String, PrimitiveInstanceMethod> methods = ImmutableMap.builder();
        BOOLEAN_METHODS = methods.build();
    }

    private static final Map<String, PrimitiveInstanceMethod> BOXED_INT_METHODS;

    static {
        ImmutableMap.Builder<String, PrimitiveInstanceMethod> methods = ImmutableMap.builder();
        methods.put("toString", new PrimitiveInstanceMethod(
            (receiver, arguments) -> methodCall(
                receiver,
                "ToString",
                list(),
                Types.STRING
            ),
            Types.STRING
        ));
        BOXED_INT_METHODS = methods.build();
    }

    private static final Map<String, PrimitiveInstanceMethod> STRING_METHODS;

    static {
        ImmutableMap.Builder<String, PrimitiveInstanceMethod> methods = ImmutableMap.builder();
        methods.put("length", new PrimitiveInstanceMethod(
            (receiver, arguments) -> fieldAccess(receiver, "Length", Types.INT),
            Types.INT
        ));
        methods.put("substring", new PrimitiveInstanceMethod(
            (receiver, arguments) -> {
                ExpressionNode startIndex = arguments.get(0);
                if (arguments.size() == 1) {
                    return methodCall(
                        receiver,
                        "Substring",
                        list(startIndex),
                        Types.STRING
                    );
                } else {
                    ExpressionNode endIndex = arguments.get(1);
                    ExpressionNode length = integerSubtract(endIndex, startIndex);
                    // TODO: handle length going beyond end-of-string
                    return methodCall(
                        receiver,
                        "Substring",
                        list(startIndex, length),
                        Types.STRING
                    );
                }
            },
            Types.STRING
        ));
        methods.put("toLowerCase", new PrimitiveInstanceMethod(
            (receiver, arguments) -> methodCall(
                receiver,
                "ToLower",
                list(),
                Types.STRING
            ),
            Types.STRING
        ));
        methods.put("equals", new PrimitiveInstanceMethod(
            (receiver, arguments) -> methodCall(
                receiver,
                "Equals",
                arguments,
                Types.STRING
            ),
            Types.STRING
        ));
        STRING_METHODS = methods.build();
    }

    private static final Map<ScalarType, Map<String, PrimitiveInstanceMethod>> METHODS =
        ImmutableMap.<ScalarType, Map<String, PrimitiveInstanceMethod>>builder()
            .put(Types.BOOLEAN, BOOLEAN_METHODS)
            .put(Types.STRING, STRING_METHODS)
            .put(Types.BOXED_INT, BOXED_INT_METHODS)
            .build();

    private static final Map<String, PrimitiveStaticMethod> STATIC_INT_METHODS =
        ImmutableMap.<String, PrimitiveStaticMethod>builder()

            .put("toString", new PrimitiveStaticMethod(
                arguments -> methodCall(
                    arguments.get(0),
                    "ToString",
                    list(),
                    Types.STRING
                ),
                Types.STRING
            ))

            .put("parseInt", new PrimitiveStaticMethod(
                arguments -> {
                    if (arguments.size() == 1 || arguments.size() == 2) {
                        return staticMethodCall(
                            ScalarType.topLevel("System.Convert"),
                            "ToInt32",
                            arguments,
                            ScalarType.topLevel("int")
                        );
                    } else {
                        throw new IllegalArgumentException();
                    }
                },
                Types.INT
            ))

            .put("valueOf", new PrimitiveStaticMethod(
                arguments -> boxInt(arguments.get(0)),
                Types.BOXED_INT
            ))

            .build();

    private static final Map<String, PrimitiveStaticMethod> STATIC_STRING_METHODS =
        ImmutableMap.<String, PrimitiveStaticMethod>builder()

            .put("join", new PrimitiveStaticMethod(
                arguments -> staticMethodCall(
                    ScalarType.topLevel("java.lang.String"),
                    "join",
                    arguments,
                    Types.STRING
                ),
                Types.STRING
            ))

            .build();

    private static final Map<String, PrimitiveStaticMethod> STATIC_INTERNAL_METHODS =
        ImmutableMap.<String, PrimitiveStaticMethod>builder()

            .put("same", new PrimitiveStaticMethod(
                arguments -> operation(Operator.EQUALS, arguments, Types.BOOLEAN),
                Types.BOOLEAN
            ))

            .build();

    private static final Map<ScalarType, Map<String, PrimitiveStaticMethod>> STATIC_METHODS =
        ImmutableMap.<ScalarType, Map<String, PrimitiveStaticMethod>>builder()
            .put(Types.BOXED_INT, STATIC_INT_METHODS)
            .put(Types.STRING, STATIC_STRING_METHODS)
            .put(InternalCouscousValue.REF, STATIC_INTERNAL_METHODS)
            .build();

    private static Optional<PrimitiveInstanceMethod> getPrimitiveMethod(ScalarType type, String methodName) {
        return Optional.ofNullable(METHODS.get(type))
            .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName)));
    }

    private static Optional<PrimitiveStaticMethod> getPrimitiveStaticMethod(ScalarType type, String methodName) {
        return Optional.ofNullable(STATIC_METHODS.get(type))
            .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName)));
    }

}