From 2934f5546ababe312aec007ce0ef1f3764a680a4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 12 Jun 2024 15:16:45 +0100 Subject: Fix parsing of integers with non-decimal bases in C# backend --- .../csharp/primitives/CsharpPrimitiveMethods.java | 23 ++++++++++----- .../backends/python/PythonPrimitiveMethods.java | 34 +++++++++++----------- 2 files changed, 33 insertions(+), 24 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/zwobble/couscous/backends/csharp/primitives/CsharpPrimitiveMethods.java b/src/main/java/org/zwobble/couscous/backends/csharp/primitives/CsharpPrimitiveMethods.java index 43ca370..3029a60 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/primitives/CsharpPrimitiveMethods.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/primitives/CsharpPrimitiveMethods.java @@ -1,7 +1,10 @@ package org.zwobble.couscous.backends.csharp.primitives; import com.google.common.collect.ImmutableMap; -import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.ExpressionNode; +import org.zwobble.couscous.ast.MethodCallNode; +import org.zwobble.couscous.ast.Operator; +import org.zwobble.couscous.ast.Receiver; import org.zwobble.couscous.ast.visitors.NodeTransformer; import org.zwobble.couscous.types.ScalarType; import org.zwobble.couscous.types.Types; @@ -144,12 +147,18 @@ public class CsharpPrimitiveMethods { )) .put("parseInt", new PrimitiveStaticMethod( - arguments -> staticMethodCall( - ScalarType.topLevel("int"), - "Parse", - list(arguments.get(0)), - ScalarType.topLevel("int") - ), + arguments -> { + if (arguments.size() == 1 || arguments.size() == 2) { + return staticMethodCall( + ScalarType.topLevel("System.Convert"), + "ToInt32", + arguments, + ScalarType.topLevel("int") + ); + } else { + throw new IllegalArgumentException(); + } + }, Types.INT )) diff --git a/src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java b/src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java index 602fe2f..7895814 100644 --- a/src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java +++ b/src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java @@ -25,12 +25,12 @@ import static org.zwobble.couscous.util.ExtraLists.list; public class PythonPrimitiveMethods { private static final Map STRING_METHODS = ImmutableMap.builder() - + .put("length", (receiver, arguments) -> pythonCall( pythonVariableReference("len"), list(receiver))) - + .put("substring", (receiver, arguments) -> pythonGetSlice(receiver, arguments)) @@ -44,7 +44,7 @@ public class PythonPrimitiveMethods { PythonBinaryOperation.pythonEqual(receiver, arguments.get(0))) .build(); - + private static final Map BOOLEAN_METHODS; static { @@ -58,15 +58,15 @@ public class PythonPrimitiveMethods { BOOLEAN_METHODS = methods.build(); } - + private static final Map INT_METHODS; - + static { ImmutableMap.Builder methods = ImmutableMap.builder(); methods.put("toString", (receiver, arguments) -> pythonCall(pythonVariableReference("str"), list(receiver))); - + addOperation(methods, Operator.ADD, PythonBinaryOperation::pythonAdd); addOperation(methods, Operator.SUBTRACT, PythonBinaryOperation::pythonSubtract); addOperation(methods, Operator.MULTIPLY, PythonBinaryOperation::pythonMultiply); @@ -86,10 +86,10 @@ public class PythonPrimitiveMethods { addOperation(methods, Operator.GREATER_THAN_OR_EQUAL, PythonBinaryOperation::pythonGreatThanOrEqual); addOperation(methods, Operator.LESS_THAN, PythonBinaryOperation::pythonLessThan); addOperation(methods, Operator.LESS_THAN_OR_EQUAL, PythonBinaryOperation::pythonLessThanOrEqual); - + INT_METHODS = methods.build(); } - + private static PythonExpressionNode internalReference(String name) { return pythonAttributeAccess(pythonVariableReference("_couscous"), name); } @@ -101,16 +101,16 @@ public class PythonPrimitiveMethods { { addOperation(methods, operator.getSymbol(), build); } - + private static void addOperation( Builder methods, String methodName, BiFunction build) { - methods.put(methodName, (receiver, arguments) -> + methods.put(methodName, (receiver, arguments) -> build.apply(receiver, arguments.get(0))); } - + private static final Map> METHODS = ImmutableMap.>builder() .put(Types.STRING, STRING_METHODS) @@ -118,12 +118,12 @@ public class PythonPrimitiveMethods { .put(Types.INT, INT_METHODS) .put(Types.BOXED_INT, 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 BOXED_INT_STATIC_METHODS = @@ -133,7 +133,7 @@ public class PythonPrimitiveMethods { .put("valueOf", arguments -> arguments.get(0)) .build(); - + private static final Map> STATIC_METHODS = ImmutableMap.>builder() .put(InternalCouscousValue.REF, INTERNAL_METHODS) @@ -144,7 +144,7 @@ public class PythonPrimitiveMethods { return Optional.ofNullable(METHODS.get(type)) .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName))); } - + @FunctionalInterface public interface PrimitiveMethodGenerator { PythonExpressionNode generate(PythonExpressionNode receiver, List arguments); @@ -154,7 +154,7 @@ public class PythonPrimitiveMethods { 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