aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/interpreter
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-06 11:02:51 +0100
committerMichael Williamson <mike@zwobble.org>2015-10-06 11:02:51 +0100
commit56470c01483077849dae33f65364bad987afac7a (patch)
tree2954d1d44f6c11a6c27299496014b0723e7d6e8d /src/main/java/org/zwobble/couscous/interpreter
parenta7669795d1d58e4962f16272cb5e92783cf01c64 (diff)
Combine type reference and class name into type name
Diffstat (limited to 'src/main/java/org/zwobble/couscous/interpreter')
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/Environment.java9
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/Interpreter.java4
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/UnexpectedValueType.java6
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/values/Callable.java4
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java34
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java4
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java4
7 files changed, 29 insertions, 36 deletions
diff --git a/src/main/java/org/zwobble/couscous/interpreter/Environment.java b/src/main/java/org/zwobble/couscous/interpreter/Environment.java
index 0c226d1..37df9f5 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/Environment.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/Environment.java
@@ -4,12 +4,11 @@ import java.util.Map;
import java.util.Optional;
import org.zwobble.couscous.Project;
-import org.zwobble.couscous.ast.ClassName;
+import org.zwobble.couscous.ast.TypeName;
import org.zwobble.couscous.ast.VariableDeclaration;
import org.zwobble.couscous.ast.VariableNode;
import org.zwobble.couscous.interpreter.values.ConcreteType;
import org.zwobble.couscous.interpreter.values.InterpreterValue;
-import org.zwobble.couscous.values.TypeReference;
import static java.util.stream.Collectors.toMap;
@@ -47,7 +46,7 @@ public class Environment {
put(variable.getDeclaration().getId(), value);
}
- public ConcreteType<?> findClass(ClassName className) {
+ public ConcreteType<?> findClass(TypeName className) {
// TODO: handle missing classes
return project.findClass(className);
}
@@ -64,7 +63,7 @@ public class Environment {
private void checkVariableType(int variableId, InterpreterValue value) {
val variableType = stackFrame.get(variableId).getType();
- val valueType = value.getType().getReference();
+ val valueType = value.getType().getName();
if (!variableType.equals(valueType)) {
throw new UnexpectedValueType(variableType, valueType);
}
@@ -74,7 +73,7 @@ public class Environment {
@Getter
@Setter
private static class VariableEntry {
- private final TypeReference type;
+ private final TypeName type;
private Optional<InterpreterValue> value;
}
}
diff --git a/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java b/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java
index 10153c8..38ea2ac 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java
@@ -4,7 +4,7 @@ import java.util.List;
import java.util.stream.Collectors;
import org.zwobble.couscous.Project;
-import org.zwobble.couscous.ast.ClassName;
+import org.zwobble.couscous.ast.TypeName;
import org.zwobble.couscous.ast.ExpressionNode;
import org.zwobble.couscous.interpreter.values.InterpreterValue;
@@ -23,7 +23,7 @@ public class Interpreter {
this.project = project;
}
- public InterpreterValue run(ClassName className, String methodName, List<InterpreterValue> arguments) {
+ public InterpreterValue run(TypeName className, String methodName, List<InterpreterValue> arguments) {
val argumentExpressions = arguments
.stream()
.<ExpressionNode>map(argument -> literal(argument.toPrimitiveValue().get()))
diff --git a/src/main/java/org/zwobble/couscous/interpreter/UnexpectedValueType.java b/src/main/java/org/zwobble/couscous/interpreter/UnexpectedValueType.java
index 8ae32c3..827b9e9 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/UnexpectedValueType.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/UnexpectedValueType.java
@@ -1,6 +1,6 @@
package org.zwobble.couscous.interpreter;
-import org.zwobble.couscous.values.TypeReference;
+import org.zwobble.couscous.ast.TypeName;
import lombok.EqualsAndHashCode;
import lombok.Value;
@@ -10,6 +10,6 @@ import lombok.Value;
public class UnexpectedValueType extends InterpreterException {
private static final long serialVersionUID = 1L;
- TypeReference expected;
- TypeReference actual;
+ TypeName expected;
+ TypeName actual;
}
diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/Callable.java b/src/main/java/org/zwobble/couscous/interpreter/values/Callable.java
index dc372bb..f96ba9a 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/values/Callable.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/values/Callable.java
@@ -2,8 +2,8 @@ package org.zwobble.couscous.interpreter.values;
import java.util.List;
-import org.zwobble.couscous.values.TypeReference;
+import org.zwobble.couscous.ast.TypeName;
public interface Callable {
- List<TypeReference> getArgumentTypes();
+ List<TypeName> getArgumentTypes();
}
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 04156d5..52cd14a 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java
@@ -5,20 +5,18 @@ import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
-import org.zwobble.couscous.ast.ClassName;
import org.zwobble.couscous.ast.ClassNode;
+import org.zwobble.couscous.ast.TypeName;
import org.zwobble.couscous.interpreter.Arguments;
import org.zwobble.couscous.interpreter.Environment;
import org.zwobble.couscous.interpreter.Executor;
import org.zwobble.couscous.interpreter.NoSuchMethod;
import org.zwobble.couscous.interpreter.UnexpectedValueType;
import org.zwobble.couscous.interpreter.WrongNumberOfArguments;
-import org.zwobble.couscous.values.TypeReference;
import com.google.common.collect.ImmutableMap;
import static java.util.stream.Collectors.toMap;
-import static org.zwobble.couscous.values.TypeReference.typeRef;
import lombok.val;
@@ -28,15 +26,15 @@ public class ConcreteType<T> {
ImmutableMap.builder();
private final ImmutableMap.Builder<String, StaticMethodValue> staticMethods =
ImmutableMap.builder();
- private final ClassName name;
+ private final TypeName name;
- public Builder(ClassName name) {
+ public Builder(TypeName name) {
this.name = name;
}
public Builder<T> method(
String name,
- List<TypeReference> argumentsTypes,
+ List<TypeName> argumentsTypes,
BiFunction<T, Arguments, InterpreterValue> method) {
methods.put(name, new MethodValue<T>(argumentsTypes, method));
return this;
@@ -44,7 +42,7 @@ public class ConcreteType<T> {
public Builder<T> staticMethod(
String name,
- List<TypeReference> argumentsTypes,
+ List<TypeName> argumentsTypes,
BiFunction<Environment, Arguments, InterpreterValue> method) {
staticMethods.put(name, new StaticMethodValue(argumentsTypes, method));
return this;
@@ -62,7 +60,7 @@ public class ConcreteType<T> {
.collect(toMap(
method -> method.getName(),
method -> {
- List<TypeReference> argumentTypes = method.getArguments()
+ List<TypeName> argumentTypes = method.getArguments()
.stream()
.map(arg -> arg.getType())
.collect(Collectors.toList());
@@ -76,12 +74,12 @@ public class ConcreteType<T> {
staticMethods);
}
- private ClassName name;
+ private TypeName name;
private Map<String, MethodValue<T>> methods;
private Map<String, StaticMethodValue> staticMethods;
public ConcreteType(
- ClassName name,
+ TypeName name,
Map<String, MethodValue<T>> methods,
Map<String, StaticMethodValue> staticMethods) {
this.name = name;
@@ -89,16 +87,16 @@ public class ConcreteType<T> {
this.staticMethods = staticMethods;
}
- public ClassName getName() {
+ public TypeName getName() {
return name;
}
- public static <T> ConcreteType.Builder<T> builder(TypeReference reference) {
- return builder(reference.getName());
+ public static <T> ConcreteType.Builder<T> builder(TypeName reference) {
+ return new Builder<>(reference);
}
public static <T> ConcreteType.Builder<T> builder(String name) {
- return new Builder<>(ClassName.of(name));
+ return builder(TypeName.of(name));
}
@SuppressWarnings("unchecked")
@@ -124,17 +122,13 @@ public class ConcreteType<T> {
for (int index = 0; index < arguments.size(); index++) {
val formalArgumentType = method.getArgumentTypes().get(index);
val actualArgumentType = arguments.get(index).getType();
- if (!formalArgumentType.equals(actualArgumentType.getReference())) {
- throw new UnexpectedValueType(formalArgumentType, actualArgumentType.getReference());
+ if (!formalArgumentType.equals(actualArgumentType.getName())) {
+ throw new UnexpectedValueType(formalArgumentType, actualArgumentType.getName());
}
}
return method;
}
- public TypeReference getReference() {
- return typeRef(name.getQualifiedName());
- }
-
@Override
public String toString() {
return "ConcreteType<" + name + ">";
diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java
index ea76870..f2e2cc1 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java
@@ -3,8 +3,8 @@ package org.zwobble.couscous.interpreter.values;
import java.util.List;
import java.util.function.BiFunction;
+import org.zwobble.couscous.ast.TypeName;
import org.zwobble.couscous.interpreter.Arguments;
-import org.zwobble.couscous.values.TypeReference;
import lombok.AccessLevel;
import lombok.Getter;
@@ -12,7 +12,7 @@ import lombok.Value;
@Value
public class MethodValue<T> implements Callable {
- List<TypeReference> argumentTypes;
+ List<TypeName> argumentTypes;
@Getter(value=AccessLevel.NONE)
BiFunction<T, Arguments, InterpreterValue> apply;
diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java
index 21697b1..4754d80 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java
@@ -3,9 +3,9 @@ package org.zwobble.couscous.interpreter.values;
import java.util.List;
import java.util.function.BiFunction;
+import org.zwobble.couscous.ast.TypeName;
import org.zwobble.couscous.interpreter.Arguments;
import org.zwobble.couscous.interpreter.Environment;
-import org.zwobble.couscous.values.TypeReference;
import lombok.AccessLevel;
import lombok.Getter;
@@ -13,7 +13,7 @@ import lombok.Value;
@Value
public class StaticMethodValue implements Callable {
- List<TypeReference> argumentTypes;
+ List<TypeName> argumentTypes;
@Getter(value=AccessLevel.NONE)
BiFunction<Environment, Arguments, InterpreterValue> apply;