aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/frontends
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-01-01 15:33:57 +0000
committerMichael Williamson <mike@zwobble.org>2016-01-01 15:33:57 +0000
commitfd0769bde1ffad49ca74a404f18aae5d7226c558 (patch)
tree64f055dd31d309dddfe4d7c11803d4c6dab1e161 /src/main/java/org/zwobble/couscous/frontends
parent17384f8b7bb6151be3f033ba7e339d9edb59be55 (diff)
Get interpreter compiler tests working
Diffstat (limited to 'src/main/java/org/zwobble/couscous/frontends')
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java17
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java45
2 files changed, 56 insertions, 6 deletions
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java
index 76dcf22..2963fd6 100644
--- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java
+++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java
@@ -12,6 +12,7 @@ import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.Set;
import java.util.function.Function;
import static com.google.common.collect.Iterables.concat;
@@ -29,6 +30,7 @@ import static org.zwobble.couscous.ast.ThisReferenceNode.thisReference;
import static org.zwobble.couscous.ast.VariableDeclaration.var;
import static org.zwobble.couscous.ast.VariableReferenceNode.reference;
import static org.zwobble.couscous.frontends.java.FreeVariables.findFreeVariables;
+import static org.zwobble.couscous.frontends.java.JavaTypes.superTypes;
import static org.zwobble.couscous.frontends.java.JavaTypes.typeOf;
import static org.zwobble.couscous.util.Casts.tryCast;
import static org.zwobble.couscous.util.ExtraLists.*;
@@ -58,6 +60,7 @@ public class JavaReader {
TypeDeclarationBody body = readTypeDeclarationBody(type.bodyDeclarations());
return ClassNode.declareClass(
name,
+ superTypes(type),
body.getFields(),
body.getConstructor(),
body.getMethods());
@@ -75,7 +78,11 @@ public class JavaReader {
lambda.getFormalArguments(),
lambda.getBody());
- GeneratedClosure closure = classWithCapture(className, emptyList(), ImmutableList.of(method));
+ GeneratedClosure closure = classWithCapture(
+ className,
+ superTypes(expression),
+ emptyList(),
+ ImmutableList.of(method));
classes.add(closure.getClassNode());
return closure;
@@ -132,13 +139,18 @@ public class JavaReader {
GeneratedClosure readAnonymousClass(AnonymousClassDeclaration declaration) {
TypeName className = generateClassName(declaration);
TypeDeclarationBody bodyDeclarations = readTypeDeclarationBody(declaration.bodyDeclarations());
- GeneratedClosure closure = classWithCapture(className, bodyDeclarations.getFields(), bodyDeclarations.getMethods());
+ GeneratedClosure closure = classWithCapture(
+ className,
+ superTypes(declaration),
+ bodyDeclarations.getFields(),
+ bodyDeclarations.getMethods());
classes.add(closure.getClassNode());
return closure;
}
private GeneratedClosure classWithCapture(
TypeName className,
+ Set<TypeName> superTypes,
List<FieldDeclarationNode> declaredFields,
List<MethodNode> methods
) {
@@ -151,6 +163,7 @@ public class JavaReader {
ClassNode classNode = ClassNode.declareClass(
className,
+ superTypes,
fields,
buildConstructor(className, freeVariables),
eagerMap(methods, method ->
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java
index d6b2160..4b62a17 100644
--- a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java
+++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java
@@ -1,10 +1,14 @@
package org.zwobble.couscous.frontends.java;
-import org.eclipse.jdt.core.dom.Expression;
-import org.eclipse.jdt.core.dom.ITypeBinding;
-import org.eclipse.jdt.core.dom.IVariableBinding;
-import org.eclipse.jdt.core.dom.Type;
+import com.google.common.collect.ImmutableSet;
+import org.eclipse.jdt.core.dom.*;
import org.zwobble.couscous.ast.TypeName;
+import org.zwobble.couscous.values.ObjectValues;
+
+import java.util.Set;
+
+import static com.google.common.collect.Iterables.transform;
+import static java.util.Arrays.asList;
class JavaTypes {
static TypeName typeOf(Expression expression) {
@@ -22,4 +26,37 @@ class JavaTypes {
static TypeName typeOf(ITypeBinding typeBinding) {
return TypeName.of(typeBinding.getQualifiedName());
}
+
+ static Set<TypeName> superTypes(TypeDeclaration declaration) {
+ return superTypes(declaration.resolveBinding());
+ }
+
+ static Set<TypeName> superTypes(AnonymousClassDeclaration declaration) {
+ return superTypes(declaration.resolveBinding());
+ }
+
+ static Set<TypeName> superTypes(LambdaExpression expression) {
+ ImmutableSet.Builder<TypeName> superTypes = ImmutableSet.builder();
+ ITypeBinding typeBinding = expression.resolveTypeBinding();
+ superTypes.addAll(superTypes(typeBinding));
+ superTypes.add(typeOf(typeBinding));
+ return superTypes.build();
+ }
+
+ private static Set<TypeName> superTypes(ITypeBinding typeBinding) {
+ ImmutableSet.Builder<TypeName> superTypes = ImmutableSet.builder();
+ superTypes.addAll(transform(
+ asList(typeBinding.getInterfaces()),
+ JavaTypes::typeOf));
+ superTypes.add(superClass(typeBinding));
+ return superTypes.build();
+ }
+
+ private static TypeName superClass(ITypeBinding typeBinding) {
+ if (typeBinding.getSuperclass() == null) {
+ return ObjectValues.OBJECT;
+ } else {
+ return typeOf(typeBinding.getSuperclass());
+ }
+ }
}