improve type equals
This commit is contained in:
@@ -696,11 +696,6 @@ public static class Parser
|
|||||||
{
|
{
|
||||||
if (TryExpectIdentifier(out var name))
|
if (TryExpectIdentifier(out var name))
|
||||||
{
|
{
|
||||||
if (name.Value == "any")
|
|
||||||
{
|
|
||||||
return new NubAnyType();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (name.Value == "void")
|
if (name.Value == "void")
|
||||||
{
|
{
|
||||||
return new NubVoidType();
|
return new NubVoidType();
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Syntax.Typing;
|
namespace Syntax.Typing;
|
||||||
|
|
||||||
public abstract class NubType
|
public abstract class NubType : IEquatable<NubType>
|
||||||
{
|
{
|
||||||
public bool Is8BitInteger => this is NubPrimitiveType { Kind: PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 };
|
public bool Is8BitInteger => this is NubPrimitiveType { Kind: PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 };
|
||||||
public bool Is16BitInteger => this is NubPrimitiveType { Kind: PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 };
|
public bool Is16BitInteger => this is NubPrimitiveType { Kind: PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 };
|
||||||
@@ -24,48 +24,32 @@ public abstract class NubType
|
|||||||
public bool IsCString => this is NubCStringType;
|
public bool IsCString => this is NubCStringType;
|
||||||
public bool IsBool => this is NubPrimitiveType { Kind: PrimitiveTypeKind.Bool };
|
public bool IsBool => this is NubPrimitiveType { Kind: PrimitiveTypeKind.Bool };
|
||||||
|
|
||||||
public abstract override bool Equals(object? obj);
|
public override bool Equals(object? obj) => obj is NubType other && Equals(other);
|
||||||
|
|
||||||
|
public abstract bool Equals(NubType? other);
|
||||||
public abstract override int GetHashCode();
|
public abstract override int GetHashCode();
|
||||||
public abstract override string ToString();
|
public abstract override string ToString();
|
||||||
|
|
||||||
|
public static bool operator ==(NubType? left, NubType? right) => Equals(left, right);
|
||||||
|
public static bool operator !=(NubType? left, NubType? right) => !Equals(left, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class NubComplexType : NubType;
|
public abstract class NubComplexType : NubType;
|
||||||
|
|
||||||
public abstract class NubSimpleType : NubType;
|
public abstract class NubSimpleType : NubType;
|
||||||
|
|
||||||
public class NubCStringType : NubComplexType
|
public class NubCStringType : NubComplexType
|
||||||
{
|
{
|
||||||
public override bool Equals(object? obj)
|
public override string ToString() => "cstring";
|
||||||
{
|
public override bool Equals(NubType? other) => other is NubCStringType;
|
||||||
return obj is NubCStringType;
|
public override int GetHashCode() => HashCode.Combine(typeof(NubCStringType));
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return "cstring".GetHashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return "cstring";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubStringType : NubComplexType
|
public class NubStringType : NubComplexType
|
||||||
{
|
{
|
||||||
public override bool Equals(object? obj)
|
public override string ToString() => "string";
|
||||||
{
|
public override bool Equals(NubType? other) => other is NubStringType;
|
||||||
return obj is NubStringType;
|
public override int GetHashCode() => HashCode.Combine(typeof(NubStringType));
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return "string".GetHashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return "string";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubFuncType(NubType returnType, List<NubType> parameters) : NubSimpleType
|
public class NubFuncType(NubType returnType, List<NubType> parameters) : NubSimpleType
|
||||||
@@ -73,19 +57,21 @@ public class NubFuncType(NubType returnType, List<NubType> parameters) : NubSimp
|
|||||||
public NubType ReturnType { get; } = returnType;
|
public NubType ReturnType { get; } = returnType;
|
||||||
public List<NubType> Parameters { get; } = parameters;
|
public List<NubType> Parameters { get; } = parameters;
|
||||||
|
|
||||||
public override bool Equals(object? obj)
|
public override string ToString() => $"func({string.Join(", ", Parameters)}): {ReturnType}";
|
||||||
{
|
|
||||||
return obj is NubFuncType other && other.ReturnType.Equals(ReturnType) && other.Parameters.SequenceEqual(Parameters);
|
public override bool Equals(NubType? other) => other is NubFuncType func && ReturnType.Equals(func.ReturnType) && Parameters.SequenceEqual(func.Parameters);
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
return HashCode.Combine(ReturnType, Parameters);
|
var hash = new HashCode();
|
||||||
|
hash.Add(typeof(NubFuncType));
|
||||||
|
hash.Add(ReturnType);
|
||||||
|
foreach (var param in Parameters)
|
||||||
|
{
|
||||||
|
hash.Add(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
return hash.ToHashCode();
|
||||||
{
|
|
||||||
return $"func({string.Join(", ", Parameters)}): {ReturnType}";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,95 +80,37 @@ public class NubCustomType(string @namespace, string name) : NubComplexType
|
|||||||
public string Namespace { get; } = @namespace;
|
public string Namespace { get; } = @namespace;
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
|
|
||||||
public override bool Equals(object? obj)
|
public override string ToString() => $"{Namespace}::{Name}";
|
||||||
{
|
|
||||||
return obj is NubCustomType other && other.Namespace == Namespace && other.Name == Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
public override bool Equals(NubType? other) => other is NubCustomType custom && Namespace == custom.Namespace && Name == custom.Name;
|
||||||
{
|
public override int GetHashCode() => HashCode.Combine(typeof(NubCustomType), Namespace, Name);
|
||||||
return HashCode.Combine(Namespace, Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return $"{Namespace}::{Name}";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubPointerType(NubType baseType) : NubSimpleType
|
public class NubPointerType(NubType baseType) : NubSimpleType
|
||||||
{
|
{
|
||||||
public NubType BaseType { get; } = baseType;
|
public NubType BaseType { get; } = baseType;
|
||||||
|
|
||||||
public override bool Equals(object? obj)
|
public override string ToString() => "^" + BaseType;
|
||||||
{
|
|
||||||
return obj is NubPointerType other && BaseType.Equals(other.BaseType);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
public override bool Equals(NubType? other) => other is NubPointerType pointer && BaseType.Equals(pointer.BaseType);
|
||||||
{
|
public override int GetHashCode() => HashCode.Combine(typeof(NubPointerType), BaseType);
|
||||||
return HashCode.Combine(BaseType);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return "^" + BaseType;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubArrayType(NubType elementType) : NubComplexType
|
public class NubArrayType(NubType elementType) : NubComplexType
|
||||||
{
|
{
|
||||||
public NubType ElementType { get; } = elementType;
|
public NubType ElementType { get; } = elementType;
|
||||||
|
|
||||||
public override bool Equals(object? obj)
|
public override string ToString() => "[]" + ElementType;
|
||||||
{
|
|
||||||
if (obj is NubArrayType other)
|
|
||||||
{
|
|
||||||
return ElementType.Equals(other.ElementType);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
public override bool Equals(NubType? other) => other is NubArrayType array && ElementType.Equals(array.ElementType);
|
||||||
}
|
public override int GetHashCode() => HashCode.Combine(typeof(NubArrayType), ElementType);
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return HashCode.Combine(ElementType);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return "[]" + ElementType;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class NubAnyType : NubSimpleType
|
|
||||||
{
|
|
||||||
public override string ToString() => "any";
|
|
||||||
|
|
||||||
public override bool Equals(object? obj)
|
|
||||||
{
|
|
||||||
return obj is NubAnyType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return "any".GetHashCode();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubVoidType : NubSimpleType
|
public class NubVoidType : NubSimpleType
|
||||||
{
|
{
|
||||||
public override string ToString() => "void";
|
public override string ToString() => "void";
|
||||||
|
public override bool Equals(NubType? other) => other is NubVoidType;
|
||||||
public override bool Equals(object? obj)
|
public override int GetHashCode() => HashCode.Combine(typeof(NubVoidType));
|
||||||
{
|
|
||||||
return obj is NubVoidType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return GetType().GetHashCode();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubPrimitiveType(PrimitiveTypeKind kind) : NubSimpleType
|
public class NubPrimitiveType(PrimitiveTypeKind kind) : NubSimpleType
|
||||||
@@ -225,16 +153,6 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubSimpleType
|
|||||||
return kind != null;
|
return kind != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool Equals(object? obj)
|
|
||||||
{
|
|
||||||
return obj is NubPrimitiveType other && other.Kind == Kind;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return HashCode.Combine(Kind);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return Kind switch
|
return Kind switch
|
||||||
@@ -256,6 +174,9 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubSimpleType
|
|||||||
_ => throw new ArgumentOutOfRangeException(nameof(kind), Kind, null)
|
_ => throw new ArgumentOutOfRangeException(nameof(kind), Kind, null)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override bool Equals(NubType? other) => other is NubPrimitiveType primitive && primitive.Kind == Kind;
|
||||||
|
public override int GetHashCode() => HashCode.Combine(typeof(NubPrimitiveType), Kind.GetHashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum PrimitiveTypeKind
|
public enum PrimitiveTypeKind
|
||||||
|
|||||||
Reference in New Issue
Block a user