diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-10-05 18:27:35 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-10-05 18:27:35 +0100 |
| commit | 1cfa806548d63fa13925e2116d579ff0b5dee7bc (patch) | |
| tree | 1a62937d502a86bf9c4f8fe20b0397a46969e740 /src/main/java/org/zwobble | |
| parent | bd717883dd37d4e3578c1e46c3dba484b6ad4f79 (diff) | |
Add support for String::substring to Python backend
Diffstat (limited to 'src/main/java/org/zwobble')
| -rw-r--r-- | src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java | 22 |
1 files changed, 19 insertions, 3 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 023d257..067e826 100644 --- a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java +++ b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.backends.python; +import java.util.List; import java.util.stream.Collectors; import org.zwobble.couscous.ast.AssignmentNode; @@ -159,17 +160,25 @@ public class PythonCodeGenerator { if (isPrimitive(methodCall.getReceiver())) { val isStringLength = methodCall.getReceiver().getType().equals(StringValue.REF) && methodCall.getMethodName() == "length"; + val isSubstring = methodCall.getReceiver().getType().equals(StringValue.REF) && + methodCall.getMethodName() == "substring"; if (isStringLength) { return pythonCall( pythonVariableReference("len"), asList(generateExpression(methodCall.getReceiver()))); + } else if (isSubstring) { + return pythonCall( + pythonAttributeAccess( + generateExpression(methodCall.getReceiver()), + "__getitem__"), + asList(pythonCall( + pythonVariableReference("slice"), + generateArguments(methodCall)))); } else { throw new UnsupportedOperationException(); } } else { - val arguments = methodCall.getArguments().stream() - .map(PythonCodeGenerator::generateExpression) - .collect(Collectors.toList()); + val arguments = generateArguments(methodCall); return pythonCall( pythonAttributeAccess( generateExpression(methodCall.getReceiver()), @@ -178,6 +187,13 @@ public class PythonCodeGenerator { } } + private List<PythonExpressionNode> generateArguments( + MethodCallNode methodCall) { + return methodCall.getArguments().stream() + .map(PythonCodeGenerator::generateExpression) + .collect(Collectors.toList()); + } + @Override public PythonExpressionNode visit(StaticMethodCallNode staticMethodCall) { throw new UnsupportedOperationException(); |
