165 lines
3.8 KiB
C#
165 lines
3.8 KiB
C#
namespace Compiler;
|
|
|
|
public abstract class NubType
|
|
{
|
|
public abstract override string ToString();
|
|
}
|
|
|
|
public sealed class NubTypeVoid : NubType
|
|
{
|
|
public static readonly NubTypeVoid Instance = new();
|
|
|
|
private NubTypeVoid()
|
|
{
|
|
}
|
|
|
|
public override string ToString() => "void";
|
|
}
|
|
|
|
public sealed class NubTypeUInt : NubType
|
|
{
|
|
private static readonly Dictionary<int, NubTypeUInt> Cache = new();
|
|
|
|
public static NubTypeUInt Get(int width)
|
|
{
|
|
if (!Cache.TryGetValue(width, out var type))
|
|
Cache[width] = type = new NubTypeUInt(width);
|
|
|
|
return type;
|
|
}
|
|
|
|
public int Width { get; }
|
|
|
|
private NubTypeUInt(int width)
|
|
{
|
|
Width = width;
|
|
}
|
|
|
|
public override string ToString() => $"u{Width}";
|
|
}
|
|
|
|
public sealed class NubTypeSInt : NubType
|
|
{
|
|
private static readonly Dictionary<int, NubTypeSInt> Cache = new();
|
|
|
|
public static NubTypeSInt Get(int width)
|
|
{
|
|
if (!Cache.TryGetValue(width, out var type))
|
|
Cache[width] = type = new NubTypeSInt(width);
|
|
|
|
return type;
|
|
}
|
|
|
|
public int Width { get; }
|
|
|
|
private NubTypeSInt(int width)
|
|
{
|
|
Width = width;
|
|
}
|
|
|
|
public override string ToString() => $"i{Width}";
|
|
}
|
|
|
|
public sealed class NubTypeBool : NubType
|
|
{
|
|
public static readonly NubTypeBool Instance = new();
|
|
|
|
private NubTypeBool()
|
|
{
|
|
}
|
|
|
|
public override string ToString() => "bool";
|
|
}
|
|
|
|
public sealed class NubTypeString : NubType
|
|
{
|
|
public static readonly NubTypeString Instance = new();
|
|
|
|
private NubTypeString()
|
|
{
|
|
}
|
|
|
|
public override string ToString() => "string";
|
|
}
|
|
|
|
public sealed class NubTypeStruct : NubType
|
|
{
|
|
public string Name { get; }
|
|
public string Module { get; }
|
|
|
|
private IReadOnlyList<Field>? _resolvedFields;
|
|
|
|
public IReadOnlyList<Field> Fields => _resolvedFields ?? throw new InvalidOperationException();
|
|
|
|
public NubTypeStruct(string module, string name)
|
|
{
|
|
Module = module;
|
|
Name = name;
|
|
}
|
|
|
|
public void ResolveFields(IReadOnlyList<Field> fields)
|
|
{
|
|
if (_resolvedFields != null)
|
|
throw new InvalidOperationException($"{Name} already resolved");
|
|
|
|
_resolvedFields = fields;
|
|
}
|
|
|
|
public override string ToString() => _resolvedFields == null ? $"struct {Module}::{Name} <unresolved>" : $"struct {Module}::{Name} {{ {string.Join(' ', Fields.Select(f => $"{f.Name}: {f.Type}"))} }}";
|
|
|
|
public sealed class Field(string name, NubType type)
|
|
{
|
|
public string Name { get; } = name;
|
|
public NubType Type { get; } = type;
|
|
}
|
|
}
|
|
|
|
public sealed class NubTypePointer : NubType
|
|
{
|
|
private static readonly Dictionary<NubType, NubTypePointer> Cache = new();
|
|
|
|
public static NubTypePointer Get(NubType to)
|
|
{
|
|
if (!Cache.TryGetValue(to, out var ptr))
|
|
Cache[to] = ptr = new NubTypePointer(to);
|
|
|
|
return ptr;
|
|
}
|
|
|
|
public NubType To { get; }
|
|
|
|
private NubTypePointer(NubType to)
|
|
{
|
|
To = to;
|
|
}
|
|
|
|
public override string ToString() => $"^{To}";
|
|
}
|
|
|
|
public sealed class NubTypeFunc : NubType
|
|
{
|
|
private static readonly Dictionary<Signature, NubTypeFunc> Cache = new();
|
|
|
|
public static NubTypeFunc Get(List<NubType> parameters, NubType returnType)
|
|
{
|
|
var sig = new Signature(parameters, returnType);
|
|
|
|
if (!Cache.TryGetValue(sig, out var func))
|
|
Cache[sig] = func = new NubTypeFunc(parameters, returnType);
|
|
|
|
return func;
|
|
}
|
|
|
|
public IReadOnlyList<NubType> Parameters { get; }
|
|
public NubType ReturnType { get; }
|
|
|
|
private NubTypeFunc(List<NubType> parameters, NubType returnType)
|
|
{
|
|
Parameters = parameters;
|
|
ReturnType = returnType;
|
|
}
|
|
|
|
public override string ToString() => $"func({string.Join(' ', Parameters)}): {ReturnType}";
|
|
|
|
private readonly record struct Signature(IReadOnlyList<NubType> Parameters, NubType ReturnType);
|
|
} |