improve type equals

This commit is contained in:
nub31
2025-07-05 15:15:45 +02:00
parent 9793adeb79
commit a4383c8290
2 changed files with 39 additions and 123 deletions

View File

@@ -696,11 +696,6 @@ public static class Parser
{
if (TryExpectIdentifier(out var name))
{
if (name.Value == "any")
{
return new NubAnyType();
}
if (name.Value == "void")
{
return new NubVoidType();

View File

@@ -2,7 +2,7 @@
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 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 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 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 NubSimpleType : NubType;
public class NubCStringType : NubComplexType
{
public override bool Equals(object? obj)
{
return obj is NubCStringType;
}
public override int GetHashCode()
{
return "cstring".GetHashCode();
}
public override string ToString()
{
return "cstring";
}
public override string ToString() => "cstring";
public override bool Equals(NubType? other) => other is NubCStringType;
public override int GetHashCode() => HashCode.Combine(typeof(NubCStringType));
}
public class NubStringType : NubComplexType
{
public override bool Equals(object? obj)
{
return obj is NubStringType;
}
public override int GetHashCode()
{
return "string".GetHashCode();
}
public override string ToString()
{
return "string";
}
public override string ToString() => "string";
public override bool Equals(NubType? other) => other is NubStringType;
public override int GetHashCode() => HashCode.Combine(typeof(NubStringType));
}
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 List<NubType> Parameters { get; } = parameters;
public override bool Equals(object? obj)
{
return obj is NubFuncType other && other.ReturnType.Equals(ReturnType) && other.Parameters.SequenceEqual(Parameters);
}
public override string ToString() => $"func({string.Join(", ", Parameters)}): {ReturnType}";
public override bool Equals(NubType? other) => other is NubFuncType func && ReturnType.Equals(func.ReturnType) && Parameters.SequenceEqual(func.Parameters);
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 $"func({string.Join(", ", Parameters)}): {ReturnType}";
return hash.ToHashCode();
}
}
@@ -94,95 +80,37 @@ public class NubCustomType(string @namespace, string name) : NubComplexType
public string Namespace { get; } = @namespace;
public string Name { get; } = name;
public override bool Equals(object? obj)
{
return obj is NubCustomType other && other.Namespace == Namespace && other.Name == Name;
}
public override string ToString() => $"{Namespace}::{Name}";
public override int GetHashCode()
{
return HashCode.Combine(Namespace, Name);
}
public override string ToString()
{
return $"{Namespace}::{Name}";
}
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);
}
public class NubPointerType(NubType baseType) : NubSimpleType
{
public NubType BaseType { get; } = baseType;
public override bool Equals(object? obj)
{
return obj is NubPointerType other && BaseType.Equals(other.BaseType);
}
public override string ToString() => "^" + BaseType;
public override int GetHashCode()
{
return HashCode.Combine(BaseType);
}
public override string ToString()
{
return "^" + BaseType;
}
public override bool Equals(NubType? other) => other is NubPointerType pointer && BaseType.Equals(pointer.BaseType);
public override int GetHashCode() => HashCode.Combine(typeof(NubPointerType), BaseType);
}
public class NubArrayType(NubType elementType) : NubComplexType
{
public NubType ElementType { get; } = elementType;
public override bool Equals(object? obj)
{
if (obj is NubArrayType other)
{
return ElementType.Equals(other.ElementType);
}
public override string ToString() => "[]" + ElementType;
return false;
}
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 override bool Equals(NubType? other) => other is NubArrayType array && ElementType.Equals(array.ElementType);
public override int GetHashCode() => HashCode.Combine(typeof(NubArrayType), ElementType);
}
public class NubVoidType : NubSimpleType
{
public override string ToString() => "void";
public override bool Equals(object? obj)
{
return obj is NubVoidType;
}
public override int GetHashCode()
{
return GetType().GetHashCode();
}
public override bool Equals(NubType? other) => other is NubVoidType;
public override int GetHashCode() => HashCode.Combine(typeof(NubVoidType));
}
public class NubPrimitiveType(PrimitiveTypeKind kind) : NubSimpleType
@@ -225,16 +153,6 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubSimpleType
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()
{
return Kind switch
@@ -256,6 +174,9 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubSimpleType
_ => 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