aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java37
1 files changed, 28 insertions, 9 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 690375d..023d257 100644
--- a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
+++ b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
@@ -156,20 +156,39 @@ public class PythonCodeGenerator {
@Override
public PythonExpressionNode visit(MethodCallNode methodCall) {
- val arguments = methodCall.getArguments().stream()
- .map(PythonCodeGenerator::generateExpression)
- .collect(Collectors.toList());
- return pythonCall(
- pythonAttributeAccess(
- generateExpression(methodCall.getReceiver()),
- methodCall.getMethodName()),
- arguments);
+ if (isPrimitive(methodCall.getReceiver())) {
+ val isStringLength = methodCall.getReceiver().getType().equals(StringValue.REF) &&
+ methodCall.getMethodName() == "length";
+ if (isStringLength) {
+ return pythonCall(
+ pythonVariableReference("len"),
+ asList(generateExpression(methodCall.getReceiver())));
+ } else {
+ throw new UnsupportedOperationException();
+ }
+ } else {
+ val arguments = methodCall.getArguments().stream()
+ .map(PythonCodeGenerator::generateExpression)
+ .collect(Collectors.toList());
+ return pythonCall(
+ pythonAttributeAccess(
+ generateExpression(methodCall.getReceiver()),
+ methodCall.getMethodName()),
+ arguments);
+ }
}
@Override
public PythonExpressionNode visit(StaticMethodCallNode staticMethodCall) {
throw new UnsupportedOperationException();
}
-
+ }
+
+ private static boolean isPrimitive(ExpressionNode value) {
+ val type = value.getType();
+ return type.equals(IntegerValue.REF) ||
+ type.equals(StringValue.REF) ||
+ type.equals(BooleanValue.REF) ||
+ type.equals(UnitValue.REF);
}
}