This commit is contained in:
nub31
2025-07-03 00:49:16 +02:00
parent 9f40d8a9c9
commit f79e6750f6
5 changed files with 119 additions and 239 deletions

View File

@@ -4,42 +4,25 @@ namespace Syntax.Typing;
public abstract class NubType
{
public abstract bool ValueIsPointer { get; }
public bool IsInteger => this is NubPrimitiveType
{
Kind: PrimitiveTypeKind.I8
or PrimitiveTypeKind.I16
or PrimitiveTypeKind.I32
or PrimitiveTypeKind.I64
or PrimitiveTypeKind.U8
or PrimitiveTypeKind.U16
or PrimitiveTypeKind.U32
or PrimitiveTypeKind.U64
};
public bool IsFloat32 => this is NubPrimitiveType
{
Kind: PrimitiveTypeKind.F32
};
public bool IsFloat64 => this is NubPrimitiveType
{
Kind: PrimitiveTypeKind.F64
};
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 Is32BitInteger => this is NubPrimitiveType { Kind: PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 };
public bool Is64BitInteger => this is NubPrimitiveType { Kind: PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 };
public bool IsNumber => IsFloat32 || IsFloat64 || IsInteger;
public bool IsSignedInteger => this is NubPrimitiveType { Kind: PrimitiveTypeKind.I8 or PrimitiveTypeKind.I16 or PrimitiveTypeKind.I32 or PrimitiveTypeKind.I64 };
public bool IsUnsignedInteger => this is NubPrimitiveType { Kind: PrimitiveTypeKind.U8 or PrimitiveTypeKind.U16 or PrimitiveTypeKind.U32 or PrimitiveTypeKind.U64 };
public bool IsInteger => IsSignedInteger || IsUnsignedInteger;
public bool IsFloat32 => this is NubPrimitiveType { Kind: PrimitiveTypeKind.F32 };
public bool IsFloat64 => this is NubPrimitiveType { Kind: PrimitiveTypeKind.F64 };
public bool IsFloat => IsFloat32 || IsFloat64;
public bool IsNumber => IsFloat || IsInteger;
public bool IsVoid => this is NubVoidType;
public bool IsString => this is NubStringType;
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 abstract override int GetHashCode();
@@ -48,8 +31,6 @@ public abstract class NubType
public class NubCStringType : NubType
{
public override bool ValueIsPointer => true;
public override bool Equals(object? obj)
{
return obj is NubCStringType;
@@ -68,8 +49,6 @@ public class NubCStringType : NubType
public class NubStringType : NubType
{
public override bool ValueIsPointer => true;
public override bool Equals(object? obj)
{
return obj is NubStringType;
@@ -88,8 +67,6 @@ public class NubStringType : NubType
public class NubFuncType(NubType returnType, List<NubType> parameters) : NubType
{
public override bool ValueIsPointer => false;
public NubType ReturnType { get; } = returnType;
public List<NubType> Parameters { get; } = parameters;
@@ -111,8 +88,6 @@ public class NubFuncType(NubType returnType, List<NubType> parameters) : NubType
public class NubStructType(string @namespace, string name) : NubType
{
public override bool ValueIsPointer => true;
public string Namespace { get; } = @namespace;
public string Name { get; } = name;
@@ -134,10 +109,8 @@ public class NubStructType(string @namespace, string name) : NubType
public class NubPointerType(NubType baseType) : NubType
{
public override bool ValueIsPointer => false;
public NubType BaseType { get; } = baseType;
public override bool Equals(object? obj)
{
return obj is NubPointerType other && BaseType.Equals(other.BaseType);
@@ -156,8 +129,6 @@ public class NubPointerType(NubType baseType) : NubType
public class NubArrayType(NubType elementType) : NubType
{
public override bool ValueIsPointer => true;
public NubType ElementType { get; } = elementType;
public override bool Equals(object? obj)
@@ -166,6 +137,7 @@ public class NubArrayType(NubType elementType) : NubType
{
return ElementType.Equals(other.ElementType);
}
return false;
}
@@ -182,10 +154,8 @@ public class NubArrayType(NubType elementType) : NubType
public class NubAnyType : NubType
{
public override bool ValueIsPointer => false;
public override string ToString() => "any";
public override bool Equals(object? obj)
{
return obj is NubAnyType;
@@ -199,10 +169,8 @@ public class NubAnyType : NubType
public class NubVoidType : NubType
{
public override bool ValueIsPointer => false;
public override string ToString() => "void";
public override bool Equals(object? obj)
{
return obj is NubVoidType;
@@ -216,23 +184,21 @@ public class NubVoidType : NubType
public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType
{
public override bool ValueIsPointer => false;
public PrimitiveTypeKind Kind { get; } = kind;
public static NubPrimitiveType I64 => new(PrimitiveTypeKind.I64);
public static NubPrimitiveType I32 => new(PrimitiveTypeKind.I32);
public static NubPrimitiveType I16 => new(PrimitiveTypeKind.I16);
public static NubPrimitiveType I8 => new(PrimitiveTypeKind.I8);
public static NubPrimitiveType U64 => new(PrimitiveTypeKind.U64);
public static NubPrimitiveType U32 => new(PrimitiveTypeKind.U32);
public static NubPrimitiveType U16 => new(PrimitiveTypeKind.U16);
public static NubPrimitiveType U8 => new(PrimitiveTypeKind.U8);
public static NubPrimitiveType F64 => new(PrimitiveTypeKind.F64);
public static NubPrimitiveType F32 => new(PrimitiveTypeKind.F32);
public static NubPrimitiveType Bool => new(PrimitiveTypeKind.Bool);
public static bool TryParse(string s, [NotNullWhen(true)] out PrimitiveTypeKind? kind)
@@ -274,15 +240,15 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType
PrimitiveTypeKind.I16 => "i16",
PrimitiveTypeKind.I32 => "i32",
PrimitiveTypeKind.I64 => "i64",
PrimitiveTypeKind.U8 => "u8",
PrimitiveTypeKind.U16 => "u16",
PrimitiveTypeKind.U32 => "u32",
PrimitiveTypeKind.U64 => "u64",
PrimitiveTypeKind.F32 => "f32",
PrimitiveTypeKind.F64 => "f64",
PrimitiveTypeKind.Bool => "bool",
_ => throw new ArgumentOutOfRangeException(nameof(kind), Kind, null)
};