aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-01-08 23:36:56 +0000
committerMichael Williamson <mike@zwobble.org>2016-01-08 23:36:56 +0000
commitb92a5cb3ef79ff845dbebc0e6eedafe9e6c41f7c (patch)
treeb848794550497a5adcf433a92cff2a8e75e613be /src/main
parent216084fc0b73882f6fdd38b90e587726c1b33586 (diff)
Remove boxInt from Python backend
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java38
-rw-r--r--src/main/resources/python/runtime/_couscous.py3
2 files changed, 22 insertions, 19 deletions
diff --git a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
index 96fc544..32910f3 100644
--- a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
+++ b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
@@ -23,7 +23,6 @@ import java.util.stream.Stream;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Iterators.singletonIterator;
-import static java.util.Arrays.asList;
import static org.zwobble.couscous.backends.python.ast.PythonAssignmentNode.pythonAssignment;
import static org.zwobble.couscous.backends.python.ast.PythonAttributeAccessNode.pythonAttributeAccess;
import static org.zwobble.couscous.backends.python.ast.PythonBooleanLiteralNode.pythonBooleanLiteral;
@@ -101,6 +100,15 @@ public class PythonCodeGenerator {
public Stream<TypeName> visit(ConstructorCallNode call) {
return Stream.of(call.getType());
}
+
+ @Override
+ public Stream<TypeName> visit(TypeCoercionNode typeCoercion) {
+ if (isIntegerBox(typeCoercion)) {
+ return Stream.of(ObjectValues.BOXED_INT);
+ } else {
+ return Stream.empty();
+ }
+ }
}))
.collect(Collectors.toSet());
}
@@ -270,30 +278,28 @@ public class PythonCodeGenerator {
public PythonExpressionNode visit(TypeCoercionNode typeCoercion) {
PythonExpressionNode value = generateExpression(typeCoercion.getExpression());
if (isIntegerBox(typeCoercion)) {
- // TODO: replace with direct access to java.lang.Integer
- return internalMethod("boxInt", list(value));
+ // TODO: perform this replacement at the start so that we don't split the import handling and actual expression
+ return visit(ConstructorCallNode.constructorCall(
+ ObjectValues.BOXED_INT,
+ list(typeCoercion.getExpression())));
} else if (isIntegerUnbox(typeCoercion)) {
return pythonAttributeAccess(value, "_value");
} else {
return value;
}
}
+ }
- private boolean isIntegerBox(TypeCoercionNode typeCoercion) {
- return isInteger(typeCoercion.getExpression().getType()) && !isInteger(typeCoercion.getType());
- }
-
- private boolean isIntegerUnbox(TypeCoercionNode typeCoercion) {
- return !isInteger(typeCoercion.getExpression().getType()) && isInteger(typeCoercion.getType());
- }
+ private static boolean isIntegerBox(TypeCoercionNode typeCoercion) {
+ return isInteger(typeCoercion.getExpression().getType()) && !isInteger(typeCoercion.getType());
+ }
- private boolean isInteger(TypeName type) {
- return type.equals(IntegerValue.REF) || type.equals(ObjectValues.BOXED_INT);
- }
+ private static boolean isIntegerUnbox(TypeCoercionNode typeCoercion) {
+ return !isInteger(typeCoercion.getExpression().getType()) && isInteger(typeCoercion.getType());
+ }
- private PythonExpressionNode internalMethod(String name, List<PythonExpressionNode> arguments) {
- return pythonCall(pythonAttributeAccess(pythonVariableReference("_couscous"), name), arguments);
- }
+ private static boolean isInteger(TypeName type) {
+ return type.equals(IntegerValue.REF) || type.equals(ObjectValues.BOXED_INT);
}
public static String toName(MethodSignature signature) {
diff --git a/src/main/resources/python/runtime/_couscous.py b/src/main/resources/python/runtime/_couscous.py
index b8ab6fd..dcbd394 100644
--- a/src/main/resources/python/runtime/_couscous.py
+++ b/src/main/resources/python/runtime/_couscous.py
@@ -7,6 +7,3 @@ def _div_round_to_zero(a, b):
def _mod_round_to_zero(a, b):
return -(-a % b) if (a < 0) ^ (b < 0) else a % b
-
-def boxInt(value):
- return Integer(value) \ No newline at end of file