From e4229671c4cf6db860fc6cf68bcfb8fdd69fdcb8 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 6 Jan 2016 08:16:52 +0000 Subject: Support method overloading in interpreter --- .../couscous/interpreter/values/ConcreteType.java | 58 ++++++++++++---------- .../values/IntegerInterpreterValue.java | 16 ++++-- 2 files changed, 43 insertions(+), 31 deletions(-) (limited to 'src/main/java/org/zwobble/couscous/interpreter/values') diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java b/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java index 157bb41..536aeb3 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java @@ -5,17 +5,14 @@ import java.util.function.BiFunction; import java.util.stream.Collectors; import org.zwobble.couscous.ast.ClassNode; import org.zwobble.couscous.ast.FormalArgumentNode; +import org.zwobble.couscous.ast.MethodNode; import org.zwobble.couscous.ast.TypeName; -import org.zwobble.couscous.interpreter.Environment; -import org.zwobble.couscous.interpreter.Executor; -import org.zwobble.couscous.interpreter.InterpreterTypes; -import org.zwobble.couscous.interpreter.NoSuchMethod; -import org.zwobble.couscous.interpreter.PositionalArguments; -import org.zwobble.couscous.interpreter.WrongNumberOfArguments; +import org.zwobble.couscous.interpreter.*; import org.zwobble.couscous.util.Casts; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import static java.util.stream.Collectors.toMap; +import static org.zwobble.couscous.util.ExtraLists.eagerMap; public class ConcreteType { @@ -33,8 +30,8 @@ public class ConcreteType { public static class Builder { private final ImmutableMap.Builder fields = ImmutableMap.builder(); - private final ImmutableMap.Builder methods = ImmutableMap.builder(); - private final ImmutableMap.Builder staticMethods = ImmutableMap.builder(); + private final ImmutableMap.Builder methods = ImmutableMap.builder(); + private final ImmutableMap.Builder staticMethods = ImmutableMap.builder(); private final Class interpreterValueType; private final TypeName name; @@ -49,14 +46,14 @@ public class ConcreteType { } public Builder method(String name, List argumentsTypes, BiFunction, InterpreterValue> method) { - methods.put(name, new MethodValue(argumentsTypes, (environment, arguments) -> { + methods.put(new MethodSignature(name, argumentsTypes), new MethodValue(argumentsTypes, (environment, arguments) -> { return Casts.tryCast(interpreterValueType, arguments.getReceiver()).map(typedReceiver -> method.apply(environment, MethodCallArguments.of(typedReceiver, arguments.getPositionalArguments()))).orElseThrow(() -> new RuntimeException("receiver is of wrong type")); })); return this; } public Builder staticMethod(String name, List argumentsTypes, BiFunction method) { - staticMethods.put(name, new StaticMethodValue(argumentsTypes, method)); + staticMethods.put(new MethodSignature(name, argumentsTypes), new StaticMethodValue(argumentsTypes, method)); return this; } @@ -74,19 +71,19 @@ public class ConcreteType { public static ConcreteType fromNode(ClassNode classNode) { Map fields = classNode.getFields().stream().collect(toMap(field -> field.getName(), field -> new FieldValue(field.getName(), field.getType()))); MethodValue constructor = new MethodValue(getArgumentTypes(classNode.getConstructor().getArguments()), (environment, arguments) -> Executor.callMethod(environment, classNode.getConstructor(), Optional.of(arguments.getReceiver()), arguments.getPositionalArguments())); - Map methods = classNode.getMethods() + Map methods = classNode.getMethods() .stream() .filter(method -> !method.isStatic()) - .collect(toMap(method -> method.getName(), method -> { + .collect(toMap(method -> signature(method), method -> { List argumentTypes = getArgumentTypes(method.getArguments()); return new MethodValue(argumentTypes, (environment, arguments) -> { return Executor.callMethod(environment, method, Optional.of(arguments.getReceiver()), arguments.getPositionalArguments()); }); })); - Map staticMethods = classNode.getMethods() + Map staticMethods = classNode.getMethods() .stream() .filter(method -> method.isStatic()) - .collect(toMap(method -> method.getName(), method -> { + .collect(toMap(method -> signature(method), method -> { List argumentTypes = getArgumentTypes(method.getArguments()); return new StaticMethodValue(argumentTypes, (environment, arguments) -> { return Executor.callMethod(environment, method, Optional.empty(), arguments); @@ -94,7 +91,13 @@ public class ConcreteType { })); return new ConcreteType(classNode.getName(), classNode.getSuperTypes(), fields, constructor, methods, staticMethods); } - + + private static MethodSignature signature(MethodNode method) { + return new MethodSignature( + method.getName(), + eagerMap(method.getArguments(), argument -> argument.getType())); + } + private static List getArgumentTypes(List arguments) { return arguments.stream().map(arg -> arg.getType()).collect(Collectors.toList()); } @@ -102,16 +105,17 @@ public class ConcreteType { private final Set superTypes; private final Map fields; private final MethodValue constructor; - private final Map methods; - private final Map staticMethods; + private final Map methods; + private final Map staticMethods; private ConcreteType( TypeName name, Set superTypes, Map fields, MethodValue constructor, - Map methods, - Map staticMethods) { + Map methods, + Map staticMethods) + { this.name = name; this.superTypes = superTypes; this.fields = fields; @@ -132,13 +136,13 @@ public class ConcreteType { return Optional.ofNullable(fields.get(fieldName)); } - public InterpreterValue callMethod(Environment environment, InterpreterValue receiver, String methodName, List arguments) { - final org.zwobble.couscous.interpreter.values.MethodValue method = findMethod(methods, methodName, arguments); + public InterpreterValue callMethod(Environment environment, InterpreterValue receiver, MethodSignature signature, List arguments) { + MethodValue method = findMethod(methods, signature, arguments); return method.apply(environment, MethodCallArguments.of(receiver, new PositionalArguments(arguments))); } - public InterpreterValue callStaticMethod(Environment environment, String methodName, List arguments) { - final org.zwobble.couscous.interpreter.values.StaticMethodValue method = findMethod(staticMethods, methodName, arguments); + public InterpreterValue callStaticMethod(Environment environment, MethodSignature signature, List arguments) { + StaticMethodValue method = findMethod(staticMethods, signature, arguments); return method.apply(environment, new PositionalArguments(arguments)); } @@ -149,11 +153,11 @@ public class ConcreteType { return object; } - private static T findMethod(Map methods, String methodName, List arguments) { - if (!methods.containsKey(methodName)) { - throw new NoSuchMethod(methodName); + private static T findMethod(Map methods, MethodSignature signature, List arguments) { + if (!methods.containsKey(signature)) { + throw new NoSuchMethod(signature); } - final T method = methods.get(methodName); + final T method = methods.get(signature); checkMethodArguments(method, arguments); return method; } diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/IntegerInterpreterValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/IntegerInterpreterValue.java index 3bcb41a..be58d56 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/IntegerInterpreterValue.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/IntegerInterpreterValue.java @@ -4,8 +4,7 @@ import java.util.Optional; import java.util.function.BiFunction; import org.zwobble.couscous.ast.Operator; -import org.zwobble.couscous.interpreter.Environment; -import org.zwobble.couscous.interpreter.NoSuchField; +import org.zwobble.couscous.interpreter.*; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.ObjectValues; import org.zwobble.couscous.values.PrimitiveValue; @@ -29,11 +28,16 @@ public final class IntegerInterpreterValue implements InterpreterValue { (environment, arguments) -> { InterpreterValue right = arguments.getPositionalArguments().get(0); if (right instanceof IntegerInterpreterValue) { - return new BooleanInterpreterValue(arguments.getReceiver().getValue() == ((IntegerInterpreterValue)right).getValue()); + return integerEquals(arguments, ((IntegerInterpreterValue)right)); } else { return new BooleanInterpreterValue(false); } }) + .method(Operator.EQUALS.getMethodName(), asList(IntegerValue.REF), + (environment, arguments) -> { + IntegerInterpreterValue right = (IntegerInterpreterValue)arguments.getPositionalArguments().get(0); + return integerEquals(arguments, right); + }) .method(Operator.NOT_EQUALS.getMethodName(), asList(IntegerValue.REF), infixReturningBoolean((left, right) -> !left.equals(right))) .method(Operator.GREATER_THAN.getMethodName(), asList(IntegerValue.REF), @@ -45,7 +49,11 @@ public final class IntegerInterpreterValue implements InterpreterValue { .method(Operator.LESS_THAN_OR_EQUAL.getMethodName(), asList(IntegerValue.REF), infixReturningBoolean((left, right) -> left <= right)) .build(); - + + private static BooleanInterpreterValue integerEquals(MethodCallArguments arguments, IntegerInterpreterValue right) { + return new BooleanInterpreterValue(arguments.getReceiver().getValue() == right.getValue()); + } + private static BiFunction, InterpreterValue> infixReturningInteger(BiFunction func) { -- cgit v1.2.3