From ad4903f04c757779b2e9a875298ef8418cb140b4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 31 Oct 2015 16:07:19 +0000 Subject: Support equality on reference types --- .../couscous/backends/python/PrimitiveMethods.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java') diff --git a/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java b/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java index c6aa62d..6605fc9 100644 --- a/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java +++ b/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java @@ -9,6 +9,7 @@ import org.zwobble.couscous.backends.python.ast.PythonExpressionNode; import org.zwobble.couscous.backends.python.ast.PythonNotNode; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.IntegerValue; +import org.zwobble.couscous.values.InternalCouscousValue; import org.zwobble.couscous.values.StringValue; import com.google.common.collect.ImmutableMap; @@ -16,6 +17,7 @@ import com.google.common.collect.ImmutableMap.Builder; import static java.util.Arrays.asList; import static org.zwobble.couscous.backends.python.ast.PythonAttributeAccessNode.pythonAttributeAccess; +import static org.zwobble.couscous.backends.python.ast.PythonBinaryOperation.pythonIs; import static org.zwobble.couscous.backends.python.ast.PythonCallNode.pythonCall; import static org.zwobble.couscous.backends.python.ast.PythonGetSliceNode.pythonGetSlice; import static org.zwobble.couscous.backends.python.ast.PythonVariableReferenceNode.pythonVariableReference; @@ -90,6 +92,18 @@ public class PrimitiveMethods { .put(IntegerValue.REF, INT_METHODS) .build(); + private static final Map INTERNAL_METHODS = + ImmutableMap.builder() + + .put("same", arguments -> pythonIs(arguments.get(0), arguments.get(1))) + + .build(); + + private static final Map> STATIC_METHODS = + ImmutableMap.>builder() + .put(InternalCouscousValue.REF, INTERNAL_METHODS) + .build(); + public static boolean isPrimitive(TypeName type) { return METHODS.containsKey(type); } @@ -103,4 +117,14 @@ public class PrimitiveMethods { public interface PrimitiveMethodGenerator { PythonExpressionNode generate(PythonExpressionNode receiver, List arguments); } + + public static Optional getPrimitiveStaticMethod(TypeName type, String methodName) { + return Optional.ofNullable(STATIC_METHODS.get(type)) + .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName))); + } + + @FunctionalInterface + public interface PrimitiveStaticMethodGenerator { + PythonExpressionNode generate(List arguments); + } } -- cgit v1.2.3