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

Added equality methods for user-defined types, changed name of the n-uple type

parent 7ff65a57
No related branches found
No related tags found
No related merge requests found
package fr.insarennes.nperier.minichamo.language.assignement.param; package fr.insarennes.nperier.minichamo.language.assignement.param;
import fr.insarennes.nperier.minichamo.language.typing.Nuple; import fr.insarennes.nperier.minichamo.language.typing.NupleType;
import fr.insarennes.nperier.minichamo.language.typing.Type; import fr.insarennes.nperier.minichamo.language.typing.Type;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -30,6 +30,6 @@ public class NupleParam extends AssignementParam { ...@@ -30,6 +30,6 @@ public class NupleParam extends AssignementParam {
@Override @Override
public Type getType() { public Type getType() {
return new Nuple(params.stream().map(AssignementParam::getType).collect(Collectors.toList())); return new NupleType(params.stream().map(AssignementParam::getType).collect(Collectors.toList()));
} }
} }
...@@ -34,4 +34,13 @@ public class AliasType extends Type { ...@@ -34,4 +34,13 @@ public class AliasType extends Type {
return this; return this;
} }
@Override
public boolean equals(Object o) {
if(super.equals(o)) {
AliasType t = (AliasType) o;
return aliased.equals(t.aliased);
}
return false;
}
} }
...@@ -47,4 +47,13 @@ public class Constructor { ...@@ -47,4 +47,13 @@ public class Constructor {
return args.map(t -> name + " of " + t).orElse(name); return args.map(t -> name + " of " + t).orElse(name);
} }
@Override
public boolean equals(Object o) {
if(! (o instanceof Constructor)) {
return false;
}
Constructor c = (Constructor) o;
return name.equals(c.name) && args.equals(c.args);
}
} }
...@@ -5,16 +5,16 @@ import fr.insarennes.nperier.minichamo.utils.ParsingException; ...@@ -5,16 +5,16 @@ import fr.insarennes.nperier.minichamo.utils.ParsingException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
public class Nuple extends Type { public class NupleType extends Type {
public Nuple(Type[] sp) { public NupleType(Type[] sp) {
super("nuple", sp); super("nuple", sp);
if(sp.length < 2) { if(sp.length < 2) {
throw new ParsingException("A n-uple must have at least two elements, found " + sp.length); throw new ParsingException("A n-uple must have at least two elements, found " + sp.length);
} }
} }
public Nuple(List<Type> sp) { public NupleType(List<Type> sp) {
super("nuple", sp); super("nuple", sp);
if(sp.size() < 2) { if(sp.size() < 2) {
throw new ParsingException("A n-uple must have at least two elements, found " + sp.size()); throw new ParsingException("A n-uple must have at least two elements, found " + sp.size());
...@@ -30,7 +30,7 @@ public class Nuple extends Type { ...@@ -30,7 +30,7 @@ public class Nuple extends Type {
} }
private static StringBuilder addTypeToBuilder(Type t, StringBuilder builder) { private static StringBuilder addTypeToBuilder(Type t, StringBuilder builder) {
if(t instanceof Nuple || t instanceof FunctionType) { if(t instanceof NupleType || t instanceof FunctionType) {
return builder.append("(").append(t).append(")"); return builder.append("(").append(t).append(")");
} }
return builder.append(t); return builder.append(t);
......
...@@ -73,4 +73,13 @@ public class SumType extends Type { ...@@ -73,4 +73,13 @@ public class SumType extends Type {
return this; return this;
} }
@Override
public boolean equals(Object o) {
if(super.equals(o)) {
SumType t = (SumType) o;
return lconstructors.equals(t.lconstructors);
}
return false;
}
} }
...@@ -53,7 +53,7 @@ public class TypeParser { ...@@ -53,7 +53,7 @@ public class TypeParser {
while(it.junkIf(TokenType.MULT)) { while(it.junkIf(TokenType.MULT)) {
sp.add(parseAndSpecify(it, ctx)); sp.add(parseAndSpecify(it, ctx));
} }
return new Nuple(sp.toArray(new Type[0])); return new NupleType(sp.toArray(new Type[0]));
} }
public static Type parseInParen(TokenPeekIterator it, Context ctx) { public static Type parseInParen(TokenPeekIterator it, Context ctx) {
......
...@@ -3,7 +3,7 @@ package fr.insarennes.nperier.minichamo.parsing; ...@@ -3,7 +3,7 @@ package fr.insarennes.nperier.minichamo.parsing;
import fr.insarennes.nperier.minichamo.language.typing.BuiltinType; import fr.insarennes.nperier.minichamo.language.typing.BuiltinType;
import fr.insarennes.nperier.minichamo.language.typing.FunctionType; import fr.insarennes.nperier.minichamo.language.typing.FunctionType;
import fr.insarennes.nperier.minichamo.language.typing.GenericType; import fr.insarennes.nperier.minichamo.language.typing.GenericType;
import fr.insarennes.nperier.minichamo.language.typing.Nuple; import fr.insarennes.nperier.minichamo.language.typing.NupleType;
import fr.insarennes.nperier.minichamo.language.typing.Type; import fr.insarennes.nperier.minichamo.language.typing.Type;
import fr.insarennes.nperier.minichamo.lexing.Lexer; import fr.insarennes.nperier.minichamo.lexing.Lexer;
import fr.insarennes.nperier.minichamo.lexing.tokens.TokenType; import fr.insarennes.nperier.minichamo.lexing.tokens.TokenType;
...@@ -61,9 +61,9 @@ public class TypeParserTest { ...@@ -61,9 +61,9 @@ public class TypeParserTest {
@Test @Test
public void nupleTypeTest() { public void nupleTypeTest() {
TokenPeekIterator it = new Lexer("(int * (int * string) * bool);;").wrap(); TokenPeekIterator it = new Lexer("(int * (int * string) * bool);;").wrap();
Type expected = new Nuple(List.of( Type expected = new NupleType(List.of(
new BuiltinType("int"), new BuiltinType("int"),
new Nuple(List.of(new BuiltinType("int"), new BuiltinType("string"))), new NupleType(List.of(new BuiltinType("int"), new BuiltinType("string"))),
new BuiltinType("bool") new BuiltinType("bool")
)); ));
Type t = TypeParser.parse(it, ctx); Type t = TypeParser.parse(it, ctx);
...@@ -122,7 +122,7 @@ public class TypeParserTest { ...@@ -122,7 +122,7 @@ public class TypeParserTest {
new BuiltinType("string") new BuiltinType("string")
}); });
Type expected2 = userdef.specify(new Type[] { Type expected2 = userdef.specify(new Type[] {
new Nuple(new Type[] { new NupleType(new Type[] {
new BuiltinType("int"), new BuiltinType("int"),
new BuiltinType("int") new BuiltinType("int")
}), }),
...@@ -164,8 +164,8 @@ public class TypeParserTest { ...@@ -164,8 +164,8 @@ public class TypeParserTest {
}) })
}); });
Type expected2 = new BuiltinType("list", new Type[]{ Type expected2 = new BuiltinType("list", new Type[]{
new Nuple(new Type[]{ new NupleType(new Type[]{
new Nuple(new Type[]{ new NupleType(new Type[]{
new BuiltinType("int"), new BuiltinType("int"),
new BuiltinType("int") new BuiltinType("int")
}), }),
...@@ -187,7 +187,7 @@ public class TypeParserTest { ...@@ -187,7 +187,7 @@ public class TypeParserTest {
Type userdef = ctx.getUserType("lol"); Type userdef = ctx.getUserType("lol");
Type expected1 = new FunctionType(new Type[] { Type expected1 = new FunctionType(new Type[] {
new BuiltinType("int"), new BuiltinType("int"),
new Nuple(new Type[] { new NupleType(new Type[] {
new BuiltinType("int"), new BuiltinType("int"),
new BuiltinType("int") new BuiltinType("int")
}), }),
...@@ -201,7 +201,7 @@ public class TypeParserTest { ...@@ -201,7 +201,7 @@ public class TypeParserTest {
new BuiltinType("int"), new BuiltinType("int"),
new BuiltinType("int") new BuiltinType("int")
}), }),
new Nuple(new Type[] { new NupleType(new Type[] {
new BuiltinType("int"), new BuiltinType("int"),
new BuiltinType("int") new BuiltinType("int")
}) })
......
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