@@ -1,31 +1,13 @@
using System.Diagnostics.CodeAnalysis ;
namespace NubLang.TypeChecking.Node ;
namespace NubLang.TypeChecking.Node ;
public abstract class TypeNode : IEquatable < TypeNode >
{
public bool IsSimpleType ( [ NotNullWhen ( true ) ] out SimpleTypeNode ? simpleType , [ NotNullWhen ( false ) ] out ComplexTypeNode ? complexType )
{
if ( this is SimpleTypeNode st )
{
complexType = null ;
simpleType = st ;
return true ;
}
if ( this is ComplexTypeNode ct )
{
complexType = ct ;
simpleType = null ;
return false ;
}
throw new ArgumentException ( $"Type {this} is not a simple type nor a complex type" ) ;
}
public abstract bool IsValueType { get ; }
public abstract bool IsScalar { get ; }
public override bool Equals ( object? obj ) = > obj is TypeNode other & & Equals ( other ) ;
public abstract bool Equals ( TypeNode ? other ) ;
public abstract override int GetHashCode ( ) ;
public abstract override string ToString ( ) ;
@@ -33,90 +15,71 @@ public abstract class TypeNode : IEquatable<TypeNode>
public static bool operator ! = ( TypeNode ? left , TypeNode ? right ) = > ! Equals ( left , right ) ;
}
public enum StorageSiz e
public class VoidTypeNode : TypeNod e
{
Void ,
I8 ,
I16 ,
I32 ,
I64 ,
U8 ,
U16 ,
U32 ,
U64 ,
F32 ,
F64
public override bool IsValueType = > false ;
public override bool IsScalar = > false ;
public override string ToString ( ) = > "void" ;
public override bool Equals ( TypeNode ? other ) = > other is VoidTypeNode ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( VoidTypeNode ) ) ;
}
public abstract class Simple TypeNode : TypeNode
public sealed class Int TypeNode( bool signed , int width ) : TypeNode
{
public abstract StorageSize StorageSize { get ; }
}
public override bool IsValueType = > true ;
public override bool IsScalar = > true ;
#region Simple types
public class IntTypeNode ( bool signed , int width ) : SimpleTypeNode
{
public bool Signed { get ; } = signed ;
public int Width { get ; } = width ;
public override StorageSize StorageSize = > Signed switch
{
true = > Width switch
{
8 = > StorageSize . I8 ,
16 = > StorageSize . I16 ,
32 = > StorageSize . I32 ,
64 = > StorageSize . I64 ,
_ = > throw new ArgumentOutOfRangeException ( nameof ( Width ) )
} ,
false = > Width switch
{
8 = > StorageSize . U8 ,
16 = > StorageSize . U16 ,
32 = > StorageSize . U32 ,
64 = > StorageSize . U64 ,
_ = > throw new ArgumentOutOfRangeException ( nameof ( Width ) )
}
} ;
public override string ToString ( ) = > $"{(Signed ? " i " : " u ")}{Width}" ;
public override bool Equals ( TypeNode ? other ) = > other is IntTypeNode @int & & @int . Width = = Width & & @int . Signed = = Signed ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( IntTypeNode ) , Signed , Width ) ;
}
public class FloatTypeNode ( int width ) : Simple TypeNode
public sealed class FloatTypeNode ( int width ) : TypeNode
{
public override bool IsValueType = > true ;
public override bool IsScalar = > true ;
public int Width { get ; } = width ;
public override StorageSize StorageSize = > Width switch
{
32 = > StorageSize . F32 ,
64 = > StorageSize . F64 ,
_ = > throw new ArgumentOutOfRangeException ( nameof ( Width ) )
} ;
public override string ToString ( ) = > $"f{Width}" ;
public override bool Equals ( TypeNode ? other ) = > other is FloatTypeNode @in t & & @in t . Width = = Width ;
public override bool Equals ( TypeNode ? other ) = > other is FloatTypeNode @floa t & & @floa t . Width = = Width ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( FloatTypeNode ) , Width ) ;
}
public class BoolTypeNode : Simple TypeNode
public class BoolTypeNode : TypeNode
{
public override StorageSize StorageSize = > StorageSize . U8 ;
public override bool IsValueType = > true ;
public override bool IsScalar = > true ;
public override string ToString ( ) = > "bool" ;
public override bool Equals ( TypeNode ? other ) = > other is BoolTypeNode ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( BoolTypeNode ) ) ;
}
public class Func TypeNode( List < TypeNode > parameters , TypeNode return Type) : Simple TypeNode
public sealed class Pointer TypeNode( TypeNode base Type) : TypeNode
{
public override bool IsValueType = > true ;
public override bool IsScalar = > true ;
public TypeNode BaseType { get ; } = baseType ;
public override string ToString ( ) = > "^" + BaseType ;
public override bool Equals ( TypeNode ? other ) = > other is PointerTypeNode pointer & & BaseType . Equals ( pointer . BaseType ) ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( PointerTypeNode ) , BaseType ) ;
}
public class FuncTypeNode ( List < TypeNode > parameters , TypeNode returnType ) : TypeNode
{
public override bool IsValueType = > true ;
public override bool IsScalar = > true ;
public List < TypeNode > Parameters { get ; } = parameters ;
public TypeNode ReturnType { get ; } = returnType ;
public override StorageSize StorageSize = > StorageSize . U64 ;
public override string ToString ( ) = > $"func({string.Join(" , ", Parameters)}): {ReturnType}" ;
public override bool Equals ( TypeNode ? other ) = > other is FuncTypeNode func & & ReturnType . Equals ( func . ReturnType ) & & Parameters . SequenceEqual ( func . Parameters ) ;
@@ -134,44 +97,19 @@ public class FuncTypeNode(List<TypeNode> parameters, TypeNode returnType) : Simp
}
}
public class Pointer TypeNode( TypeNode baseType ) : Simple TypeNode
public class Struct TypeNode( string module , string name , List < StructTypeField > fields , List < StructTypeFunc > functions ) : TypeNode
{
public TypeNode BaseType { get ; } = baseTyp e;
public override bool IsValueType = > tru e;
public override bool IsScalar = > false ;
public override StorageSize StorageSize = > StorageSize . U64 ;
public string Module { get ; } = module ;
public string Name { get ; } = name ;
public List < StructTypeField > Fields { get ; set ; } = fields ;
public List < StructTypeFunc > Functions { get ; set ; } = functions ;
public override string ToString ( ) = > "^" + BaseTyp e;
public override bool Equals ( TypeNode ? other ) = > other is Pointer TypeNode pointer & & BaseType . Equals ( pointer . BaseType ) ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( Pointer TypeNode) , BaseTyp e) ;
}
public class VoidTypeNode : SimpleTypeNode
{
public override StorageSize StorageSize = > StorageSize . Void ;
public override string ToString ( ) = > "void" ;
public override bool Equals ( TypeNode ? other ) = > other is VoidTypeNode ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( VoidTypeNode ) ) ;
}
#endregion
public abstract class ComplexTypeNode : TypeNode ;
#region Complex types
public class CStringTypeNode : ComplexTypeNode
{
public override string ToString ( ) = > "cstring" ;
public override bool Equals ( TypeNode ? other ) = > other is CStringTypeNode ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( CStringTypeNode ) ) ;
}
public class StringTypeNode : ComplexTypeNode
{
public override string ToString ( ) = > "string" ;
public override bool Equals ( TypeNode ? other ) = > other is StringTypeNode ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( StringTypeNode ) ) ;
public override string ToString ( ) = > Nam e;
public override bool Equals ( TypeNode ? other ) = > other is Struct TypeNode structType & & Name = = structType . Name & & Module = = structType . Module ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( Struct TypeNode) , Nam e) ;
}
public class StructTypeField ( string name , TypeNode type , bool hasDefaultValue )
@@ -187,26 +125,34 @@ public class StructTypeFunc(string name, FuncTypeNode type)
public FuncTypeNode Type { get ; } = type ;
}
public class StructTypeNode ( string module , string name , List < StructTypeField > fields , List < StructTypeFunc > functions ) : Complex TypeNode
public class C StringTypeNode : TypeNode
{
public string Module { get ; } = modul e;
public string Name { get ; } = nam e;
public List < StructTypeField > Fields { get ; set ; } = fields ;
public List < StructTypeFunc > Functions { get ; set ; } = functions ;
public override bool IsValueType = > fals e;
public override bool IsScalar = > fals e;
public override string ToString ( ) = > Name ;
public override bool Equals ( TypeNode ? other ) = > other is StructTypeNode structType & & Name = = structType . Name & & Module = = structType . Modul e ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( Struct TypeNode ) , Name );
public override string ToString ( ) = > "cstring" ;
public override bool Equals ( TypeNode ? other ) = > other is C StringTypeNod e;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( C String TypeNode) ) ;
}
public class ArrayTypeNode ( TypeNode elementType ) : Complex TypeNode
public class StringTypeNode : TypeNode
{
public override bool IsValueType = > false ;
public override bool IsScalar = > false ;
public override string ToString ( ) = > "string" ;
public override bool Equals ( TypeNode ? other ) = > other is StringTypeNode ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( StringTypeNode ) ) ;
}
public class ArrayTypeNode ( TypeNode elementType ) : TypeNode
{
public override bool IsValueType = > false ;
public override bool IsScalar = > false ;
public TypeNode ElementType { get ; } = elementType ;
public override string ToString ( ) = > "[]" + ElementType ;
public override bool Equals ( TypeNode ? other ) = > other is ArrayTypeNode array & & ElementType . Equals ( array . ElementType ) ;
public override int GetHashCode ( ) = > HashCode . Combine ( typeof ( ArrayTypeNode ) , ElementType ) ;
}
#endregion
}