aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-01-06 21:58:23 +0000
committerMichael Williamson <mike@zwobble.org>2016-01-06 21:58:23 +0000
commit368fd234d5cb94a1b86e8258a83744e27f814a3e (patch)
tree86b8bf0779b765423168d518f351631eeef6dab0
parentb69c8f7c8e2c16aade681ceaae046456a5ef2a85 (diff)
Allow concrete type builder to add constructors
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java23
1 files changed, 18 insertions, 5 deletions
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 6abb56c..9c688ab 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java
@@ -27,6 +27,7 @@ public class ConcreteType {
public static class Builder<T> {
private final ImmutableMap.Builder<String, FieldValue> fields = ImmutableMap.builder();
+ private MethodValue constructor = new MethodValue(ImmutableList.of(), (environment, arguments) -> InterpreterValues.UNIT);
private final ImmutableMap.Builder<MethodSignature, MethodValue> methods = ImmutableMap.builder();
private final ImmutableMap.Builder<MethodSignature, StaticMethodValue> staticMethods = ImmutableMap.builder();
private final Class<T> interpreterValueType;
@@ -42,13 +43,25 @@ public class ConcreteType {
return this;
}
+ public Builder<T> constructor(List<TypeName> argumentsTypes, BiFunction<Environment, MethodCallArguments<T>, InterpreterValue> method) {
+ constructor = toMethodValue(argumentsTypes, method);
+ return this;
+ }
+
public Builder<T> method(String name, List<TypeName> argumentsTypes, BiFunction<Environment, MethodCallArguments<T>, InterpreterValue> method) {
- 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"));
- }));
+ methods.put(new MethodSignature(name, argumentsTypes), toMethodValue(argumentsTypes, method));
return this;
}
-
+
+ private MethodValue toMethodValue(List<TypeName> argumentsTypes, BiFunction<Environment, MethodCallArguments<T>, InterpreterValue> method) {
+ return new MethodValue(
+ argumentsTypes,
+ (environment, arguments) ->
+ Casts.tryCast(interpreterValueType, arguments.getReceiver())
+ .map(typedReceiver -> method.apply(environment, MethodCallArguments.of(typedReceiver, arguments.getPositionalArguments())))
+ .orElseThrow(() -> new RuntimeException("receiver is of wrong type")));
+ }
+
public Builder<T> staticMethod(String name, List<TypeName> argumentsTypes, BiFunction<Environment, PositionalArguments, InterpreterValue> method) {
staticMethods.put(new MethodSignature(name, argumentsTypes), new StaticMethodValue(argumentsTypes, method));
return this;
@@ -59,7 +72,7 @@ public class ConcreteType {
name,
Collections.emptySet(),
fields.build(),
- new MethodValue(ImmutableList.of(), (environment, arguments) -> InterpreterValues.UNIT),
+ constructor,
methods.build(),
staticMethods.build());
}