This commit is contained in:
nub31
2025-06-02 18:19:48 +02:00
parent a4bda0b715
commit 81fe75446d
2 changed files with 48 additions and 48 deletions

View File

@@ -1,38 +1,13 @@
namespace main
struct Ape {
name: string
age: i64
}
struct Human {
parent: Ape
name: string
age: i64
}
export func main(args: []^string) {
// c::printf("%d\n", args.count)
// let i: i64
// while i < args.count {
// c::printf("%s\n", args[i])
// i = i + 1
// }
let human = alloc Human {
parent = alloc Ape {
name = "john"
age = 24
}
age = 23
name = "oliver"
}
c::printf("%s is %d years old\n", human.name, human.age)
c::printf("parent name is %s\n", human.parent.name)
// human.name = "hubert"
// human.age = 92
}

View File

@@ -4,48 +4,58 @@ namespace Nub.Lang.Frontend.Typing;
public abstract class NubType
{
protected NubType(string name)
{
Name = name;
}
public string Name { get; }
public static bool IsCompatibleWith(NubType sourceType, NubType targetType)
{
return targetType.Equals(NubPrimitiveType.Any) || sourceType.Equals(targetType);
}
public override bool Equals(object? obj) => obj is NubType item && Name.Equals(item.Name);
public override int GetHashCode() => HashCode.Combine(Name);
public override string ToString() => Name;
public abstract override bool Equals(object? obj);
public abstract override int GetHashCode();
public abstract override string ToString();
}
public class NubStructType(string @namespace, string name) : NubType(name)
public class NubStructType(string @namespace, string name) : NubType
{
public string Namespace { get; } = @namespace;
public string Name { get; } = name;
public override bool Equals(object? obj)
{
return obj is NubStructType other && other.Namespace == Namespace && other.Name == Name;
}
public override int GetHashCode()
{
return HashCode.Combine(Namespace, Name);
}
public override string ToString()
{
return $"{Namespace}::{Name}";
}
}
public class NubPointerType(NubType baseType) : NubType("^" + baseType)
public class NubPointerType(NubType baseType) : NubType
{
public NubType BaseType { get; } = baseType;
public override bool Equals(object? obj)
{
if (obj is NubPointerType other)
{
return BaseType.Equals(other.BaseType);
}
return false;
return obj is NubPointerType other && BaseType.Equals(other.BaseType);
}
public override int GetHashCode()
{
return HashCode.Combine(base.GetHashCode(), BaseType);
return HashCode.Combine(BaseType);
}
public override string ToString()
{
return "^" + BaseType;
}
}
public class NubArrayType(NubType baseType) : NubType("[]" + baseType)
public class NubArrayType(NubType baseType) : NubType
{
public NubType BaseType { get; } = baseType;
@@ -60,11 +70,16 @@ public class NubArrayType(NubType baseType) : NubType("[]" + baseType)
public override int GetHashCode()
{
return HashCode.Combine(base.GetHashCode(), BaseType);
return HashCode.Combine(BaseType);
}
public override string ToString()
{
return "[]" + BaseType;
}
}
public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType(KindToString(kind))
public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType
{
public PrimitiveTypeKind Kind { get; } = kind;
@@ -108,7 +123,17 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType(KindToString(kin
return kind != null;
}
public static string KindToString(PrimitiveTypeKind kind)
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
{