Skip to content
Snippets Groups Projects
Commit 60caa3a1 authored by Nathan PERIER's avatar Nathan PERIER
Browse files

Added a strict equality for types, corrected the `toString` method

parent 30dde96d
No related branches found
No related tags found
No related merge requests found
...@@ -21,7 +21,7 @@ public class BuiltinType extends Type { ...@@ -21,7 +21,7 @@ public class BuiltinType extends Type {
BUILTIN_TYPES.put(TokenType.TYPE_LIST, new BuiltinType("list", 1)); BUILTIN_TYPES.put(TokenType.TYPE_LIST, new BuiltinType("list", 1));
} }
private BuiltinType(String n) { public BuiltinType(String n) {
super(n); super(n);
} }
...@@ -29,7 +29,7 @@ public class BuiltinType extends Type { ...@@ -29,7 +29,7 @@ public class BuiltinType extends Type {
super(n, arity); super(n, arity);
} }
private BuiltinType(String n, Type[] sp) { public BuiltinType(String n, Type[] sp) {
super(n, sp); super(n, sp);
} }
......
...@@ -3,6 +3,7 @@ package fr.insarennes.nperier.minichamo.language.typing; ...@@ -3,6 +3,7 @@ package fr.insarennes.nperier.minichamo.language.typing;
import fr.insarennes.nperier.minichamo.utils.ParsingException; import fr.insarennes.nperier.minichamo.utils.ParsingException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
public class FunctionType extends Type { public class FunctionType extends Type {
...@@ -13,6 +14,13 @@ public class FunctionType extends Type { ...@@ -13,6 +14,13 @@ public class FunctionType extends Type {
} }
} }
public FunctionType(List<Type> sp) {
super("function", sp);
if(sp.size() < 2) {
throw new ParsingException("A function must have at least two elements, found " + sp.size());
}
}
public Type[] getArgumentTypes() { public Type[] getArgumentTypes() {
Type[] res = new Type[getArity() - 1]; Type[] res = new Type[getArity() - 1];
for(int i = 0; i < res.length; i++) { for(int i = 0; i < res.length; i++) {
......
...@@ -61,18 +61,37 @@ public abstract class Type { ...@@ -61,18 +61,37 @@ public abstract class Type {
public abstract Type specify(Type[] sp); public abstract Type specify(Type[] sp);
@Override
public boolean equals(Object o) {
if(!getClass().equals(o.getClass())) {
return false;
}
Type t = (Type) o;
if(!(t.name.equals(name) && t.getArity() == getArity())) {
return false;
}
for(int i = 0; i < specifics.length; i++) {
if(!specifics[i].equals(t.specifics[i])) {
return false;
}
}
return true;
}
@Override @Override
public String toString() { public String toString() {
if(!isComplex()) { if(!isComplex()) {
return name; return name;
} }
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for(Type t: specifics) { if(specifics.length == 1 && ! specifics[0].isComplex()) {
if(t.isComplex()) { builder.append(specifics[0]).append(" ");
builder.append("(").append(t.toString()).append(") "); } else {
} else { builder.append("(").append(specifics[0]);
builder.append(t.toString()).append(" "); for(int i = 1; i < specifics.length; i++) {
builder.append(", ").append(specifics[i]);
} }
builder.append(") ");
} }
builder.append(name); builder.append(name);
return builder.toString(); return builder.toString();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment