WIP: dev #1
@@ -1,29 +1,51 @@
|
|||||||
namespace Compiler;
|
namespace Compiler;
|
||||||
|
|
||||||
public sealed class Diagnostic(DiagnosticSeverity severity, string message, string? help, FileInfo? file)
|
public class Diagnostic
|
||||||
{
|
{
|
||||||
public static DiagnosticBuilder Info(string message) => new DiagnosticBuilder(DiagnosticSeverity.Info, message);
|
public static Builder Info(string message) => new Builder(DiagnosticSeverity.Info, message);
|
||||||
public static DiagnosticBuilder Warning(string message) => new DiagnosticBuilder(DiagnosticSeverity.Warning, message);
|
public static Builder Warning(string message) => new Builder(DiagnosticSeverity.Warning, message);
|
||||||
public static DiagnosticBuilder Error(string message) => new DiagnosticBuilder(DiagnosticSeverity.Error, message);
|
public static Builder Error(string message) => new Builder(DiagnosticSeverity.Error, message);
|
||||||
|
|
||||||
public DiagnosticSeverity Severity { get; } = severity;
|
private Diagnostic(DiagnosticSeverity severity, string message, string? help, FileInfo? file)
|
||||||
public string Message { get; } = message;
|
{
|
||||||
public string? Help { get; } = help;
|
Severity = severity;
|
||||||
public FileInfo? File { get; } = file;
|
Message = message;
|
||||||
|
Help = help;
|
||||||
|
File = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string message)
|
public class FileInfo(string file, int line, int column, int length)
|
||||||
|
{
|
||||||
|
public string File { get; } = file;
|
||||||
|
public int Line { get; } = line;
|
||||||
|
public int Column { get; } = column;
|
||||||
|
public int Length { get; } = length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DiagnosticSeverity Severity { get; }
|
||||||
|
public string Message { get; }
|
||||||
|
public string? Help { get; }
|
||||||
|
public FileInfo? File { get; }
|
||||||
|
|
||||||
|
public enum DiagnosticSeverity
|
||||||
|
{
|
||||||
|
Info,
|
||||||
|
Warning,
|
||||||
|
Error,
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Builder(DiagnosticSeverity severity, string message)
|
||||||
{
|
{
|
||||||
private FileInfo? file;
|
private FileInfo? file;
|
||||||
private string? help;
|
private string? help;
|
||||||
|
|
||||||
public DiagnosticBuilder At(string fileName, int line, int column, int length)
|
public Builder At(string fileName, int line, int column, int length)
|
||||||
{
|
{
|
||||||
file = new FileInfo(fileName, line, column, length);
|
file = new FileInfo(fileName, line, column, length);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiagnosticBuilder At(string fileName, Token? token)
|
public Builder At(string fileName, Token? token)
|
||||||
{
|
{
|
||||||
if (token != null)
|
if (token != null)
|
||||||
{
|
{
|
||||||
@@ -33,7 +55,7 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiagnosticBuilder At(string fileName, Node? node)
|
public Builder At(string fileName, Node? node)
|
||||||
{
|
{
|
||||||
if (node != null && node.Tokens.Count != 0)
|
if (node != null && node.Tokens.Count != 0)
|
||||||
{
|
{
|
||||||
@@ -44,7 +66,7 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiagnosticBuilder At(string fileName, TypedNode? node)
|
public Builder At(string fileName, TypedNode? node)
|
||||||
{
|
{
|
||||||
if (node != null && node.Tokens.Count != 0)
|
if (node != null && node.Tokens.Count != 0)
|
||||||
{
|
{
|
||||||
@@ -55,7 +77,7 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiagnosticBuilder WithHelp(string helpMessage)
|
public Builder WithHelp(string helpMessage)
|
||||||
{
|
{
|
||||||
help = helpMessage;
|
help = helpMessage;
|
||||||
return this;
|
return this;
|
||||||
@@ -66,23 +88,9 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
|
|||||||
return new Diagnostic(severity, message, help, file);
|
return new Diagnostic(severity, message, help, file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class FileInfo(string file, int line, int column, int length)
|
|
||||||
{
|
|
||||||
public string File { get; } = file;
|
|
||||||
public int Line { get; } = line;
|
|
||||||
public int Column { get; } = column;
|
|
||||||
public int Length { get; } = length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum DiagnosticSeverity
|
public class CompileException(Diagnostic diagnostic) : Exception
|
||||||
{
|
|
||||||
Info,
|
|
||||||
Warning,
|
|
||||||
Error,
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed class CompileException(Diagnostic diagnostic) : Exception
|
|
||||||
{
|
{
|
||||||
public Diagnostic Diagnostic { get; } = diagnostic;
|
public Diagnostic Diagnostic { get; } = diagnostic;
|
||||||
}
|
}
|
||||||
@@ -93,9 +101,9 @@ public static class DiagnosticFormatter
|
|||||||
{
|
{
|
||||||
var (label, color) = diagnostic.Severity switch
|
var (label, color) = diagnostic.Severity switch
|
||||||
{
|
{
|
||||||
DiagnosticSeverity.Info => ("info", Ansi.Cyan),
|
Diagnostic.DiagnosticSeverity.Info => ("info", Ansi.Cyan),
|
||||||
DiagnosticSeverity.Warning => ("warning", Ansi.Yellow),
|
Diagnostic.DiagnosticSeverity.Warning => ("warning", Ansi.Yellow),
|
||||||
DiagnosticSeverity.Error => ("error", Ansi.Red),
|
Diagnostic.DiagnosticSeverity.Error => ("error", Ansi.Red),
|
||||||
_ => ("unknown", Ansi.Reset),
|
_ => ("unknown", Ansi.Reset),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,23 @@
|
|||||||
|
|
||||||
namespace Compiler;
|
namespace Compiler;
|
||||||
|
|
||||||
public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph, bool compileLib)
|
public class Generator
|
||||||
{
|
{
|
||||||
public static string Emit(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph, bool compileLib)
|
public static string Emit(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph, bool compileLib)
|
||||||
{
|
{
|
||||||
return new Generator(functions, moduleGraph, compileLib).Emit();
|
return new Generator(functions, moduleGraph, compileLib).Emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Generator(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph, bool compileLib)
|
||||||
|
{
|
||||||
|
this.functions = functions;
|
||||||
|
this.moduleGraph = moduleGraph;
|
||||||
|
this.compileLib = compileLib;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly List<TypedNodeDefinitionFunc> functions;
|
||||||
|
private readonly ModuleGraph moduleGraph;
|
||||||
|
private readonly bool compileLib;
|
||||||
private readonly IndentedTextWriter writer = new();
|
private readonly IndentedTextWriter writer = new();
|
||||||
private readonly Dictionary<NubTypeStruct, string> structTypeNames = new();
|
private readonly Dictionary<NubTypeStruct, string> structTypeNames = new();
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,16 @@ using System.Diagnostics.CodeAnalysis;
|
|||||||
|
|
||||||
namespace Compiler;
|
namespace Compiler;
|
||||||
|
|
||||||
public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
public class ModuleGraph
|
||||||
{
|
{
|
||||||
public static Builder Create() => new();
|
public static Builder CreateBuilder() => new();
|
||||||
|
|
||||||
|
private ModuleGraph(Dictionary<string, Module> modules)
|
||||||
|
{
|
||||||
|
this.modules = modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly Dictionary<string, Module> modules;
|
||||||
|
|
||||||
public List<Module> GetModules()
|
public List<Module> GetModules()
|
||||||
{
|
{
|
||||||
@@ -23,7 +30,7 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
|||||||
return new Manifest(1, GetModules().Select(x => x.CreateManifestModule()).ToList());
|
return new Manifest(1, GetModules().Select(x => x.CreateManifestModule()).ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class Module(string name)
|
public class Module(string name)
|
||||||
{
|
{
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
private readonly Dictionary<string, CustomTypeInfo> customTypes = new();
|
private readonly Dictionary<string, CustomTypeInfo> customTypes = new();
|
||||||
@@ -108,7 +115,7 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
|||||||
|
|
||||||
public ManifestCustomTypeInfo CreateManifestCustomTypeInfo()
|
public ManifestCustomTypeInfo CreateManifestCustomTypeInfo()
|
||||||
{
|
{
|
||||||
return new ManifestCustomTypeInfo(TypeMangler.Encode(Type), Exported);
|
return new ManifestCustomTypeInfo(TypeEncoder.Encode(Type), Exported);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +127,7 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
|||||||
|
|
||||||
public ManifestIdentifierInfo CreateManifestIdentifierInfo()
|
public ManifestIdentifierInfo CreateManifestIdentifierInfo()
|
||||||
{
|
{
|
||||||
return new ManifestIdentifierInfo(TypeMangler.Encode(Type), Exported);
|
return new ManifestIdentifierInfo(TypeEncoder.Encode(Type), Exported);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -159,13 +166,13 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
|||||||
|
|
||||||
foreach (var customType in manifestModule.CustomTypes)
|
foreach (var customType in manifestModule.CustomTypes)
|
||||||
{
|
{
|
||||||
var decoded = TypeMangler.Decode(customType.Value.EncodedType);
|
var decoded = TypeDecoder.Decode(customType.Value.EncodedType);
|
||||||
module.AddCustomType(customType.Key, new Module.CustomTypeInfo(decoded, customType.Value.Exported, Module.Source.Lib));
|
module.AddCustomType(customType.Key, new Module.CustomTypeInfo(decoded, customType.Value.Exported, Module.Source.Lib));
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var identifier in manifestModule.Identifiers)
|
foreach (var identifier in manifestModule.Identifiers)
|
||||||
{
|
{
|
||||||
var decoded = TypeMangler.Decode(identifier.Value.EncodedType);
|
var decoded = TypeDecoder.Decode(identifier.Value.EncodedType);
|
||||||
module.AddIdentifier(identifier.Key, new Module.IdentifierInfo(decoded, identifier.Value.Exported, Module.Source.Lib));
|
module.AddIdentifier(identifier.Key, new Module.IdentifierInfo(decoded, identifier.Value.Exported, Module.Source.Lib));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -234,7 +241,7 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (diagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
if (diagnostics.Any(x => x.Severity == Diagnostic.DiagnosticSeverity.Error))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return new ModuleGraph(modules);
|
return new ModuleGraph(modules);
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using System.Net;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Compiler;
|
namespace Compiler;
|
||||||
@@ -8,7 +7,7 @@ public abstract class NubType
|
|||||||
public abstract override string ToString();
|
public abstract override string ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypeVoid : NubType
|
public class NubTypeVoid : NubType
|
||||||
{
|
{
|
||||||
public static readonly NubTypeVoid Instance = new();
|
public static readonly NubTypeVoid Instance = new();
|
||||||
|
|
||||||
@@ -19,7 +18,7 @@ public sealed class NubTypeVoid : NubType
|
|||||||
public override string ToString() => "void";
|
public override string ToString() => "void";
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypeUInt : NubType
|
public class NubTypeUInt : NubType
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<int, NubTypeUInt> Cache = new();
|
private static readonly Dictionary<int, NubTypeUInt> Cache = new();
|
||||||
|
|
||||||
@@ -41,7 +40,7 @@ public sealed class NubTypeUInt : NubType
|
|||||||
public override string ToString() => $"u{Width}";
|
public override string ToString() => $"u{Width}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypeSInt : NubType
|
public class NubTypeSInt : NubType
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<int, NubTypeSInt> Cache = new();
|
private static readonly Dictionary<int, NubTypeSInt> Cache = new();
|
||||||
|
|
||||||
@@ -63,7 +62,7 @@ public sealed class NubTypeSInt : NubType
|
|||||||
public override string ToString() => $"i{Width}";
|
public override string ToString() => $"i{Width}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypeBool : NubType
|
public class NubTypeBool : NubType
|
||||||
{
|
{
|
||||||
public static readonly NubTypeBool Instance = new();
|
public static readonly NubTypeBool Instance = new();
|
||||||
|
|
||||||
@@ -74,7 +73,7 @@ public sealed class NubTypeBool : NubType
|
|||||||
public override string ToString() => "bool";
|
public override string ToString() => "bool";
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypeString : NubType
|
public class NubTypeString : NubType
|
||||||
{
|
{
|
||||||
public static readonly NubTypeString Instance = new();
|
public static readonly NubTypeString Instance = new();
|
||||||
|
|
||||||
@@ -85,7 +84,7 @@ public sealed class NubTypeString : NubType
|
|||||||
public override string ToString() => "string";
|
public override string ToString() => "string";
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypeStruct : NubType
|
public class NubTypeStruct : NubType
|
||||||
{
|
{
|
||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
public string Module { get; }
|
public string Module { get; }
|
||||||
@@ -112,14 +111,14 @@ public sealed class NubTypeStruct : NubType
|
|||||||
|
|
||||||
public override string ToString() => _resolvedFields == null ? $"struct {Module}::{Name} <unresolved>" : $"struct {Module}::{Name} {{ {string.Join(' ', Fields.Select(f => $"{f.Name}: {f.Type}"))} }}";
|
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 class Field(string name, NubType type)
|
||||||
{
|
{
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
public NubType Type { get; } = type;
|
public NubType Type { get; } = type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypePointer : NubType
|
public class NubTypePointer : NubType
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<NubType, NubTypePointer> Cache = new();
|
private static readonly Dictionary<NubType, NubTypePointer> Cache = new();
|
||||||
|
|
||||||
@@ -141,7 +140,7 @@ public sealed class NubTypePointer : NubType
|
|||||||
public override string ToString() => $"^{To}";
|
public override string ToString() => $"^{To}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypeFunc : NubType
|
public class NubTypeFunc : NubType
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<Signature, NubTypeFunc> Cache = new();
|
private static readonly Dictionary<Signature, NubTypeFunc> Cache = new();
|
||||||
|
|
||||||
@@ -171,7 +170,7 @@ public sealed class NubTypeFunc : NubType
|
|||||||
private readonly record struct Signature(IReadOnlyList<NubType> Parameters, NubType ReturnType);
|
private readonly record struct Signature(IReadOnlyList<NubType> Parameters, NubType ReturnType);
|
||||||
}
|
}
|
||||||
|
|
||||||
static class TypeMangler
|
static class TypeEncoder
|
||||||
{
|
{
|
||||||
public static string Encode(NubType type)
|
public static string Encode(NubType type)
|
||||||
{
|
{
|
||||||
@@ -241,63 +240,78 @@ static class TypeMangler
|
|||||||
throw new NotSupportedException(type.GetType().Name);
|
throw new NotSupportedException(type.GetType().Name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NubType Decode(string encoded)
|
|
||||||
{
|
|
||||||
int pos = 0;
|
|
||||||
return Parse(encoded, ref pos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NubType Parse(string s, ref int pos)
|
class TypeDecoder
|
||||||
{
|
{
|
||||||
if (pos >= s.Length)
|
public static NubType Decode(string encoded)
|
||||||
|
{
|
||||||
|
return new TypeDecoder(encoded).Decode();
|
||||||
|
}
|
||||||
|
|
||||||
|
private TypeDecoder(string encoded)
|
||||||
|
{
|
||||||
|
this.encoded = encoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly string encoded;
|
||||||
|
private int pos;
|
||||||
|
|
||||||
|
private NubType Decode()
|
||||||
|
{
|
||||||
|
return Parse();
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubType Parse()
|
||||||
|
{
|
||||||
|
if (pos >= encoded.Length)
|
||||||
throw new InvalidOperationException("Unexpected end of string");
|
throw new InvalidOperationException("Unexpected end of string");
|
||||||
|
|
||||||
char c = s[pos++];
|
char c = encoded[pos++];
|
||||||
return c switch
|
return c switch
|
||||||
{
|
{
|
||||||
'V' => NubTypeVoid.Instance,
|
'V' => NubTypeVoid.Instance,
|
||||||
'B' => NubTypeBool.Instance,
|
'B' => NubTypeBool.Instance,
|
||||||
'S' => NubTypeString.Instance,
|
'S' => NubTypeString.Instance,
|
||||||
'U' => ParseUInt(s, ref pos),
|
'U' => ParseUInt(),
|
||||||
'I' => ParseSInt(s, ref pos),
|
'I' => ParseSInt(),
|
||||||
'P' => ParsePointer(s, ref pos),
|
'P' => ParsePointer(),
|
||||||
'T' => ParseStruct(s, ref pos),
|
'T' => ParseStruct(),
|
||||||
'F' => ParseFunc(s, ref pos),
|
'F' => ParseFunc(),
|
||||||
_ => throw new NotSupportedException($"Unknown type code '{c}' at position {pos - 1}")
|
_ => throw new NotSupportedException($"Unknown type code '{c}' at position {pos - 1}")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NubTypeUInt ParseUInt(string s, ref int pos)
|
private NubTypeUInt ParseUInt()
|
||||||
{
|
{
|
||||||
ExpectChar(s, ref pos, '(');
|
ExpectChar('(');
|
||||||
int width = ReadNumber(s, ref pos);
|
int width = ReadNumber();
|
||||||
ExpectChar(s, ref pos, ')');
|
ExpectChar(')');
|
||||||
return NubTypeUInt.Get(width);
|
return NubTypeUInt.Get(width);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NubTypeSInt ParseSInt(string s, ref int pos)
|
private NubTypeSInt ParseSInt()
|
||||||
{
|
{
|
||||||
ExpectChar(s, ref pos, '(');
|
ExpectChar('(');
|
||||||
int width = ReadNumber(s, ref pos);
|
int width = ReadNumber();
|
||||||
ExpectChar(s, ref pos, ')');
|
ExpectChar(')');
|
||||||
return NubTypeSInt.Get(width);
|
return NubTypeSInt.Get(width);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NubTypePointer ParsePointer(string s, ref int pos)
|
private NubTypePointer ParsePointer()
|
||||||
{
|
{
|
||||||
ExpectChar(s, ref pos, '(');
|
ExpectChar('(');
|
||||||
var to = Parse(s, ref pos);
|
var to = Parse();
|
||||||
ExpectChar(s, ref pos, ')');
|
ExpectChar(')');
|
||||||
return NubTypePointer.Get(to);
|
return NubTypePointer.Get(to);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NubTypeStruct ParseStruct(string s, ref int pos)
|
private NubTypeStruct ParseStruct()
|
||||||
{
|
{
|
||||||
ExpectChar(s, ref pos, '(');
|
ExpectChar('(');
|
||||||
int start = pos;
|
int start = pos;
|
||||||
while (pos < s.Length && s[pos] != ',' && s[pos] != '{') pos++;
|
while (pos < encoded.Length && encoded[pos] != ',' && encoded[pos] != '{') pos++;
|
||||||
var fullName = s[start..pos];
|
var fullName = encoded[start..pos];
|
||||||
var parts = fullName.Split("::");
|
var parts = fullName.Split("::");
|
||||||
if (parts.Length != 2)
|
if (parts.Length != 2)
|
||||||
throw new InvalidOperationException($"Invalid struct name: {fullName}");
|
throw new InvalidOperationException($"Invalid struct name: {fullName}");
|
||||||
@@ -305,64 +319,64 @@ static class TypeMangler
|
|||||||
string module = parts[0], name = parts[1];
|
string module = parts[0], name = parts[1];
|
||||||
|
|
||||||
bool packed = false;
|
bool packed = false;
|
||||||
if (s[pos] == ',')
|
if (encoded[pos] == ',')
|
||||||
{
|
{
|
||||||
pos += 1;
|
pos += 1;
|
||||||
packed = s[pos += 1] == '1';
|
packed = encoded[pos += 1] == '1';
|
||||||
}
|
}
|
||||||
|
|
||||||
var st = new NubTypeStruct(module, name, packed);
|
var st = new NubTypeStruct(module, name, packed);
|
||||||
|
|
||||||
ExpectChar(s, ref pos, '{');
|
ExpectChar('{');
|
||||||
|
|
||||||
var fields = new List<NubTypeStruct.Field>();
|
var fields = new List<NubTypeStruct.Field>();
|
||||||
while (s[pos] != '}')
|
while (encoded[pos] != '}')
|
||||||
{
|
{
|
||||||
int nameStart = pos;
|
int nameStart = pos;
|
||||||
while (s[pos] != ':') pos += 1;
|
while (encoded[pos] != ':') pos += 1;
|
||||||
string fieldName = s[nameStart..pos];
|
string fieldName = encoded[nameStart..pos];
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
|
||||||
var fieldType = Parse(s, ref pos);
|
var fieldType = Parse();
|
||||||
fields.Add(new NubTypeStruct.Field(fieldName, fieldType));
|
fields.Add(new NubTypeStruct.Field(fieldName, fieldType));
|
||||||
|
|
||||||
if (s[pos] == ',') pos += 1;
|
if (encoded[pos] == ',') pos += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ExpectChar(s, ref pos, '}');
|
ExpectChar('}');
|
||||||
ExpectChar(s, ref pos, ')');
|
ExpectChar(')');
|
||||||
|
|
||||||
st.ResolveFields(fields);
|
st.ResolveFields(fields);
|
||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NubTypeFunc ParseFunc(string s, ref int pos)
|
private NubTypeFunc ParseFunc()
|
||||||
{
|
{
|
||||||
ExpectChar(s, ref pos, '(');
|
ExpectChar('(');
|
||||||
var parameters = new List<NubType>();
|
var parameters = new List<NubType>();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (s[pos] == ')')
|
if (encoded[pos] == ')')
|
||||||
{
|
{
|
||||||
pos++;
|
pos++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var param = Parse(s, ref pos);
|
var param = Parse();
|
||||||
parameters.Add(param);
|
parameters.Add(param);
|
||||||
|
|
||||||
if (s[pos] == ',')
|
if (encoded[pos] == ',')
|
||||||
{
|
{
|
||||||
pos += 1;
|
pos += 1;
|
||||||
}
|
}
|
||||||
else if (s[pos] == ')')
|
else if (encoded[pos] == ')')
|
||||||
{
|
{
|
||||||
pos += 1;
|
pos += 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Unexpected char '{s[pos]}' in function type at {pos}");
|
throw new InvalidOperationException($"Unexpected char '{encoded[pos]}' in function type at {pos}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -375,20 +389,20 @@ static class TypeMangler
|
|||||||
return NubTypeFunc.Get(paramTypes, returnType);
|
return NubTypeFunc.Get(paramTypes, returnType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ExpectChar(string s, ref int pos, char expected)
|
private void ExpectChar(char expected)
|
||||||
{
|
{
|
||||||
if (pos >= s.Length || s[pos] != expected)
|
if (pos >= encoded.Length || encoded[pos] != expected)
|
||||||
throw new InvalidOperationException($"Expected '{expected}' at position {pos}");
|
throw new InvalidOperationException($"Expected '{expected}' at position {pos}");
|
||||||
|
|
||||||
pos += 1;
|
pos += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int ReadNumber(string s, ref int pos)
|
private int ReadNumber()
|
||||||
{
|
{
|
||||||
int start = pos;
|
int start = pos;
|
||||||
while (pos < s.Length && char.IsDigit(s[pos])) pos += 1;
|
while (pos < encoded.Length && char.IsDigit(encoded[pos])) pos += 1;
|
||||||
if (start == pos) throw new InvalidOperationException($"Expected number at position {start}");
|
if (start == pos) throw new InvalidOperationException($"Expected number at position {start}");
|
||||||
return int.Parse(s[start..pos]);
|
return int.Parse(encoded[start..pos]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,7 +428,7 @@ static class SymbolNameGen
|
|||||||
{
|
{
|
||||||
public static string Exported(string module, string function, NubType type)
|
public static string Exported(string module, string function, NubType type)
|
||||||
{
|
{
|
||||||
var canonical = TypeMangler.Encode(type);
|
var canonical = TypeEncoder.Encode(type);
|
||||||
var hash = Hashing.Fnv1a64(canonical);
|
var hash = Hashing.Fnv1a64(canonical);
|
||||||
|
|
||||||
return $"nub_{Sanitize(module)}_{Sanitize(function)}_{hash:x16}";
|
return $"nub_{Sanitize(module)}_{Sanitize(function)}_{hash:x16}";
|
||||||
|
|||||||
@@ -2,13 +2,21 @@
|
|||||||
|
|
||||||
namespace Compiler;
|
namespace Compiler;
|
||||||
|
|
||||||
public sealed class Parser(string fileName, List<Token> tokens)
|
public class Parser
|
||||||
{
|
{
|
||||||
public static Ast? Parse(string fileName, List<Token> tokens, out List<Diagnostic> diagnostics)
|
public static Ast? Parse(string fileName, List<Token> tokens, out List<Diagnostic> diagnostics)
|
||||||
{
|
{
|
||||||
return new Parser(fileName, tokens).Parse(out diagnostics);
|
return new Parser(fileName, tokens).Parse(out diagnostics);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Parser(string fileName, List<Token> tokens)
|
||||||
|
{
|
||||||
|
this.fileName = fileName;
|
||||||
|
this.tokens = tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly string fileName;
|
||||||
|
private readonly List<Token> tokens;
|
||||||
private int index;
|
private int index;
|
||||||
|
|
||||||
private Ast? Parse(out List<Diagnostic> diagnostics)
|
private Ast? Parse(out List<Diagnostic> diagnostics)
|
||||||
@@ -33,7 +41,7 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
diagnostics.Add(e.Diagnostic);
|
diagnostics.Add(e.Diagnostic);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (moduleName == null || diagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
if (moduleName == null || diagnostics.Any(x => x.Severity == Diagnostic.DiagnosticSeverity.Error))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return new Ast(fileName, moduleName, definitions);
|
return new Ast(fileName, moduleName, definitions);
|
||||||
@@ -594,7 +602,7 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class Ast(string fileName, TokenIdent moduleName, List<NodeDefinition> definitions)
|
public class Ast(string fileName, TokenIdent moduleName, List<NodeDefinition> definitions)
|
||||||
{
|
{
|
||||||
public string FileName { get; } = fileName;
|
public string FileName { get; } = fileName;
|
||||||
public TokenIdent ModuleName { get; } = moduleName;
|
public TokenIdent ModuleName { get; } = moduleName;
|
||||||
@@ -608,7 +616,7 @@ public abstract class Node(List<Token> tokens)
|
|||||||
|
|
||||||
public abstract class NodeDefinition(List<Token> tokens) : Node(tokens);
|
public abstract class NodeDefinition(List<Token> tokens) : Node(tokens);
|
||||||
|
|
||||||
public sealed class NodeDefinitionFunc(List<Token> tokens, bool exported, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeStatement body, NodeType returnType) : NodeDefinition(tokens)
|
public class NodeDefinitionFunc(List<Token> tokens, bool exported, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeStatement body, NodeType returnType) : NodeDefinition(tokens)
|
||||||
{
|
{
|
||||||
public bool Exported { get; } = exported;
|
public bool Exported { get; } = exported;
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
@@ -616,28 +624,28 @@ public sealed class NodeDefinitionFunc(List<Token> tokens, bool exported, TokenI
|
|||||||
public NodeStatement Body { get; } = body;
|
public NodeStatement Body { get; } = body;
|
||||||
public NodeType ReturnType { get; } = returnType;
|
public NodeType ReturnType { get; } = returnType;
|
||||||
|
|
||||||
public sealed class Param(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens)
|
public class Param(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public NodeType Type { get; } = type;
|
public NodeType Type { get; } = type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeDefinitionStruct(List<Token> tokens, bool exported, bool packed, TokenIdent name, List<NodeDefinitionStruct.Field> fields) : NodeDefinition(tokens)
|
public class NodeDefinitionStruct(List<Token> tokens, bool exported, bool packed, TokenIdent name, List<NodeDefinitionStruct.Field> fields) : NodeDefinition(tokens)
|
||||||
{
|
{
|
||||||
public bool Exported { get; } = exported;
|
public bool Exported { get; } = exported;
|
||||||
public bool Packed { get; } = packed;
|
public bool Packed { get; } = packed;
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public List<Field> Fields { get; } = fields;
|
public List<Field> Fields { get; } = fields;
|
||||||
|
|
||||||
public sealed class Field(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens)
|
public class Field(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public NodeType Type { get; } = type;
|
public NodeType Type { get; } = type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeDefinitionGlobalVariable(List<Token> tokens, bool exported, TokenIdent name, NodeType type) : NodeDefinition(tokens)
|
public class NodeDefinitionGlobalVariable(List<Token> tokens, bool exported, TokenIdent name, NodeType type) : NodeDefinition(tokens)
|
||||||
{
|
{
|
||||||
public bool Exported { get; } = exported;
|
public bool Exported { get; } = exported;
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
@@ -646,42 +654,42 @@ public sealed class NodeDefinitionGlobalVariable(List<Token> tokens, bool export
|
|||||||
|
|
||||||
public abstract class NodeStatement(List<Token> tokens) : Node(tokens);
|
public abstract class NodeStatement(List<Token> tokens) : Node(tokens);
|
||||||
|
|
||||||
public sealed class NodeStatementBlock(List<Token> tokens, List<NodeStatement> statements) : NodeStatement(tokens)
|
public class NodeStatementBlock(List<Token> tokens, List<NodeStatement> statements) : NodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public List<NodeStatement> Statements { get; } = statements;
|
public List<NodeStatement> Statements { get; } = statements;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeStatementExpression(List<Token> tokens, NodeExpression expression) : NodeStatement(tokens)
|
public class NodeStatementExpression(List<Token> tokens, NodeExpression expression) : NodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public NodeExpression Expression { get; } = expression;
|
public NodeExpression Expression { get; } = expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeStatementReturn(List<Token> tokens, NodeExpression value) : NodeStatement(tokens)
|
public class NodeStatementReturn(List<Token> tokens, NodeExpression value) : NodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public NodeExpression Value { get; } = value;
|
public NodeExpression Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NodeType type, NodeExpression value) : NodeStatement(tokens)
|
public class NodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NodeType type, NodeExpression value) : NodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public NodeType Type { get; } = type;
|
public NodeType Type { get; } = type;
|
||||||
public NodeExpression Value { get; } = value;
|
public NodeExpression Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeStatementAssignment(List<Token> tokens, NodeExpression target, NodeExpression value) : NodeStatement(tokens)
|
public class NodeStatementAssignment(List<Token> tokens, NodeExpression target, NodeExpression value) : NodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public NodeExpression Target { get; } = target;
|
public NodeExpression Target { get; } = target;
|
||||||
public NodeExpression Value { get; } = value;
|
public NodeExpression Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeStatementIf(List<Token> tokens, NodeExpression condition, NodeStatement thenBlock, NodeStatement? elseBlock) : NodeStatement(tokens)
|
public class NodeStatementIf(List<Token> tokens, NodeExpression condition, NodeStatement thenBlock, NodeStatement? elseBlock) : NodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public NodeExpression Condition { get; } = condition;
|
public NodeExpression Condition { get; } = condition;
|
||||||
public NodeStatement ThenBlock { get; } = thenBlock;
|
public NodeStatement ThenBlock { get; } = thenBlock;
|
||||||
public NodeStatement? ElseBlock { get; } = elseBlock;
|
public NodeStatement? ElseBlock { get; } = elseBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeStatementWhile(List<Token> tokens, NodeExpression condition, NodeStatement block) : NodeStatement(tokens)
|
public class NodeStatementWhile(List<Token> tokens, NodeExpression condition, NodeStatement block) : NodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public NodeExpression Condition { get; } = condition;
|
public NodeExpression Condition { get; } = condition;
|
||||||
public NodeStatement Block { get; } = block;
|
public NodeStatement Block { get; } = block;
|
||||||
@@ -689,58 +697,58 @@ public sealed class NodeStatementWhile(List<Token> tokens, NodeExpression condit
|
|||||||
|
|
||||||
public abstract class NodeExpression(List<Token> tokens) : Node(tokens);
|
public abstract class NodeExpression(List<Token> tokens) : Node(tokens);
|
||||||
|
|
||||||
public sealed class NodeExpressionIntLiteral(List<Token> tokens, TokenIntLiteral value) : NodeExpression(tokens)
|
public class NodeExpressionIntLiteral(List<Token> tokens, TokenIntLiteral value) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public TokenIntLiteral Value { get; } = value;
|
public TokenIntLiteral Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeExpressionStringLiteral(List<Token> tokens, TokenStringLiteral value) : NodeExpression(tokens)
|
public class NodeExpressionStringLiteral(List<Token> tokens, TokenStringLiteral value) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public TokenStringLiteral Value { get; } = value;
|
public TokenStringLiteral Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeExpressionBoolLiteral(List<Token> tokens, TokenBoolLiteral value) : NodeExpression(tokens)
|
public class NodeExpressionBoolLiteral(List<Token> tokens, TokenBoolLiteral value) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public TokenBoolLiteral Value { get; } = value;
|
public TokenBoolLiteral Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeExpressionStructLiteral(List<Token> tokens, TokenIdent module, TokenIdent name, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens)
|
public class NodeExpressionStructLiteral(List<Token> tokens, TokenIdent module, TokenIdent name, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Module { get; } = module;
|
public TokenIdent Module { get; } = module;
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public List<Initializer> Initializers { get; } = initializers;
|
public List<Initializer> Initializers { get; } = initializers;
|
||||||
|
|
||||||
public sealed class Initializer(List<Token> tokens, TokenIdent name, NodeExpression value) : Node(tokens)
|
public class Initializer(List<Token> tokens, TokenIdent name, NodeExpression value) : Node(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public NodeExpression Value { get; } = value;
|
public NodeExpression Value { get; } = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeExpressionMemberAccess(List<Token> tokens, NodeExpression target, TokenIdent name) : NodeExpression(tokens)
|
public class NodeExpressionMemberAccess(List<Token> tokens, NodeExpression target, TokenIdent name) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public NodeExpression Target { get; } = target;
|
public NodeExpression Target { get; } = target;
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeExpressionFuncCall(List<Token> tokens, NodeExpression target, List<NodeExpression> parameters) : NodeExpression(tokens)
|
public class NodeExpressionFuncCall(List<Token> tokens, NodeExpression target, List<NodeExpression> parameters) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public NodeExpression Target { get; } = target;
|
public NodeExpression Target { get; } = target;
|
||||||
public List<NodeExpression> Parameters { get; } = parameters;
|
public List<NodeExpression> Parameters { get; } = parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeExpressionLocalIdent(List<Token> tokens, TokenIdent value) : NodeExpression(tokens)
|
public class NodeExpressionLocalIdent(List<Token> tokens, TokenIdent value) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Value { get; } = value;
|
public TokenIdent Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeExpressionModuleIdent(List<Token> tokens, TokenIdent module, TokenIdent value) : NodeExpression(tokens)
|
public class NodeExpressionModuleIdent(List<Token> tokens, TokenIdent module, TokenIdent value) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Module { get; } = module;
|
public TokenIdent Module { get; } = module;
|
||||||
public TokenIdent Value { get; } = value;
|
public TokenIdent Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeExpressionBinary(List<Token> tokens, NodeExpression left, NodeExpressionBinary.Op operation, NodeExpression right) : NodeExpression(tokens)
|
public class NodeExpressionBinary(List<Token> tokens, NodeExpression left, NodeExpressionBinary.Op operation, NodeExpression right) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public NodeExpression Left { get; } = left;
|
public NodeExpression Left { get; } = left;
|
||||||
public Op Operation { get; } = operation;
|
public Op Operation { get; } = operation;
|
||||||
@@ -773,7 +781,7 @@ public sealed class NodeExpressionBinary(List<Token> tokens, NodeExpression left
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeExpressionUnary(List<Token> tokens, NodeExpression target, NodeExpressionUnary.Op op) : NodeExpression(tokens)
|
public class NodeExpressionUnary(List<Token> tokens, NodeExpression target, NodeExpressionUnary.Op op) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public NodeExpression Target { get; } = target;
|
public NodeExpression Target { get; } = target;
|
||||||
public Op Operation { get; } = op;
|
public Op Operation { get; } = op;
|
||||||
@@ -787,34 +795,34 @@ public sealed class NodeExpressionUnary(List<Token> tokens, NodeExpression targe
|
|||||||
|
|
||||||
public abstract class NodeType(List<Token> tokens) : Node(tokens);
|
public abstract class NodeType(List<Token> tokens) : Node(tokens);
|
||||||
|
|
||||||
public sealed class NodeTypeVoid(List<Token> tokens) : NodeType(tokens);
|
public class NodeTypeVoid(List<Token> tokens) : NodeType(tokens);
|
||||||
|
|
||||||
public sealed class NodeTypeUInt(List<Token> tokens, int width) : NodeType(tokens)
|
public class NodeTypeUInt(List<Token> tokens, int width) : NodeType(tokens)
|
||||||
{
|
{
|
||||||
public int Width { get; } = width;
|
public int Width { get; } = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeTypeSInt(List<Token> tokens, int width) : NodeType(tokens)
|
public class NodeTypeSInt(List<Token> tokens, int width) : NodeType(tokens)
|
||||||
{
|
{
|
||||||
public int Width { get; } = width;
|
public int Width { get; } = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeTypeBool(List<Token> tokens) : NodeType(tokens);
|
public class NodeTypeBool(List<Token> tokens) : NodeType(tokens);
|
||||||
|
|
||||||
public sealed class NodeTypeString(List<Token> tokens) : NodeType(tokens);
|
public class NodeTypeString(List<Token> tokens) : NodeType(tokens);
|
||||||
|
|
||||||
public sealed class NodeTypeCustom(List<Token> tokens, TokenIdent module, TokenIdent name) : NodeType(tokens)
|
public class NodeTypeCustom(List<Token> tokens, TokenIdent module, TokenIdent name) : NodeType(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Module { get; } = module;
|
public TokenIdent Module { get; } = module;
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeTypePointer(List<Token> tokens, NodeType to) : NodeType(tokens)
|
public class NodeTypePointer(List<Token> tokens, NodeType to) : NodeType(tokens)
|
||||||
{
|
{
|
||||||
public NodeType To { get; } = to;
|
public NodeType To { get; } = to;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeTypeFunc(List<Token> tokens, List<NodeType> parameters, NodeType returnType) : NodeType(tokens)
|
public class NodeTypeFunc(List<Token> tokens, List<NodeType> parameters, NodeType returnType) : NodeType(tokens)
|
||||||
{
|
{
|
||||||
public List<NodeType> Parameters { get; } = parameters;
|
public List<NodeType> Parameters { get; } = parameters;
|
||||||
public NodeType ReturnType { get; } = returnType;
|
public NodeType ReturnType { get; } = returnType;
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ for (int i = 0; i < args.Length; i++)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var moduleGraphBuilder = ModuleGraph.Create();
|
var moduleGraphBuilder = ModuleGraph.CreateBuilder();
|
||||||
var asts = new List<Ast>();
|
var asts = new List<Ast>();
|
||||||
var archivePaths = new List<string>();
|
var archivePaths = new List<string>();
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,21 @@ using System.Text;
|
|||||||
|
|
||||||
namespace Compiler;
|
namespace Compiler;
|
||||||
|
|
||||||
public sealed class Tokenizer(string fileName, string contents)
|
public class Tokenizer
|
||||||
{
|
{
|
||||||
public static List<Token>? Tokenize(string fileName, string contents, out List<Diagnostic> diagnostics)
|
public static List<Token>? Tokenize(string fileName, string contents, out List<Diagnostic> diagnostics)
|
||||||
{
|
{
|
||||||
return new Tokenizer(fileName, contents).Tokenize(out diagnostics);
|
return new Tokenizer(fileName, contents).Tokenize(out diagnostics);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Tokenizer(string fileName, string contents)
|
||||||
|
{
|
||||||
|
this.fileName = fileName;
|
||||||
|
this.contents = contents;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly string fileName;
|
||||||
|
private readonly string contents;
|
||||||
private int index;
|
private int index;
|
||||||
private int line = 1;
|
private int line = 1;
|
||||||
private int column = 1;
|
private int column = 1;
|
||||||
@@ -53,7 +61,7 @@ public sealed class Tokenizer(string fileName, string contents)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (diagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
if (diagnostics.Any(x => x.Severity == Diagnostic.DiagnosticSeverity.Error))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return tokens;
|
return tokens;
|
||||||
@@ -459,22 +467,22 @@ public abstract class Token(int line, int column, int length)
|
|||||||
public int Length { get; } = length;
|
public int Length { get; } = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TokenIdent(int line, int column, int length, string ident) : Token(line, column, length)
|
public class TokenIdent(int line, int column, int length, string ident) : Token(line, column, length)
|
||||||
{
|
{
|
||||||
public string Ident { get; } = ident;
|
public string Ident { get; } = ident;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TokenIntLiteral(int line, int column, int length, BigInteger value) : Token(line, column, length)
|
public class TokenIntLiteral(int line, int column, int length, BigInteger value) : Token(line, column, length)
|
||||||
{
|
{
|
||||||
public BigInteger Value { get; } = value;
|
public BigInteger Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TokenStringLiteral(int line, int column, int length, string value) : Token(line, column, length)
|
public class TokenStringLiteral(int line, int column, int length, string value) : Token(line, column, length)
|
||||||
{
|
{
|
||||||
public string Value { get; } = value;
|
public string Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TokenBoolLiteral(int line, int column, int length, bool value) : Token(line, column, length)
|
public class TokenBoolLiteral(int line, int column, int length, bool value) : Token(line, column, length)
|
||||||
{
|
{
|
||||||
public bool Value { get; } = value;
|
public bool Value { get; } = value;
|
||||||
}
|
}
|
||||||
@@ -516,7 +524,7 @@ public enum Symbol
|
|||||||
PipePipe,
|
PipePipe,
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TokenSymbol(int line, int column, int length, Symbol symbol) : Token(line, column, length)
|
public class TokenSymbol(int line, int column, int length, Symbol symbol) : Token(line, column, length)
|
||||||
{
|
{
|
||||||
public Symbol Symbol { get; } = symbol;
|
public Symbol Symbol { get; } = symbol;
|
||||||
}
|
}
|
||||||
@@ -535,7 +543,7 @@ public enum Keyword
|
|||||||
Export,
|
Export,
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TokenKeyword(int line, int column, int length, Keyword keyword) : Token(line, column, length)
|
public class TokenKeyword(int line, int column, int length, Keyword keyword) : Token(line, column, length)
|
||||||
{
|
{
|
||||||
public Keyword Keyword { get; } = keyword;
|
public Keyword Keyword { get; } = keyword;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,24 @@
|
|||||||
namespace Compiler;
|
namespace Compiler;
|
||||||
|
|
||||||
public sealed class TypeChecker(string fileName, string moduleName, NodeDefinitionFunc function, ModuleGraph moduleGraph)
|
public class TypeChecker
|
||||||
{
|
{
|
||||||
public static TypedNodeDefinitionFunc? CheckFunction(string fileName, string moduleName, NodeDefinitionFunc function, ModuleGraph moduleGraph, out List<Diagnostic> diagnostics)
|
public static TypedNodeDefinitionFunc? CheckFunction(string fileName, string moduleName, NodeDefinitionFunc function, ModuleGraph moduleGraph, out List<Diagnostic> diagnostics)
|
||||||
{
|
{
|
||||||
return new TypeChecker(fileName, moduleName, function, moduleGraph).CheckFunction(out diagnostics);
|
return new TypeChecker(fileName, moduleName, function, moduleGraph).CheckFunction(out diagnostics);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private TypeChecker(string fileName, string moduleName, NodeDefinitionFunc function, ModuleGraph moduleGraph)
|
||||||
|
{
|
||||||
|
this.fileName = fileName;
|
||||||
|
this.moduleName = moduleName;
|
||||||
|
this.function = function;
|
||||||
|
this.moduleGraph = moduleGraph;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly string fileName;
|
||||||
|
private readonly string moduleName;
|
||||||
|
private readonly NodeDefinitionFunc function;
|
||||||
|
private readonly ModuleGraph moduleGraph;
|
||||||
private readonly Scope scope = new(null);
|
private readonly Scope scope = new(null);
|
||||||
|
|
||||||
private TypedNodeDefinitionFunc? CheckFunction(out List<Diagnostic> diagnostics)
|
private TypedNodeDefinitionFunc? CheckFunction(out List<Diagnostic> diagnostics)
|
||||||
@@ -422,7 +434,7 @@ public abstract class TypedNodeDefinition(List<Token> tokens, string module) : T
|
|||||||
public string Module { get; } = module;
|
public string Module { get; } = module;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeDefinitionFunc(List<Token> tokens, string module, TokenIdent name, List<TypedNodeDefinitionFunc.Param> parameters, TypedNodeStatement body, NubType returnType) : TypedNodeDefinition(tokens, module)
|
public class TypedNodeDefinitionFunc(List<Token> tokens, string module, TokenIdent name, List<TypedNodeDefinitionFunc.Param> parameters, TypedNodeStatement body, NubType returnType) : TypedNodeDefinition(tokens, module)
|
||||||
{
|
{
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public List<Param> Parameters { get; } = parameters;
|
public List<Param> Parameters { get; } = parameters;
|
||||||
@@ -439,7 +451,7 @@ public sealed class TypedNodeDefinitionFunc(List<Token> tokens, string module, T
|
|||||||
return SymbolNameGen.Exported(Module, Name.Ident, GetNubType());
|
return SymbolNameGen.Exported(Module, Name.Ident, GetNubType());
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class Param(List<Token> tokens, TokenIdent name, NubType type) : TypedNode(tokens)
|
public class Param(List<Token> tokens, TokenIdent name, NubType type) : TypedNode(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public NubType Type { get; } = type;
|
public NubType Type { get; } = type;
|
||||||
@@ -448,43 +460,43 @@ public sealed class TypedNodeDefinitionFunc(List<Token> tokens, string module, T
|
|||||||
|
|
||||||
public abstract class TypedNodeStatement(List<Token> tokens) : TypedNode(tokens);
|
public abstract class TypedNodeStatement(List<Token> tokens) : TypedNode(tokens);
|
||||||
|
|
||||||
public sealed class TypedNodeStatementBlock(List<Token> tokens, List<TypedNodeStatement> statements) : TypedNodeStatement(tokens)
|
public class TypedNodeStatementBlock(List<Token> tokens, List<TypedNodeStatement> statements) : TypedNodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public List<TypedNodeStatement> Statements { get; } = statements;
|
public List<TypedNodeStatement> Statements { get; } = statements;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeStatementFuncCall(List<Token> tokens, TypedNodeExpression target, List<TypedNodeExpression> parameters) : TypedNodeStatement(tokens)
|
public class TypedNodeStatementFuncCall(List<Token> tokens, TypedNodeExpression target, List<TypedNodeExpression> parameters) : TypedNodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public TypedNodeExpression Target { get; } = target;
|
public TypedNodeExpression Target { get; } = target;
|
||||||
public List<TypedNodeExpression> Parameters { get; } = parameters;
|
public List<TypedNodeExpression> Parameters { get; } = parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeStatementReturn(List<Token> tokens, TypedNodeExpression value) : TypedNodeStatement(tokens)
|
public class TypedNodeStatementReturn(List<Token> tokens, TypedNodeExpression value) : TypedNodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public TypedNodeExpression Value { get; } = value;
|
public TypedNodeExpression Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NubType type, TypedNodeExpression value) : TypedNodeStatement(tokens)
|
public class TypedNodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NubType type, TypedNodeExpression value) : TypedNodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public NubType Type { get; } = type;
|
public NubType Type { get; } = type;
|
||||||
public TypedNodeExpression Value { get; } = value;
|
public TypedNodeExpression Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeStatementAssignment(List<Token> tokens, TypedNodeExpression target, TypedNodeExpression value) : TypedNodeStatement(tokens)
|
public class TypedNodeStatementAssignment(List<Token> tokens, TypedNodeExpression target, TypedNodeExpression value) : TypedNodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public TypedNodeExpression Target { get; } = target;
|
public TypedNodeExpression Target { get; } = target;
|
||||||
public TypedNodeExpression Value { get; } = value;
|
public TypedNodeExpression Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeStatementIf(List<Token> tokens, TypedNodeExpression condition, TypedNodeStatement thenBlock, TypedNodeStatement? elseBlock) : TypedNodeStatement(tokens)
|
public class TypedNodeStatementIf(List<Token> tokens, TypedNodeExpression condition, TypedNodeStatement thenBlock, TypedNodeStatement? elseBlock) : TypedNodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public TypedNodeExpression Condition { get; } = condition;
|
public TypedNodeExpression Condition { get; } = condition;
|
||||||
public TypedNodeStatement ThenBlock { get; } = thenBlock;
|
public TypedNodeStatement ThenBlock { get; } = thenBlock;
|
||||||
public TypedNodeStatement? ElseBlock { get; } = elseBlock;
|
public TypedNodeStatement? ElseBlock { get; } = elseBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeStatementWhile(List<Token> tokens, TypedNodeExpression condition, TypedNodeStatement block) : TypedNodeStatement(tokens)
|
public class TypedNodeStatementWhile(List<Token> tokens, TypedNodeExpression condition, TypedNodeStatement block) : TypedNodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public TypedNodeExpression Condition { get; } = condition;
|
public TypedNodeExpression Condition { get; } = condition;
|
||||||
public TypedNodeStatement Block { get; } = block;
|
public TypedNodeStatement Block { get; } = block;
|
||||||
@@ -495,56 +507,56 @@ public abstract class TypedNodeExpression(List<Token> tokens, NubType type) : Ty
|
|||||||
public NubType Type { get; } = type;
|
public NubType Type { get; } = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeExpressionIntLiteral(List<Token> tokens, NubType type, TokenIntLiteral value) : TypedNodeExpression(tokens, type)
|
public class TypedNodeExpressionIntLiteral(List<Token> tokens, NubType type, TokenIntLiteral value) : TypedNodeExpression(tokens, type)
|
||||||
{
|
{
|
||||||
public TokenIntLiteral Value { get; } = value;
|
public TokenIntLiteral Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeExpressionStringLiteral(List<Token> tokens, NubType type, TokenStringLiteral value) : TypedNodeExpression(tokens, type)
|
public class TypedNodeExpressionStringLiteral(List<Token> tokens, NubType type, TokenStringLiteral value) : TypedNodeExpression(tokens, type)
|
||||||
{
|
{
|
||||||
public TokenStringLiteral Value { get; } = value;
|
public TokenStringLiteral Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeExpressionBoolLiteral(List<Token> tokens, NubType type, TokenBoolLiteral value) : TypedNodeExpression(tokens, type)
|
public class TypedNodeExpressionBoolLiteral(List<Token> tokens, NubType type, TokenBoolLiteral value) : TypedNodeExpression(tokens, type)
|
||||||
{
|
{
|
||||||
public TokenBoolLiteral Value { get; } = value;
|
public TokenBoolLiteral Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeExpressionStructLiteral(List<Token> tokens, NubType type, List<TypedNodeExpressionStructLiteral.Initializer> initializers) : TypedNodeExpression(tokens, type)
|
public class TypedNodeExpressionStructLiteral(List<Token> tokens, NubType type, List<TypedNodeExpressionStructLiteral.Initializer> initializers) : TypedNodeExpression(tokens, type)
|
||||||
{
|
{
|
||||||
public List<Initializer> Initializers { get; } = initializers;
|
public List<Initializer> Initializers { get; } = initializers;
|
||||||
|
|
||||||
public sealed class Initializer(List<Token> tokens, TokenIdent name, TypedNodeExpression value) : Node(tokens)
|
public class Initializer(List<Token> tokens, TokenIdent name, TypedNodeExpression value) : Node(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public TypedNodeExpression Value { get; } = value;
|
public TypedNodeExpression Value { get; } = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeExpressionMemberAccess(List<Token> tokens, NubType type, TypedNodeExpression target, TokenIdent name) : TypedNodeExpression(tokens, type)
|
public class TypedNodeExpressionMemberAccess(List<Token> tokens, NubType type, TypedNodeExpression target, TokenIdent name) : TypedNodeExpression(tokens, type)
|
||||||
{
|
{
|
||||||
public TypedNodeExpression Target { get; } = target;
|
public TypedNodeExpression Target { get; } = target;
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeExpressionFuncCall(List<Token> tokens, NubType type, TypedNodeExpression target, List<TypedNodeExpression> parameters) : TypedNodeExpression(tokens, type)
|
public class TypedNodeExpressionFuncCall(List<Token> tokens, NubType type, TypedNodeExpression target, List<TypedNodeExpression> parameters) : TypedNodeExpression(tokens, type)
|
||||||
{
|
{
|
||||||
public TypedNodeExpression Target { get; } = target;
|
public TypedNodeExpression Target { get; } = target;
|
||||||
public List<TypedNodeExpression> Parameters { get; } = parameters;
|
public List<TypedNodeExpression> Parameters { get; } = parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeExpressionLocalIdent(List<Token> tokens, NubType type, TokenIdent value) : TypedNodeExpression(tokens, type)
|
public class TypedNodeExpressionLocalIdent(List<Token> tokens, NubType type, TokenIdent value) : TypedNodeExpression(tokens, type)
|
||||||
{
|
{
|
||||||
public TokenIdent Value { get; } = value;
|
public TokenIdent Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeExpressionModuleIdent(List<Token> tokens, NubType type, TokenIdent module, TokenIdent value) : TypedNodeExpression(tokens, type)
|
public class TypedNodeExpressionModuleIdent(List<Token> tokens, NubType type, TokenIdent module, TokenIdent value) : TypedNodeExpression(tokens, type)
|
||||||
{
|
{
|
||||||
public TokenIdent Module { get; } = module;
|
public TokenIdent Module { get; } = module;
|
||||||
public TokenIdent Value { get; } = value;
|
public TokenIdent Value { get; } = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeExpressionBinary(List<Token> tokens, NubType type, TypedNodeExpression left, TypedNodeExpressionBinary.Op operation, TypedNodeExpression right) : TypedNodeExpression(tokens, type)
|
public class TypedNodeExpressionBinary(List<Token> tokens, NubType type, TypedNodeExpression left, TypedNodeExpressionBinary.Op operation, TypedNodeExpression right) : TypedNodeExpression(tokens, type)
|
||||||
{
|
{
|
||||||
public TypedNodeExpression Left { get; } = left;
|
public TypedNodeExpression Left { get; } = left;
|
||||||
public Op Operation { get; } = operation;
|
public Op Operation { get; } = operation;
|
||||||
@@ -577,7 +589,7 @@ public sealed class TypedNodeExpressionBinary(List<Token> tokens, NubType type,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class TypedNodeExpressionUnary(List<Token> tokens, NubType type, TypedNodeExpression target, TypedNodeExpressionUnary.Op op) : TypedNodeExpression(tokens, type)
|
public class TypedNodeExpressionUnary(List<Token> tokens, NubType type, TypedNodeExpression target, TypedNodeExpressionUnary.Op op) : TypedNodeExpression(tokens, type)
|
||||||
{
|
{
|
||||||
public TypedNodeExpression Target { get; } = target;
|
public TypedNodeExpression Target { get; } = target;
|
||||||
public Op Operation { get; } = op;
|
public Op Operation { get; } = op;
|
||||||
|
|||||||
Reference in New Issue
Block a user