aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-05 18:39:11 +0100
committerMichael Williamson <mike@zwobble.org>2015-10-05 18:39:11 +0100
commit105d6a00835e62fd212ed10e21c89b742885b145 (patch)
treeac6b3a474a4ac948096d04ff3f266ae069520087 /src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java
parent1cfa806548d63fa13925e2116d579ff0b5dee7bc (diff)
Create separate class for generating primitive methods in Python
Diffstat (limited to 'src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java')
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java b/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java
new file mode 100644
index 0000000..129fb19
--- /dev/null
+++ b/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java
@@ -0,0 +1,56 @@
+package org.zwobble.couscous.backends.python;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import org.zwobble.couscous.backends.python.ast.PythonExpressionNode;
+import org.zwobble.couscous.values.StringValue;
+import org.zwobble.couscous.values.TypeReference;
+
+import com.google.common.collect.ImmutableMap;
+
+import static java.util.Arrays.asList;
+import static org.zwobble.couscous.backends.python.ast.PythonAttributeAccessNode.pythonAttributeAccess;
+import static org.zwobble.couscous.backends.python.ast.PythonCallNode.pythonCall;
+import static org.zwobble.couscous.backends.python.ast.PythonVariableReferenceNode.pythonVariableReference;
+
+public class PrimitiveMethods {
+ private static final Map<String, PrimitiveMethodGenerator> STRING_METHODS =
+ ImmutableMap.<String, PrimitiveMethodGenerator>builder()
+
+ .put("length", (receiver, arguments) ->
+ pythonCall(
+ pythonVariableReference("len"),
+ asList(receiver)))
+
+ .put("substring", (receiver, arguments) ->
+ pythonCall(
+ pythonAttributeAccess(
+ receiver,
+ "__getitem__"),
+ asList(pythonCall(
+ pythonVariableReference("slice"),
+ arguments))))
+
+ .build();
+
+ private static final Map<TypeReference, Map<String, PrimitiveMethodGenerator>> METHODS =
+ ImmutableMap.<TypeReference, Map<String, PrimitiveMethodGenerator>>builder()
+ .put(StringValue.REF, STRING_METHODS)
+ .build();
+
+ public static boolean isPrimitive(TypeReference type) {
+ return METHODS.containsKey(type);
+ }
+
+ public static Optional<PrimitiveMethodGenerator> getPrimitiveMethod(TypeReference type, String methodName) {
+ return Optional.ofNullable(METHODS.get(type))
+ .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName)));
+ }
+
+ @FunctionalInterface
+ public interface PrimitiveMethodGenerator {
+ PythonExpressionNode generate(PythonExpressionNode receiver, List<PythonExpressionNode> arguments);
+ }
+}