diff options
Diffstat (limited to 'src/main/java/org')
72 files changed, 479 insertions, 367 deletions
diff --git a/src/main/java/org/zwobble/couscous/ast/AnnotationNode.java b/src/main/java/org/zwobble/couscous/ast/AnnotationNode.java index e102af7..b70ed12 100644 --- a/src/main/java/org/zwobble/couscous/ast/AnnotationNode.java +++ b/src/main/java/org/zwobble/couscous/ast/AnnotationNode.java @@ -1,20 +1,21 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class AnnotationNode implements Node { - public static AnnotationNode annotation(TypeName type) { + public static AnnotationNode annotation(Type type) { return new AnnotationNode(type); } - private final TypeName type; + private final Type type; - private AnnotationNode(TypeName type) { + private AnnotationNode(Type type) { this.type = type; } - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/AssignmentNode.java b/src/main/java/org/zwobble/couscous/ast/AssignmentNode.java index ec781b3..64fb973 100644 --- a/src/main/java/org/zwobble/couscous/ast/AssignmentNode.java +++ b/src/main/java/org/zwobble/couscous/ast/AssignmentNode.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -52,7 +53,7 @@ public class AssignmentNode implements ExpressionNode { } @Override - public TypeName getType() { + public Type getType() { return value.getType(); } diff --git a/src/main/java/org/zwobble/couscous/ast/CastNode.java b/src/main/java/org/zwobble/couscous/ast/CastNode.java index 20ae9c6..75201e7 100644 --- a/src/main/java/org/zwobble/couscous/ast/CastNode.java +++ b/src/main/java/org/zwobble/couscous/ast/CastNode.java @@ -1,17 +1,18 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class CastNode implements ExpressionNode { - public static ExpressionNode cast(ExpressionNode expression, TypeName type) { + public static ExpressionNode cast(ExpressionNode expression, Type type) { return new CastNode(expression, type); } private final ExpressionNode expression; - private final TypeName type; + private final Type type; - private CastNode(ExpressionNode expression, TypeName type) { + private CastNode(ExpressionNode expression, Type type) { this.expression = expression; this.type = type; } @@ -21,7 +22,7 @@ public class CastNode implements ExpressionNode { } @Override - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/ClassNode.java b/src/main/java/org/zwobble/couscous/ast/ClassNode.java index 2342315..d50051c 100644 --- a/src/main/java/org/zwobble/couscous/ast/ClassNode.java +++ b/src/main/java/org/zwobble/couscous/ast/ClassNode.java @@ -1,5 +1,7 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -7,7 +9,7 @@ import java.util.List; import java.util.Set; public class ClassNode implements TypeNode { - public static ClassNodeBuilder builder(TypeName name) { + public static ClassNodeBuilder builder(ScalarType name) { return new ClassNodeBuilder(name); } public static ClassNodeBuilder builder(String name) { @@ -15,9 +17,9 @@ public class ClassNode implements TypeNode { } public static ClassNode declareClass( - TypeName name, + ScalarType name, List<FormalTypeParameterNode> typeParameters, - Set<TypeName> superTypes, + Set<Type> superTypes, List<FieldDeclarationNode> fields, List<StatementNode> staticConstructor, ConstructorNode constructor, @@ -26,18 +28,18 @@ public class ClassNode implements TypeNode { return new ClassNode(name, typeParameters, superTypes, fields, staticConstructor, constructor, methods); } - private final TypeName name; + private final ScalarType name; private final List<FormalTypeParameterNode> typeParameters; - private final Set<TypeName> superTypes; + private final Set<Type> superTypes; private final List<FieldDeclarationNode> fields; private final List<StatementNode> staticConstructor; private final ConstructorNode constructor; private final List<MethodNode> methods; public ClassNode( - TypeName name, + ScalarType name, List<FormalTypeParameterNode> typeParameters, - Set<TypeName> superTypes, + Set<Type> superTypes, List<FieldDeclarationNode> fields, List<StatementNode> staticConstructor, ConstructorNode constructor, @@ -53,7 +55,7 @@ public class ClassNode implements TypeNode { } @Override - public TypeName getName() { + public ScalarType getName() { return name; } @@ -63,7 +65,7 @@ public class ClassNode implements TypeNode { } @Override - public Set<TypeName> getSuperTypes() { + public Set<Type> getSuperTypes() { return superTypes; } diff --git a/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java b/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java index 282df33..9dc5098 100644 --- a/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java +++ b/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java @@ -3,6 +3,8 @@ package org.zwobble.couscous.ast; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.values.UnitValue; import java.util.List; @@ -15,15 +17,15 @@ import static org.zwobble.couscous.ast.VariableDeclaration.var; import static org.zwobble.couscous.util.ExtraLists.list; public class ClassNodeBuilder { - private final TypeName name; - private final ImmutableSet.Builder<TypeName> superTypes; + private final ScalarType name; + private final ImmutableSet.Builder<Type> superTypes; private final ImmutableList.Builder<FormalTypeParameterNode> typeParameters; private final ImmutableList.Builder<FieldDeclarationNode> fields; private List<StatementNode> staticConstructor; private Optional<ConstructorNode> constructor; private final ImmutableList.Builder<MethodNode> methods; - public ClassNodeBuilder(TypeName name) { + public ClassNodeBuilder(ScalarType name) { this.name = name; this.typeParameters = ImmutableList.builder(); this.superTypes = ImmutableSet.builder(); @@ -34,7 +36,7 @@ public class ClassNodeBuilder { } public ClassNodeBuilder(String name) { - this(TypeName.of(name)); + this(ScalarType.of(name)); } public ClassNodeBuilder addTypeParameter(String name) { @@ -43,15 +45,15 @@ public class ClassNodeBuilder { } public ClassNodeBuilder addSuperType(String type) { - this.superTypes.add(TypeName.of(type)); + this.superTypes.add(ScalarType.of(type)); return this; } - public ClassNodeBuilder staticField(String name, TypeName type) { + public ClassNodeBuilder staticField(String name, ScalarType type) { return field(FieldDeclarationNode.staticField(name, type)); } - public ClassNodeBuilder field(String name, TypeName type) { + public ClassNodeBuilder field(String name, ScalarType type) { return field(FieldDeclarationNode.field(name, type)); } @@ -132,7 +134,7 @@ public class ClassNodeBuilder { private final Function<MethodBuilder<?>, T> build; private final ImmutableList.Builder<AnnotationNode> annotations; private final ImmutableList.Builder<FormalArgumentNode> arguments; - private TypeName returnType = UnitValue.REF; + private ScalarType returnType = UnitValue.REF; private final ImmutableList.Builder<StatementNode> statements; public MethodBuilder(Function<MethodBuilder<?>, T> build) { @@ -142,7 +144,7 @@ public class ClassNodeBuilder { this.statements = ImmutableList.builder(); } - public MethodBuilder<T> annotation(TypeName type) { + public MethodBuilder<T> annotation(ScalarType type) { annotations.add(AnnotationNode.annotation(type)); return this; } @@ -151,12 +153,12 @@ public class ClassNodeBuilder { return ThisReferenceNode.thisReference(name); } - public MethodBuilder<T> argument(Identifier id, String name, TypeName type) { + public MethodBuilder<T> argument(Identifier id, String name, ScalarType type) { arguments.add(formalArg(var(id, name, type))); return this; } - public MethodBuilder<T> returns(TypeName returnType) { + public MethodBuilder<T> returns(ScalarType returnType) { this.returnType = returnType; return this; } diff --git a/src/main/java/org/zwobble/couscous/ast/ConstructorCallNode.java b/src/main/java/org/zwobble/couscous/ast/ConstructorCallNode.java index b5439bf..5a597d1 100644 --- a/src/main/java/org/zwobble/couscous/ast/ConstructorCallNode.java +++ b/src/main/java/org/zwobble/couscous/ast/ConstructorCallNode.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -7,23 +8,25 @@ import java.util.List; public class ConstructorCallNode implements ExpressionNode { public static ConstructorCallNode constructorCall( - TypeName type, - List<? extends ExpressionNode> arguments) { + Type type, + List<? extends ExpressionNode> arguments) + { return new ConstructorCallNode(type, arguments); } - private final TypeName type; + private final Type type; private final List<? extends ExpressionNode> arguments; public ConstructorCallNode( - TypeName type, - List<? extends ExpressionNode> arguments) { + Type type, + List<? extends ExpressionNode> arguments) + { this.type = type; this.arguments = arguments; } @Override - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/ExpressionNode.java b/src/main/java/org/zwobble/couscous/ast/ExpressionNode.java index a7842d3..2452183 100644 --- a/src/main/java/org/zwobble/couscous/ast/ExpressionNode.java +++ b/src/main/java/org/zwobble/couscous/ast/ExpressionNode.java @@ -1,11 +1,12 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public interface ExpressionNode extends Node { - TypeName getType(); + Type getType(); <T> T accept(ExpressionNodeMapper<T> visitor); @Override diff --git a/src/main/java/org/zwobble/couscous/ast/FieldAccessNode.java b/src/main/java/org/zwobble/couscous/ast/FieldAccessNode.java index a949630..e417638 100644 --- a/src/main/java/org/zwobble/couscous/ast/FieldAccessNode.java +++ b/src/main/java/org/zwobble/couscous/ast/FieldAccessNode.java @@ -1,5 +1,7 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.AssignableExpressionNodeVisitor; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -11,15 +13,15 @@ public class FieldAccessNode implements AssignableExpressionNode { public static FieldAccessNode fieldAccess( ExpressionNode left, String fieldName, - TypeName type) + Type type) { return fieldAccess(instanceReceiver(left), fieldName, type); } public static FieldAccessNode fieldAccess( - TypeName left, + ScalarType left, String fieldName, - TypeName type) + Type type) { return fieldAccess(staticReceiver(left), fieldName, type); } @@ -27,19 +29,19 @@ public class FieldAccessNode implements AssignableExpressionNode { public static FieldAccessNode fieldAccess( Receiver left, String fieldName, - TypeName type) + Type type) { return new FieldAccessNode(left, fieldName, type); } private final Receiver left; private final String fieldName; - private final TypeName type; + private final Type type; public FieldAccessNode( Receiver left, String fieldName, - TypeName type) + Type type) { this.left = left; this.fieldName = fieldName; @@ -54,7 +56,7 @@ public class FieldAccessNode implements AssignableExpressionNode { return fieldName; } - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/FieldDeclarationNode.java b/src/main/java/org/zwobble/couscous/ast/FieldDeclarationNode.java index a9f9b76..a207b82 100644 --- a/src/main/java/org/zwobble/couscous/ast/FieldDeclarationNode.java +++ b/src/main/java/org/zwobble/couscous/ast/FieldDeclarationNode.java @@ -1,26 +1,27 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class FieldDeclarationNode implements Node { - public static FieldDeclarationNode field(String name, TypeName type) { + public static FieldDeclarationNode field(String name, Type type) { return field(false, name, type); } - public static FieldDeclarationNode staticField(String name, TypeName type) { + public static FieldDeclarationNode staticField(String name, Type type) { return field(true, name, type); } - public static FieldDeclarationNode field(boolean isStatic, String name, TypeName type) { + public static FieldDeclarationNode field(boolean isStatic, String name, Type type) { return new FieldDeclarationNode(isStatic, name, type); } private final boolean isStatic; private final String name; - private final TypeName type; + private final Type type; - private FieldDeclarationNode(boolean isStatic, String name, TypeName type) { + private FieldDeclarationNode(boolean isStatic, String name, Type type) { this.isStatic = isStatic; this.name = name; this.type = type; @@ -34,7 +35,7 @@ public class FieldDeclarationNode implements Node { return name; } - public TypeName getType() { + public Type getType() { return type; } @@ -45,7 +46,8 @@ public class FieldDeclarationNode implements Node { public FieldDeclarationNode transform(NodeTransformer transformer) { return new FieldDeclarationNode( - isStatic, name, + isStatic, + name, transformer.transform(type)); } diff --git a/src/main/java/org/zwobble/couscous/ast/FormalArgumentNode.java b/src/main/java/org/zwobble/couscous/ast/FormalArgumentNode.java index f7d7f42..7df3ead 100644 --- a/src/main/java/org/zwobble/couscous/ast/FormalArgumentNode.java +++ b/src/main/java/org/zwobble/couscous/ast/FormalArgumentNode.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -18,7 +19,7 @@ public class FormalArgumentNode implements VariableNode, Node { return declaration; } - public TypeName getType() { + public Type getType() { return declaration.getType(); } diff --git a/src/main/java/org/zwobble/couscous/ast/InterfaceNode.java b/src/main/java/org/zwobble/couscous/ast/InterfaceNode.java index e5b876d..97faae4 100644 --- a/src/main/java/org/zwobble/couscous/ast/InterfaceNode.java +++ b/src/main/java/org/zwobble/couscous/ast/InterfaceNode.java @@ -1,5 +1,7 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -8,23 +10,23 @@ import java.util.Set; public class InterfaceNode implements TypeNode { public static InterfaceNode declareInterface( - TypeName name, + ScalarType name, List<FormalTypeParameterNode> typeParameters, - Set<TypeName> superTypes, + Set<Type> superTypes, List<MethodNode> methods) { return new InterfaceNode(name, typeParameters, superTypes, methods); } - private final TypeName name; + private final ScalarType name; private final List<FormalTypeParameterNode> typeParameters; - private final Set<TypeName> superTypes; + private final Set<Type> superTypes; private final List<MethodNode> methods; private InterfaceNode( - TypeName name, + ScalarType name, List<FormalTypeParameterNode> typeParameters, - Set<TypeName> superTypes, + Set<Type> superTypes, List<MethodNode> methodNodes) { this.name = name; @@ -34,7 +36,7 @@ public class InterfaceNode implements TypeNode { } @Override - public TypeName getName() { + public ScalarType getName() { return name; } @@ -44,7 +46,7 @@ public class InterfaceNode implements TypeNode { } @Override - public Set<TypeName> getSuperTypes() { + public Set<Type> getSuperTypes() { return superTypes; } diff --git a/src/main/java/org/zwobble/couscous/ast/LiteralNode.java b/src/main/java/org/zwobble/couscous/ast/LiteralNode.java index a76ca80..bb38562 100644 --- a/src/main/java/org/zwobble/couscous/ast/LiteralNode.java +++ b/src/main/java/org/zwobble/couscous/ast/LiteralNode.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; import org.zwobble.couscous.values.*; @@ -25,23 +26,23 @@ public class LiteralNode implements ExpressionNode { return new LiteralNode(value(value), BooleanValue.REF); } - public static LiteralNode literal(TypeName type) { + public static LiteralNode literal(ScalarType type) { return new LiteralNode(value(type), ObjectValues.CLASS); } - public static LiteralNode of(PrimitiveValue value, TypeName type) { + public static LiteralNode of(PrimitiveValue value, ScalarType type) { return new LiteralNode(value, type); } private final PrimitiveValue value; - private final TypeName type; + private final ScalarType type; - private LiteralNode(PrimitiveValue value, TypeName type) { + private LiteralNode(PrimitiveValue value, ScalarType type) { this.value = value; this.type = type; } - public TypeName getType() { + public ScalarType getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/LocalVariableDeclarationNode.java b/src/main/java/org/zwobble/couscous/ast/LocalVariableDeclarationNode.java index 03e4226..5b571d0 100644 --- a/src/main/java/org/zwobble/couscous/ast/LocalVariableDeclarationNode.java +++ b/src/main/java/org/zwobble/couscous/ast/LocalVariableDeclarationNode.java @@ -1,6 +1,7 @@ package org.zwobble.couscous.ast; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeTransformer; import org.zwobble.couscous.ast.visitors.StatementNodeMapper; @@ -10,7 +11,7 @@ public class LocalVariableDeclarationNode implements VariableNode, StatementNode public static LocalVariableDeclarationNode localVariableDeclaration( Identifier id, String name, - TypeName type, + Type type, ExpressionNode initialValue) { return localVariableDeclaration(var(id, name, type), initialValue); } @@ -43,7 +44,7 @@ public class LocalVariableDeclarationNode implements VariableNode, StatementNode return declaration.getName(); } - public TypeName getType() { + public Type getType() { return declaration.getType(); } diff --git a/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java b/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java index c4e0321..43993b8 100644 --- a/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java +++ b/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java @@ -1,5 +1,7 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -14,15 +16,15 @@ public class MethodCallNode implements ExpressionNode { String className, String methodName, List<ExpressionNode> arguments, - TypeName type) { - return staticMethodCall(TypeName.of(className), methodName, arguments, type); + Type type) { + return staticMethodCall(ScalarType.of(className), methodName, arguments, type); } public static ExpressionNode staticMethodCall( - TypeName className, + ScalarType className, String methodName, List<ExpressionNode> arguments, - TypeName type) { + Type type) { return MethodCallNode.methodCall(staticReceiver(className), methodName, arguments, type); } @@ -30,7 +32,7 @@ public class MethodCallNode implements ExpressionNode { ExpressionNode receiver, String methodName, List<ExpressionNode> arguments, - TypeName type) + Type type) { return methodCall(instanceReceiver(receiver), methodName, arguments, type); } @@ -39,7 +41,7 @@ public class MethodCallNode implements ExpressionNode { Receiver receiver, String methodName, List<ExpressionNode> arguments, - TypeName type) + Type type) { return new MethodCallNode(receiver, methodName, arguments, type); } @@ -47,13 +49,13 @@ public class MethodCallNode implements ExpressionNode { private final Receiver receiver; private final String methodName; private final List<ExpressionNode> arguments; - private final TypeName type; + private final Type type; private MethodCallNode( Receiver receiver, String methodName, List<ExpressionNode> arguments, - TypeName type) + Type type) { this.receiver = receiver; this.methodName = methodName; @@ -73,7 +75,7 @@ public class MethodCallNode implements ExpressionNode { return arguments; } - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/MethodNode.java b/src/main/java/org/zwobble/couscous/ast/MethodNode.java index 4909093..8589eb1 100644 --- a/src/main/java/org/zwobble/couscous/ast/MethodNode.java +++ b/src/main/java/org/zwobble/couscous/ast/MethodNode.java @@ -1,6 +1,7 @@ package org.zwobble.couscous.ast; import com.google.common.collect.ImmutableList; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; import org.zwobble.couscous.values.UnitValue; @@ -27,7 +28,7 @@ public class MethodNode implements Node { private final String name; private final ImmutableList.Builder<FormalArgumentNode> arguments = ImmutableList.builder(); - private TypeName returnType = UnitValue.REF; + private Type returnType = UnitValue.REF; private final ImmutableList.Builder<StatementNode> body = ImmutableList.builder(); @@ -60,7 +61,7 @@ public class MethodNode implements Node { return this; } - public Builder returns(TypeName returnType) { + public Builder returns(Type returnType) { this.returnType = returnType; return this; } @@ -91,7 +92,7 @@ public class MethodNode implements Node { boolean isStatic, String name, List<FormalArgumentNode> arguments, - TypeName returnType, + Type returnType, Optional<List<StatementNode>> body) { return new MethodNode(annotations, isStatic, name, arguments, returnType, body); @@ -101,7 +102,7 @@ public class MethodNode implements Node { private final boolean isStatic; private final String name; private final List<FormalArgumentNode> arguments; - private final TypeName returnType; + private final Type returnType; private final Optional<List<StatementNode>> body; private MethodNode( @@ -109,7 +110,7 @@ public class MethodNode implements Node { boolean isStatic, String name, List<FormalArgumentNode> arguments, - TypeName returnType, + Type returnType, Optional<List<StatementNode>> body) { this.annotations = annotations; @@ -136,7 +137,7 @@ public class MethodNode implements Node { return arguments; } - public TypeName getReturnType() { + public Type getReturnType() { return returnType; } diff --git a/src/main/java/org/zwobble/couscous/ast/MethodSignature.java b/src/main/java/org/zwobble/couscous/ast/MethodSignature.java index 908e673..93d4f58 100644 --- a/src/main/java/org/zwobble/couscous/ast/MethodSignature.java +++ b/src/main/java/org/zwobble/couscous/ast/MethodSignature.java @@ -1,13 +1,15 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; + import java.util.List; public class MethodSignature { private final String name; - private final List<TypeName> arguments; - private final TypeName returnType; + private final List<Type> arguments; + private final Type returnType; - public MethodSignature(String name, List<TypeName> arguments, TypeName returnType) { + public MethodSignature(String name, List<Type> arguments, Type returnType) { this.name = name; this.arguments = arguments; this.returnType = returnType; @@ -17,11 +19,11 @@ public class MethodSignature { return name; } - public List<TypeName> getArguments() { + public List<Type> getArguments() { return arguments; } - public TypeName getReturnType() { + public Type getReturnType() { return returnType; } diff --git a/src/main/java/org/zwobble/couscous/ast/OperationNode.java b/src/main/java/org/zwobble/couscous/ast/OperationNode.java index 4348c33..d6453e3 100644 --- a/src/main/java/org/zwobble/couscous/ast/OperationNode.java +++ b/src/main/java/org/zwobble/couscous/ast/OperationNode.java @@ -1,20 +1,21 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; import java.util.List; public class OperationNode implements ExpressionNode { - public static OperationNode operation(Operator operator, List<ExpressionNode> arguments, TypeName type) { + public static OperationNode operation(Operator operator, List<ExpressionNode> arguments, Type type) { return new OperationNode(operator, arguments, type); } private final Operator operator; private final List<ExpressionNode> arguments; - private TypeName type; + private Type type; - OperationNode(Operator operator, List<ExpressionNode> arguments, TypeName type) { + OperationNode(Operator operator, List<ExpressionNode> arguments, Type type) { this.operator = operator; this.arguments = arguments; this.type = type; @@ -41,7 +42,7 @@ public class OperationNode implements ExpressionNode { } @Override - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/Receiver.java b/src/main/java/org/zwobble/couscous/ast/Receiver.java index 1afb9b7..90395d2 100644 --- a/src/main/java/org/zwobble/couscous/ast/Receiver.java +++ b/src/main/java/org/zwobble/couscous/ast/Receiver.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.visitors.NodeTransformer; public interface Receiver extends Node { @@ -8,6 +9,6 @@ public interface Receiver extends Node { interface Mapper<T> { T visit(ExpressionNode receiver); - T visit(TypeName receiver); + T visit(ScalarType receiver); } } diff --git a/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java b/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java index 84cf266..3c6e32c 100644 --- a/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java +++ b/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java @@ -1,20 +1,21 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class StaticReceiver implements Receiver { - public static Receiver staticReceiver(TypeName type) { + public static Receiver staticReceiver(ScalarType type) { return new StaticReceiver(type); } - private final TypeName type; + private final ScalarType type; - public StaticReceiver(TypeName type) { + public StaticReceiver(ScalarType type) { this.type = type; } - public TypeName getType() { + public ScalarType getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/TernaryConditionalNode.java b/src/main/java/org/zwobble/couscous/ast/TernaryConditionalNode.java index 3caec58..2d46473 100644 --- a/src/main/java/org/zwobble/couscous/ast/TernaryConditionalNode.java +++ b/src/main/java/org/zwobble/couscous/ast/TernaryConditionalNode.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -50,7 +51,7 @@ public class TernaryConditionalNode implements ExpressionNode { } @Override - public TypeName getType() { + public Type getType() { return ifTrue.getType(); } diff --git a/src/main/java/org/zwobble/couscous/ast/ThisReferenceNode.java b/src/main/java/org/zwobble/couscous/ast/ThisReferenceNode.java index ebc2d8d..cdf995b 100644 --- a/src/main/java/org/zwobble/couscous/ast/ThisReferenceNode.java +++ b/src/main/java/org/zwobble/couscous/ast/ThisReferenceNode.java @@ -1,20 +1,21 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class ThisReferenceNode implements ExpressionNode, ReferenceNode { - public static ThisReferenceNode thisReference(TypeName type) { + public static ThisReferenceNode thisReference(Type type) { return new ThisReferenceNode(type); } - private final TypeName type; + private final Type type; - private ThisReferenceNode(TypeName type) { + private ThisReferenceNode(Type type) { this.type = type; } - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/TypeCoercionNode.java b/src/main/java/org/zwobble/couscous/ast/TypeCoercionNode.java index 680ecea..c8c2a33 100644 --- a/src/main/java/org/zwobble/couscous/ast/TypeCoercionNode.java +++ b/src/main/java/org/zwobble/couscous/ast/TypeCoercionNode.java @@ -1,17 +1,19 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class TypeCoercionNode implements ExpressionNode { - public static TypeCoercionNode typeCoercion(ExpressionNode expression, TypeName type) { + public static TypeCoercionNode typeCoercion(ExpressionNode expression, Type type) { return new TypeCoercionNode(expression, type); } private final ExpressionNode expression; - private final TypeName type; + private final Type type; - private TypeCoercionNode(ExpressionNode expression, TypeName type) { + private TypeCoercionNode(ExpressionNode expression, Type type) { this.expression = expression; this.type = type; } @@ -21,7 +23,7 @@ public class TypeCoercionNode implements ExpressionNode { } @Override - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/TypeNode.java b/src/main/java/org/zwobble/couscous/ast/TypeNode.java index e3a78af..af4ab81 100644 --- a/src/main/java/org/zwobble/couscous/ast/TypeNode.java +++ b/src/main/java/org/zwobble/couscous/ast/TypeNode.java @@ -1,14 +1,16 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeTransformer; import java.util.List; import java.util.Set; public interface TypeNode extends Node { - TypeName getName(); + ScalarType getName(); List<FormalTypeParameterNode> getTypeParameters(); - Set<TypeName> getSuperTypes(); + Set<Type> getSuperTypes(); List<MethodNode> getMethods(); TypeNode transform(NodeTransformer transformer); } diff --git a/src/main/java/org/zwobble/couscous/ast/VariableDeclaration.java b/src/main/java/org/zwobble/couscous/ast/VariableDeclaration.java index 5932371..55d4efb 100644 --- a/src/main/java/org/zwobble/couscous/ast/VariableDeclaration.java +++ b/src/main/java/org/zwobble/couscous/ast/VariableDeclaration.java @@ -1,17 +1,18 @@ package org.zwobble.couscous.ast; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.Type; public class VariableDeclaration { - public static VariableDeclaration var(Identifier id, String name, TypeName type) { + public static VariableDeclaration var(Identifier id, String name, Type type) { return new VariableDeclaration(id, name, type); } private final Identifier id; private final String name; - private final TypeName type; + private final Type type; - private VariableDeclaration(Identifier id, String name, TypeName type) { + private VariableDeclaration(Identifier id, String name, Type type) { this.id = id; this.name = name; this.type = type; @@ -25,7 +26,7 @@ public class VariableDeclaration { return name; } - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/VariableReferenceNode.java b/src/main/java/org/zwobble/couscous/ast/VariableReferenceNode.java index f9800a5..2b81fbb 100644 --- a/src/main/java/org/zwobble/couscous/ast/VariableReferenceNode.java +++ b/src/main/java/org/zwobble/couscous/ast/VariableReferenceNode.java @@ -1,6 +1,7 @@ package org.zwobble.couscous.ast; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.AssignableExpressionNodeVisitor; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -34,7 +35,7 @@ public class VariableReferenceNode implements AssignableExpressionNode, Referenc } @Override - public TypeName getType() { + public Type getType() { return referent.getType(); } diff --git a/src/main/java/org/zwobble/couscous/ast/sugar/AnonymousClass.java b/src/main/java/org/zwobble/couscous/ast/sugar/AnonymousClass.java index fea72be..f8a8c48 100644 --- a/src/main/java/org/zwobble/couscous/ast/sugar/AnonymousClass.java +++ b/src/main/java/org/zwobble/couscous/ast/sugar/AnonymousClass.java @@ -2,18 +2,18 @@ package org.zwobble.couscous.ast.sugar; import org.zwobble.couscous.ast.FieldDeclarationNode; import org.zwobble.couscous.ast.MethodNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import java.util.List; import java.util.Set; public class AnonymousClass { - private final Set<TypeName> superTypes; + private final Set<Type> superTypes; private final List<FieldDeclarationNode> fields; private final List<MethodNode> methods; public AnonymousClass( - Set<TypeName> superTypes, + Set<Type> superTypes, List<FieldDeclarationNode> fields, List<MethodNode> methods) { @@ -23,7 +23,7 @@ public class AnonymousClass { this.methods = methods; } - public Set<TypeName> getSuperTypes() { + public Set<Type> getSuperTypes() { return superTypes; } diff --git a/src/main/java/org/zwobble/couscous/ast/TypeName.java b/src/main/java/org/zwobble/couscous/ast/types/ScalarType.java index e2c32d0..69b9f97 100644 --- a/src/main/java/org/zwobble/couscous/ast/TypeName.java +++ b/src/main/java/org/zwobble/couscous/ast/types/ScalarType.java @@ -1,20 +1,20 @@ -package org.zwobble.couscous.ast; +package org.zwobble.couscous.ast.types; import com.google.common.base.Strings; import java.util.Optional; -public class TypeName { - public static TypeName of(String qualifiedName) { +public class ScalarType implements Type { + public static ScalarType of(String qualifiedName) { if (Strings.isNullOrEmpty(qualifiedName)) { throw new IllegalArgumentException("qualifiedName cannot be blank"); } - return new TypeName(qualifiedName); + return new ScalarType(qualifiedName); } private final String qualifiedName; - private TypeName(String qualifiedName) { + private ScalarType(String qualifiedName) { this.qualifiedName = qualifiedName; } @@ -37,7 +37,7 @@ public class TypeName { @Override public String toString() { - return "TypeName(qualifiedName=" + qualifiedName + ")"; + return "ScalarType(qualifiedName=" + qualifiedName + ")"; } @Override @@ -57,7 +57,7 @@ public class TypeName { return false; if (getClass() != obj.getClass()) return false; - TypeName other = (TypeName) obj; + ScalarType other = (ScalarType) obj; if (qualifiedName == null) { if (other.qualifiedName != null) return false; @@ -65,4 +65,9 @@ public class TypeName { return false; return true; } + + @Override + public <T> T accept(Visitor<T> visitor) { + return visitor.visit(this); + } } diff --git a/src/main/java/org/zwobble/couscous/ast/types/Type.java b/src/main/java/org/zwobble/couscous/ast/types/Type.java new file mode 100644 index 0000000..e936279 --- /dev/null +++ b/src/main/java/org/zwobble/couscous/ast/types/Type.java @@ -0,0 +1,9 @@ +package org.zwobble.couscous.ast.types; + +public interface Type { + <T> T accept(Visitor<T> visitor); + + interface Visitor<T> { + T visit(ScalarType type); + } +} diff --git a/src/main/java/org/zwobble/couscous/ast/types/Types.java b/src/main/java/org/zwobble/couscous/ast/types/Types.java new file mode 100644 index 0000000..c222c54 --- /dev/null +++ b/src/main/java/org/zwobble/couscous/ast/types/Types.java @@ -0,0 +1,12 @@ +package org.zwobble.couscous.ast.types; + +public class Types { + public static ScalarType erasure(Type type) { + return type.accept(new Type.Visitor<ScalarType>() { + @Override + public ScalarType visit(ScalarType type) { + return type; + } + }); + } +} diff --git a/src/main/java/org/zwobble/couscous/ast/visitors/NodeTransformer.java b/src/main/java/org/zwobble/couscous/ast/visitors/NodeTransformer.java index 5d0539a..38c5709 100644 --- a/src/main/java/org/zwobble/couscous/ast/visitors/NodeTransformer.java +++ b/src/main/java/org/zwobble/couscous/ast/visitors/NodeTransformer.java @@ -5,6 +5,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import java.util.List; import java.util.Map; @@ -154,7 +156,7 @@ public class NodeTransformer { public static class Builder { private Function<ExpressionNode, Optional<ExpressionNode>> transformExpression = expression -> Optional.empty(); - private Function<TypeName, TypeName> transformType = type -> type; + private Function<Type, Type> transformType = type -> type; private Function<MethodSignature, String> transformMethodName = MethodSignature::getName; public Builder transformExpression( @@ -165,7 +167,7 @@ public class NodeTransformer { } public Builder transformType( - Function<TypeName, TypeName> transformType) + Function<Type, Type> transformType) { this.transformType = transformType; return this; @@ -182,12 +184,12 @@ public class NodeTransformer { } private final Function<ExpressionNode, Optional<ExpressionNode>> transformExpression; - private final Function<TypeName, TypeName> transformType; + private final Function<Type, Type> transformType; private final Function<MethodSignature, String> transformMethodName; private NodeTransformer( Function<ExpressionNode, Optional<ExpressionNode>> transformExpression, - Function<TypeName, TypeName> transformType, + Function<Type, Type> transformType, Function<MethodSignature, String> transformMethodName) { this.transformExpression = transformExpression; @@ -195,10 +197,14 @@ public class NodeTransformer { this.transformMethodName = transformMethodName; } - public TypeName transform(TypeName type) { + public Type transform(Type type) { return transformType.apply(type); } + public ScalarType transform(ScalarType type) { + return (ScalarType)transformType.apply(type); + } + public String transformMethodName(MethodSignature signature) { return transformMethodName.apply(signature); } @@ -256,7 +262,7 @@ public class NodeTransformer { return transformList(annotations, this::transformAnnotation); } - public Set<TypeName> transformTypes(Set<TypeName> superTypes) { + public Set<Type> transformTypes(Set<Type> superTypes) { return transformSet(superTypes, this::transform); } diff --git a/src/main/java/org/zwobble/couscous/backends/Names.java b/src/main/java/org/zwobble/couscous/backends/Names.java index d467296..094de02 100644 --- a/src/main/java/org/zwobble/couscous/backends/Names.java +++ b/src/main/java/org/zwobble/couscous/backends/Names.java @@ -3,9 +3,10 @@ package org.zwobble.couscous.backends; import com.google.common.base.Joiner; import com.google.common.collect.Iterables; import org.zwobble.couscous.ast.MethodSignature; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import static com.google.common.collect.Iterables.transform; +import static org.zwobble.couscous.ast.types.Types.erasure; import static org.zwobble.couscous.util.ExtraLists.list; public class Names { @@ -16,7 +17,7 @@ public class Names { list(typeToString(signature.getReturnType())))); } - private static String typeToString(TypeName argument) { - return argument.getQualifiedName().replace('.', '_'); + private static String typeToString(Type argument) { + return erasure(argument).getQualifiedName().replace('.', '_'); } } diff --git a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpBackend.java b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpBackend.java index e98a526..3c58dae 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpBackend.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpBackend.java @@ -33,7 +33,7 @@ public class CsharpBackend implements Backend { public void compile(List<TypeNode> classes) throws IOException { Files.write( directoryPath.resolve("Program.cs"), - Iterables.concat( + Iterables.<String>concat( transform( classes, classNode -> compileClass(classNode)), diff --git a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java index 5a90dc7..8388421 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java @@ -2,6 +2,8 @@ package org.zwobble.couscous.backends.csharp; import com.google.common.collect.ImmutableMap; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeTransformer; import org.zwobble.couscous.backends.Names; import org.zwobble.couscous.values.*; @@ -9,15 +11,16 @@ import org.zwobble.couscous.values.*; import java.util.Map; import java.util.Optional; +import static org.zwobble.couscous.ast.types.Types.erasure; import static org.zwobble.couscous.util.Casts.tryCast; public class CsharpCodeGenerator { - private final static Map<TypeName, TypeName> PRIMITIVES = ImmutableMap.<TypeName, TypeName>builder() - .put(IntegerValue.REF, TypeName.of("int")) - .put(StringValue.REF, TypeName.of("string")) - .put(BooleanValue.REF, TypeName.of("bool")) - .put(ObjectValues.CLASS, TypeName.of("System.Type")) - .put(UnitValue.REF, TypeName.of("void")) + private final static Map<ScalarType, ScalarType> PRIMITIVES = ImmutableMap.<ScalarType, ScalarType>builder() + .put(IntegerValue.REF, ScalarType.of("int")) + .put(StringValue.REF, ScalarType.of("string")) + .put(BooleanValue.REF, ScalarType.of("bool")) + .put(ObjectValues.CLASS, ScalarType.of("System.Type")) + .put(UnitValue.REF, ScalarType.of("void")) .build(); private final String namespace; @@ -40,7 +43,7 @@ public class CsharpCodeGenerator { return nodeTransformer.transform(node); } - private TypeName transformType(TypeName type) { + private Type transformType(Type type) { if (PRIMITIVES.containsKey(type)) { return PRIMITIVES.get(type); } else { @@ -61,21 +64,26 @@ public class CsharpCodeGenerator { return call.getReceiver().accept(new Receiver.Mapper<Optional<ExpressionNode>>() { @Override public Optional<ExpressionNode> visit(ExpressionNode receiver) { - return CsharpPrimitiveMethods.getPrimitiveMethod(receiver.getType(), call.getMethodName()) + return CsharpPrimitiveMethods.getPrimitiveMethod(erasure(receiver.getType()), call.getMethodName()) .map(generator -> generator.generate( nodeTransformer.transformExpression(receiver), nodeTransformer.transformExpressions(call.getArguments()))); } @Override - public Optional<ExpressionNode> visit(TypeName receiver) { + public Optional<ExpressionNode> visit(ScalarType receiver) { return CsharpPrimitiveMethods.getPrimitiveStaticMethod(receiver, call.getMethodName()) .map(generator -> generator.generate(nodeTransformer.transformExpressions(call.getArguments()))); } }); } - private TypeName addPrefix(TypeName name, String namespace) { - return TypeName.of(namespace + "." + name.getQualifiedName()); + private Type addPrefix(Type type, String namespace) { + return type.accept(new Type.Visitor<Type>() { + @Override + public Type visit(ScalarType type) { + return ScalarType.of(namespace + "." + type.getQualifiedName()); + } + }); } } diff --git a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpPrimitiveMethods.java b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpPrimitiveMethods.java index 72aa528..ced0a82 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpPrimitiveMethods.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpPrimitiveMethods.java @@ -2,7 +2,7 @@ package org.zwobble.couscous.backends.csharp; import com.google.common.collect.ImmutableMap; import org.zwobble.couscous.ast.ExpressionNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.ObjectValues; @@ -67,8 +67,8 @@ public class CsharpPrimitiveMethods { STRING_METHODS = methods.build(); } - private static final Map<TypeName, Map<String, PrimitiveMethodGenerator>> METHODS = - ImmutableMap.<TypeName, Map<String, PrimitiveMethodGenerator>>builder() + private static final Map<ScalarType, Map<String, PrimitiveMethodGenerator>> METHODS = + ImmutableMap.<ScalarType, Map<String, PrimitiveMethodGenerator>>builder() .put(BooleanValue.REF, BOOLEAN_METHODS) .put(StringValue.REF, STRING_METHODS) .put(ObjectValues.BOXED_INT, BOXED_INT_METHODS) @@ -78,19 +78,19 @@ public class CsharpPrimitiveMethods { ImmutableMap.<String, PrimitiveStaticMethodGenerator>builder() .put("parseInt", arguments -> staticMethodCall( - TypeName.of("int"), + ScalarType.of("int"), "Parse", list(arguments.get(0)), - TypeName.of("int"))) + ScalarType.of("int"))) .build(); - private static final Map<TypeName, Map<String, PrimitiveStaticMethodGenerator>> STATIC_METHODS = - ImmutableMap.<TypeName, Map<String, PrimitiveStaticMethodGenerator>>builder() + private static final Map<ScalarType, Map<String, PrimitiveStaticMethodGenerator>> STATIC_METHODS = + ImmutableMap.<ScalarType, Map<String, PrimitiveStaticMethodGenerator>>builder() .put(ObjectValues.BOXED_INT, STATIC_INT_METHODS) .build(); - public static Optional<PrimitiveMethodGenerator> getPrimitiveMethod(TypeName type, String methodName) { + public static Optional<PrimitiveMethodGenerator> getPrimitiveMethod(ScalarType type, String methodName) { return Optional.ofNullable(METHODS.get(type)) .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName))); } @@ -100,7 +100,7 @@ public class CsharpPrimitiveMethods { ExpressionNode generate(ExpressionNode receiver, List<ExpressionNode> arguments); } - public static Optional<PrimitiveStaticMethodGenerator> getPrimitiveStaticMethod(TypeName type, String methodName) { + public static Optional<PrimitiveStaticMethodGenerator> getPrimitiveStaticMethod(ScalarType type, String methodName) { return Optional.ofNullable(STATIC_METHODS.get(type)) .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName))); } diff --git a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java index 39cd443..c898525 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java @@ -1,6 +1,8 @@ package org.zwobble.couscous.backends.csharp; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeVisitor; import org.zwobble.couscous.backends.SourceCodeWriter; import org.zwobble.couscous.util.Action; @@ -66,7 +68,7 @@ public class CsharpSerializer implements NodeVisitor { } @Override - public Void visitType(TypeName value) { + public Void visitType(ScalarType value) { writer.writeKeyword("typeof"); writer.writeSymbol("("); writeTypeReference(value); @@ -413,11 +415,17 @@ public class CsharpSerializer implements NodeVisitor { } } - private void writeTypeReference(TypeName value) { - writer.writeIdentifier(typeReference(value)); + private void writeTypeReference(Type value) { + value.accept(new Type.Visitor<Void>() { + @Override + public Void visit(ScalarType type) { + writer.writeIdentifier(typeReference(type)); + return null; + } + }); } - private String typeReference(TypeName value) { + private String typeReference(ScalarType value) { return value.getQualifiedName(); } } diff --git a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java index 7d134ef..49e7b80 100644 --- a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java +++ b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java @@ -6,6 +6,8 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Iterators; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.structure.NodeStructure; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeMapperWithDefault; import org.zwobble.couscous.ast.visitors.StatementNodeMapper; @@ -20,6 +22,7 @@ import java.util.stream.Stream; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; import static com.google.common.collect.Iterators.singletonIterator; +import static org.zwobble.couscous.ast.types.Types.erasure; import static org.zwobble.couscous.backends.python.ast.PythonAssignmentNode.pythonAssignment; import static org.zwobble.couscous.backends.python.ast.PythonAttributeAccessNode.pythonAttributeAccess; import static org.zwobble.couscous.backends.python.ast.PythonBooleanLiteralNode.pythonBooleanLiteral; @@ -72,7 +75,7 @@ public class PythonCodeGenerator { } private static Stream<PythonImportNode> generateImports(TypeNode classNode) { - Set<TypeName> classes = findReferencedClasses(classNode); + Set<ScalarType> classes = findReferencedClasses(classNode); return classes.stream() .filter(name -> !name.equals(InternalCouscousValue.REF)) .map(name -> pythonImport(importPathToRoot(classNode) + name.getQualifiedName(), list(pythonImportAlias(name.getSimpleName())))); @@ -93,21 +96,21 @@ public class PythonCodeGenerator { return depth; } - private static Set<TypeName> findReferencedClasses(TypeNode classNode) { + private static Set<ScalarType> findReferencedClasses(TypeNode classNode) { return NodeStructure.descendantNodes(classNode) - .flatMap(node -> node.accept(new NodeMapperWithDefault<Stream<TypeName>>(Stream.empty()) { + .flatMap(node -> node.accept(new NodeMapperWithDefault<Stream<ScalarType>>(Stream.empty()) { @Override - public Stream<TypeName> visit(StaticReceiver receiver) { + public Stream<ScalarType> visit(StaticReceiver receiver) { return Stream.of(receiver.getType()); } @Override - public Stream<TypeName> visit(ConstructorCallNode call) { - return Stream.of(call.getType()); + public Stream<ScalarType> visit(ConstructorCallNode call) { + return Stream.of(erasure(call.getType())); } @Override - public Stream<TypeName> visit(TypeCoercionNode typeCoercion) { + public Stream<ScalarType> visit(TypeCoercionNode typeCoercion) { if (isIntegerBox(typeCoercion)) { return Stream.of(ObjectValues.BOXED_INT); } else { @@ -116,7 +119,7 @@ public class PythonCodeGenerator { } @Override - public Stream<TypeName> visit(LiteralNode literal) { + public Stream<ScalarType> visit(LiteralNode literal) { if (literal.getValue() instanceof TypeValue) { return Stream.of(((TypeValue)literal.getValue()).getValue()); } else { @@ -151,7 +154,7 @@ public class PythonCodeGenerator { } @Override - public PythonExpressionNode visitType(TypeName value) { + public PythonExpressionNode visitType(ScalarType value) { return typeReference(value); } }); @@ -272,12 +275,12 @@ public class PythonCodeGenerator { return methodCall.getReceiver().accept(new Receiver.Mapper<Optional<PythonExpressionNode>>() { @Override public Optional<PythonExpressionNode> visit(ExpressionNode receiver) { - return PythonPrimitiveMethods.getPrimitiveMethod(receiver.getType(), methodCall.getMethodName()) + return PythonPrimitiveMethods.getPrimitiveMethod(erasure(receiver.getType()), methodCall.getMethodName()) .map(generator -> generator.generate(pythonReceiver, pythonArguments)); } @Override - public Optional<PythonExpressionNode> visit(TypeName receiver) { + public Optional<PythonExpressionNode> visit(ScalarType receiver) { return PythonPrimitiveMethods.getPrimitiveStaticMethod(receiver, methodCall.getMethodName()) .map(generator -> generator.generate(pythonArguments)); } @@ -286,7 +289,7 @@ public class PythonCodeGenerator { @Override public PythonExpressionNode visit(ConstructorCallNode call) { - final org.zwobble.couscous.ast.TypeName className = call.getType(); + final Type className = call.getType(); final org.zwobble.couscous.backends.python.ast.PythonVariableReferenceNode classReference = typeReference(className); final java.util.List<org.zwobble.couscous.backends.python.ast.PythonExpressionNode> arguments = generateExpressions(call.getArguments()); return pythonCall(classReference, arguments); @@ -334,7 +337,7 @@ public class PythonCodeGenerator { } @Override - public PythonExpressionNode visit(TypeName receiver) { + public PythonExpressionNode visit(ScalarType receiver) { return typeReference(receiver); } }); @@ -348,11 +351,11 @@ public class PythonCodeGenerator { return !isInteger(typeCoercion.getExpression().getType()) && isInteger(typeCoercion.getType()); } - private static boolean isInteger(TypeName type) { + private static boolean isInteger(Type type) { return type.equals(IntegerValue.REF) || type.equals(ObjectValues.BOXED_INT); } - private static PythonVariableReferenceNode typeReference(TypeName className) { - return pythonVariableReference(className.getSimpleName()); + private static PythonVariableReferenceNode typeReference(Type className) { + return pythonVariableReference(erasure(className).getSimpleName()); } }
\ No newline at end of file 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 87fc0ac..49d9abf 100644 --- a/src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java +++ b/src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java @@ -3,7 +3,7 @@ package org.zwobble.couscous.backends.python; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import org.zwobble.couscous.ast.Operator; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.backends.python.ast.PythonBinaryOperation; import org.zwobble.couscous.backends.python.ast.PythonExpressionNode; import org.zwobble.couscous.backends.python.ast.PythonNotNode; @@ -110,8 +110,8 @@ public class PythonPrimitiveMethods { build.apply(receiver, arguments.get(0))); } - private static final Map<TypeName, Map<String, PrimitiveMethodGenerator>> METHODS = - ImmutableMap.<TypeName, Map<String, PrimitiveMethodGenerator>>builder() + private static final Map<ScalarType, Map<String, PrimitiveMethodGenerator>> METHODS = + ImmutableMap.<ScalarType, Map<String, PrimitiveMethodGenerator>>builder() .put(StringValue.REF, STRING_METHODS) .put(BooleanValue.REF, BOOLEAN_METHODS) .put(IntegerValue.REF, INT_METHODS) @@ -132,13 +132,13 @@ public class PythonPrimitiveMethods { .build(); - private static final Map<TypeName, Map<String, PrimitiveStaticMethodGenerator>> STATIC_METHODS = - ImmutableMap.<TypeName, Map<String, PrimitiveStaticMethodGenerator>>builder() + private static final Map<ScalarType, Map<String, PrimitiveStaticMethodGenerator>> STATIC_METHODS = + ImmutableMap.<ScalarType, Map<String, PrimitiveStaticMethodGenerator>>builder() .put(InternalCouscousValue.REF, INTERNAL_METHODS) .put(ObjectValues.BOXED_INT, BOXED_INT_STATIC_METHODS) .build(); - public static Optional<PrimitiveMethodGenerator> getPrimitiveMethod(TypeName type, String methodName) { + public static Optional<PrimitiveMethodGenerator> getPrimitiveMethod(ScalarType type, String methodName) { return Optional.ofNullable(METHODS.get(type)) .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName))); } @@ -148,7 +148,7 @@ public class PythonPrimitiveMethods { PythonExpressionNode generate(PythonExpressionNode receiver, List<PythonExpressionNode> arguments); } - public static Optional<PrimitiveStaticMethodGenerator> getPrimitiveStaticMethod(TypeName type, String methodName) { + public static Optional<PrimitiveStaticMethodGenerator> getPrimitiveStaticMethod(ScalarType type, String methodName) { return Optional.ofNullable(STATIC_METHODS.get(type)) .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName))); } diff --git a/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java b/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java index 9386ff6..18f43be 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java @@ -2,8 +2,7 @@ package org.zwobble.couscous.frontends.java; import org.zwobble.couscous.ast.ClassNode; import org.zwobble.couscous.ast.ReferenceNode; -import org.zwobble.couscous.ast.TypeName; -import org.zwobble.couscous.ast.VariableDeclaration; +import org.zwobble.couscous.ast.types.ScalarType; import java.util.List; @@ -20,7 +19,7 @@ public class GeneratedClosure { return classNode; } - public TypeName getType() { + public ScalarType getType() { return classNode.getName(); } diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java index 545b1c0..b08bd0c 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java @@ -5,8 +5,12 @@ import org.eclipse.jdt.core.dom.ExpressionMethodReference; import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.Modifier; -import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.ExpressionNode; +import org.zwobble.couscous.ast.FormalArgumentNode; +import org.zwobble.couscous.ast.VariableReferenceNode; import org.zwobble.couscous.ast.sugar.Lambda; +import org.zwobble.couscous.ast.types.Type; +import org.zwobble.couscous.ast.types.Types; import java.util.List; @@ -52,11 +56,11 @@ public class JavaExpressionMethodReferenceReader { IMethodBinding methodBinding = expression.resolveMethodBinding(); String methodName = expression.getName().getIdentifier(); List<ExpressionNode> arguments = eagerMap(formalArguments, VariableReferenceNode::reference); - TypeName type = typeOf(methodBinding.getReturnType()); + Type type = typeOf(methodBinding.getReturnType()); if (Modifier.isStatic(methodBinding.getModifiers())) { return staticMethodCall( - typeOf(methodBinding.getDeclaringClass()), + Types.erasure(typeOf(methodBinding.getDeclaringClass())), methodName, arguments, type); diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java index 3d8e1c0..98f2686 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java @@ -2,6 +2,8 @@ package org.zwobble.couscous.frontends.java; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.Type; +import org.zwobble.couscous.ast.types.Types; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.ObjectValues; @@ -31,12 +33,12 @@ public class JavaExpressionReader { this.javaReader = javaReader; } - ExpressionNode readExpression(TypeName targetType, Expression expression) { + ExpressionNode readExpression(Type targetType, Expression expression) { ExpressionNode couscousExpression = readExpressionWithoutBoxing(expression); return coerceExpression(targetType, couscousExpression); } - static ExpressionNode coerceExpression(TypeName targetType, ExpressionNode couscousExpression) { + static ExpressionNode coerceExpression(Type targetType, ExpressionNode couscousExpression) { if (targetType.equals(couscousExpression.getType())) { return couscousExpression; } else { @@ -44,7 +46,7 @@ public class JavaExpressionReader { } } - private static TypeName unboxedType(TypeName type) { + private static Type unboxedType(Type type) { if (type.equals(ObjectValues.BOXED_INT)) { return IntegerValue.REF; } else if (type.equals(ObjectValues.BOXED_BOOLEAN)) { @@ -131,7 +133,7 @@ public class JavaExpressionReader { } private ExpressionNode readTypeLiteral(TypeLiteral expression) { - return literal(typeOf(expression.getType())); + return literal(Types.erasure(typeOf(expression.getType()))); } private ExpressionNode readSimpleName(SimpleName expression) { @@ -148,9 +150,9 @@ public class JavaExpressionReader { return scope.reference(binding.getKey()); } else { boolean isStatic = Modifier.isStatic(binding.getModifiers()); - TypeName classType = typeOf(binding.getDeclaringClass()); + Type classType = typeOf(binding.getDeclaringClass()); Receiver receiver = isStatic - ? new StaticReceiver(classType) + ? new StaticReceiver(Types.erasure(classType)) : new InstanceReceiver(ThisReferenceNode.thisReference(classType)); return FieldAccessNode.fieldAccess( receiver, @@ -167,7 +169,7 @@ public class JavaExpressionReader { IVariableBinding fieldBinding = expression.resolveFieldBinding(); boolean isStatic = Modifier.isStatic(fieldBinding.getModifiers()); Receiver receiver = isStatic - ? new StaticReceiver(typeOf(fieldBinding.getDeclaringClass())) + ? new StaticReceiver(Types.erasure(typeOf(fieldBinding.getDeclaringClass()))) : new InstanceReceiver(readExpressionWithoutBoxing(expression.getExpression())); return FieldAccessNode.fieldAccess( @@ -182,13 +184,13 @@ public class JavaExpressionReader { List<ExpressionNode> arguments = readArguments( expression.resolveMethodBinding(), expression.arguments()); - final TypeName type = typeOf(expression); + final Type type = typeOf(expression); IMethodBinding methodBinding = expression.resolveMethodBinding(); - TypeName receiverType = typeOf(methodBinding.getDeclaringClass()); + Type receiverType = typeOf(methodBinding.getDeclaringClass()); if ((Modifier.isStatic(methodBinding.getModifiers()))) { return staticMethodCall( - receiverType, + Types.erasure(receiverType), methodName, arguments, type); @@ -268,15 +270,15 @@ public class JavaExpressionReader { } private static boolean isPrimitiveOperation(InfixExpression expression) { - TypeName leftOperandType = typeOf(expression.getLeftOperand()); - TypeName rightOperandType = typeOf(expression.getRightOperand()); + Type leftOperandType = typeOf(expression.getLeftOperand()); + Type rightOperandType = typeOf(expression.getRightOperand()); return isPrimitive(leftOperandType) || isPrimitive(rightOperandType) || (expression.getOperator() != InfixExpression.Operator.NOT_EQUALS && expression.getOperator() != InfixExpression.Operator.EQUALS); } - private static boolean isPrimitive(TypeName type) { + private static boolean isPrimitive(Type type) { return type.equals(BooleanValue.REF) || type.equals(IntegerValue.REF); } @@ -297,7 +299,7 @@ public class JavaExpressionReader { } private ExpressionNode readConditionalExpression(ConditionalExpression expression) { - TypeName type = typeOf(expression); + Type type = typeOf(expression); return TernaryConditionalNode.ternaryConditional( readExpression(BooleanValue.REF, expression.getExpression()), readExpression(type, expression.getThenExpression()), diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java index ee4fc06..30dc2f8 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java @@ -3,13 +3,13 @@ package org.zwobble.couscous.frontends.java; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.FormalArgumentNode; import org.zwobble.couscous.ast.StatementNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.*; import org.zwobble.couscous.ast.sugar.Lambda; +import org.zwobble.couscous.ast.types.Type; import java.util.List; import java.util.Optional; -import static java.util.Arrays.asList; import static org.zwobble.couscous.ast.ReturnNode.returns; import static org.zwobble.couscous.ast.sugar.Lambda.lambda; import static org.zwobble.couscous.frontends.java.JavaTypes.typeOf; @@ -44,7 +44,7 @@ public class JavaLambdaExpressionReader { } private List<StatementNode> readLambdaExpressionBody(Scope scope, LambdaExpression expression) { - TypeName returnType = typeOf(expression.resolveTypeBinding().getFunctionalInterfaceMethod().getReturnType()); + Type returnType = typeOf(expression.resolveTypeBinding().getFunctionalInterfaceMethod().getReturnType()); if (expression.getBody() instanceof Block) { @SuppressWarnings("unchecked") List<Statement> statements = ((Block) expression.getBody()).statements(); 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 73cf749..950e678 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -8,6 +8,8 @@ import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.sugar.AnonymousClass; import org.zwobble.couscous.ast.sugar.Lambda; import org.zwobble.couscous.ast.sugar.TypeDeclarationBody; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeTransformer; import org.zwobble.couscous.util.ExtraLists; @@ -30,6 +32,7 @@ import static org.zwobble.couscous.ast.InstanceReceiver.instanceReceiver; import static org.zwobble.couscous.ast.StaticReceiver.staticReceiver; import static org.zwobble.couscous.ast.ThisReferenceNode.thisReference; import static org.zwobble.couscous.ast.VariableReferenceNode.reference; +import static org.zwobble.couscous.ast.types.Types.erasure; import static org.zwobble.couscous.frontends.java.FreeVariables.findFreeVariables; import static org.zwobble.couscous.frontends.java.JavaTypes.*; import static org.zwobble.couscous.util.Casts.tryCast; @@ -62,7 +65,7 @@ public class JavaReader { private final ImmutableList.Builder<TypeNode> classes; private int anonymousClassCount = 0; private final Scope topScope = Scope.create(); - private final Map<TypeName, List<ReferenceNode>> nestedClasses = new HashMap<>(); + private final Map<ScalarType, List<ReferenceNode>> nestedClasses = new HashMap<>(); private JavaReader() { classes = ImmutableList.builder(); @@ -74,18 +77,18 @@ public class JavaReader { NodeTransformer transformer = NodeTransformer.builder() .transformExpression(expression -> tryCast(ConstructorCallNode.class, expression) - .flatMap(call -> lookup(nestedClasses, call.getType()).map(captures -> + .flatMap(call -> lookup(nestedClasses, erasure(call.getType())).map(captures -> new ConstructorCallNode(call.getType(), ExtraLists.concat(captures, call.getArguments()))))) .build(); return eagerMap(classes, classNode -> classNode.transform(transformer)); } private TypeNode readTypeDeclaration(TypeDeclaration type) { - TypeName name = typeOf(type.resolveBinding()); + ScalarType name = erasure(typeOf(type.resolveBinding())); Scope scope = topScope.enterClass(name); List<FormalTypeParameterNode> typeParameters = readTypeParameters(type); TypeDeclarationBody body = readTypeDeclarationBody(scope, name, type.bodyDeclarations()); - Set<TypeName> superTypes = superTypes(type); + Set<Type> superTypes = superTypes(type); if (type.isInterface()) { // TODO: throw exception if other parts of body are declared return InterfaceNode.declareInterface(name, typeParameters, superTypes, body.getMethods()); @@ -122,7 +125,7 @@ public class JavaReader { } private GeneratedClosure readLambda(Scope outerScope, Expression expression, Function<Scope, Lambda> lambda) { - TypeName name = generateAnonymousName(expression); + ScalarType name = generateAnonymousName(expression); Scope scope = outerScope.enterClass(name); IMethodBinding functionalInterfaceMethod = expression.resolveTypeBinding().getFunctionalInterfaceMethod(); return generateClosure(scope, name, toAnonymousClass(functionalInterfaceMethod, lambda.apply(scope))); @@ -144,7 +147,7 @@ public class JavaReader { } private List<StatementNode> replaceCaptureReferences( - TypeName className, + ScalarType className, List<StatementNode> body, List<CapturedVariable> freeVariables) { @@ -157,7 +160,7 @@ public class JavaReader { private ConstructorNode buildConstructor( Scope outerScope, - TypeName type, + ScalarType type, List<CapturedVariable> freeVariables, ConstructorNode existing) { @@ -178,13 +181,13 @@ public class JavaReader { return new ConstructorNode(arguments.build(), body.build()); } - private FieldAccessNode captureAccess(TypeName type, CapturedVariable freeVariable) { + private FieldAccessNode captureAccess(ScalarType type, CapturedVariable freeVariable) { FieldDeclarationNode field = freeVariable.field; return fieldAccess(thisReference(type), field.getName(), field.getType()); } GeneratedClosure readAnonymousClass(Scope outerScope, AnonymousClassDeclaration declaration) { - TypeName className = generateAnonymousName(declaration); + ScalarType className = generateAnonymousName(declaration); Scope scope = outerScope.enterClass(className); TypeDeclarationBody bodyDeclarations = readTypeDeclarationBody(scope, className, declaration.bodyDeclarations()); AnonymousClass anonymousClass = new AnonymousClass( @@ -194,7 +197,7 @@ public class JavaReader { return generateClosure(scope, className, anonymousClass); } - GeneratedClosure generateClosure(Scope scope, TypeName className, AnonymousClass anonymousClass) { + GeneratedClosure generateClosure(Scope scope, ScalarType className, AnonymousClass anonymousClass) { GeneratedClosure closure = classWithCapture(scope, className, anonymousClass); classes.add(closure.getClassNode()); return closure; @@ -245,7 +248,7 @@ public class JavaReader { return new GeneratedClosure(generatedClass, freeVariables); } - private boolean isThisReference(TypeName name, ReferenceNode reference) { + private boolean isThisReference(ScalarType name, ReferenceNode reference) { return tryCast(ThisReferenceNode.class, reference) .map(node -> node.getType().equals(name)) .orElse(false); @@ -253,7 +256,7 @@ public class JavaReader { private GeneratedClosure classWithCapture( Scope scope, - TypeName className, + ScalarType className, AnonymousClass anonymousClass ) { return classWithCapture(scope, ClassNode.declareClass( @@ -275,19 +278,19 @@ public class JavaReader { @Override public FieldDeclarationNode visit(ThisReferenceNode thisReference) { - TypeName type = thisReference.getType(); - String name = "this_" + type.getQualifiedName().replace(".", "__"); + Type type = thisReference.getType(); + String name = "this_" + erasure(type).getQualifiedName().replace(".", "__"); return field(name, type); } }); } - private TypeName generateAnonymousName(ASTNode node) { + private ScalarType generateAnonymousName(ASTNode node) { ITypeBinding type = findDeclaringClass(node); while (type.isAnonymous()) { type = type.getDeclaringClass(); } - return TypeName.of(type.getQualifiedName() + "_Anonymous_" + (anonymousClassCount++)); + return ScalarType.of(type.getQualifiedName() + "_Anonymous_" + (anonymousClassCount++)); } private ITypeBinding findDeclaringClass(ASTNode node) { @@ -297,7 +300,7 @@ public class JavaReader { return ((AbstractTypeDeclaration)node).resolveBinding(); } - private TypeDeclarationBody readTypeDeclarationBody(Scope scope, TypeName type, List<Object> bodyDeclarations) { + private TypeDeclarationBody readTypeDeclarationBody(Scope scope, ScalarType type, List<Object> bodyDeclarations) { TypeDeclarationBody.Builder body = TypeDeclarationBody.builder(); for (Object declaration : bodyDeclarations) { @@ -318,7 +321,7 @@ public class JavaReader { return body.build(); } - private TypeNode readNestedTypeDeclaration(TypeName outerType, TypeDeclaration typeDeclaration) { + private TypeNode readNestedTypeDeclaration(ScalarType outerType, TypeDeclaration typeDeclaration) { TypeNode typeNode = readTypeDeclaration(typeDeclaration); // TODO: can we remove duplication of scope creation with readTypeDeclaration()? Scope scope = topScope.enterClass(typeNode.getName()); @@ -332,10 +335,10 @@ public class JavaReader { .orElse(typeNode); } - private void readField(TypeDeclarationBody.Builder builder, Scope scope, TypeName declaringType, FieldDeclaration field) { + private void readField(TypeDeclarationBody.Builder builder, Scope scope, ScalarType declaringType, FieldDeclaration field) { @SuppressWarnings("unchecked") List<VariableDeclarationFragment> fragments = field.fragments(); - TypeName type = typeOf(field.getType()); + Type type = typeOf(field.getType()); fragments.forEach(fragment -> { builder.field(field(Modifier.isStatic(field.getModifiers()), fragment.getName().getIdentifier(), type)); if (fragment.getInitializer() != null) { @@ -358,7 +361,7 @@ public class JavaReader { List<FormalArgumentNode> formalArguments = readFormalArguments(scope, method); List<AnnotationNode> annotations = readAnnotations(method); - Optional<TypeName> returnType = Optional.ofNullable(method.getReturnType2()) + Optional<Type> returnType = Optional.ofNullable(method.getReturnType2()) .map(JavaTypes::typeOf); Optional<List<StatementNode>> body = readBody(scope, method, returnType); @@ -391,7 +394,7 @@ public class JavaReader { return eagerMap(parameters, parameter -> JavaVariableDeclarationReader.read(scope, parameter)); } - private Optional<List<StatementNode>> readBody(Scope scope, MethodDeclaration method, Optional<TypeName> returnType) { + private Optional<List<StatementNode>> readBody(Scope scope, MethodDeclaration method, Optional<Type> returnType) { if (method.getBody() == null) { return Optional.empty(); } else { @@ -406,12 +409,12 @@ public class JavaReader { return statementReader.readStatement(statement); } - List<StatementNode> readStatements(Scope scope, List<Statement> body, Optional<TypeName> returnType) { + List<StatementNode> readStatements(Scope scope, List<Statement> body, Optional<Type> returnType) { JavaStatementReader statementReader = new JavaStatementReader(scope, expressionReader(scope), returnType); return eagerFlatMap(body, statementReader::readStatement); } - ExpressionNode readExpression(Scope scope, TypeName targetType, Expression body) { + ExpressionNode readExpression(Scope scope, org.zwobble.couscous.ast.types.Type targetType, Expression body) { return expressionReader(scope).readExpression(targetType, body); } diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java index fb40ed3..ac039fb 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java @@ -3,6 +3,7 @@ package org.zwobble.couscous.frontends.java; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.sugar.SwitchCaseNode; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.ObjectValues; @@ -29,12 +30,12 @@ import static org.zwobble.couscous.util.UpToAndIncludingIterable.upToAndIncludin class JavaStatementReader { private final Scope scope; private final JavaExpressionReader expressionReader; - private final Optional<TypeName> returnType; + private final Optional<Type> returnType; JavaStatementReader( Scope scope, JavaExpressionReader expressionReader, - Optional<TypeName> returnType + Optional<org.zwobble.couscous.ast.types.Type> returnType ) { this.scope = scope; this.expressionReader = expressionReader; @@ -183,13 +184,13 @@ class JavaStatementReader { private List<StatementNode> readVariableDeclarationStatement(VariableDeclarationStatement statement) { @SuppressWarnings("unchecked") List<VariableDeclarationFragment> fragments = statement.fragments(); - TypeName type = typeOf(statement.getType()); + Type type = typeOf(statement.getType()); return readDeclarationFragments(fragments, type); } private List<StatementNode> readDeclarationFragments( List<VariableDeclarationFragment> fragments, - TypeName type + Type type ) { return eagerMap(fragments, fragment -> scope.localVariable( fragment.resolveBinding().getKey(), @@ -202,7 +203,7 @@ class JavaStatementReader { return expressionReader.readExpressionWithoutBoxing(expression); } - private ExpressionNode readExpression(TypeName targetType, Expression expression) { + private ExpressionNode readExpression(Type targetType, Expression expression) { return expressionReader.readExpression(targetType, expression); } } 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 117f3ca..fd59e8c 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java @@ -2,7 +2,9 @@ package org.zwobble.couscous.frontends.java; import com.google.common.collect.ImmutableSet; import org.eclipse.jdt.core.dom.*; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; +import org.zwobble.couscous.ast.types.Types; import org.zwobble.couscous.values.ObjectValues; import java.util.Optional; @@ -12,44 +14,44 @@ import static com.google.common.collect.Iterables.transform; import static java.util.Arrays.asList; class JavaTypes { - static TypeName typeOf(Expression expression) { + static Type typeOf(Expression expression) { return typeOf(expression.resolveTypeBinding()); } - static TypeName typeOf(IVariableBinding variableBinding) { + static Type typeOf(IVariableBinding variableBinding) { return typeOf(variableBinding.getType()); } - static TypeName typeOf(Type type) { + static Type typeOf(org.eclipse.jdt.core.dom.Type type) { return typeOf(type.resolveBinding()); } - static TypeName typeOf(ITypeBinding typeBinding) { + static Type typeOf(ITypeBinding typeBinding) { ITypeBinding outerClass = typeBinding.getDeclaringClass(); if (outerClass == null) { - return TypeName.of(typeBinding.getErasure().getQualifiedName()); + return ScalarType.of(typeBinding.getErasure().getQualifiedName()); } else { - return TypeName.of(typeOf(outerClass).getQualifiedName() + "__" + typeBinding.getName()); + return ScalarType.of(Types.erasure(typeOf(outerClass)).getQualifiedName() + "__" + typeBinding.getName()); } } - static Set<TypeName> superTypes(TypeDeclaration declaration) { + static Set<Type> superTypes(TypeDeclaration declaration) { return superTypes(declaration.resolveBinding()); } - static Set<TypeName> superTypes(AnonymousClassDeclaration declaration) { + static Set<Type> superTypes(AnonymousClassDeclaration declaration) { return superTypes(declaration.resolveBinding()); } - static Set<TypeName> superTypesAndSelf(ITypeBinding typeBinding) { - ImmutableSet.Builder<TypeName> superTypes = ImmutableSet.builder(); + static Set<Type> superTypesAndSelf(ITypeBinding typeBinding) { + ImmutableSet.Builder<Type> superTypes = ImmutableSet.builder(); superTypes.addAll(superTypes(typeBinding)); superTypes.add(typeOf(typeBinding)); return superTypes.build(); } - private static Set<TypeName> superTypes(ITypeBinding typeBinding) { - ImmutableSet.Builder<TypeName> superTypes = ImmutableSet.builder(); + private static Set<Type> superTypes(ITypeBinding typeBinding) { + ImmutableSet.Builder<Type> superTypes = ImmutableSet.builder(); superClass(typeBinding).ifPresent(superTypes::add); superTypes.addAll(transform( asList(typeBinding.getInterfaces()), @@ -57,7 +59,7 @@ class JavaTypes { return superTypes.build(); } - private static Optional<TypeName> superClass(ITypeBinding typeBinding) { + private static Optional<Type> superClass(ITypeBinding typeBinding) { return Optional.ofNullable(typeBinding.getSuperclass()) .map(JavaTypes::typeOf) .filter(type -> !type.equals(ObjectValues.OBJECT)); diff --git a/src/main/java/org/zwobble/couscous/frontends/java/Scope.java b/src/main/java/org/zwobble/couscous/frontends/java/Scope.java index 7ddc8ca..b02601a 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/Scope.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/Scope.java @@ -2,6 +2,8 @@ package org.zwobble.couscous.frontends.java; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.util.NaturalNumbers; import java.util.*; @@ -28,7 +30,7 @@ public class Scope { this.temporaryCounter = temporaryCounter; } - public Scope enterClass(TypeName className) { + public Scope enterClass(ScalarType className) { return enter("class#" + className.getQualifiedName()); } @@ -45,12 +47,12 @@ public class Scope { return new Scope(variablesByKey, identifiers, identifier.extend(name), temporaryCounter); } - public FormalArgumentNode formalArgument(String name, TypeName type) { + public FormalArgumentNode formalArgument(String name, Type type) { VariableDeclaration declaration = generateVariable(name, type); return FormalArgumentNode.formalArg(declaration); } - public FormalArgumentNode formalArgument(String key, String name, TypeName type) { + public FormalArgumentNode formalArgument(String key, String name, Type type) { VariableDeclaration declaration = generateVariable(key, name, type); return FormalArgumentNode.formalArg(declaration); } @@ -58,7 +60,7 @@ public class Scope { public LocalVariableDeclarationNode localVariable( String key, String name, - TypeName type, + Type type, ExpressionNode initialValue ) { VariableDeclaration declaration = generateVariable(key, name, type); @@ -67,7 +69,7 @@ public class Scope { public LocalVariableDeclarationNode localVariable( String name, - TypeName type, + Type type, ExpressionNode initialValue ) { VariableDeclaration declaration = generateVariable(name, type); @@ -78,7 +80,7 @@ public class Scope { return temporaryVariable(initialValue.getType(), initialValue); } - public LocalVariableDeclarationNode temporaryVariable(TypeName type, ExpressionNode initialValue) { + public LocalVariableDeclarationNode temporaryVariable(Type type, ExpressionNode initialValue) { return localVariable("_couscous_tmp_" + temporaryCounter.next(), type, initialValue); } @@ -90,7 +92,7 @@ public class Scope { return VariableReferenceNode.reference(variable); } - private VariableDeclaration generateVariable(String key, String name, TypeName type) { + private VariableDeclaration generateVariable(String key, String name, Type type) { if (variablesByKey.containsKey(key)) { throw new IllegalArgumentException(key + " is already mapped"); } @@ -99,7 +101,7 @@ public class Scope { return variable; } - public VariableDeclaration generateVariable(String name, TypeName type) { + public VariableDeclaration generateVariable(String name, Type type) { Identifier identifier = generateIdentifier(name); return VariableDeclaration.var(identifier, name, type); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/Environment.java b/src/main/java/org/zwobble/couscous/interpreter/Environment.java index daee3ed..fa5958b 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/Environment.java +++ b/src/main/java/org/zwobble/couscous/interpreter/Environment.java @@ -1,9 +1,10 @@ package org.zwobble.couscous.interpreter; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.VariableDeclaration; import org.zwobble.couscous.ast.VariableNode; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.errors.UnboundVariable; import org.zwobble.couscous.interpreter.errors.VariableNotInScope; import org.zwobble.couscous.interpreter.values.InterpreterValue; @@ -16,7 +17,7 @@ import java.util.Optional; import static java.util.stream.Collectors.toMap; public class Environment { - private final Map<TypeName, StaticReceiverValue> types; + private final Map<ScalarType, StaticReceiverValue> types; private final Optional<InterpreterValue> thisValue; private final Map<Identifier, VariableEntry> stackFrame; private final Project project; @@ -51,7 +52,7 @@ public class Environment { put(variable.getDeclaration().getId(), value); } - public StaticReceiverValue findClass(TypeName className) { + public StaticReceiverValue findClass(ScalarType className) { if (!types.containsKey(className)) { StaticReceiverValue value = new StaticReceiverValue(project.findClass(className)); types.put(className, value); @@ -71,24 +72,24 @@ public class Environment { } private void checkVariableType(Identifier variableId, InterpreterValue value) { - TypeName variableType = stackFrame.get(variableId).getType(); + Type variableType = stackFrame.get(variableId).getType(); InterpreterTypes.checkIsInstance(variableType, value); } private static class VariableEntry { - private final TypeName type; + private final Type type; private Optional<InterpreterValue> value; - private VariableEntry(final TypeName type, final Optional<InterpreterValue> value) { + private VariableEntry(final Type type, final Optional<InterpreterValue> value) { this.type = type; this.value = value; } - public static VariableEntry of(final TypeName type, final Optional<InterpreterValue> value) { + public static VariableEntry of(final Type type, final Optional<InterpreterValue> value) { return new VariableEntry(type, value); } - public TypeName getType() { + public Type getType() { return this.type; } diff --git a/src/main/java/org/zwobble/couscous/interpreter/Evaluator.java b/src/main/java/org/zwobble/couscous/interpreter/Evaluator.java index 1d88c6b..37038ab 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/Evaluator.java +++ b/src/main/java/org/zwobble/couscous/interpreter/Evaluator.java @@ -1,6 +1,7 @@ package org.zwobble.couscous.interpreter; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.visitors.AssignableExpressionNodeVisitor; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.interpreter.errors.ConditionMustBeBoolean; @@ -12,6 +13,8 @@ import org.zwobble.couscous.values.IntegerValue; import java.util.List; import java.util.stream.Collectors; +import static org.zwobble.couscous.ast.types.Types.erasure; + public class Evaluator implements ExpressionNodeMapper<InterpreterValue> { public static InterpreterValue eval(Environment environment, ExpressionNode expression) { return new Evaluator(environment).eval(expression); @@ -93,7 +96,7 @@ public class Evaluator implements ExpressionNodeMapper<InterpreterValue> { @Override public InterpreterValue visit(ConstructorCallNode call) { - StaticReceiverValue clazz = environment.findClass(call.getType()); + StaticReceiverValue clazz = environment.findClass(erasure(call.getType())); List<InterpreterValue> arguments = evalArguments(call.getArguments()); return clazz.callConstructor(environment, arguments); } @@ -127,7 +130,7 @@ public class Evaluator implements ExpressionNodeMapper<InterpreterValue> { public InterpreterValue visit(CastNode cast) { InterpreterValue value = eval(cast.getExpression()); if (!InterpreterTypes.isSubType(cast.getType(), value.getType())) { - throw new InvalidCast(cast.getType(), value.getType().getName()); + throw new InvalidCast(cast.getType(), value.getType().getType()); } return value; } @@ -166,7 +169,7 @@ public class Evaluator implements ExpressionNodeMapper<InterpreterValue> { } @Override - public ReceiverValue visit(TypeName receiver) { + public ReceiverValue visit(ScalarType receiver) { return environment.findClass(receiver); } }); diff --git a/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java b/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java index 8ec461a..fcf20d9 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java +++ b/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java @@ -2,7 +2,8 @@ package org.zwobble.couscous.interpreter; import com.google.common.collect.ImmutableMap; import org.zwobble.couscous.ast.MethodSignature; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.values.InterpreterValue; import org.zwobble.couscous.interpreter.values.StaticReceiverValue; @@ -18,7 +19,7 @@ public class Interpreter { this.project = project; } - public InterpreterValue run(TypeName className, String methodName, List<InterpreterValue> arguments, TypeName returnType) { + public InterpreterValue run(ScalarType className, String methodName, List<InterpreterValue> arguments, Type returnType) { final Environment environment = new Environment( project, Optional.empty(), @@ -27,7 +28,7 @@ public class Interpreter { StaticReceiverValue clazz = environment.findClass(className); MethodSignature signature = new MethodSignature( methodName, - eagerMap(arguments, argument -> argument.getType().getName()), + eagerMap(arguments, argument -> argument.getType().getType()), returnType); return clazz.callMethod(environment, signature, arguments); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/InterpreterTypes.java b/src/main/java/org/zwobble/couscous/interpreter/InterpreterTypes.java index b9221de..0ec63b0 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/InterpreterTypes.java +++ b/src/main/java/org/zwobble/couscous/interpreter/InterpreterTypes.java @@ -1,26 +1,26 @@ package org.zwobble.couscous.interpreter; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.errors.UnexpectedValueType; import org.zwobble.couscous.interpreter.types.InterpreterType; import org.zwobble.couscous.interpreter.values.InterpreterValue; import org.zwobble.couscous.values.ObjectValues; public class InterpreterTypes { - public static void checkIsInstance(TypeName type, InterpreterValue value) { + public static void checkIsInstance(Type type, InterpreterValue value) { checkIsSubType(type, value.getType()); } - private static void checkIsSubType(TypeName superTypeName, InterpreterType subType) { - if (!isSubType(superTypeName, subType)) { - throw new UnexpectedValueType(superTypeName, subType.getName()); + private static void checkIsSubType(Type superType, InterpreterType subType) { + if (!isSubType(superType, subType)) { + throw new UnexpectedValueType(superType, subType.getType()); } } - public static boolean isSubType(TypeName superTypeName, InterpreterType subType) { + public static boolean isSubType(Type superType, InterpreterType subType) { return - superTypeName.equals(subType.getName()) || - superTypeName.equals(ObjectValues.OBJECT) || - subType.getSuperTypes().contains(superTypeName); + superType.equals(subType.getType()) || + superType.equals(ObjectValues.OBJECT) || + subType.getSuperTypes().contains(superType); } } diff --git a/src/main/java/org/zwobble/couscous/interpreter/MapBackedProject.java b/src/main/java/org/zwobble/couscous/interpreter/MapBackedProject.java index eca6ba9..ebc31ae 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/MapBackedProject.java +++ b/src/main/java/org/zwobble/couscous/interpreter/MapBackedProject.java @@ -1,25 +1,27 @@ package org.zwobble.couscous.interpreter; import com.google.common.collect.ImmutableMap; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.interpreter.types.InterpreterType; import java.util.Map; +import static org.zwobble.couscous.ast.types.Types.erasure; + public class MapBackedProject implements Project { public static Builder builder() { return new Builder(); } public static class Builder { - private final ImmutableMap.Builder<TypeName, InterpreterType> classes; + private final ImmutableMap.Builder<ScalarType, InterpreterType> classes; private Builder() { classes = ImmutableMap.builder(); } public Builder addClass(InterpreterType clazz) { - classes.put(clazz.getName(), clazz); + classes.put(erasure(clazz.getType()), clazz); return this; } @@ -35,14 +37,14 @@ public class MapBackedProject implements Project { } } - private Map<TypeName, InterpreterType> classes; + private Map<ScalarType, InterpreterType> classes; - public MapBackedProject(Map<TypeName, InterpreterType> classes) { + public MapBackedProject(Map<ScalarType, InterpreterType> classes) { this.classes = classes; } @Override - public InterpreterType findClass(TypeName name) { + public InterpreterType findClass(ScalarType name) { if (classes.containsKey(name)) { return classes.get(name); } else { diff --git a/src/main/java/org/zwobble/couscous/interpreter/Project.java b/src/main/java/org/zwobble/couscous/interpreter/Project.java index 7e749b4..edfbaba 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/Project.java +++ b/src/main/java/org/zwobble/couscous/interpreter/Project.java @@ -1,8 +1,8 @@ package org.zwobble.couscous.interpreter; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.interpreter.types.InterpreterType; public interface Project { - InterpreterType findClass(TypeName name); + InterpreterType findClass(ScalarType name); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/errors/InvalidCast.java b/src/main/java/org/zwobble/couscous/interpreter/errors/InvalidCast.java index 2aef47b..a13612b 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/errors/InvalidCast.java +++ b/src/main/java/org/zwobble/couscous/interpreter/errors/InvalidCast.java @@ -1,22 +1,22 @@ package org.zwobble.couscous.interpreter.errors; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; public class InvalidCast extends InterpreterException { - private final TypeName expected; - private final TypeName actual; + private final Type expected; + private final Type actual; - public InvalidCast(TypeName expected, TypeName actual) { + public InvalidCast(Type expected, Type actual) { this.expected = expected; this.actual = actual; } - public TypeName getExpected() { + public Type getExpected() { return expected; } - public TypeName getActual() { + public Type getActual() { return actual; } } diff --git a/src/main/java/org/zwobble/couscous/interpreter/errors/UnexpectedValueType.java b/src/main/java/org/zwobble/couscous/interpreter/errors/UnexpectedValueType.java index ca40c7c..459d9d7 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/errors/UnexpectedValueType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/errors/UnexpectedValueType.java @@ -1,22 +1,22 @@ package org.zwobble.couscous.interpreter.errors; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; public final class UnexpectedValueType extends InterpreterException { private static final long serialVersionUID = 1L; - private final TypeName expected; - private final TypeName actual; + private final Type expected; + private final Type actual; - public UnexpectedValueType(final TypeName expected, final TypeName actual) { + public UnexpectedValueType(final Type expected, final Type actual) { this.expected = expected; this.actual = actual; } - public TypeName getExpected() { + public Type getExpected() { return this.expected; } - public TypeName getActual() { + public Type getActual() { return this.actual; } diff --git a/src/main/java/org/zwobble/couscous/interpreter/types/InterpreterType.java b/src/main/java/org/zwobble/couscous/interpreter/types/InterpreterType.java index f5316ae..5ee1ae0 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/types/InterpreterType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/types/InterpreterType.java @@ -3,7 +3,7 @@ package org.zwobble.couscous.interpreter.types; import org.zwobble.couscous.ast.FieldDeclarationNode; import org.zwobble.couscous.ast.MethodSignature; import org.zwobble.couscous.ast.StatementNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Environment; import org.zwobble.couscous.interpreter.values.InterpreterValue; @@ -12,8 +12,8 @@ import java.util.Optional; import java.util.Set; public interface InterpreterType { - TypeName getName(); - Set<TypeName> getSuperTypes(); + Type getType(); + Set<Type> getSuperTypes(); Optional<FieldDeclarationNode> getField(String fieldName); List<StatementNode> getStaticConstructor(); InterpreterValue callConstructor(Environment environment, List<InterpreterValue> arguments); diff --git a/src/main/java/org/zwobble/couscous/interpreter/types/IntrinsicInterpreterType.java b/src/main/java/org/zwobble/couscous/interpreter/types/IntrinsicInterpreterType.java index 5bfc221..96973cb 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/types/IntrinsicInterpreterType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/types/IntrinsicInterpreterType.java @@ -3,8 +3,9 @@ package org.zwobble.couscous.interpreter.types; import com.google.common.collect.ImmutableMap; import org.zwobble.couscous.ast.FieldDeclarationNode; import org.zwobble.couscous.ast.MethodSignature; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.StatementNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Environment; import org.zwobble.couscous.interpreter.PositionalArguments; import org.zwobble.couscous.interpreter.errors.NoSuchMethod; @@ -21,12 +22,12 @@ import static org.zwobble.couscous.util.ExtraLists.list; import static org.zwobble.couscous.util.ExtraSets.set; public class IntrinsicInterpreterType implements InterpreterType { - public static <T> Builder<T> builder(Class<T> interpreterValueType, TypeName reference) { + public static <T> Builder<T> builder(Class<T> interpreterValueType, ScalarType reference) { return new Builder<>(interpreterValueType, reference); } public static <T> Builder<T> builder(Class<T> interpreterValueType, String name) { - return builder(interpreterValueType, TypeName.of(name)); + return builder(interpreterValueType, ScalarType.of(name)); } public static Builder<ObjectInterpreterValue> classBuilder(String name) { @@ -39,34 +40,34 @@ public class IntrinsicInterpreterType implements InterpreterType { private final ImmutableMap.Builder<MethodSignature, MethodValue> methods = ImmutableMap.builder(); private final ImmutableMap.Builder<MethodSignature, StaticMethodValue> staticMethods = ImmutableMap.builder(); private final Class<T> interpreterValueType; - private final TypeName name; + private final ScalarType name; - public Builder(Class<T> interpreterValueType, TypeName name) { + public Builder(Class<T> interpreterValueType, ScalarType name) { this.interpreterValueType = interpreterValueType; this.name = name; } - public Builder<T> field(String name, TypeName type) { + public Builder<T> field(String name, ScalarType type) { fields.put(name, FieldDeclarationNode.field(name, type)); return this; } - public Builder<T> constructor(List<TypeName> argumentsTypes, BiFunction<Environment, MethodCallArguments<T>, InterpreterValue> method) { + public Builder<T> constructor(List<Type> argumentsTypes, BiFunction<Environment, MethodCallArguments<T>, InterpreterValue> method) { constructor = toMethodValue(argumentsTypes, method); return this; } public Builder<T> method( String name, - List<TypeName> argumentsTypes, - TypeName returnType, + List<Type> argumentsTypes, + Type returnType, BiFunction<Environment, MethodCallArguments<T>, InterpreterValue> method) { methods.put(new MethodSignature(name, argumentsTypes, returnType), toMethodValue(argumentsTypes, method)); return this; } - private MethodValue toMethodValue(List<TypeName> argumentsTypes, BiFunction<Environment, MethodCallArguments<T>, InterpreterValue> method) { + private MethodValue toMethodValue(List<Type> argumentsTypes, BiFunction<Environment, MethodCallArguments<T>, InterpreterValue> method) { return new MethodValue( argumentsTypes, (environment, arguments) -> @@ -77,8 +78,8 @@ public class IntrinsicInterpreterType implements InterpreterType { public Builder<T> staticMethod( String name, - List<TypeName> argumentsTypes, - TypeName returnType, + List<Type> argumentsTypes, + Type returnType, BiFunction<Environment, PositionalArguments, InterpreterValue> method) { staticMethods.put(new MethodSignature(name, argumentsTypes, returnType), new StaticMethodValue(argumentsTypes, method)); @@ -95,14 +96,14 @@ public class IntrinsicInterpreterType implements InterpreterType { } } - private final TypeName name; + private final ScalarType name; private final Map<String, FieldDeclarationNode> fields; private final MethodValue constructor; private final Map<MethodSignature, MethodValue> methods; private final Map<MethodSignature, StaticMethodValue> staticMethods; private IntrinsicInterpreterType( - TypeName name, + ScalarType name, Map<String, FieldDeclarationNode> fields, MethodValue constructor, Map<MethodSignature, MethodValue> methods, @@ -116,12 +117,12 @@ public class IntrinsicInterpreterType implements InterpreterType { } @Override - public TypeName getName() { + public ScalarType getType() { return name; } @Override - public Set<TypeName> getSuperTypes() { + public Set<Type> getSuperTypes() { return set(); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java b/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java index a253180..6d596ee 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java @@ -1,6 +1,8 @@ package org.zwobble.couscous.interpreter.types; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Environment; import org.zwobble.couscous.interpreter.Executor; import org.zwobble.couscous.interpreter.InterpreterTypes; @@ -38,12 +40,12 @@ public class UserDefinedInterpreterType implements InterpreterType { } @Override - public TypeName getName() { + public ScalarType getType() { return type.getName(); } @Override - public Set<TypeName> getSuperTypes() { + public Set<Type> getSuperTypes() { return type.getSuperTypes(); } @@ -65,7 +67,7 @@ public class UserDefinedInterpreterType implements InterpreterType { // TODO: add test for this case .orElseThrow(() -> new RuntimeException("Cannot instantiate non-class types")) .getConstructor(); - List<TypeName> formalArgumentTypes = eagerMap( + List<Type> formalArgumentTypes = eagerMap( constructor.getArguments(), FormalArgumentNode::getType); checkMethodArguments(formalArgumentTypes, arguments); @@ -78,12 +80,12 @@ public class UserDefinedInterpreterType implements InterpreterType { return object; } - private static void checkMethodArguments(final List<TypeName> argumentTypes, List<InterpreterValue> arguments) { + private static void checkMethodArguments(final List<Type> argumentTypes, List<InterpreterValue> arguments) { if (argumentTypes.size() != arguments.size()) { throw new WrongNumberOfArguments(argumentTypes.size(), arguments.size()); } for (int index = 0; index < arguments.size(); index++) { - TypeName formalArgumentType = argumentTypes.get(index); + Type formalArgumentType = argumentTypes.get(index); InterpreterTypes.checkIsInstance(formalArgumentType, arguments.get(index)); } } diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java index 5c477af..18d0c34 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java @@ -38,7 +38,7 @@ public class BoxedIntegerInterpreterValue { .method("equals", list(ObjectValues.OBJECT), BooleanValue.REF, (environment, arguments) -> { InterpreterValue right = arguments.getPositionalArguments().get(0); - if (right.getType().getName().equals(ObjectValues.BOXED_INT)) { + if (right.getType().getType().equals(ObjectValues.BOXED_INT)) { IntegerInterpreterValue leftValue = (IntegerInterpreterValue)arguments.getReceiver().getField("value"); IntegerInterpreterValue rightValue = (IntegerInterpreterValue)right.getField("value"); return BooleanInterpreterValue.of(leftValue.getValue() == rightValue.getValue()); 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 f96ba9a..c296797 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,9 @@ package org.zwobble.couscous.interpreter.values; import java.util.List; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; public interface Callable { - List<TypeName> getArgumentTypes(); + List<Type> getArgumentTypes(); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/InterpreterValues.java b/src/main/java/org/zwobble/couscous/interpreter/values/InterpreterValues.java index 4e8f1e6..3841d7c 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/InterpreterValues.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/InterpreterValues.java @@ -1,6 +1,6 @@ package org.zwobble.couscous.interpreter.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.values.PrimitiveValue; import org.zwobble.couscous.values.PrimitiveValueVisitor; @@ -40,7 +40,7 @@ public class InterpreterValues { } @Override - public InterpreterValue visitType(TypeName value) { + public InterpreterValue visitType(ScalarType value) { return TypeInterpreterValue.of(value); } }); 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 01b2f35..eeda6f4 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java @@ -1,25 +1,25 @@ package org.zwobble.couscous.interpreter.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Environment; import java.util.List; import java.util.function.BiFunction; public final class MethodValue implements Callable { - private final List<TypeName> argumentTypes; + private final List<Type> argumentTypes; private final BiFunction<Environment, MethodCallArguments<InterpreterValue>, InterpreterValue> apply; public InterpreterValue apply(Environment environment, MethodCallArguments<InterpreterValue> arguments) { return apply.apply(environment, arguments); } - public MethodValue(final List<TypeName> argumentTypes, final BiFunction<Environment, MethodCallArguments<InterpreterValue>, InterpreterValue> apply) { + public MethodValue(final List<Type> argumentTypes, final BiFunction<Environment, MethodCallArguments<InterpreterValue>, InterpreterValue> apply) { this.argumentTypes = argumentTypes; this.apply = apply; } - public List<TypeName> getArgumentTypes() { + public List<Type> getArgumentTypes() { return this.argumentTypes; } }
\ No newline at end of file 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 fabf1ad..e8223ac 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java @@ -1,6 +1,6 @@ package org.zwobble.couscous.interpreter.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Environment; import org.zwobble.couscous.interpreter.PositionalArguments; @@ -8,19 +8,19 @@ import java.util.List; import java.util.function.BiFunction; public final class StaticMethodValue implements Callable { - private final List<TypeName> argumentTypes; + private final List<Type> argumentTypes; private final BiFunction<Environment, PositionalArguments, InterpreterValue> apply; public InterpreterValue apply(Environment environment, PositionalArguments arguments) { return apply.apply(environment, arguments); } - public StaticMethodValue(final List<TypeName> argumentTypes, final BiFunction<Environment, PositionalArguments, InterpreterValue> apply) { + public StaticMethodValue(final List<Type> argumentTypes, final BiFunction<Environment, PositionalArguments, InterpreterValue> apply) { this.argumentTypes = argumentTypes; this.apply = apply; } - public List<TypeName> getArgumentTypes() { + public List<Type> getArgumentTypes() { return this.argumentTypes; } }
\ No newline at end of file diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/TypeInterpreterValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/TypeInterpreterValue.java index f221ea0..a4efddb 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/TypeInterpreterValue.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/TypeInterpreterValue.java @@ -1,6 +1,6 @@ package org.zwobble.couscous.interpreter.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.interpreter.errors.NoSuchField; import org.zwobble.couscous.interpreter.types.InterpreterType; import org.zwobble.couscous.interpreter.types.IntrinsicInterpreterType; @@ -15,13 +15,13 @@ public class TypeInterpreterValue implements InterpreterValue { .builder(TypeInterpreterValue.class, ObjectValues.CLASS) .build(); - public static InterpreterValue of(TypeName type) { + public static InterpreterValue of(ScalarType type) { return new TypeInterpreterValue(type); } - private final TypeName type; + private final ScalarType type; - public TypeInterpreterValue(TypeName type) { + public TypeInterpreterValue(ScalarType type) { this.type = type; } diff --git a/src/main/java/org/zwobble/couscous/values/BooleanValue.java b/src/main/java/org/zwobble/couscous/values/BooleanValue.java index 887cddf..fd387e0 100644 --- a/src/main/java/org/zwobble/couscous/values/BooleanValue.java +++ b/src/main/java/org/zwobble/couscous/values/BooleanValue.java @@ -1,9 +1,9 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public final class BooleanValue implements PrimitiveValue { - public static final TypeName REF = TypeName.of("boolean"); + public static final ScalarType REF = ScalarType.of("boolean"); private final boolean value; @@ -21,7 +21,7 @@ public final class BooleanValue implements PrimitiveValue { } @Override - public TypeName getType() { + public ScalarType getType() { return REF; } diff --git a/src/main/java/org/zwobble/couscous/values/IntegerValue.java b/src/main/java/org/zwobble/couscous/values/IntegerValue.java index ed0040a..22a233b 100644 --- a/src/main/java/org/zwobble/couscous/values/IntegerValue.java +++ b/src/main/java/org/zwobble/couscous/values/IntegerValue.java @@ -1,9 +1,9 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public final class IntegerValue implements PrimitiveValue { - public static final TypeName REF = TypeName.of("int"); + public static final ScalarType REF = ScalarType.of("int"); private final int value; @Override @@ -12,7 +12,7 @@ public final class IntegerValue implements PrimitiveValue { } @Override - public TypeName getType() { + public ScalarType getType() { return REF; } diff --git a/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java b/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java index c08104f..b56abec 100644 --- a/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java +++ b/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java @@ -1,7 +1,7 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public class InternalCouscousValue { - public static final TypeName REF = TypeName.of("_couscous"); + public static final ScalarType REF = ScalarType.of("_couscous"); } diff --git a/src/main/java/org/zwobble/couscous/values/ObjectValues.java b/src/main/java/org/zwobble/couscous/values/ObjectValues.java index 2681259..5dd94a7 100644 --- a/src/main/java/org/zwobble/couscous/values/ObjectValues.java +++ b/src/main/java/org/zwobble/couscous/values/ObjectValues.java @@ -1,10 +1,10 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public class ObjectValues { - public static final TypeName CLASS = TypeName.of("java.lang.Class"); - public static final TypeName OBJECT = TypeName.of("java.lang.Object"); - public static final TypeName BOXED_INT = TypeName.of("java.lang.Integer"); - public static final TypeName BOXED_BOOLEAN = TypeName.of("java.lang.Boolean"); + public static final ScalarType CLASS = ScalarType.of("java.lang.Class"); + public static final ScalarType OBJECT = ScalarType.of("java.lang.Object"); + public static final ScalarType BOXED_INT = ScalarType.of("java.lang.Integer"); + public static final ScalarType BOXED_BOOLEAN = ScalarType.of("java.lang.Boolean"); } diff --git a/src/main/java/org/zwobble/couscous/values/PrimitiveValue.java b/src/main/java/org/zwobble/couscous/values/PrimitiveValue.java index aefff21..cd6a078 100644 --- a/src/main/java/org/zwobble/couscous/values/PrimitiveValue.java +++ b/src/main/java/org/zwobble/couscous/values/PrimitiveValue.java @@ -1,8 +1,8 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public interface PrimitiveValue { <T> T accept(PrimitiveValueVisitor<T> visitor); - TypeName getType(); + ScalarType getType(); } diff --git a/src/main/java/org/zwobble/couscous/values/PrimitiveValueVisitor.java b/src/main/java/org/zwobble/couscous/values/PrimitiveValueVisitor.java index 18b6916..302118d 100644 --- a/src/main/java/org/zwobble/couscous/values/PrimitiveValueVisitor.java +++ b/src/main/java/org/zwobble/couscous/values/PrimitiveValueVisitor.java @@ -1,11 +1,11 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public interface PrimitiveValueVisitor<T> { T visitInteger(int value); T visitString(String value); T visitBoolean(boolean value); T visitUnit(); - T visitType(TypeName value); + T visitType(ScalarType value); } diff --git a/src/main/java/org/zwobble/couscous/values/PrimitiveValues.java b/src/main/java/org/zwobble/couscous/values/PrimitiveValues.java index 8022a9d..9aac702 100644 --- a/src/main/java/org/zwobble/couscous/values/PrimitiveValues.java +++ b/src/main/java/org/zwobble/couscous/values/PrimitiveValues.java @@ -1,6 +1,6 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public class PrimitiveValues { public static final PrimitiveValue UNIT = UnitValue.UNIT; @@ -19,7 +19,7 @@ public class PrimitiveValues { return value ? TRUE : FALSE; } - public static PrimitiveValue value(TypeName type) { + public static PrimitiveValue value(ScalarType type) { return new TypeValue(type); } } diff --git a/src/main/java/org/zwobble/couscous/values/StringValue.java b/src/main/java/org/zwobble/couscous/values/StringValue.java index d2a0a6a..4d45ae8 100644 --- a/src/main/java/org/zwobble/couscous/values/StringValue.java +++ b/src/main/java/org/zwobble/couscous/values/StringValue.java @@ -1,13 +1,13 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public final class StringValue implements PrimitiveValue { public static StringValue of(String value) { return new StringValue(value); } - public static final TypeName REF = TypeName.of("java.lang.String"); + public static final ScalarType REF = ScalarType.of("java.lang.String"); private final String value; @Override @@ -16,7 +16,7 @@ public final class StringValue implements PrimitiveValue { } @Override - public TypeName getType() { + public ScalarType getType() { return REF; } diff --git a/src/main/java/org/zwobble/couscous/values/TypeValue.java b/src/main/java/org/zwobble/couscous/values/TypeValue.java index 663673e..e7f5fe4 100644 --- a/src/main/java/org/zwobble/couscous/values/TypeValue.java +++ b/src/main/java/org/zwobble/couscous/values/TypeValue.java @@ -1,15 +1,15 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public class TypeValue implements PrimitiveValue { - private final TypeName value; + private final ScalarType value; - public TypeValue(TypeName value) { + public TypeValue(ScalarType value) { this.value = value; } - public TypeName getValue() { + public ScalarType getValue() { return value; } @@ -19,7 +19,7 @@ public class TypeValue implements PrimitiveValue { } @Override - public TypeName getType() { + public ScalarType getType() { return ObjectValues.CLASS; } diff --git a/src/main/java/org/zwobble/couscous/values/UnitValue.java b/src/main/java/org/zwobble/couscous/values/UnitValue.java index dfa11d4..398f04a 100644 --- a/src/main/java/org/zwobble/couscous/values/UnitValue.java +++ b/src/main/java/org/zwobble/couscous/values/UnitValue.java @@ -1,9 +1,9 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public class UnitValue implements PrimitiveValue { - public static final TypeName REF = TypeName.of("org.zwobble.couscous.runtime.Unit"); + public static final ScalarType REF = ScalarType.of("org.zwobble.couscous.runtime.Unit"); static final UnitValue UNIT = new UnitValue(); @@ -16,7 +16,7 @@ public class UnitValue implements PrimitiveValue { } @Override - public TypeName getType() { + public ScalarType getType() { return REF; } } |
