Compare commits
70 Commits
d3e2dcede8
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa5a494d39 | ||
|
|
bc65c3f4fd | ||
|
|
bdeb2c4d73 | ||
|
|
48760dcdda | ||
|
|
63d0eb660b | ||
|
|
918d25f8c8 | ||
|
|
6e631ba1ba | ||
|
|
832184053f | ||
|
|
2ab20652f8 | ||
|
|
6507624e90 | ||
|
|
83255980d7 | ||
|
|
1a1dc1389d | ||
|
|
4210ad878b | ||
|
|
32042d0769 | ||
|
|
dd44e3edc4 | ||
|
|
24d41d3dcb | ||
|
|
c9bf212aa2 | ||
|
|
0e099d0baf | ||
|
|
0be4e35628 | ||
|
|
7dd635fa91 | ||
|
|
693b119781 | ||
|
|
2b7eb56895 | ||
|
|
5f4a4172bf | ||
|
|
9da370e387 | ||
|
|
faf506f409 | ||
|
|
16d27c76ab | ||
|
|
270be1f740 | ||
|
|
da2fa81c39 | ||
|
|
677c7dea9e | ||
|
|
6ba05d00d7 | ||
|
|
d58e006be4 | ||
|
|
84627dde45 | ||
|
|
e7aad861d3 | ||
|
|
ecb628c4c2 | ||
|
|
e5227f7a99 | ||
|
|
73ecbf8e9b | ||
|
|
e512650440 | ||
|
|
272ea33616 | ||
|
|
b7dc77cb1c | ||
|
|
3323d760e8 | ||
|
|
aa5bf0b568 | ||
|
|
660166221c | ||
|
|
49734544e6 | ||
|
|
cb4aeb9c01 | ||
|
|
d771396bd4 | ||
|
|
7b1e202424 | ||
|
|
282dd0502a | ||
|
|
3ebaa67b6d | ||
|
|
35867ffa28 | ||
|
|
76efc84984 | ||
|
|
583c01201f | ||
|
|
1511d5d2b8 | ||
|
|
caa3b378b3 | ||
|
|
cbe27c0ae8 | ||
|
|
4d0f7c715f | ||
|
|
c342b2e102 | ||
|
|
ee485e4119 | ||
|
|
21a095f691 | ||
|
|
0a5b39b217 | ||
|
|
ab2f5750dc | ||
|
|
b45a0fa8cc | ||
|
|
7daccba9f3 | ||
|
|
924bdae89b | ||
|
|
5364b0420a | ||
|
|
c65fbeba13 | ||
|
|
9d8d8f255a | ||
|
|
576abe1240 | ||
|
|
d0c31ad17f | ||
|
|
7872a4b6b8 | ||
|
|
6ae10d5f90 |
@@ -1,88 +1,96 @@
|
|||||||
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;
|
|
||||||
public FileInfo? File { get; } = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string message)
|
|
||||||
{
|
|
||||||
private FileInfo? file;
|
|
||||||
private string? help;
|
|
||||||
|
|
||||||
public DiagnosticBuilder At(string fileName, int line, int column, int length)
|
|
||||||
{
|
{
|
||||||
file = new FileInfo(fileName, line, column, length);
|
Severity = severity;
|
||||||
return this;
|
Message = message;
|
||||||
|
Help = help;
|
||||||
|
File = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DiagnosticBuilder At(string fileName, Token? token)
|
public class FileInfo(string file, int line, int column, int length)
|
||||||
{
|
{
|
||||||
if (token != null)
|
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 string? help;
|
||||||
|
|
||||||
|
public Builder At(string fileName, int line, int column, int length)
|
||||||
{
|
{
|
||||||
At(fileName, token.Line, token.Column, token.Length);
|
file = new FileInfo(fileName, line, column, length);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
public Builder At(string fileName, Token? token)
|
||||||
}
|
|
||||||
|
|
||||||
public DiagnosticBuilder At(string fileName, Node? node)
|
|
||||||
{
|
|
||||||
if (node != null && node.Tokens.Count != 0)
|
|
||||||
{
|
{
|
||||||
// todo(nub31): Calculate length based on last token
|
if (token != null)
|
||||||
At(fileName, node.Tokens[0]);
|
{
|
||||||
|
At(fileName, token.Line, token.Column, token.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
public Builder At(string fileName, Node? node)
|
||||||
}
|
|
||||||
|
|
||||||
public DiagnosticBuilder At(string fileName, TypedNode? node)
|
|
||||||
{
|
|
||||||
if (node != null && node.Tokens.Count != 0)
|
|
||||||
{
|
{
|
||||||
// todo(nub31): Calculate length based on last token
|
if (node != null && node.Tokens.Count != 0)
|
||||||
At(fileName, node.Tokens[0]);
|
{
|
||||||
|
// todo(nub31): Calculate length based on last token
|
||||||
|
At(fileName, node.Tokens[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
public Builder At(string fileName, TypedNode? node)
|
||||||
}
|
{
|
||||||
|
if (node != null && node.Tokens.Count != 0)
|
||||||
|
{
|
||||||
|
// todo(nub31): Calculate length based on last token
|
||||||
|
At(fileName, node.Tokens[0]);
|
||||||
|
}
|
||||||
|
|
||||||
public DiagnosticBuilder WithHelp(string helpMessage)
|
return this;
|
||||||
{
|
}
|
||||||
help = helpMessage;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Diagnostic Build()
|
public Builder WithHelp(string helpMessage)
|
||||||
{
|
{
|
||||||
return new Diagnostic(severity, message, help, file);
|
help = helpMessage;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Diagnostic Build()
|
||||||
|
{
|
||||||
|
return new Diagnostic(severity, message, help, file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class FileInfo(string file, int line, int column, int length)
|
public class CompileException(Diagnostic diagnostic) : Exception
|
||||||
{
|
|
||||||
public string File { get; } = file;
|
|
||||||
public int Line { get; } = line;
|
|
||||||
public int Column { get; } = column;
|
|
||||||
public int Length { get; } = length;
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum DiagnosticSeverity
|
|
||||||
{
|
|
||||||
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),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -3,165 +3,444 @@ 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()
|
||||||
{
|
{
|
||||||
return modules.Values.ToList();
|
return modules.Values.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool TryResolveIdentifier(string moduleName, string identifierName, bool searchPrivate, [NotNullWhen(true)] out Module.IdentifierInfo? info)
|
||||||
|
{
|
||||||
|
if (!TryResolveModule(moduleName, out var module))
|
||||||
|
{
|
||||||
|
info = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!module.TryResolveIdentifier(identifierName, searchPrivate, out info))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryResolveType(string moduleName, string typeName, bool searchPrivate, [NotNullWhen(true)] out Module.TypeInfo? info)
|
||||||
|
{
|
||||||
|
if (!TryResolveModule(moduleName, out var module))
|
||||||
|
{
|
||||||
|
info = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!module.TryResolveType(typeName, searchPrivate, out info))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public bool TryResolveModule(string moduleName, [NotNullWhen(true)] out Module? module)
|
public bool TryResolveModule(string moduleName, [NotNullWhen(true)] out Module? module)
|
||||||
{
|
{
|
||||||
module = modules.GetValueOrDefault(moduleName);
|
module = modules.GetValueOrDefault(moduleName);
|
||||||
return module != null;
|
return module != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class Module(string name)
|
|
||||||
{
|
|
||||||
public string Name { get; } = name;
|
|
||||||
private readonly Dictionary<string, NubType> customTypes = new();
|
|
||||||
private readonly Dictionary<string, NubType> identifierTypes = new();
|
|
||||||
|
|
||||||
public List<NubType> GetCustomTypes()
|
|
||||||
{
|
|
||||||
return customTypes.Values.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryResolveCustomType(string name, [NotNullWhen(true)] out NubType? customType)
|
|
||||||
{
|
|
||||||
customType = customTypes.GetValueOrDefault(name);
|
|
||||||
return customType != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryResolveIdentifierType(string name, [NotNullWhen(true)] out NubType? identifier)
|
|
||||||
{
|
|
||||||
identifier = identifierTypes.GetValueOrDefault(name);
|
|
||||||
return identifier != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddCustomType(string name, NubType type)
|
|
||||||
{
|
|
||||||
customTypes.Add(name, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddIdentifier(string name, NubType identifier)
|
|
||||||
{
|
|
||||||
identifierTypes.Add(name, identifier);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Builder
|
public class Builder
|
||||||
{
|
{
|
||||||
private readonly List<Ast> asts = [];
|
private readonly List<Ast> asts = [];
|
||||||
|
private readonly List<Manifest> manifests = [];
|
||||||
|
|
||||||
public void AddAst(Ast ast)
|
public void AddAst(Ast ast)
|
||||||
{
|
{
|
||||||
asts.Add(ast);
|
asts.Add(ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ModuleGraph Build(out List<Diagnostic> diagnostics)
|
public void AddManifest(Manifest manifest)
|
||||||
|
{
|
||||||
|
manifests.Add(manifest);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ModuleGraph? Build(out List<Diagnostic> diagnostics)
|
||||||
{
|
{
|
||||||
diagnostics = [];
|
diagnostics = [];
|
||||||
|
|
||||||
var astModuleCache = new Dictionary<Ast, Module>();
|
|
||||||
var modules = new Dictionary<string, Module>();
|
var modules = new Dictionary<string, Module>();
|
||||||
|
|
||||||
// First pass: Register modules
|
foreach (var manifest in manifests)
|
||||||
foreach (var ast in asts)
|
|
||||||
{
|
{
|
||||||
var moduleDefinitions = ast.Definitions.OfType<NodeDefinitionModule>().ToList();
|
foreach (var (moduleName, manifestModule) in manifest.Modules)
|
||||||
|
|
||||||
if (moduleDefinitions.Count == 0)
|
|
||||||
diagnostics.Add(Diagnostic.Error("Missing module declaration").At(ast.FileName, 1, 1, 1).Build());
|
|
||||||
|
|
||||||
foreach (var extraModuleDefinition in moduleDefinitions.Skip(1))
|
|
||||||
diagnostics.Add(Diagnostic.Warning("Duplicate module declaration will be ignored").At(ast.FileName, extraModuleDefinition).Build());
|
|
||||||
|
|
||||||
if (moduleDefinitions.Count >= 1)
|
|
||||||
{
|
{
|
||||||
var currentModule = moduleDefinitions[0].Name.Ident;
|
var module = GetOrCreateModule(moduleName);
|
||||||
|
|
||||||
if (!modules.ContainsKey(currentModule))
|
foreach (var (name, type) in manifestModule.Types)
|
||||||
{
|
{
|
||||||
var module = new Module(currentModule);
|
switch (type)
|
||||||
modules.Add(currentModule, module);
|
{
|
||||||
astModuleCache[ast] = module;
|
case Manifest.Module.TypeInfoStruct s:
|
||||||
|
{
|
||||||
|
var info = new Module.TypeInfoStruct(Module.DefinitionSource.Imported, s.Exported, s.Packed);
|
||||||
|
var fields = s.Fields.Select(x => new Module.TypeInfoStruct.Field(x.Name, x.Type)).ToList();
|
||||||
|
info.SetFields(fields);
|
||||||
|
module.AddType(name, info);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Manifest.Module.TypeInfoEnum e:
|
||||||
|
{
|
||||||
|
var info = new Module.TypeInfoEnum(Module.DefinitionSource.Imported, e.Exported);
|
||||||
|
var variants = e.Variants.Select(v => new Module.TypeInfoEnum.Variant(v.Name, v.Type)).ToList();
|
||||||
|
info.SetVariants(variants);
|
||||||
|
module.AddType(name, info);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(type));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var (name, identifier) in manifestModule.Identifiers)
|
||||||
|
{
|
||||||
|
module.AddIdentifier(name, new Module.IdentifierInfo(Module.DefinitionSource.Imported, identifier.Exported, identifier.Extern, identifier.Type, identifier.MangledName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Second pass: Register struct types without fields
|
|
||||||
foreach (var ast in asts)
|
foreach (var ast in asts)
|
||||||
{
|
{
|
||||||
var module = astModuleCache.GetValueOrDefault(ast);
|
var module = GetOrCreateModule(ast.ModuleName.Ident);
|
||||||
if (module == null) continue;
|
|
||||||
|
|
||||||
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
|
||||||
module.AddCustomType(structDef.Name.Ident, new NubTypeStruct(module.Name, structDef.Name.Ident));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Third pass: Resolve struct fields
|
|
||||||
foreach (var ast in asts)
|
|
||||||
{
|
|
||||||
var module = astModuleCache.GetValueOrDefault(ast);
|
|
||||||
if (module == null) continue;
|
|
||||||
|
|
||||||
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
||||||
{
|
{
|
||||||
if (!module.TryResolveCustomType(structDef.Name.Ident, out var customType))
|
module.AddType(structDef.Name.Ident, new Module.TypeInfoStruct(Module.DefinitionSource.Internal, structDef.Exported, structDef.Packed));
|
||||||
throw new UnreachableException($"{nameof(customType)} should always be registered");
|
}
|
||||||
|
|
||||||
var fields = structDef.Fields.Select(f => new NubTypeStruct.Field(f.Name.Ident, ResolveType(f.Type))).ToList();
|
foreach (var enumDef in ast.Definitions.OfType<NodeDefinitionEnum>())
|
||||||
((NubTypeStruct)customType).ResolveFields(fields);
|
{
|
||||||
|
module.AddType(enumDef.Name.Ident, new Module.TypeInfoEnum(Module.DefinitionSource.Internal, enumDef.Exported));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fourth pass: Register identifiers
|
|
||||||
foreach (var ast in asts)
|
foreach (var ast in asts)
|
||||||
{
|
{
|
||||||
var module = astModuleCache.GetValueOrDefault(ast);
|
var module = GetOrCreateModule(ast.ModuleName.Ident);
|
||||||
if (module == null) continue;
|
|
||||||
|
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
||||||
|
{
|
||||||
|
if (!module.TryResolveType(structDef.Name.Ident, true, out var typeInfo))
|
||||||
|
throw new UnreachableException($"{nameof(typeInfo)} should always be registered");
|
||||||
|
|
||||||
|
if (typeInfo is Module.TypeInfoStruct structType)
|
||||||
|
{
|
||||||
|
var fields = structDef.Fields.Select(f => new Module.TypeInfoStruct.Field(f.Name.Ident, ResolveType(f.Type, module.Name))).ToList();
|
||||||
|
structType.SetFields(fields);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var enumDef in ast.Definitions.OfType<NodeDefinitionEnum>())
|
||||||
|
{
|
||||||
|
if (!module.TryResolveType(enumDef.Name.Ident, true, out var typeInfo))
|
||||||
|
throw new UnreachableException($"{nameof(typeInfo)} should always be registered");
|
||||||
|
|
||||||
|
if (typeInfo is Module.TypeInfoEnum enumType)
|
||||||
|
{
|
||||||
|
var variants = enumDef.Variants.Select(v => new Module.TypeInfoEnum.Variant(v.Name.Ident, v.Type == null ? null : ResolveType(v.Type, module.Name))).ToList();
|
||||||
|
enumType.SetVariants(variants);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var ast in asts)
|
||||||
|
{
|
||||||
|
var module = GetOrCreateModule(ast.ModuleName.Ident);
|
||||||
|
|
||||||
foreach (var funcDef in ast.Definitions.OfType<NodeDefinitionFunc>())
|
foreach (var funcDef in ast.Definitions.OfType<NodeDefinitionFunc>())
|
||||||
{
|
{
|
||||||
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type)).ToList();
|
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type, module.Name)).ToList();
|
||||||
var returnType = ResolveType(funcDef.ReturnType);
|
var returnType = funcDef.ReturnType == null ? NubTypeVoid.Instance : ResolveType(funcDef.ReturnType, module.Name);
|
||||||
var funcType = NubTypeFunc.Get(parameters, returnType);
|
var funcType = NubTypeFunc.Get(parameters, returnType);
|
||||||
module.AddIdentifier(funcDef.Name.Ident, funcType);
|
var info = new Module.IdentifierInfo(Module.DefinitionSource.Internal, funcDef.Exported, false, funcType, NameMangler.Mangle(module.Name, funcDef.Name.Ident, funcType));
|
||||||
|
module.AddIdentifier(funcDef.Name.Ident, info);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var funcDef in ast.Definitions.OfType<NodeDefinitionExternFunc>())
|
||||||
|
{
|
||||||
|
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type, module.Name)).ToList();
|
||||||
|
var returnType = funcDef.ReturnType == null ? NubTypeVoid.Instance : ResolveType(funcDef.ReturnType, module.Name);
|
||||||
|
var funcType = NubTypeFunc.Get(parameters, returnType);
|
||||||
|
var info = new Module.IdentifierInfo(Module.DefinitionSource.Internal, funcDef.Exported, true, funcType, funcDef.Name.Ident);
|
||||||
|
module.AddIdentifier(funcDef.Name.Ident, info);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var globalVariable in ast.Definitions.OfType<NodeDefinitionGlobalVariable>())
|
||||||
|
{
|
||||||
|
var type = ResolveType(globalVariable.Type, module.Name);
|
||||||
|
var info = new Module.IdentifierInfo(Module.DefinitionSource.Internal, globalVariable.Exported, false, type, NameMangler.Mangle(module.Name, globalVariable.Name.Ident, type));
|
||||||
|
module.AddIdentifier(globalVariable.Name.Ident, info);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var globalVariable in ast.Definitions.OfType<NodeDefinitionExternGlobalVariable>())
|
||||||
|
{
|
||||||
|
var type = ResolveType(globalVariable.Type, module.Name);
|
||||||
|
var info = new Module.IdentifierInfo(Module.DefinitionSource.Internal, globalVariable.Exported, true, type, NameMangler.Mangle(module.Name, globalVariable.Name.Ident, type));
|
||||||
|
module.AddIdentifier(globalVariable.Name.Ident, info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (diagnostics.Any(x => x.Severity == Diagnostic.DiagnosticSeverity.Error))
|
||||||
|
return null;
|
||||||
|
|
||||||
return new ModuleGraph(modules);
|
return new ModuleGraph(modules);
|
||||||
|
|
||||||
NubType ResolveType(NodeType node)
|
NubType ResolveType(NodeType node, string currentModule)
|
||||||
{
|
{
|
||||||
return node switch
|
return node switch
|
||||||
{
|
{
|
||||||
NodeTypeBool => NubTypeBool.Instance,
|
NodeTypeBool => NubTypeBool.Instance,
|
||||||
NodeTypeCustom type => ResolveCustomType(type),
|
NodeTypeNamed type => ResolveNamedType(type, currentModule),
|
||||||
NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(ResolveType).ToList(), ResolveType(type.ReturnType)),
|
NodeTypeAnonymousStruct type => NubTypeAnonymousStruct.Get(type.Fields.Select(x => new NubTypeAnonymousStruct.Field(x.Name.Ident, ResolveType(x.Type, currentModule))).ToList()),
|
||||||
NodeTypePointer type => NubTypePointer.Get(ResolveType(type.To)),
|
NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(x => ResolveType(x, currentModule)).ToList(), ResolveType(type.ReturnType, currentModule)),
|
||||||
|
NodeTypePointer type => NubTypePointer.Get(ResolveType(type.To, currentModule)),
|
||||||
NodeTypeSInt type => NubTypeSInt.Get(type.Width),
|
NodeTypeSInt type => NubTypeSInt.Get(type.Width),
|
||||||
NodeTypeUInt type => NubTypeUInt.Get(type.Width),
|
NodeTypeUInt type => NubTypeUInt.Get(type.Width),
|
||||||
NodeTypeString => NubTypeString.Instance,
|
NodeTypeString => NubTypeString.Instance,
|
||||||
|
NodeTypeChar => NubTypeChar.Instance,
|
||||||
NodeTypeVoid => NubTypeVoid.Instance,
|
NodeTypeVoid => NubTypeVoid.Instance,
|
||||||
|
NodeTypeArray type => NubTypeArray.Get(ResolveType(type.ElementType, currentModule)),
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
NubType ResolveCustomType(NodeTypeCustom type)
|
NubType ResolveNamedType(NodeTypeNamed type, string currentModule)
|
||||||
{
|
{
|
||||||
var module = modules.GetValueOrDefault(type.Module.Ident);
|
return type.Sections.Count switch
|
||||||
if (module == null)
|
{
|
||||||
throw new CompileException(Diagnostic.Error($"Unknown module: {type.Module.Ident}").Build());
|
3 => ResolveThreePartType(type.Sections[0], type.Sections[1], type.Sections[2], currentModule),
|
||||||
|
2 => ResolveTwoPartType(type.Sections[0], type.Sections[1], currentModule),
|
||||||
|
1 => ResolveOnePartType(type.Sections[0], currentModule),
|
||||||
|
_ => throw BasicError("Invalid type name")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (!module.TryResolveCustomType(type.Name.Ident, out var customType))
|
NubType ResolveThreePartType(TokenIdent first, TokenIdent second, TokenIdent third, string currentModule)
|
||||||
throw new CompileException(Diagnostic.Error($"Unknown custom type: {type.Module.Ident}::{type.Name.Ident}").Build());
|
{
|
||||||
|
var module = ResolveModule(first);
|
||||||
|
if (!module.TryResolveType(second.Ident, currentModule == module.Name, out var typeInfo))
|
||||||
|
throw BasicError($"Named type '{module.Name}::{second.Ident}' not found");
|
||||||
|
|
||||||
return customType;
|
if (typeInfo is not Module.TypeInfoEnum enumInfo)
|
||||||
|
throw BasicError($"'{module.Name}::{second.Ident}' is not an enum");
|
||||||
|
|
||||||
|
var variant = enumInfo.Variants.FirstOrDefault(v => v.Name == third.Ident);
|
||||||
|
if (variant == null)
|
||||||
|
throw BasicError($"Enum '{module.Name}::{second.Ident}' does not have a variant named '{third.Ident}'");
|
||||||
|
|
||||||
|
return NubTypeEnumVariant.Get(NubTypeEnum.Get(module.Name, second.Ident), third.Ident);
|
||||||
|
}
|
||||||
|
|
||||||
|
NubType ResolveTwoPartType(TokenIdent first, TokenIdent second, string currentModule)
|
||||||
|
{
|
||||||
|
if (TryResolveEnumVariant(currentModule, first.Ident, second.Ident, out var variant))
|
||||||
|
return variant;
|
||||||
|
|
||||||
|
var module = ResolveModule(first);
|
||||||
|
if (!module.TryResolveType(second.Ident, currentModule == module.Name, out var typeInfo))
|
||||||
|
throw BasicError($"Named type '{module.Name}::{second.Ident}' not found");
|
||||||
|
|
||||||
|
return typeInfo switch
|
||||||
|
{
|
||||||
|
Module.TypeInfoStruct => NubTypeStruct.Get(module.Name, second.Ident),
|
||||||
|
Module.TypeInfoEnum => NubTypeEnum.Get(module.Name, second.Ident),
|
||||||
|
_ => throw new ArgumentOutOfRangeException(nameof(typeInfo))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
NubType ResolveOnePartType(TokenIdent name, string currentModule)
|
||||||
|
{
|
||||||
|
if (!modules.TryGetValue(currentModule, out var module))
|
||||||
|
throw BasicError($"Module '{currentModule}' not found");
|
||||||
|
|
||||||
|
if (!module.TryResolveType(name.Ident, true, out var typeInfo))
|
||||||
|
throw BasicError($"Named type '{module.Name}::{name.Ident}' not found");
|
||||||
|
|
||||||
|
return typeInfo switch
|
||||||
|
{
|
||||||
|
Module.TypeInfoStruct => NubTypeStruct.Get(module.Name, name.Ident),
|
||||||
|
Module.TypeInfoEnum => NubTypeEnum.Get(module.Name, name.Ident),
|
||||||
|
_ => throw new ArgumentOutOfRangeException(nameof(typeInfo))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Module ResolveModule(TokenIdent name)
|
||||||
|
{
|
||||||
|
if (!modules.TryGetValue(name.Ident, out var module))
|
||||||
|
throw BasicError($"Module '{name.Ident}' not found");
|
||||||
|
|
||||||
|
return module;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryResolveEnumVariant(string moduleName, string enumName, string variantName, [NotNullWhen(true)] out NubType? result)
|
||||||
|
{
|
||||||
|
result = null;
|
||||||
|
|
||||||
|
if (!modules.TryGetValue(moduleName, out var module))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!module.TryResolveType(enumName, true, out var typeInfo))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (typeInfo is not Module.TypeInfoEnum enumInfo)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var variant = enumInfo.Variants.FirstOrDefault(v => v.Name == variantName);
|
||||||
|
if (variant == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
result = NubTypeEnumVariant.Get(
|
||||||
|
NubTypeEnum.Get(moduleName, enumName),
|
||||||
|
variantName);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Exception BasicError(string message)
|
||||||
|
{
|
||||||
|
return new CompileException(Diagnostic.Error(message).Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
Module GetOrCreateModule(string name)
|
||||||
|
{
|
||||||
|
if (!modules.TryGetValue(name, out var module))
|
||||||
|
{
|
||||||
|
module = new Module(name);
|
||||||
|
modules.Add(name, module);
|
||||||
|
}
|
||||||
|
|
||||||
|
return module;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class Module(string name)
|
||||||
|
{
|
||||||
|
public string Name { get; } = name;
|
||||||
|
|
||||||
|
private Dictionary<string, TypeInfo> types = new();
|
||||||
|
private Dictionary<string, IdentifierInfo> identifiers = new();
|
||||||
|
|
||||||
|
public IReadOnlyDictionary<string, TypeInfo> GetTypes() => types;
|
||||||
|
public IReadOnlyDictionary<string, IdentifierInfo> GetIdentifiers() => identifiers;
|
||||||
|
|
||||||
|
public bool TryResolveType(string name, bool searchPrivate, [NotNullWhen(true)] out TypeInfo? customType)
|
||||||
|
{
|
||||||
|
var info = types.GetValueOrDefault(name);
|
||||||
|
if (info == null)
|
||||||
|
{
|
||||||
|
customType = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (searchPrivate || info.Source == DefinitionSource.Internal)
|
||||||
|
{
|
||||||
|
customType = info;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
customType = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryResolveIdentifier(string name, bool searchPrivate, [NotNullWhen(true)] out IdentifierInfo? identifierType)
|
||||||
|
{
|
||||||
|
var info = identifiers.GetValueOrDefault(name);
|
||||||
|
if (info == null)
|
||||||
|
{
|
||||||
|
identifierType = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (searchPrivate || info.Source == DefinitionSource.Internal)
|
||||||
|
{
|
||||||
|
identifierType = info;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
identifierType = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddType(string name, TypeInfo info)
|
||||||
|
{
|
||||||
|
types.Add(name, info);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddIdentifier(string name, IdentifierInfo info)
|
||||||
|
{
|
||||||
|
identifiers.Add(name, info);
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum DefinitionSource
|
||||||
|
{
|
||||||
|
Internal,
|
||||||
|
Imported,
|
||||||
|
}
|
||||||
|
|
||||||
|
public class IdentifierInfo(DefinitionSource source, bool exported, bool @extern, NubType type, string mangledName)
|
||||||
|
{
|
||||||
|
public DefinitionSource Source { get; } = source;
|
||||||
|
public bool Exported { get; } = exported;
|
||||||
|
public bool Extern { get; } = @extern;
|
||||||
|
public NubType Type { get; } = type;
|
||||||
|
public string MangledName { get; } = mangledName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class TypeInfo(DefinitionSource source, bool exported)
|
||||||
|
{
|
||||||
|
public DefinitionSource Source { get; } = source;
|
||||||
|
public bool Exported { get; } = exported;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TypeInfoStruct(DefinitionSource source, bool exported, bool packed) : TypeInfo(source, exported)
|
||||||
|
{
|
||||||
|
private IReadOnlyList<Field>? fields;
|
||||||
|
|
||||||
|
public bool Packed { get; } = packed;
|
||||||
|
public IReadOnlyList<Field> Fields => fields ?? throw new InvalidOperationException("Fields has not been set yet");
|
||||||
|
|
||||||
|
public void SetFields(IReadOnlyList<Field> fields)
|
||||||
|
{
|
||||||
|
this.fields = fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Field(string name, NubType type)
|
||||||
|
{
|
||||||
|
public string Name { get; } = name;
|
||||||
|
public NubType Type { get; } = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TypeInfoEnum(DefinitionSource source, bool exported) : TypeInfo(source, exported)
|
||||||
|
{
|
||||||
|
private IReadOnlyList<Variant>? variants;
|
||||||
|
|
||||||
|
public IReadOnlyList<Variant> Variants => variants ?? throw new InvalidOperationException("Fields has not been set yet");
|
||||||
|
|
||||||
|
public void SetVariants(IReadOnlyList<Variant> variants)
|
||||||
|
{
|
||||||
|
this.variants = variants;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Variant(string name, NubType? type)
|
||||||
|
{
|
||||||
|
public string Name { get; } = name;
|
||||||
|
public NubType? Type { get; } = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
110
compiler/NubLib.cs
Normal file
110
compiler/NubLib.cs
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
using System.IO.Compression;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Compiler;
|
||||||
|
|
||||||
|
public class NubLib
|
||||||
|
{
|
||||||
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||||
|
{
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
WriteIndented = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
public static void Pack(string outputPath, string archivePath, Manifest manifest)
|
||||||
|
{
|
||||||
|
using var fs = new FileStream(outputPath, FileMode.Create);
|
||||||
|
using var zip = new ZipArchive(fs, ZipArchiveMode.Create);
|
||||||
|
|
||||||
|
var manifestEntry = zip.CreateEntry("manifest.json");
|
||||||
|
using (var writer = new StreamWriter(manifestEntry.Open()))
|
||||||
|
{
|
||||||
|
var serialized = JsonSerializer.Serialize(manifest, JsonOptions);
|
||||||
|
|
||||||
|
writer.Write(serialized);
|
||||||
|
}
|
||||||
|
|
||||||
|
var archiveEntry = zip.CreateEntry("lib.a");
|
||||||
|
|
||||||
|
using var entryStream = archiveEntry.Open();
|
||||||
|
using var fileStream = File.OpenRead(archivePath);
|
||||||
|
|
||||||
|
fileStream.CopyTo(entryStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NubLibLoadResult Unpack(string nublibPath)
|
||||||
|
{
|
||||||
|
using var fs = new FileStream(nublibPath, FileMode.Open, FileAccess.Read);
|
||||||
|
using var zip = new ZipArchive(fs, ZipArchiveMode.Read);
|
||||||
|
|
||||||
|
var manifestEntry = zip.GetEntry("manifest.json") ?? throw new FileNotFoundException("Manifest not found in nublib", "manifest.json");
|
||||||
|
|
||||||
|
Manifest manifest;
|
||||||
|
using (var reader = new StreamReader(manifestEntry.Open()))
|
||||||
|
{
|
||||||
|
var json = reader.ReadToEnd();
|
||||||
|
manifest = JsonSerializer.Deserialize<Manifest>(json, JsonOptions) ?? throw new InvalidDataException("Failed to deserialize manifest.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
var archiveEntry = zip.Entries.FirstOrDefault(e => e.Name.EndsWith(".a")) ?? throw new FileNotFoundException("Archive not found in nublib", "*.a");
|
||||||
|
|
||||||
|
string tempArchivePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".a");
|
||||||
|
using (var entryStream = archiveEntry.Open())
|
||||||
|
using (var tempFile = File.Create(tempArchivePath))
|
||||||
|
{
|
||||||
|
entryStream.CopyTo(tempFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new NubLibLoadResult(manifest, tempArchivePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public record NubLibLoadResult(Manifest Manifest, string ArchivePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public record Manifest(Dictionary<string, Manifest.Module> Modules)
|
||||||
|
{
|
||||||
|
public static Manifest Create(ModuleGraph moduleGraph)
|
||||||
|
{
|
||||||
|
var modules = new Dictionary<string, Module>();
|
||||||
|
|
||||||
|
foreach (var module in moduleGraph.GetModules())
|
||||||
|
{
|
||||||
|
var types = module.GetTypes().ToDictionary(x => x.Key, x => ConvertType(x.Value));
|
||||||
|
var identifiers = module.GetIdentifiers().ToDictionary(x => x.Key, x => new Module.IdentifierInfo(x.Value.Type, x.Value.Exported, x.Value.Extern, x.Value.MangledName));
|
||||||
|
modules[module.Name] = new Module(types, identifiers);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Manifest(modules);
|
||||||
|
|
||||||
|
static Module.TypeInfo ConvertType(Compiler.Module.TypeInfo typeInfo)
|
||||||
|
{
|
||||||
|
return typeInfo switch
|
||||||
|
{
|
||||||
|
Compiler.Module.TypeInfoStruct s => new Module.TypeInfoStruct(s.Exported, s.Packed, s.Fields.Select(x => new Module.TypeInfoStruct.Field(x.Name, x.Type)).ToList()),
|
||||||
|
Compiler.Module.TypeInfoEnum e => new Module.TypeInfoEnum(e.Exported, e.Variants.Select(v => new Module.TypeInfoEnum.Variant(v.Name, v.Type)).ToList()),
|
||||||
|
_ => throw new ArgumentOutOfRangeException(nameof(typeInfo))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public record Module(Dictionary<string, Module.TypeInfo> Types, Dictionary<string, Module.IdentifierInfo> Identifiers)
|
||||||
|
{
|
||||||
|
public record IdentifierInfo(NubType Type, bool Exported, bool Extern, string MangledName);
|
||||||
|
|
||||||
|
[JsonDerivedType(typeof(TypeInfoStruct), "struct")]
|
||||||
|
[JsonDerivedType(typeof(TypeInfoEnum), "enum")]
|
||||||
|
public abstract record TypeInfo(bool Exported);
|
||||||
|
|
||||||
|
public record TypeInfoStruct(bool Exported, bool Packed, IReadOnlyList<TypeInfoStruct.Field> Fields) : TypeInfo(Exported)
|
||||||
|
{
|
||||||
|
public record Field(string Name, NubType Type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public record TypeInfoEnum(bool Exported, IReadOnlyList<TypeInfoEnum.Variant> Variants) : TypeInfo(Exported)
|
||||||
|
{
|
||||||
|
public record Variant(string Name, NubType? Type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,31 @@
|
|||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace Compiler;
|
namespace Compiler;
|
||||||
|
|
||||||
|
[JsonConverter(typeof(NubTypeJsonConverter))]
|
||||||
public abstract class NubType
|
public abstract class NubType
|
||||||
{
|
{
|
||||||
public abstract override string ToString();
|
public abstract override string ToString();
|
||||||
|
|
||||||
|
[Obsolete("Use IsAssignableTo instead of ==", error: true)]
|
||||||
|
public static bool operator ==(NubType? a, NubType? b) => throw new InvalidOperationException("Use IsAssignableTo");
|
||||||
|
|
||||||
|
[Obsolete("Use IsAssignableTo instead of ==", error: true)]
|
||||||
|
public static bool operator !=(NubType? a, NubType? b) => throw new InvalidOperationException("Use IsAssignableTo");
|
||||||
|
|
||||||
|
public bool IsAssignableTo(NubType target)
|
||||||
|
{
|
||||||
|
return (this, target) switch
|
||||||
|
{
|
||||||
|
(NubTypeEnumVariant variant, NubTypeEnum targetEnum) => ReferenceEquals(variant.EnumType, targetEnum),
|
||||||
|
_ => ReferenceEquals(this, target),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypeVoid : NubType
|
public class NubTypeVoid : NubType
|
||||||
{
|
{
|
||||||
public static readonly NubTypeVoid Instance = new();
|
public static readonly NubTypeVoid Instance = new();
|
||||||
|
|
||||||
@@ -16,7 +36,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();
|
||||||
|
|
||||||
@@ -38,7 +58,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();
|
||||||
|
|
||||||
@@ -60,7 +80,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();
|
||||||
|
|
||||||
@@ -71,7 +91,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();
|
||||||
|
|
||||||
@@ -82,39 +102,122 @@ public sealed class NubTypeString : NubType
|
|||||||
public override string ToString() => "string";
|
public override string ToString() => "string";
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypeStruct : NubType
|
public class NubTypeChar : NubType
|
||||||
{
|
{
|
||||||
public string Name { get; }
|
public static readonly NubTypeChar Instance = new();
|
||||||
public string Module { get; }
|
|
||||||
|
|
||||||
private IReadOnlyList<Field>? _resolvedFields;
|
private NubTypeChar()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public IReadOnlyList<Field> Fields => _resolvedFields ?? throw new InvalidOperationException();
|
public override string ToString() => "char";
|
||||||
|
}
|
||||||
|
|
||||||
public NubTypeStruct(string module, string name)
|
public class NubTypeStruct : NubType
|
||||||
|
{
|
||||||
|
private static readonly Dictionary<(string Module, string Name), NubTypeStruct> Cache = new();
|
||||||
|
|
||||||
|
public static NubTypeStruct Get(string module, string name)
|
||||||
|
{
|
||||||
|
if (!Cache.TryGetValue((module, name), out var structType))
|
||||||
|
Cache[(module, name)] = structType = new NubTypeStruct(module, name);
|
||||||
|
|
||||||
|
return structType;
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubTypeStruct(string module, string name)
|
||||||
{
|
{
|
||||||
Module = module;
|
Module = module;
|
||||||
Name = name;
|
Name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ResolveFields(IReadOnlyList<Field> fields)
|
public string Module { get; }
|
||||||
{
|
public string Name { get; }
|
||||||
if (_resolvedFields != null)
|
|
||||||
throw new InvalidOperationException($"{Name} already resolved");
|
|
||||||
|
|
||||||
_resolvedFields = fields;
|
public override string ToString() => $"{Module}::{Name}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NubTypeAnonymousStruct : NubType
|
||||||
|
{
|
||||||
|
private static readonly Dictionary<Signature, NubTypeAnonymousStruct> Cache = new();
|
||||||
|
|
||||||
|
public static NubTypeAnonymousStruct Get(List<Field> fields)
|
||||||
|
{
|
||||||
|
var sig = new Signature(fields);
|
||||||
|
|
||||||
|
if (!Cache.TryGetValue(sig, out var func))
|
||||||
|
Cache[sig] = func = new NubTypeAnonymousStruct(fields);
|
||||||
|
|
||||||
|
return func;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() => _resolvedFields == null ? $"struct {Module}::{Name} <unresolved>" : $"struct {Module}::{Name} {{ {string.Join(' ', Fields.Select(f => $"{f.Name}: {f.Type}"))} }}";
|
private NubTypeAnonymousStruct(IReadOnlyList<Field> fields)
|
||||||
|
{
|
||||||
|
Fields = fields;
|
||||||
|
}
|
||||||
|
|
||||||
public sealed class Field(string name, NubType type)
|
public IReadOnlyList<Field> Fields { get; }
|
||||||
|
|
||||||
|
public override string ToString() => $"{{ {string.Join(", ", Fields.Select(x => $"{x.Name}: {x.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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record Signature(IReadOnlyList<Field> Fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NubTypePointer : NubType
|
public class NubTypeEnum : NubType
|
||||||
|
{
|
||||||
|
private static readonly Dictionary<(string Module, string Name), NubTypeEnum> Cache = new();
|
||||||
|
|
||||||
|
public static NubTypeEnum Get(string module, string name)
|
||||||
|
{
|
||||||
|
if (!Cache.TryGetValue((module, name), out var enumType))
|
||||||
|
Cache[(module, name)] = enumType = new NubTypeEnum(module, name);
|
||||||
|
|
||||||
|
return enumType;
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubTypeEnum(string module, string name)
|
||||||
|
{
|
||||||
|
Module = module;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Module { get; }
|
||||||
|
public string Name { get; }
|
||||||
|
|
||||||
|
public override string ToString() => $"{Module}::{Name}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NubTypeEnumVariant : NubType
|
||||||
|
{
|
||||||
|
private static readonly Dictionary<(NubTypeEnum EnumType, string Variant), NubTypeEnumVariant> Cache = new();
|
||||||
|
|
||||||
|
public static NubTypeEnumVariant Get(NubTypeEnum enumType, string variant)
|
||||||
|
{
|
||||||
|
if (!Cache.TryGetValue((enumType, variant), out var variantType))
|
||||||
|
Cache[(enumType, variant)] = variantType = new NubTypeEnumVariant(enumType, variant);
|
||||||
|
|
||||||
|
return variantType;
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubTypeEnumVariant(NubTypeEnum enumType, string variant)
|
||||||
|
{
|
||||||
|
EnumType = enumType;
|
||||||
|
Variant = variant;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NubTypeEnum EnumType { get; }
|
||||||
|
public string Variant { get; }
|
||||||
|
|
||||||
|
public override string ToString() => $"{EnumType}.{Variant}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NubTypePointer : NubType
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<NubType, NubTypePointer> Cache = new();
|
private static readonly Dictionary<NubType, NubTypePointer> Cache = new();
|
||||||
|
|
||||||
@@ -136,7 +239,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();
|
||||||
|
|
||||||
@@ -161,5 +264,445 @@ public sealed class NubTypeFunc : NubType
|
|||||||
|
|
||||||
public override string ToString() => $"func({string.Join(' ', Parameters)}): {ReturnType}";
|
public override string ToString() => $"func({string.Join(' ', Parameters)}): {ReturnType}";
|
||||||
|
|
||||||
private readonly record struct Signature(IReadOnlyList<NubType> Parameters, NubType ReturnType);
|
private record Signature(IReadOnlyList<NubType> Parameters, NubType ReturnType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class NubTypeArray : NubType
|
||||||
|
{
|
||||||
|
private static readonly Dictionary<NubType, NubTypeArray> Cache = new();
|
||||||
|
|
||||||
|
public static NubTypeArray Get(NubType to)
|
||||||
|
{
|
||||||
|
if (!Cache.TryGetValue(to, out var ptr))
|
||||||
|
Cache[to] = ptr = new NubTypeArray(to);
|
||||||
|
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NubType ElementType { get; }
|
||||||
|
|
||||||
|
private NubTypeArray(NubType elementType)
|
||||||
|
{
|
||||||
|
ElementType = elementType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => $"[]{ElementType}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TypeEncoder
|
||||||
|
{
|
||||||
|
public static string Encode(NubType type)
|
||||||
|
{
|
||||||
|
return new TypeEncoder().EncodeRoot(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TypeEncoder()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private string EncodeRoot(NubType type)
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
EncodeType(sb, type);
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EncodeType(StringBuilder sb, NubType type)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case NubTypeVoid:
|
||||||
|
sb.Append('V');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeBool:
|
||||||
|
sb.Append('B');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeUInt u:
|
||||||
|
sb.Append($"U(");
|
||||||
|
sb.Append(u.Width);
|
||||||
|
sb.Append(')');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeSInt s:
|
||||||
|
sb.Append($"I(");
|
||||||
|
sb.Append(s.Width);
|
||||||
|
sb.Append(')');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeString:
|
||||||
|
sb.Append('S');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeChar:
|
||||||
|
sb.Append('C');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypePointer p:
|
||||||
|
sb.Append("P(");
|
||||||
|
EncodeType(sb, p.To);
|
||||||
|
sb.Append(')');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeStruct st:
|
||||||
|
sb.Append("TN(");
|
||||||
|
sb.Append(st.Module);
|
||||||
|
sb.Append(':');
|
||||||
|
sb.Append(st.Name);
|
||||||
|
sb.Append(')');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeEnum e:
|
||||||
|
sb.Append("EN(");
|
||||||
|
sb.Append(e.Module);
|
||||||
|
sb.Append(':');
|
||||||
|
sb.Append(e.Name);
|
||||||
|
sb.Append(')');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeEnumVariant ev:
|
||||||
|
sb.Append("EV(");
|
||||||
|
sb.Append(ev.EnumType.Module);
|
||||||
|
sb.Append(':');
|
||||||
|
sb.Append(ev.EnumType.Name);
|
||||||
|
sb.Append(':');
|
||||||
|
sb.Append(ev.Variant);
|
||||||
|
sb.Append(')');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeFunc fn:
|
||||||
|
sb.Append("F(");
|
||||||
|
for (int i = 0; i < fn.Parameters.Count; i++)
|
||||||
|
{
|
||||||
|
EncodeType(sb, fn.Parameters[i]);
|
||||||
|
}
|
||||||
|
EncodeType(sb, fn.ReturnType);
|
||||||
|
sb.Append(')');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeAnonymousStruct s:
|
||||||
|
sb.Append("TA(");
|
||||||
|
foreach (var field in s.Fields)
|
||||||
|
{
|
||||||
|
sb.Append(field.Name);
|
||||||
|
sb.Append(':');
|
||||||
|
EncodeType(sb, field.Type);
|
||||||
|
}
|
||||||
|
sb.Append(')');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NubTypeArray a:
|
||||||
|
sb.Append("A(");
|
||||||
|
EncodeType(sb, a.ElementType);
|
||||||
|
sb.Append(')');
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException(type.GetType().Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Definition(int index)
|
||||||
|
{
|
||||||
|
public int Index { get; } = index;
|
||||||
|
public string? Encoded { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TypeDecoder
|
||||||
|
{
|
||||||
|
public static NubType Decode(string encoded)
|
||||||
|
{
|
||||||
|
return new TypeDecoder(encoded).DecodeType();
|
||||||
|
}
|
||||||
|
|
||||||
|
private TypeDecoder(string encoded)
|
||||||
|
{
|
||||||
|
this.encoded = encoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly string encoded;
|
||||||
|
private int index = 0;
|
||||||
|
|
||||||
|
private NubType DecodeType()
|
||||||
|
{
|
||||||
|
var start = Consume();
|
||||||
|
return start switch
|
||||||
|
{
|
||||||
|
'V' => NubTypeVoid.Instance,
|
||||||
|
'B' => NubTypeBool.Instance,
|
||||||
|
'U' => DecodeUInt(),
|
||||||
|
'I' => DecodeSInt(),
|
||||||
|
'S' => NubTypeString.Instance,
|
||||||
|
'C' => NubTypeChar.Instance,
|
||||||
|
'P' => DecodePointer(),
|
||||||
|
'F' => DecodeFunc(),
|
||||||
|
'T' => DecodeStruct(),
|
||||||
|
'E' => DecodeEnum(),
|
||||||
|
'A' => DecodeArray(),
|
||||||
|
_ => throw new Exception($"'{start}' is not a valid start to a type")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubTypeUInt DecodeUInt()
|
||||||
|
{
|
||||||
|
Expect('(');
|
||||||
|
var width = ExpectInt();
|
||||||
|
Expect(')');
|
||||||
|
return NubTypeUInt.Get(width);
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubTypeSInt DecodeSInt()
|
||||||
|
{
|
||||||
|
Expect('(');
|
||||||
|
var width = ExpectInt();
|
||||||
|
Expect(')');
|
||||||
|
return NubTypeSInt.Get(width);
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubTypePointer DecodePointer()
|
||||||
|
{
|
||||||
|
Expect('(');
|
||||||
|
var to = DecodeType();
|
||||||
|
Expect(')');
|
||||||
|
return NubTypePointer.Get(to);
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubTypeFunc DecodeFunc()
|
||||||
|
{
|
||||||
|
var types = new List<NubType>();
|
||||||
|
|
||||||
|
Expect('(');
|
||||||
|
while (!TryExpect(')'))
|
||||||
|
{
|
||||||
|
types.Add(DecodeType());
|
||||||
|
}
|
||||||
|
|
||||||
|
return NubTypeFunc.Get(types.Take(types.Count - 1).ToList(), types.Last());
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubType DecodeStruct()
|
||||||
|
{
|
||||||
|
|
||||||
|
if (TryExpect('A'))
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
var fields = new List<NubTypeAnonymousStruct.Field>();
|
||||||
|
|
||||||
|
Expect('(');
|
||||||
|
while (!TryExpect(')'))
|
||||||
|
{
|
||||||
|
while (!TryExpect(':'))
|
||||||
|
{
|
||||||
|
sb.Append(Consume());
|
||||||
|
}
|
||||||
|
|
||||||
|
var name = sb.ToString();
|
||||||
|
sb.Clear();
|
||||||
|
|
||||||
|
var type = DecodeType();
|
||||||
|
|
||||||
|
fields.Add(new NubTypeAnonymousStruct.Field(name, type));
|
||||||
|
}
|
||||||
|
|
||||||
|
return NubTypeAnonymousStruct.Get(fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TryExpect('N'))
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
Expect('(');
|
||||||
|
while (!TryExpect(':'))
|
||||||
|
sb.Append(Consume());
|
||||||
|
|
||||||
|
var module = sb.ToString();
|
||||||
|
sb.Clear();
|
||||||
|
|
||||||
|
while (!TryExpect(')'))
|
||||||
|
sb.Append(Consume());
|
||||||
|
|
||||||
|
var name = sb.ToString();
|
||||||
|
|
||||||
|
return NubTypeStruct.Get(module, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Exception("Expected 'A' or 'N'");
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubType DecodeEnum()
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
if (TryExpect('V'))
|
||||||
|
{
|
||||||
|
Expect('(');
|
||||||
|
while (!TryExpect(':'))
|
||||||
|
sb.Append(Consume());
|
||||||
|
|
||||||
|
var module = sb.ToString();
|
||||||
|
sb.Clear();
|
||||||
|
|
||||||
|
while (!TryExpect(':'))
|
||||||
|
sb.Append(Consume());
|
||||||
|
|
||||||
|
var name = sb.ToString();
|
||||||
|
|
||||||
|
while (!TryExpect(')'))
|
||||||
|
sb.Append(Consume());
|
||||||
|
|
||||||
|
var variant = sb.ToString();
|
||||||
|
|
||||||
|
return NubTypeEnumVariant.Get(NubTypeEnum.Get(module, name), variant);
|
||||||
|
}
|
||||||
|
else if (TryExpect('N'))
|
||||||
|
{
|
||||||
|
Expect('(');
|
||||||
|
while (!TryExpect(':'))
|
||||||
|
sb.Append(Consume());
|
||||||
|
|
||||||
|
var module = sb.ToString();
|
||||||
|
sb.Clear();
|
||||||
|
|
||||||
|
while (!TryExpect(')'))
|
||||||
|
sb.Append(Consume());
|
||||||
|
|
||||||
|
var name = sb.ToString();
|
||||||
|
|
||||||
|
return NubTypeEnum.Get(module, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Exception($"Expected 'V' or 'N'");
|
||||||
|
}
|
||||||
|
|
||||||
|
private NubTypeArray DecodeArray()
|
||||||
|
{
|
||||||
|
Expect('(');
|
||||||
|
var elementType = DecodeType();
|
||||||
|
Expect(')');
|
||||||
|
return NubTypeArray.Get(elementType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool TryPeek(out char c)
|
||||||
|
{
|
||||||
|
if (index >= encoded.Length)
|
||||||
|
{
|
||||||
|
c = '\0';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
c = encoded[index];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool TryConsume(out char c)
|
||||||
|
{
|
||||||
|
if (index >= encoded.Length)
|
||||||
|
{
|
||||||
|
c = '\0';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
c = encoded[index];
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private char Consume()
|
||||||
|
{
|
||||||
|
if (!TryConsume(out var c))
|
||||||
|
throw new Exception("Unexpected end of string");
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool TryExpect(char c)
|
||||||
|
{
|
||||||
|
if (index >= encoded.Length)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (encoded[index] != c)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Consume();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Expect(char c)
|
||||||
|
{
|
||||||
|
if (!TryExpect(c))
|
||||||
|
throw new Exception($"Expected '{c}'");
|
||||||
|
}
|
||||||
|
|
||||||
|
private int ExpectInt()
|
||||||
|
{
|
||||||
|
var buf = string.Empty;
|
||||||
|
|
||||||
|
while (TryPeek(out var c))
|
||||||
|
{
|
||||||
|
if (!char.IsDigit(c))
|
||||||
|
break;
|
||||||
|
|
||||||
|
buf += Consume();
|
||||||
|
}
|
||||||
|
|
||||||
|
return int.Parse(buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NubTypeJsonConverter : JsonConverter<NubType>
|
||||||
|
{
|
||||||
|
public override NubType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return TypeDecoder.Decode(reader.GetString()!);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, NubType value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
writer.WriteStringValue(TypeEncoder.Encode(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Hashing
|
||||||
|
{
|
||||||
|
public static ulong Fnv1a64(string text)
|
||||||
|
{
|
||||||
|
const ulong offset = 14695981039346656037UL;
|
||||||
|
const ulong prime = 1099511628211UL;
|
||||||
|
|
||||||
|
ulong hash = offset;
|
||||||
|
foreach (var c in Encoding.UTF8.GetBytes(text))
|
||||||
|
{
|
||||||
|
hash ^= c;
|
||||||
|
hash *= prime;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class NameMangler
|
||||||
|
{
|
||||||
|
public static string Mangle(string module, string name, NubType type)
|
||||||
|
{
|
||||||
|
var canonical = TypeEncoder.Encode(type);
|
||||||
|
var hash = Hashing.Fnv1a64(canonical);
|
||||||
|
|
||||||
|
return $"nub_{Sanitize(module)}_{Sanitize(name)}_{hash:x16}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string Sanitize(string s)
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder(s.Length);
|
||||||
|
foreach (var c in s)
|
||||||
|
{
|
||||||
|
if (char.IsLetterOrDigit(c))
|
||||||
|
sb.Append(c);
|
||||||
|
else
|
||||||
|
sb.Append('_');
|
||||||
|
}
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,22 +2,35 @@
|
|||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
var definitions = new List<NodeDefinition>();
|
var definitions = new List<NodeDefinition>();
|
||||||
diagnostics = [];
|
diagnostics = [];
|
||||||
|
|
||||||
|
TokenIdent? moduleName = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
ExpectKeyword(Keyword.Module);
|
||||||
|
moduleName = ExpectIdent();
|
||||||
|
|
||||||
while (Peek() != null)
|
while (Peek() != null)
|
||||||
{
|
{
|
||||||
definitions.Add(ParseDefinition());
|
definitions.Add(ParseDefinition());
|
||||||
@@ -28,38 +41,77 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
diagnostics.Add(e.Diagnostic);
|
diagnostics.Add(e.Diagnostic);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Ast(definitions, fileName);
|
if (moduleName == null || diagnostics.Any(x => x.Severity == Diagnostic.DiagnosticSeverity.Error))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return new Ast(fileName, moduleName, definitions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private NodeDefinition ParseDefinition()
|
private NodeDefinition ParseDefinition()
|
||||||
{
|
{
|
||||||
var startIndex = index;
|
var startIndex = index;
|
||||||
|
|
||||||
|
Dictionary<Keyword, TokenKeyword> modifiers = [];
|
||||||
|
|
||||||
|
while (Peek() is TokenKeyword keyword)
|
||||||
|
{
|
||||||
|
switch (keyword.Keyword)
|
||||||
|
{
|
||||||
|
case Keyword.Export:
|
||||||
|
Next();
|
||||||
|
modifiers[Keyword.Export] = keyword;
|
||||||
|
break;
|
||||||
|
case Keyword.Packed:
|
||||||
|
Next();
|
||||||
|
modifiers[Keyword.Packed] = keyword;
|
||||||
|
break;
|
||||||
|
case Keyword.Extern:
|
||||||
|
Next();
|
||||||
|
modifiers[Keyword.Extern] = keyword;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
goto modifier_done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
modifier_done:
|
||||||
|
|
||||||
if (TryExpectKeyword(Keyword.Func))
|
if (TryExpectKeyword(Keyword.Func))
|
||||||
{
|
{
|
||||||
|
var exported = modifiers.Remove(Keyword.Export);
|
||||||
|
var @extern = modifiers.Remove(Keyword.Extern);
|
||||||
|
|
||||||
|
foreach (var modifier in modifiers)
|
||||||
|
// todo(nub31): Add to diagnostics instead of throwing
|
||||||
|
throw BasicError("Invalid modifier for function", modifier.Value);
|
||||||
|
|
||||||
var name = ExpectIdent();
|
var name = ExpectIdent();
|
||||||
var parameters = new List<NodeDefinitionFunc.Param>();
|
var parameters = ParseFuncParameters();
|
||||||
|
|
||||||
ExpectSymbol(Symbol.OpenParen);
|
NodeType? returnType = null;
|
||||||
while (!TryExpectSymbol(Symbol.CloseParen))
|
if (TryExpectSymbol(Symbol.Colon))
|
||||||
|
returnType = ParseType();
|
||||||
|
|
||||||
|
if (@extern)
|
||||||
{
|
{
|
||||||
var paramStartIndex = index;
|
return new NodeDefinitionExternFunc(TokensFrom(startIndex), exported, name, parameters, returnType);
|
||||||
var parameterName = ExpectIdent();
|
}
|
||||||
ExpectSymbol(Symbol.Colon);
|
else
|
||||||
var parameterType = ParseType();
|
{
|
||||||
parameters.Add(new NodeDefinitionFunc.Param(TokensFrom(paramStartIndex), parameterName, parameterType));
|
var body = ParseStatement();
|
||||||
|
return new NodeDefinitionFunc(TokensFrom(startIndex), exported, name, parameters, body, returnType);
|
||||||
}
|
}
|
||||||
|
|
||||||
ExpectSymbol(Symbol.Colon);
|
|
||||||
var returnType = ParseType();
|
|
||||||
|
|
||||||
var body = ParseStatement();
|
|
||||||
|
|
||||||
return new NodeDefinitionFunc(TokensFrom(startIndex), name, parameters, body, returnType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TryExpectKeyword(Keyword.Struct))
|
if (TryExpectKeyword(Keyword.Struct))
|
||||||
{
|
{
|
||||||
|
var exported = modifiers.Remove(Keyword.Export);
|
||||||
|
var packed = modifiers.Remove(Keyword.Packed);
|
||||||
|
|
||||||
|
foreach (var modifier in modifiers)
|
||||||
|
// todo(nub31): Add to diagnostics instead of throwing
|
||||||
|
throw BasicError("Invalid modifier for struct", modifier.Value);
|
||||||
|
|
||||||
var name = ExpectIdent();
|
var name = ExpectIdent();
|
||||||
var fields = new List<NodeDefinitionStruct.Field>();
|
var fields = new List<NodeDefinitionStruct.Field>();
|
||||||
|
|
||||||
@@ -70,19 +122,87 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
var fieldName = ExpectIdent();
|
var fieldName = ExpectIdent();
|
||||||
ExpectSymbol(Symbol.Colon);
|
ExpectSymbol(Symbol.Colon);
|
||||||
var fieldType = ParseType();
|
var fieldType = ParseType();
|
||||||
|
TryExpectSymbol(Symbol.Comma);
|
||||||
fields.Add(new NodeDefinitionStruct.Field(TokensFrom(fieldStartIndex), fieldName, fieldType));
|
fields.Add(new NodeDefinitionStruct.Field(TokensFrom(fieldStartIndex), fieldName, fieldType));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new NodeDefinitionStruct(TokensFrom(startIndex), name, fields);
|
return new NodeDefinitionStruct(TokensFrom(startIndex), exported, packed, name, fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TryExpectKeyword(Keyword.Module))
|
if (TryExpectKeyword(Keyword.Enum))
|
||||||
{
|
{
|
||||||
|
var exported = modifiers.Remove(Keyword.Export);
|
||||||
|
|
||||||
|
foreach (var modifier in modifiers)
|
||||||
|
// todo(nub31): Add to diagnostics instead of throwing
|
||||||
|
throw BasicError("Invalid modifier for struct", modifier.Value);
|
||||||
|
|
||||||
var name = ExpectIdent();
|
var name = ExpectIdent();
|
||||||
return new NodeDefinitionModule(TokensFrom(startIndex), name);
|
var variants = new List<NodeDefinitionEnum.Variant>();
|
||||||
|
|
||||||
|
ExpectSymbol(Symbol.OpenCurly);
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseCurly))
|
||||||
|
{
|
||||||
|
var variantsStartIndex = index;
|
||||||
|
var variantName = ExpectIdent();
|
||||||
|
|
||||||
|
NodeType? variantType = null;
|
||||||
|
if (TryExpectSymbol(Symbol.Colon))
|
||||||
|
variantType = ParseType();
|
||||||
|
|
||||||
|
TryExpectSymbol(Symbol.Comma);
|
||||||
|
|
||||||
|
variants.Add(new NodeDefinitionEnum.Variant(TokensFrom(variantsStartIndex), variantName, variantType));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new NodeDefinitionEnum(TokensFrom(startIndex), exported, name, variants);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new CompileException(Diagnostic.Error("Not a valid definition").At(fileName, Peek()).Build());
|
if (TryExpectKeyword(Keyword.Let))
|
||||||
|
{
|
||||||
|
var exported = modifiers.Remove(Keyword.Export);
|
||||||
|
|
||||||
|
foreach (var modifier in modifiers)
|
||||||
|
// todo(nub31): Add to diagnostics instead of throwing
|
||||||
|
throw BasicError("Invalid modifier for global variable", modifier.Value);
|
||||||
|
|
||||||
|
var name = ExpectIdent();
|
||||||
|
ExpectSymbol(Symbol.Colon);
|
||||||
|
var type = ParseType();
|
||||||
|
|
||||||
|
return new NodeDefinitionGlobalVariable(TokensFrom(startIndex), exported, name, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw BasicError("Not a valid definition", Peek());
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<NodeDefinitionFunc.Param> ParseFuncParameters()
|
||||||
|
{
|
||||||
|
var parameters = new List<NodeDefinitionFunc.Param>();
|
||||||
|
|
||||||
|
ExpectSymbol(Symbol.OpenParen);
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (TryExpectSymbol(Symbol.CloseParen))
|
||||||
|
break;
|
||||||
|
|
||||||
|
var startIndex = index;
|
||||||
|
|
||||||
|
var name = ExpectIdent();
|
||||||
|
ExpectSymbol(Symbol.Colon);
|
||||||
|
var type = ParseType();
|
||||||
|
|
||||||
|
parameters.Add(new NodeDefinitionFunc.Param(TokensFrom(startIndex), name, type));
|
||||||
|
|
||||||
|
if (!TryExpectSymbol(Symbol.Comma))
|
||||||
|
{
|
||||||
|
ExpectSymbol(Symbol.CloseParen);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
private NodeStatement ParseStatement()
|
private NodeStatement ParseStatement()
|
||||||
@@ -100,17 +220,31 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
|
|
||||||
if (TryExpectKeyword(Keyword.Return))
|
if (TryExpectKeyword(Keyword.Return))
|
||||||
{
|
{
|
||||||
var value = ParseExpression();
|
NodeExpression? value = null;
|
||||||
|
|
||||||
|
if (Peek() is TokenIdent token && token.Ident == "void")
|
||||||
|
{
|
||||||
|
Next();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = ParseExpression();
|
||||||
|
}
|
||||||
|
|
||||||
return new NodeStatementReturn(TokensFrom(startIndex), value);
|
return new NodeStatementReturn(TokensFrom(startIndex), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TryExpectKeyword(Keyword.Let))
|
if (TryExpectKeyword(Keyword.Let))
|
||||||
{
|
{
|
||||||
var name = ExpectIdent();
|
var name = ExpectIdent();
|
||||||
ExpectSymbol(Symbol.Colon);
|
|
||||||
var type = ParseType();
|
NodeType? type = null;
|
||||||
|
if (TryExpectSymbol(Symbol.Colon))
|
||||||
|
type = ParseType();
|
||||||
|
|
||||||
ExpectSymbol(Symbol.Equal);
|
ExpectSymbol(Symbol.Equal);
|
||||||
var value = ParseExpression();
|
var value = ParseExpression();
|
||||||
|
|
||||||
return new NodeStatementVariableDeclaration(TokensFrom(startIndex), name, type, value);
|
return new NodeStatementVariableDeclaration(TokensFrom(startIndex), name, type, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,19 +263,48 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
if (TryExpectKeyword(Keyword.While))
|
if (TryExpectKeyword(Keyword.While))
|
||||||
{
|
{
|
||||||
var condition = ParseExpression();
|
var condition = ParseExpression();
|
||||||
var thenBlock = ParseStatement();
|
var block = ParseStatement();
|
||||||
return new NodeStatementWhile(TokensFrom(startIndex), condition, thenBlock);
|
return new NodeStatementWhile(TokensFrom(startIndex), condition, block);
|
||||||
}
|
}
|
||||||
|
|
||||||
var target = ParseExpression();
|
if (TryExpectKeyword(Keyword.For))
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.Equal))
|
|
||||||
{
|
{
|
||||||
var value = ParseExpression();
|
var variableName = ExpectIdent();
|
||||||
return new NodeStatementAssignment(TokensFrom(startIndex), target, value);
|
ExpectKeyword(Keyword.In);
|
||||||
|
var array = ParseExpression();
|
||||||
|
var block = ParseStatement();
|
||||||
|
return new NodeStatementFor(TokensFrom(startIndex), variableName, array, block);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new NodeStatementExpression(TokensFrom(startIndex), target);
|
if (TryExpectKeyword(Keyword.Match))
|
||||||
|
{
|
||||||
|
var target = ParseExpression();
|
||||||
|
var cases = new List<NodeStatementMatch.Case>();
|
||||||
|
|
||||||
|
ExpectSymbol(Symbol.OpenCurly);
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseCurly))
|
||||||
|
{
|
||||||
|
var caseStartIndex = index;
|
||||||
|
var variant = ExpectIdent();
|
||||||
|
TryExpectIdent(out var variableName);
|
||||||
|
var body = ParseStatement();
|
||||||
|
cases.Add(new NodeStatementMatch.Case(TokensFrom(caseStartIndex), variant, variableName, body));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new NodeStatementMatch(TokensFrom(startIndex), target, cases);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var target = ParseExpression();
|
||||||
|
|
||||||
|
if (TryExpectSymbol(Symbol.Equal))
|
||||||
|
{
|
||||||
|
var value = ParseExpression();
|
||||||
|
return new NodeStatementAssignment(TokensFrom(startIndex), target, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new NodeStatementExpression(TokensFrom(startIndex), target);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private NodeExpression ParseExpression(int minPrecedence = -1)
|
private NodeExpression ParseExpression(int minPrecedence = -1)
|
||||||
@@ -209,6 +372,33 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
var target = ParseExpression();
|
var target = ParseExpression();
|
||||||
expr = new NodeExpressionUnary(TokensFrom(startIndex), target, NodeExpressionUnary.Op.Negate);
|
expr = new NodeExpressionUnary(TokensFrom(startIndex), target, NodeExpressionUnary.Op.Negate);
|
||||||
}
|
}
|
||||||
|
else if (TryExpectSymbol(Symbol.OpenSquare))
|
||||||
|
{
|
||||||
|
var values = new List<NodeExpression>();
|
||||||
|
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseSquare))
|
||||||
|
{
|
||||||
|
var value = ParseExpression();
|
||||||
|
values.Add(value);
|
||||||
|
TryExpectSymbol(Symbol.Comma);
|
||||||
|
}
|
||||||
|
|
||||||
|
expr = new NodeExpressionArrayLiteral(TokensFrom(startIndex), values);
|
||||||
|
}
|
||||||
|
else if (TryExpectSymbol(Symbol.OpenCurly))
|
||||||
|
{
|
||||||
|
var initializers = new List<NodeExpressionStructLiteral.Initializer>();
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseCurly))
|
||||||
|
{
|
||||||
|
var initializerStartIndex = startIndex;
|
||||||
|
var fieldName = ExpectIdent();
|
||||||
|
ExpectSymbol(Symbol.Equal);
|
||||||
|
var fieldValue = ParseExpression();
|
||||||
|
initializers.Add(new NodeExpressionStructLiteral.Initializer(TokensFrom(initializerStartIndex), fieldName, fieldValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
expr = new NodeExpressionStructLiteral(TokensFrom(startIndex), null, initializers);
|
||||||
|
}
|
||||||
else if (TryExpectSymbol(Symbol.Bang))
|
else if (TryExpectSymbol(Symbol.Bang))
|
||||||
{
|
{
|
||||||
var target = ParseExpression();
|
var target = ParseExpression();
|
||||||
@@ -228,39 +418,63 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
}
|
}
|
||||||
else if (TryExpectIdent(out var ident))
|
else if (TryExpectIdent(out var ident))
|
||||||
{
|
{
|
||||||
if (TryExpectSymbol(Symbol.ColonColon))
|
List<TokenIdent> sections = [ident];
|
||||||
|
|
||||||
|
while (TryExpectSymbol(Symbol.ColonColon))
|
||||||
{
|
{
|
||||||
var name = ExpectIdent();
|
sections.Add(ExpectIdent());
|
||||||
expr = new NodeExpressionModuleIdent(TokensFrom(startIndex), ident, name);
|
}
|
||||||
|
|
||||||
|
expr = new NodeExpressionIdent(TokensFrom(startIndex), sections);
|
||||||
|
}
|
||||||
|
else if (TryExpectKeyword(Keyword.New))
|
||||||
|
{
|
||||||
|
var type = ParseType();
|
||||||
|
|
||||||
|
if (type is NodeTypeString)
|
||||||
|
{
|
||||||
|
ExpectSymbol(Symbol.OpenParen);
|
||||||
|
var value = ParseExpression();
|
||||||
|
ExpectSymbol(Symbol.CloseParen);
|
||||||
|
|
||||||
|
expr = new NodeExpressionStringConstructor(TokensFrom(startIndex), value);
|
||||||
|
}
|
||||||
|
else if (type is NodeTypeNamed namedType)
|
||||||
|
{
|
||||||
|
if (TryExpectSymbol(Symbol.OpenParen))
|
||||||
|
{
|
||||||
|
var value = ParseExpression();
|
||||||
|
ExpectSymbol(Symbol.CloseParen);
|
||||||
|
|
||||||
|
expr = new NodeExpressionEnumLiteral(TokensFrom(startIndex), namedType, value);
|
||||||
|
}
|
||||||
|
else if (TryExpectSymbol(Symbol.OpenCurly))
|
||||||
|
{
|
||||||
|
var initializers = new List<NodeExpressionStructLiteral.Initializer>();
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseCurly))
|
||||||
|
{
|
||||||
|
var initializerStartIndex = startIndex;
|
||||||
|
var fieldName = ExpectIdent();
|
||||||
|
ExpectSymbol(Symbol.Equal);
|
||||||
|
var fieldValue = ParseExpression();
|
||||||
|
initializers.Add(new NodeExpressionStructLiteral.Initializer(TokensFrom(initializerStartIndex), fieldName, fieldValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
expr = new NodeExpressionStructLiteral(TokensFrom(startIndex), null, initializers);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
expr = new NodeExpressionEnumLiteral(TokensFrom(startIndex), namedType, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
expr = new NodeExpressionLocalIdent(TokensFrom(startIndex), ident);
|
throw BasicError($"Expected named type or string", type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (TryExpectKeyword(Keyword.Struct))
|
|
||||||
{
|
|
||||||
var module = ExpectIdent();
|
|
||||||
ExpectSymbol(Symbol.ColonColon);
|
|
||||||
var name = ExpectIdent();
|
|
||||||
|
|
||||||
var initializers = new List<NodeExpressionStructLiteral.Initializer>();
|
|
||||||
|
|
||||||
ExpectSymbol(Symbol.OpenCurly);
|
|
||||||
while (!TryExpectSymbol(Symbol.CloseCurly))
|
|
||||||
{
|
|
||||||
var initializerStartIndex = startIndex;
|
|
||||||
var fieldName = ExpectIdent();
|
|
||||||
ExpectSymbol(Symbol.Equal);
|
|
||||||
var fieldValue = ParseExpression();
|
|
||||||
initializers.Add(new NodeExpressionStructLiteral.Initializer(TokensFrom(initializerStartIndex), fieldName, fieldValue));
|
|
||||||
}
|
|
||||||
|
|
||||||
expr = new NodeExpressionStructLiteral(TokensFrom(startIndex), module, name, initializers);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new CompileException(Diagnostic.Error("Expected start of expression").At(fileName, Peek()).Build());
|
throw BasicError("Expected start of expression", Peek());
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
@@ -275,7 +489,10 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
var parameters = new List<NodeExpression>();
|
var parameters = new List<NodeExpression>();
|
||||||
|
|
||||||
while (!TryExpectSymbol(Symbol.CloseParen))
|
while (!TryExpectSymbol(Symbol.CloseParen))
|
||||||
|
{
|
||||||
parameters.Add(ParseExpression());
|
parameters.Add(ParseExpression());
|
||||||
|
TryExpectSymbol(Symbol.Comma);
|
||||||
|
}
|
||||||
|
|
||||||
expr = new NodeExpressionFuncCall(TokensFrom(startIndex), expr, parameters);
|
expr = new NodeExpressionFuncCall(TokensFrom(startIndex), expr, parameters);
|
||||||
}
|
}
|
||||||
@@ -314,6 +531,28 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
return new NodeTypeFunc(TokensFrom(startIndex), parameters, returnType);
|
return new NodeTypeFunc(TokensFrom(startIndex), parameters, returnType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (TryExpectSymbol(Symbol.OpenCurly))
|
||||||
|
{
|
||||||
|
var fields = new List<NodeTypeAnonymousStruct.Field>();
|
||||||
|
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseCurly))
|
||||||
|
{
|
||||||
|
var name = ExpectIdent();
|
||||||
|
ExpectSymbol(Symbol.Colon);
|
||||||
|
var type = ParseType();
|
||||||
|
fields.Add(new NodeTypeAnonymousStruct.Field(name, type));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new NodeTypeAnonymousStruct(TokensFrom(startIndex), fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TryExpectSymbol(Symbol.OpenSquare))
|
||||||
|
{
|
||||||
|
ExpectSymbol(Symbol.CloseSquare);
|
||||||
|
var elementType = ParseType();
|
||||||
|
return new NodeTypeArray(TokensFrom(startIndex), elementType);
|
||||||
|
}
|
||||||
|
|
||||||
if (TryExpectIdent(out var ident))
|
if (TryExpectIdent(out var ident))
|
||||||
{
|
{
|
||||||
switch (ident.Ident)
|
switch (ident.Ident)
|
||||||
@@ -322,8 +561,12 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
return new NodeTypeVoid(TokensFrom(startIndex));
|
return new NodeTypeVoid(TokensFrom(startIndex));
|
||||||
case "string":
|
case "string":
|
||||||
return new NodeTypeString(TokensFrom(startIndex));
|
return new NodeTypeString(TokensFrom(startIndex));
|
||||||
|
case "char":
|
||||||
|
return new NodeTypeChar(TokensFrom(startIndex));
|
||||||
case "bool":
|
case "bool":
|
||||||
return new NodeTypeBool(TokensFrom(startIndex));
|
return new NodeTypeBool(TokensFrom(startIndex));
|
||||||
|
case "int":
|
||||||
|
return new NodeTypeSInt(TokensFrom(startIndex), 64);
|
||||||
case "i8":
|
case "i8":
|
||||||
return new NodeTypeSInt(TokensFrom(startIndex), 8);
|
return new NodeTypeSInt(TokensFrom(startIndex), 8);
|
||||||
case "i16":
|
case "i16":
|
||||||
@@ -332,6 +575,8 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
return new NodeTypeSInt(TokensFrom(startIndex), 32);
|
return new NodeTypeSInt(TokensFrom(startIndex), 32);
|
||||||
case "i64":
|
case "i64":
|
||||||
return new NodeTypeSInt(TokensFrom(startIndex), 64);
|
return new NodeTypeSInt(TokensFrom(startIndex), 64);
|
||||||
|
case "uint":
|
||||||
|
return new NodeTypeUInt(TokensFrom(startIndex), 64);
|
||||||
case "u8":
|
case "u8":
|
||||||
return new NodeTypeUInt(TokensFrom(startIndex), 8);
|
return new NodeTypeUInt(TokensFrom(startIndex), 8);
|
||||||
case "u16":
|
case "u16":
|
||||||
@@ -341,13 +586,18 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
case "u64":
|
case "u64":
|
||||||
return new NodeTypeUInt(TokensFrom(startIndex), 64);
|
return new NodeTypeUInt(TokensFrom(startIndex), 64);
|
||||||
default:
|
default:
|
||||||
ExpectSymbol(Symbol.ColonColon);
|
List<TokenIdent> secitons = [ident];
|
||||||
var name = ExpectIdent();
|
while (TryExpectSymbol(Symbol.ColonColon))
|
||||||
return new NodeTypeCustom(TokensFrom(startIndex), ident, name);
|
{
|
||||||
|
ident = ExpectIdent();
|
||||||
|
secitons.Add(ident);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new NodeTypeNamed(TokensFrom(startIndex), secitons);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new CompileException(Diagnostic.Error("Expected type").At(fileName, Peek()).Build());
|
throw BasicError("Expected type", Peek());
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Token> TokensFrom(int startIndex)
|
private List<Token> TokensFrom(int startIndex)
|
||||||
@@ -355,6 +605,17 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
return tokens.GetRange(startIndex, index - startIndex);
|
return tokens.GetRange(startIndex, index - startIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ExpectKeyword(Keyword keyword)
|
||||||
|
{
|
||||||
|
if (Peek() is TokenKeyword token && token.Keyword == keyword)
|
||||||
|
{
|
||||||
|
Next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw BasicError($"Expected '{keyword.AsString()}'", Peek());
|
||||||
|
}
|
||||||
|
|
||||||
private bool TryExpectKeyword(Keyword keyword)
|
private bool TryExpectKeyword(Keyword keyword)
|
||||||
{
|
{
|
||||||
if (Peek() is TokenKeyword token && token.Keyword == keyword)
|
if (Peek() is TokenKeyword token && token.Keyword == keyword)
|
||||||
@@ -374,7 +635,7 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new CompileException(Diagnostic.Error($"Expected '{symbol.AsString()}'").At(fileName, Peek()).Build());
|
throw BasicError($"Expected '{symbol.AsString()}'", Peek());
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool TryExpectSymbol(Symbol symbol)
|
private bool TryExpectSymbol(Symbol symbol)
|
||||||
@@ -396,7 +657,7 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new CompileException(Diagnostic.Error("Expected identifier").At(fileName, Peek()).Build());
|
throw BasicError("Expected identifier", Peek());
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool TryExpectIdent([NotNullWhen(true)] out TokenIdent? ident)
|
private bool TryExpectIdent([NotNullWhen(true)] out TokenIdent? ident)
|
||||||
@@ -454,7 +715,7 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
private void Next()
|
private void Next()
|
||||||
{
|
{
|
||||||
if (index >= tokens.Count)
|
if (index >= tokens.Count)
|
||||||
throw new CompileException(Diagnostic.Error("Unexpected end of tokens").At(fileName, Peek()).Build());
|
throw BasicError("Unexpected end of tokens", Peek());
|
||||||
|
|
||||||
index += 1;
|
index += 1;
|
||||||
}
|
}
|
||||||
@@ -527,11 +788,22 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private CompileException BasicError(string message, Token? ident)
|
||||||
|
{
|
||||||
|
return new CompileException(Diagnostic.Error(message).At(fileName, ident).Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
private CompileException BasicError(string message, Node? node)
|
||||||
|
{
|
||||||
|
return new CompileException(Diagnostic.Error(message).At(fileName, node).Build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class Ast(List<NodeDefinition> definitions, string fileName)
|
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 List<NodeDefinition> Definitions { get; } = definitions;
|
public List<NodeDefinition> Definitions { get; } = definitions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -542,134 +814,196 @@ 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 NodeDefinitionModule(List<Token> tokens, TokenIdent name) : NodeDefinition(tokens)
|
public class NodeDefinitionExternFunc(List<Token> tokens, bool exported, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeType? returnType) : NodeDefinition(tokens)
|
||||||
{
|
{
|
||||||
|
public bool Exported { get; } = exported;
|
||||||
public TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
|
public List<NodeDefinitionFunc.Param> Parameters { get; } = parameters;
|
||||||
|
public NodeType? ReturnType { get; } = returnType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeDefinitionFunc(List<Token> tokens, 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 TokenIdent Name { get; } = name;
|
public TokenIdent Name { get; } = name;
|
||||||
public List<Param> Parameters { get; } = parameters;
|
public List<Param> Parameters { get; } = parameters;
|
||||||
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, 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 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 class NodeDefinitionEnum(List<Token> tokens, bool exported, TokenIdent name, List<NodeDefinitionEnum.Variant> variants) : NodeDefinition(tokens)
|
||||||
|
{
|
||||||
|
public bool Exported { get; } = exported;
|
||||||
|
public TokenIdent Name { get; } = name;
|
||||||
|
public List<Variant> Variants { get; } = variants;
|
||||||
|
|
||||||
|
public class Variant(List<Token> tokens, TokenIdent name, NodeType? type) : Node(tokens)
|
||||||
|
{
|
||||||
|
public TokenIdent Name { get; } = name;
|
||||||
|
public NodeType? Type { get; } = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NodeDefinitionExternGlobalVariable(List<Token> tokens, bool exported, TokenIdent name, NodeType type) : NodeDefinition(tokens)
|
||||||
|
{
|
||||||
|
public bool Exported { get; } = exported;
|
||||||
|
public TokenIdent Name { get; } = name;
|
||||||
|
public NodeType Type { get; } = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NodeDefinitionGlobalVariable(List<Token> tokens, bool exported, TokenIdent name, NodeType type) : NodeDefinition(tokens)
|
||||||
|
{
|
||||||
|
public bool Exported { get; } = exported;
|
||||||
|
public TokenIdent Name { get; } = name;
|
||||||
|
public NodeType Type { get; } = type;
|
||||||
|
}
|
||||||
|
|
||||||
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 body) : NodeStatement(tokens)
|
||||||
{
|
{
|
||||||
public NodeExpression Condition { get; } = condition;
|
public NodeExpression Condition { get; } = condition;
|
||||||
public NodeStatement Block { get; } = block;
|
public NodeStatement Body { get; } = body;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NodeStatementFor(List<Token> tokens, TokenIdent variableName, NodeExpression array, NodeStatement body) : NodeStatement(tokens)
|
||||||
|
{
|
||||||
|
public TokenIdent VariableName { get; } = variableName;
|
||||||
|
public NodeExpression Array { get; } = array;
|
||||||
|
public NodeStatement Body { get; } = body;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NodeStatementMatch(List<Token> tokens, NodeExpression target, List<NodeStatementMatch.Case> cases) : NodeStatement(tokens)
|
||||||
|
{
|
||||||
|
public NodeExpression Target { get; } = target;
|
||||||
|
public List<Case> Cases { get; } = cases;
|
||||||
|
|
||||||
|
public class Case(List<Token> tokens, TokenIdent type, TokenIdent? variableName, NodeStatement body) : Node(tokens)
|
||||||
|
{
|
||||||
|
public TokenIdent Variant { get; } = type;
|
||||||
|
public TokenIdent? VariableName { get; } = variableName;
|
||||||
|
public NodeStatement Body { get; } = body;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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, NodeTypeNamed? type, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Module { get; } = module;
|
public NodeTypeNamed? Type { get; } = type;
|
||||||
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 NodeExpressionEnumLiteral(List<Token> tokens, NodeTypeNamed type, NodeExpression? value) : NodeExpression(tokens)
|
||||||
|
{
|
||||||
|
public NodeTypeNamed Type { get; } = type;
|
||||||
|
public NodeExpression? Value { get; } = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NodeExpressionArrayLiteral(List<Token> tokens, List<NodeExpression> values) : NodeExpression(tokens)
|
||||||
|
{
|
||||||
|
public List<NodeExpression> Values { get; } = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NodeExpressionStringConstructor(List<Token> tokens, NodeExpression value) : NodeExpression(tokens)
|
||||||
|
{
|
||||||
|
public NodeExpression Value { get; } = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 NodeExpressionIdent(List<Token> tokens, List<TokenIdent> sections) : NodeExpression(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Value { get; } = value;
|
public List<TokenIdent> Sections { get; } = sections;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeExpressionModuleIdent(List<Token> tokens, TokenIdent module, TokenIdent value) : NodeExpression(tokens)
|
public class NodeExpressionBinary(List<Token> tokens, NodeExpression left, NodeExpressionBinary.Op operation, NodeExpression right) : NodeExpression(tokens)
|
||||||
{
|
|
||||||
public TokenIdent Module { get; } = module;
|
|
||||||
public TokenIdent Value { get; } = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed 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;
|
||||||
@@ -702,7 +1036,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;
|
||||||
@@ -716,35 +1050,52 @@ 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 NodeTypeChar(List<Token> tokens) : NodeType(tokens);
|
||||||
|
|
||||||
|
public class NodeTypeNamed(List<Token> tokens, List<TokenIdent> sections) : NodeType(tokens)
|
||||||
{
|
{
|
||||||
public TokenIdent Module { get; } = module;
|
public List<TokenIdent> Sections { get; } = sections;
|
||||||
public TokenIdent Name { get; } = name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NodeTypePointer(List<Token> tokens, NodeType to) : NodeType(tokens)
|
public class NodeTypeAnonymousStruct(List<Token> tokens, List<NodeTypeAnonymousStruct.Field> fields) : NodeType(tokens)
|
||||||
|
{
|
||||||
|
public List<Field> Fields { get; } = fields;
|
||||||
|
|
||||||
|
public class Field(TokenIdent name, NodeType type)
|
||||||
|
{
|
||||||
|
public TokenIdent Name { get; } = name;
|
||||||
|
public NodeType Type { get; } = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NodeTypeArray(List<Token> tokens, NodeType elementType) : NodeType(tokens)
|
||||||
|
{
|
||||||
|
public NodeType ElementType { get; } = elementType;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,75 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using Compiler;
|
using Compiler;
|
||||||
|
|
||||||
var moduleGraphBuilder = ModuleGraph.Create();
|
var nubFiles = new List<string>();
|
||||||
var asts = new List<Ast>();
|
var libFiles = new List<string>();
|
||||||
|
var compileLib = false;
|
||||||
|
|
||||||
foreach (var fileName in args)
|
for (int i = 0; i < args.Length; i++)
|
||||||
|
{
|
||||||
|
string arg = args[i];
|
||||||
|
|
||||||
|
if (arg.StartsWith("--type="))
|
||||||
|
{
|
||||||
|
var value = arg.Split("--type=")[1];
|
||||||
|
switch (value)
|
||||||
|
{
|
||||||
|
case "lib":
|
||||||
|
compileLib = true;
|
||||||
|
break;
|
||||||
|
case "exe":
|
||||||
|
compileLib = false;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
DiagnosticFormatter.Print(Diagnostic.Error("Type must be 'exe' or 'lib'").Build(), Console.Error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (arg.EndsWith(".nub"))
|
||||||
|
{
|
||||||
|
nubFiles.Add(arg);
|
||||||
|
}
|
||||||
|
else if (arg.EndsWith(".nublib"))
|
||||||
|
{
|
||||||
|
libFiles.Add(arg);
|
||||||
|
}
|
||||||
|
else if (arg == "--help")
|
||||||
|
{
|
||||||
|
Console.WriteLine("""
|
||||||
|
Usage: nubc [options] <files>
|
||||||
|
|
||||||
|
Options:
|
||||||
|
--type=exe Compile the input files into an executable (default)
|
||||||
|
--type=lib Compile the input files into a library
|
||||||
|
--help Show this help message
|
||||||
|
|
||||||
|
Files:
|
||||||
|
*.nub Nub source files to compile
|
||||||
|
*.nublib Precompiled Nub libraries to link
|
||||||
|
|
||||||
|
Example:
|
||||||
|
nubc --type=exe main.nub utils.nub math.nublib
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DiagnosticFormatter.Print(Diagnostic.Error($"Unrecognized option '{arg}'").Build(), Console.Error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var moduleGraphBuilder = ModuleGraph.CreateBuilder();
|
||||||
|
var asts = new List<Ast>();
|
||||||
|
var archivePaths = new List<string>();
|
||||||
|
|
||||||
|
foreach (var libPath in libFiles)
|
||||||
|
{
|
||||||
|
var lib = NubLib.Unpack(libPath);
|
||||||
|
archivePaths.Add(lib.ArchivePath);
|
||||||
|
moduleGraphBuilder.AddManifest(lib.Manifest);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var fileName in nubFiles)
|
||||||
{
|
{
|
||||||
var file = File.ReadAllText(fileName);
|
var file = File.ReadAllText(fileName);
|
||||||
|
|
||||||
@@ -13,7 +78,7 @@ foreach (var fileName in args)
|
|||||||
foreach (var diagnostic in tokenizerDiagnostics)
|
foreach (var diagnostic in tokenizerDiagnostics)
|
||||||
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
||||||
|
|
||||||
if (tokenizerDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
if (tokens == null)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
var ast = Parser.Parse(fileName, tokens, out var parserDiagnostics);
|
var ast = Parser.Parse(fileName, tokens, out var parserDiagnostics);
|
||||||
@@ -21,7 +86,7 @@ foreach (var fileName in args)
|
|||||||
foreach (var diagnostic in parserDiagnostics)
|
foreach (var diagnostic in parserDiagnostics)
|
||||||
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
||||||
|
|
||||||
if (parserDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
if (ast == null)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
moduleGraphBuilder.AddAst(ast);
|
moduleGraphBuilder.AddAst(ast);
|
||||||
@@ -33,7 +98,7 @@ var moduleGraph = moduleGraphBuilder.Build(out var moduleGraphDiagnostics);
|
|||||||
foreach (var diagnostic in moduleGraphDiagnostics)
|
foreach (var diagnostic in moduleGraphDiagnostics)
|
||||||
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
||||||
|
|
||||||
if (moduleGraphDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
if (moduleGraph == null)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
var functions = new List<TypedNodeDefinitionFunc>();
|
var functions = new List<TypedNodeDefinitionFunc>();
|
||||||
@@ -42,7 +107,7 @@ foreach (var ast in asts)
|
|||||||
{
|
{
|
||||||
foreach (var func in ast.Definitions.OfType<NodeDefinitionFunc>())
|
foreach (var func in ast.Definitions.OfType<NodeDefinitionFunc>())
|
||||||
{
|
{
|
||||||
var typedFunction = TypeChecker.CheckFunction(ast.FileName, func, moduleGraph, out var typeCheckerDiagnostics);
|
var typedFunction = TypeChecker.CheckFunction(ast.FileName, ast.ModuleName.Ident, func, moduleGraph, out var typeCheckerDiagnostics);
|
||||||
|
|
||||||
foreach (var diagnostic in typeCheckerDiagnostics)
|
foreach (var diagnostic in typeCheckerDiagnostics)
|
||||||
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
||||||
@@ -54,13 +119,61 @@ foreach (var ast in asts)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var output = Generator.Emit(functions, moduleGraph);
|
if (Directory.Exists(".build"))
|
||||||
|
{
|
||||||
|
CleanDirectory(".build");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(".build");
|
||||||
|
}
|
||||||
|
|
||||||
Directory.Delete(".build", recursive: true);
|
string? entryPoint = null;
|
||||||
Directory.CreateDirectory(".build");
|
|
||||||
|
|
||||||
File.WriteAllText(".build/out.c", output);
|
if (!compileLib)
|
||||||
|
{
|
||||||
|
if (!moduleGraph.TryResolveIdentifier("main", "main", true, out var info) || info.Type is not NubTypeFunc entryPointType)
|
||||||
|
{
|
||||||
|
DiagnosticFormatter.Print(Diagnostic.Error("func main::main(): i32 is not defined. If you wanted to compile as a library, specify --type=lib").Build(), Console.Error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
Process.Start("gcc", ["-Og", "-g", "-o", ".build/out", ".build/out.c"]);
|
if (!entryPointType.ReturnType.IsAssignableTo(NubTypeSInt.Get(32)))
|
||||||
|
{
|
||||||
|
DiagnosticFormatter.Print(Diagnostic.Error($"Entrypoint must return an i32 (currently '{entryPointType.ReturnType}')").Build(), Console.Error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
entryPoint = info.MangledName;
|
||||||
|
}
|
||||||
|
|
||||||
|
var outFile = Generator.Emit(functions, moduleGraph, entryPoint);
|
||||||
|
|
||||||
|
if (compileLib)
|
||||||
|
{
|
||||||
|
Process.Start("gcc", ["-Og", "-g", "-Wall", "-Werror", "-c", "-o", ".build/out.o", outFile, .. archivePaths]).WaitForExit();
|
||||||
|
Process.Start("ar", ["rcs", ".build/out.a", ".build/out.o"]).WaitForExit();
|
||||||
|
NubLib.Pack(".build/out.nublib", ".build/out.a", Manifest.Create(moduleGraph));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Process.Start("gcc", ["-Og", "-g", "-Wall", "-Werror", "-o", ".build/out", outFile, .. archivePaths]).WaitForExit();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
static void CleanDirectory(string dirName)
|
||||||
|
{
|
||||||
|
var dir = new DirectoryInfo(dirName);
|
||||||
|
|
||||||
|
foreach (var file in dir.GetFiles())
|
||||||
|
{
|
||||||
|
file.Delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var subdir in dir.GetDirectories())
|
||||||
|
{
|
||||||
|
CleanDirectory(subdir.FullName);
|
||||||
|
subdir.Delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,18 +3,26 @@ 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;
|
||||||
|
|
||||||
private List<Token> Tokenize(out List<Diagnostic> diagnostics)
|
private List<Token>? Tokenize(out List<Diagnostic> diagnostics)
|
||||||
{
|
{
|
||||||
var tokens = new List<Token>();
|
var tokens = new List<Token>();
|
||||||
diagnostics = [];
|
diagnostics = [];
|
||||||
@@ -53,6 +61,9 @@ public sealed class Tokenizer(string fileName, string contents)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (diagnostics.Any(x => x.Severity == Diagnostic.DiagnosticSeverity.Error))
|
||||||
|
return null;
|
||||||
|
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,330 +77,348 @@ public sealed class Tokenizer(string fileName, string contents)
|
|||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case '0' when Peek(1) is 'x':
|
case '0' when Peek(1) is 'x':
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
|
|
||||||
var parsed = BigInteger.Zero;
|
|
||||||
var seenDigit = false;
|
|
||||||
|
|
||||||
while (TryPeek(out c))
|
|
||||||
{
|
{
|
||||||
if (c == '_')
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!char.IsAsciiHexDigit(c))
|
|
||||||
break;
|
|
||||||
|
|
||||||
seenDigit = true;
|
|
||||||
parsed <<= 4;
|
|
||||||
|
|
||||||
Consume();
|
Consume();
|
||||||
parsed += c switch
|
Consume();
|
||||||
|
|
||||||
|
var parsed = BigInteger.Zero;
|
||||||
|
var seenDigit = false;
|
||||||
|
|
||||||
|
while (TryPeek(out c))
|
||||||
{
|
{
|
||||||
>= '0' and <= '9' => c - '0',
|
if (c == '_')
|
||||||
>= 'a' and <= 'f' => c - 'a' + 10,
|
{
|
||||||
>= 'A' and <= 'F' => c - 'A' + 10,
|
Consume();
|
||||||
_ => 0
|
continue;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
if (!char.IsAsciiHexDigit(c))
|
||||||
|
break;
|
||||||
|
|
||||||
|
seenDigit = true;
|
||||||
|
parsed <<= 4;
|
||||||
|
|
||||||
|
Consume();
|
||||||
|
parsed += c switch
|
||||||
|
{
|
||||||
|
>= '0' and <= '9' => c - '0',
|
||||||
|
>= 'a' and <= 'f' => c - 'a' + 10,
|
||||||
|
>= 'A' and <= 'F' => c - 'A' + 10,
|
||||||
|
_ => 0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!seenDigit)
|
||||||
|
throw new CompileException(Diagnostic.Error("Expected hexadecimal digits after 0x").At(fileName, line, startColumn, column - startColumn).Build());
|
||||||
|
|
||||||
|
return new TokenIntLiteral(line, startColumn, column - startColumn, parsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!seenDigit)
|
|
||||||
throw new CompileException(Diagnostic.Error("Expected hexadecimal digits after 0x").At(fileName, line, startColumn, column - startColumn).Build());
|
|
||||||
|
|
||||||
return new TokenIntLiteral(line, startColumn, column - startColumn, parsed);
|
|
||||||
}
|
|
||||||
case '0' when Peek(1) is 'b':
|
case '0' when Peek(1) is 'b':
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
|
|
||||||
var parsed = BigInteger.Zero;
|
|
||||||
var seenDigit = false;
|
|
||||||
|
|
||||||
while (TryPeek(out c))
|
|
||||||
{
|
{
|
||||||
if (c == '_')
|
Consume();
|
||||||
|
Consume();
|
||||||
|
|
||||||
|
var parsed = BigInteger.Zero;
|
||||||
|
var seenDigit = false;
|
||||||
|
|
||||||
|
while (TryPeek(out c))
|
||||||
{
|
{
|
||||||
Consume();
|
if (c == '_')
|
||||||
continue;
|
{
|
||||||
|
Consume();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c is not '0' and not '1')
|
||||||
|
break;
|
||||||
|
|
||||||
|
seenDigit = true;
|
||||||
|
parsed <<= 1;
|
||||||
|
|
||||||
|
if (Consume() == '1')
|
||||||
|
parsed += BigInteger.One;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c is not '0' and not '1')
|
if (!seenDigit)
|
||||||
break;
|
throw new CompileException(Diagnostic.Error("Expected binary digits after 0b").At(fileName, line, startColumn, column - startColumn).Build());
|
||||||
|
|
||||||
seenDigit = true;
|
return new TokenIntLiteral(line, startColumn, column - startColumn, parsed);
|
||||||
parsed <<= 1;
|
|
||||||
|
|
||||||
if (Consume() == '1')
|
|
||||||
parsed += BigInteger.One;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!seenDigit)
|
|
||||||
throw new CompileException(Diagnostic.Error("Expected binary digits after 0b").At(fileName, line, startColumn, column - startColumn).Build());
|
|
||||||
|
|
||||||
return new TokenIntLiteral(line, startColumn, column - startColumn, parsed);
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
{
|
|
||||||
var parsed = BigInteger.Zero;
|
|
||||||
|
|
||||||
while (TryPeek(out c))
|
|
||||||
{
|
{
|
||||||
if (c == '_')
|
var parsed = BigInteger.Zero;
|
||||||
|
|
||||||
|
while (TryPeek(out c))
|
||||||
{
|
{
|
||||||
Consume();
|
if (c == '_')
|
||||||
continue;
|
{
|
||||||
|
Consume();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!char.IsDigit(c))
|
||||||
|
break;
|
||||||
|
|
||||||
|
parsed *= 10;
|
||||||
|
parsed += Consume() - '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!char.IsDigit(c))
|
return new TokenIntLiteral(line, startColumn, column - startColumn, parsed);
|
||||||
break;
|
|
||||||
|
|
||||||
parsed *= 10;
|
|
||||||
parsed += Consume() - '0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new TokenIntLiteral(line, startColumn, column - startColumn, parsed);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case '"':
|
case '"':
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
var buf = new StringBuilder();
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (!TryPeek(out c))
|
|
||||||
throw new CompileException(Diagnostic.Error("Unterminated string literal").At(fileName, line, column, 0).Build());
|
|
||||||
|
|
||||||
if (c == '"')
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (c == '\n')
|
|
||||||
throw new CompileException(Diagnostic.Error("Unterminated string literal").At(fileName, line, column, 1).Build());
|
|
||||||
|
|
||||||
buf.Append(Consume());
|
|
||||||
}
|
|
||||||
|
|
||||||
Consume();
|
|
||||||
return new TokenStringLiteral(line, startColumn, column - startColumn, buf.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
case '{':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.OpenCurly);
|
|
||||||
}
|
|
||||||
case '}':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.CloseCurly);
|
|
||||||
}
|
|
||||||
case '(':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.OpenParen);
|
|
||||||
}
|
|
||||||
case ')':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.CloseParen);
|
|
||||||
}
|
|
||||||
case ',':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Comma);
|
|
||||||
}
|
|
||||||
case '.':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Period);
|
|
||||||
}
|
|
||||||
case ':' when Peek(1) is ':':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.ColonColon);
|
|
||||||
}
|
|
||||||
case ':':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Colon);
|
|
||||||
}
|
|
||||||
case '^':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Caret);
|
|
||||||
}
|
|
||||||
case '!' when Peek(1) is '=':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.BangEqual);
|
|
||||||
}
|
|
||||||
case '!':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Bang);
|
|
||||||
}
|
|
||||||
case '=' when Peek(1) is '=':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.EqualEqual);
|
|
||||||
}
|
|
||||||
case '=':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Equal);
|
|
||||||
}
|
|
||||||
case '<' when Peek(1) is '<':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.LessThanLessThan);
|
|
||||||
}
|
|
||||||
case '<' when Peek(1) is '=':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.LessThanEqual);
|
|
||||||
}
|
|
||||||
case '<':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.LessThan);
|
|
||||||
}
|
|
||||||
case '>' when Peek(1) is '>':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.GreaterThanGreaterThan);
|
|
||||||
}
|
|
||||||
case '>' when Peek(1) is '=':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.GreaterThanEqual);
|
|
||||||
}
|
|
||||||
case '>':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.GreaterThan);
|
|
||||||
}
|
|
||||||
case '+' when Peek(1) is '=':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.PlusEqual);
|
|
||||||
}
|
|
||||||
case '+':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Plus);
|
|
||||||
}
|
|
||||||
case '-' when Peek(1) is '=':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.MinusEqual);
|
|
||||||
}
|
|
||||||
case '-':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Minus);
|
|
||||||
}
|
|
||||||
case '*' when Peek(1) is '=':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.StarEqual);
|
|
||||||
}
|
|
||||||
case '*':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Star);
|
|
||||||
}
|
|
||||||
case '/' when Peek(1) is '=':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.ForwardSlashEqual);
|
|
||||||
}
|
|
||||||
case '/':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.ForwardSlash);
|
|
||||||
}
|
|
||||||
case '%' when Peek(1) is '=':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.PercentEqual);
|
|
||||||
}
|
|
||||||
case '%':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Percent);
|
|
||||||
}
|
|
||||||
case '&' when Peek(1) is '&':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.AmpersandAmpersand);
|
|
||||||
}
|
|
||||||
case '&':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Ampersand);
|
|
||||||
}
|
|
||||||
case '|' when Peek(1) is '|':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.PipePipe);
|
|
||||||
}
|
|
||||||
case '|':
|
|
||||||
{
|
|
||||||
Consume();
|
|
||||||
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Pipe);
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
if (char.IsLetter(c) || c == '_')
|
|
||||||
{
|
{
|
||||||
|
Consume();
|
||||||
var buf = new StringBuilder();
|
var buf = new StringBuilder();
|
||||||
|
|
||||||
while (TryPeek(out c) && (char.IsLetterOrDigit(c) || c == '_'))
|
while (true)
|
||||||
buf.Append(Consume());
|
|
||||||
|
|
||||||
var value = buf.ToString();
|
|
||||||
|
|
||||||
return value switch
|
|
||||||
{
|
{
|
||||||
"func" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Func),
|
if (!TryPeek(out c))
|
||||||
"struct" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Struct),
|
throw new CompileException(Diagnostic.Error("Unterminated string literal").At(fileName, line, column, 0).Build());
|
||||||
"let" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Let),
|
|
||||||
"if" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.If),
|
if (c == '"')
|
||||||
"else" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Else),
|
break;
|
||||||
"while" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.While),
|
|
||||||
"return" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Return),
|
if (c == '\n')
|
||||||
"module" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Module),
|
throw new CompileException(Diagnostic.Error("Unterminated string literal").At(fileName, line, column, 1).Build());
|
||||||
"true" => new TokenBoolLiteral(line, startColumn, column - startColumn, true),
|
|
||||||
"false" => new TokenBoolLiteral(line, startColumn, column - startColumn, false),
|
buf.Append(Consume());
|
||||||
_ => new TokenIdent(line, startColumn, column - startColumn, value)
|
}
|
||||||
};
|
|
||||||
|
Consume();
|
||||||
|
return new TokenStringLiteral(line, startColumn, column - startColumn, buf.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new CompileException(Diagnostic.Error($"Unexpected character '{c}'").At(fileName, line, column, 1).Build());
|
case '{':
|
||||||
}
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.OpenCurly);
|
||||||
|
}
|
||||||
|
case '}':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.CloseCurly);
|
||||||
|
}
|
||||||
|
case '[':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.OpenSquare);
|
||||||
|
}
|
||||||
|
case ']':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.CloseSquare);
|
||||||
|
}
|
||||||
|
case '(':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.OpenParen);
|
||||||
|
}
|
||||||
|
case ')':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.CloseParen);
|
||||||
|
}
|
||||||
|
case ',':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Comma);
|
||||||
|
}
|
||||||
|
case '.':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Period);
|
||||||
|
}
|
||||||
|
case ':' when Peek(1) is ':':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.ColonColon);
|
||||||
|
}
|
||||||
|
case ':':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Colon);
|
||||||
|
}
|
||||||
|
case '^':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Caret);
|
||||||
|
}
|
||||||
|
case '!' when Peek(1) is '=':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.BangEqual);
|
||||||
|
}
|
||||||
|
case '!':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Bang);
|
||||||
|
}
|
||||||
|
case '=' when Peek(1) is '=':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.EqualEqual);
|
||||||
|
}
|
||||||
|
case '=':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Equal);
|
||||||
|
}
|
||||||
|
case '<' when Peek(1) is '<':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.LessThanLessThan);
|
||||||
|
}
|
||||||
|
case '<' when Peek(1) is '=':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.LessThanEqual);
|
||||||
|
}
|
||||||
|
case '<':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.LessThan);
|
||||||
|
}
|
||||||
|
case '>' when Peek(1) is '>':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.GreaterThanGreaterThan);
|
||||||
|
}
|
||||||
|
case '>' when Peek(1) is '=':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.GreaterThanEqual);
|
||||||
|
}
|
||||||
|
case '>':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.GreaterThan);
|
||||||
|
}
|
||||||
|
case '+' when Peek(1) is '=':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.PlusEqual);
|
||||||
|
}
|
||||||
|
case '+':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Plus);
|
||||||
|
}
|
||||||
|
case '-' when Peek(1) is '=':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.MinusEqual);
|
||||||
|
}
|
||||||
|
case '-':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Minus);
|
||||||
|
}
|
||||||
|
case '*' when Peek(1) is '=':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.StarEqual);
|
||||||
|
}
|
||||||
|
case '*':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Star);
|
||||||
|
}
|
||||||
|
case '/' when Peek(1) is '=':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.ForwardSlashEqual);
|
||||||
|
}
|
||||||
|
case '/':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.ForwardSlash);
|
||||||
|
}
|
||||||
|
case '%' when Peek(1) is '=':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.PercentEqual);
|
||||||
|
}
|
||||||
|
case '%':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Percent);
|
||||||
|
}
|
||||||
|
case '&' when Peek(1) is '&':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.AmpersandAmpersand);
|
||||||
|
}
|
||||||
|
case '&':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Ampersand);
|
||||||
|
}
|
||||||
|
case '|' when Peek(1) is '|':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.PipePipe);
|
||||||
|
}
|
||||||
|
case '|':
|
||||||
|
{
|
||||||
|
Consume();
|
||||||
|
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.Pipe);
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
if (char.IsLetter(c) || c == '_')
|
||||||
|
{
|
||||||
|
var buf = new StringBuilder();
|
||||||
|
|
||||||
|
while (TryPeek(out c) && (char.IsLetterOrDigit(c) || c == '_'))
|
||||||
|
buf.Append(Consume());
|
||||||
|
|
||||||
|
var value = buf.ToString();
|
||||||
|
|
||||||
|
return value switch
|
||||||
|
{
|
||||||
|
"func" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Func),
|
||||||
|
"struct" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Struct),
|
||||||
|
"packed" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Packed),
|
||||||
|
"enum" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Enum),
|
||||||
|
"new" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.New),
|
||||||
|
"match" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Match),
|
||||||
|
"let" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Let),
|
||||||
|
"if" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.If),
|
||||||
|
"else" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Else),
|
||||||
|
"while" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.While),
|
||||||
|
"for" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.For),
|
||||||
|
"in" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.In),
|
||||||
|
"return" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Return),
|
||||||
|
"module" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Module),
|
||||||
|
"export" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Export),
|
||||||
|
"extern" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Extern),
|
||||||
|
"true" => new TokenBoolLiteral(line, startColumn, column - startColumn, true),
|
||||||
|
"false" => new TokenBoolLiteral(line, startColumn, column - startColumn, false),
|
||||||
|
_ => new TokenIdent(line, startColumn, column - startColumn, value)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new CompileException(Diagnostic.Error($"Unexpected character '{c}'").At(fileName, line, column, 1).Build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -454,22 +483,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;
|
||||||
}
|
}
|
||||||
@@ -480,6 +509,8 @@ public enum Symbol
|
|||||||
CloseCurly,
|
CloseCurly,
|
||||||
OpenParen,
|
OpenParen,
|
||||||
CloseParen,
|
CloseParen,
|
||||||
|
OpenSquare,
|
||||||
|
CloseSquare,
|
||||||
Comma,
|
Comma,
|
||||||
Period,
|
Period,
|
||||||
Colon,
|
Colon,
|
||||||
@@ -511,7 +542,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;
|
||||||
}
|
}
|
||||||
@@ -520,15 +551,23 @@ public enum Keyword
|
|||||||
{
|
{
|
||||||
Func,
|
Func,
|
||||||
Struct,
|
Struct,
|
||||||
|
Packed,
|
||||||
|
Enum,
|
||||||
|
New,
|
||||||
|
Match,
|
||||||
Let,
|
Let,
|
||||||
If,
|
If,
|
||||||
Else,
|
Else,
|
||||||
While,
|
While,
|
||||||
|
For,
|
||||||
|
In,
|
||||||
Return,
|
Return,
|
||||||
Module,
|
Module,
|
||||||
|
Export,
|
||||||
|
Extern,
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -543,6 +582,8 @@ public static class TokenExtensions
|
|||||||
Symbol.CloseCurly => "}",
|
Symbol.CloseCurly => "}",
|
||||||
Symbol.OpenParen => "(",
|
Symbol.OpenParen => "(",
|
||||||
Symbol.CloseParen => ")",
|
Symbol.CloseParen => ")",
|
||||||
|
Symbol.OpenSquare => "[",
|
||||||
|
Symbol.CloseSquare => "]",
|
||||||
Symbol.Comma => ",",
|
Symbol.Comma => ",",
|
||||||
Symbol.Period => ".",
|
Symbol.Period => ".",
|
||||||
Symbol.Colon => ":",
|
Symbol.Colon => ":",
|
||||||
@@ -582,12 +623,20 @@ public static class TokenExtensions
|
|||||||
{
|
{
|
||||||
Keyword.Func => "func",
|
Keyword.Func => "func",
|
||||||
Keyword.Struct => "struct",
|
Keyword.Struct => "struct",
|
||||||
|
Keyword.Packed => "packed",
|
||||||
|
Keyword.Enum => "enum",
|
||||||
|
Keyword.New => "new",
|
||||||
|
Keyword.Match => "enum",
|
||||||
Keyword.Let => "let",
|
Keyword.Let => "let",
|
||||||
Keyword.If => "if",
|
Keyword.If => "if",
|
||||||
Keyword.Else => "else",
|
Keyword.Else => "else",
|
||||||
Keyword.While => "while",
|
Keyword.While => "while",
|
||||||
|
Keyword.For => "for",
|
||||||
|
Keyword.In => "in",
|
||||||
Keyword.Return => "return",
|
Keyword.Return => "return",
|
||||||
Keyword.Module => "module",
|
Keyword.Module => "module",
|
||||||
|
Keyword.Export => "export",
|
||||||
|
Keyword.Extern => "extern",
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(symbol), symbol, null)
|
_ => throw new ArgumentOutOfRangeException(nameof(symbol), symbol, null)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,27 +0,0 @@
|
|||||||
module main
|
|
||||||
|
|
||||||
func main(): i32 {
|
|
||||||
let x: i32 = 23
|
|
||||||
x = 24
|
|
||||||
|
|
||||||
if !true {
|
|
||||||
x = 49
|
|
||||||
return x
|
|
||||||
} else {
|
|
||||||
x = 3
|
|
||||||
}
|
|
||||||
|
|
||||||
let i: i32 = 0
|
|
||||||
|
|
||||||
x = 1 + 2 * 34
|
|
||||||
while i < 10 {
|
|
||||||
i = i + 1
|
|
||||||
x = i
|
|
||||||
}
|
|
||||||
|
|
||||||
let me: test::person = struct test::person { age = 21 name = "Oliver" }
|
|
||||||
|
|
||||||
x = test::do_something(me.name)
|
|
||||||
test::do_something(me.name)
|
|
||||||
return x
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
module test
|
|
||||||
|
|
||||||
struct person {
|
|
||||||
age: i32
|
|
||||||
name: string
|
|
||||||
}
|
|
||||||
|
|
||||||
func do_something(name: string): i32 {
|
|
||||||
return 3
|
|
||||||
}
|
|
||||||
1
examples/.gitignore
vendored
Normal file
1
examples/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.build
|
||||||
21
examples/build
Executable file
21
examples/build
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
SCRIPT_DIR=$(dirname "$0")
|
||||||
|
|
||||||
|
pushd $SCRIPT_DIR
|
||||||
|
pushd ../compiler
|
||||||
|
dotnet build -c Release
|
||||||
|
popd
|
||||||
|
|
||||||
|
pushd core
|
||||||
|
time ../../compiler/bin/Release/net9.0/Compiler sys.nub print.nub --type=lib
|
||||||
|
popd
|
||||||
|
|
||||||
|
pushd program
|
||||||
|
time ../../compiler/bin/Release/net9.0/Compiler main.nub ../core/.build/out.nublib
|
||||||
|
popd
|
||||||
|
|
||||||
|
./program/.build/out
|
||||||
|
popd
|
||||||
10
examples/core/print.nub
Normal file
10
examples/core/print.nub
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
module core
|
||||||
|
|
||||||
|
export func print(text: string) {
|
||||||
|
sys::write(0, text.ptr, text.length)
|
||||||
|
}
|
||||||
|
|
||||||
|
export func println(text: string) {
|
||||||
|
print(text)
|
||||||
|
print("\n")
|
||||||
|
}
|
||||||
5
examples/core/sys.nub
Normal file
5
examples/core/sys.nub
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
module sys
|
||||||
|
|
||||||
|
export extern func read(fd: u32, buf: ^char, count: u64): i64
|
||||||
|
export extern func write(fd: u32, buf: ^char, count: u64): i64
|
||||||
|
export extern func open(fileName: ^char, flags: i32, mode: u16): i64
|
||||||
32
examples/program/main.nub
Normal file
32
examples/program/main.nub
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
struct Human {
|
||||||
|
name: string
|
||||||
|
age: i32
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Message {
|
||||||
|
Quit
|
||||||
|
Say: string
|
||||||
|
}
|
||||||
|
|
||||||
|
func main(): i32 {
|
||||||
|
|
||||||
|
let x = new string("test".ptr) + " " + "uwu"
|
||||||
|
core::println(x)
|
||||||
|
|
||||||
|
let messages: []Message = [new Message::Say("first"), new Message::Quit]
|
||||||
|
|
||||||
|
for message in messages {
|
||||||
|
match message {
|
||||||
|
Say msg {
|
||||||
|
core::println(msg)
|
||||||
|
}
|
||||||
|
Quit {
|
||||||
|
core::println("quit")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user