This repository has been archived on 2025-10-23. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nub-lang-archive/Nub.Lang/Nub.Lang/Type.cs
2025-01-27 19:24:09 +01:00

58 lines
1.3 KiB
C#

using Nub.Core;
namespace Nub.Lang;
public abstract record Type;
public record PrimitiveType : Type
{
public PrimitiveType(PrimitiveTypeKind kind)
{
Kind = kind;
}
public static PrimitiveType Parse(string value)
{
var kind = value switch
{
"bool" => PrimitiveTypeKind.Bool,
"char" => PrimitiveTypeKind.Char,
"int64" => PrimitiveTypeKind.Int64,
"int32" => PrimitiveTypeKind.Int32,
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null)
};
return new PrimitiveType(kind);
}
public PrimitiveTypeKind Kind { get; }
public override string ToString() => Kind.ToString();
}
public enum PrimitiveTypeKind
{
Bool,
Char,
Int64,
Int32,
}
public record StringType : Type
{
public override string ToString() => "String";
}
public record DelegateType : Type
{
public DelegateType(IReadOnlyCollection<Type> parameters, Optional<Type> returnType)
{
Parameters = parameters;
ReturnType = returnType;
}
public IReadOnlyCollection<Type> Parameters { get; }
public Optional<Type> ReturnType { get; }
public override string ToString() => $"({string.Join(", ", Parameters)}): {(ReturnType.HasValue ? ReturnType.Value.ToString() : "")}";
}