wip: enums
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using NubLang.CLI;
|
||||
using System.Diagnostics;
|
||||
using NubLang.CLI;
|
||||
using NubLang.Code;
|
||||
using NubLang.Diagnostics;
|
||||
using NubLang.Generation.QBE;
|
||||
@@ -9,6 +10,8 @@ using NubLang.Tokenization;
|
||||
using NubLang.TypeChecking;
|
||||
using NubLang.TypeChecking.Node;
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
|
||||
var options = new Options();
|
||||
|
||||
for (var i = 0; i < args.Length; i++)
|
||||
@@ -35,6 +38,9 @@ for (var i = 0; i < args.Length; i++)
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Parse cli args: {sw.ElapsedMilliseconds}ms");
|
||||
sw.Restart();
|
||||
|
||||
foreach (var file in options.Files)
|
||||
{
|
||||
if (!File.Exists(file.Path))
|
||||
@@ -44,6 +50,9 @@ foreach (var file in options.Files)
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Check file exists: {sw.ElapsedMilliseconds}ms");
|
||||
sw.Restart();
|
||||
|
||||
var diagnostics = new List<Diagnostic>();
|
||||
|
||||
var syntaxTrees = new List<SyntaxTree>();
|
||||
@@ -53,16 +62,26 @@ foreach (var file in options.Files)
|
||||
var tokens = tokenizer.Tokenize().ToList();
|
||||
diagnostics.AddRange(tokenizer.GetDiagnostics());
|
||||
|
||||
Console.WriteLine($"Tokenize: {Path.GetFileName(file.Path)}: {sw.ElapsedMilliseconds}ms");
|
||||
sw.Restart();
|
||||
|
||||
var parser = new Parser();
|
||||
var syntaxTree = parser.Parse(tokens);
|
||||
|
||||
diagnostics.AddRange(parser.GetDiagnostics());
|
||||
|
||||
Console.WriteLine($"Parse: {Path.GetFileName(file.Path)}: {sw.ElapsedMilliseconds}ms");
|
||||
sw.Restart();
|
||||
|
||||
syntaxTrees.Add(syntaxTree);
|
||||
}
|
||||
|
||||
sw.Restart();
|
||||
|
||||
var moduleRepository = new ModuleRepository(syntaxTrees);
|
||||
|
||||
Console.WriteLine($"Create module repository: {sw.ElapsedMilliseconds}ms");
|
||||
sw.Restart();
|
||||
|
||||
var definitions = new List<DefinitionNode>();
|
||||
|
||||
var referencedStructTypes = new HashSet<NubStructType>();
|
||||
@@ -72,6 +91,9 @@ foreach (var syntaxTree in syntaxTrees)
|
||||
var typeChecker = new TypeChecker(syntaxTree, moduleRepository);
|
||||
typeChecker.Check();
|
||||
|
||||
Console.WriteLine($"Type check {syntaxTree.Metadata.ModuleName}: {sw.ElapsedMilliseconds}ms");
|
||||
sw.Restart();
|
||||
|
||||
definitions.AddRange(typeChecker.Definitions);
|
||||
diagnostics.AddRange(typeChecker.Diagnostics);
|
||||
|
||||
@@ -81,11 +103,16 @@ foreach (var syntaxTree in syntaxTrees)
|
||||
}
|
||||
}
|
||||
|
||||
sw.Restart();
|
||||
|
||||
foreach (var diagnostic in diagnostics)
|
||||
{
|
||||
Console.Error.WriteLine(diagnostic.FormatANSI());
|
||||
}
|
||||
|
||||
Console.WriteLine($"Print diagnostics: {sw.ElapsedMilliseconds}ms");
|
||||
sw.Restart();
|
||||
|
||||
if (diagnostics.Any(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error))
|
||||
{
|
||||
return 1;
|
||||
@@ -93,17 +120,28 @@ if (diagnostics.Any(diagnostic => diagnostic.Severity == DiagnosticSeverity.Erro
|
||||
|
||||
Directory.CreateDirectory(".build");
|
||||
|
||||
sw.Restart();
|
||||
|
||||
var generator = new QBEGenerator(definitions, referencedStructTypes);
|
||||
var ssa = generator.Emit();
|
||||
var ssaFilePath = Path.Combine(".build", "out.ssa");
|
||||
File.WriteAllText(ssaFilePath, ssa);
|
||||
|
||||
Console.WriteLine($"Emit ssa: {sw.ElapsedMilliseconds}ms");
|
||||
sw.Restart();
|
||||
|
||||
var asmFilePath = Path.Combine(".build", "out.asm");
|
||||
var qbeSuccess = await QBE.Invoke(ssaFilePath, asmFilePath);
|
||||
if (!qbeSuccess) return 1;
|
||||
|
||||
Console.WriteLine($"Emit asm: {sw.ElapsedMilliseconds}ms");
|
||||
sw.Restart();
|
||||
|
||||
var objFilePath = Path.Combine(".build", "out.o");
|
||||
var asmSuccess = await GCC.Assemble(asmFilePath, objFilePath);
|
||||
if (!asmSuccess) return 1;
|
||||
|
||||
Console.WriteLine($"Assemble: {sw.ElapsedMilliseconds}ms");
|
||||
sw.Restart();
|
||||
|
||||
return 0;
|
||||
@@ -8,10 +8,10 @@ namespace NubLang.Parsing;
|
||||
public sealed class Parser
|
||||
{
|
||||
private readonly List<Diagnostic> _diagnostics = [];
|
||||
private readonly HashSet<string> _templateArguments = [];
|
||||
private List<Token> _tokens = [];
|
||||
private int _tokenIndex;
|
||||
private string _moduleName = string.Empty;
|
||||
private HashSet<string> _templateArguments = [];
|
||||
|
||||
private Token? CurrentToken => _tokenIndex < _tokens.Count ? _tokens[_tokenIndex] : null;
|
||||
private bool HasToken => CurrentToken != null;
|
||||
@@ -86,10 +86,11 @@ public sealed class Parser
|
||||
}
|
||||
|
||||
var keyword = ExpectSymbol();
|
||||
DefinitionSyntax definition = keyword.Symbol switch
|
||||
var definition = keyword.Symbol switch
|
||||
{
|
||||
Symbol.Func => ParseFunc(startIndex, exported, null),
|
||||
Symbol.Struct => ParseStruct(startIndex, exported),
|
||||
Symbol.Enum => ParseEnum(startIndex, exported),
|
||||
_ => throw new ParseException(Diagnostic
|
||||
.Error($"Expected 'func' or 'struct' but found '{keyword.Symbol}'")
|
||||
.WithHelp("Valid definition keywords are 'func' and 'struct'")
|
||||
@@ -228,6 +229,36 @@ public sealed class Parser
|
||||
return new StructSyntax(GetTokens(startIndex), name.Value, exported, fields, funcs);
|
||||
}
|
||||
|
||||
private EnumSyntax ParseEnum(int startIndex, bool exported)
|
||||
{
|
||||
var name = ExpectIdentifier();
|
||||
|
||||
TypeSyntax? type = null;
|
||||
if (TryExpectSymbol(Symbol.Colon))
|
||||
{
|
||||
type = ParseType();
|
||||
}
|
||||
|
||||
var values = new List<EnumValueSyntax>();
|
||||
|
||||
ExpectSymbol(Symbol.OpenBrace);
|
||||
while (!TryExpectSymbol(Symbol.CloseBrace))
|
||||
{
|
||||
var valueStartIndex = _tokenIndex;
|
||||
var valueName = ExpectIdentifier().Value;
|
||||
|
||||
ExpressionSyntax? valueValue = null;
|
||||
if (TryExpectSymbol(Symbol.Assign))
|
||||
{
|
||||
valueValue = ParseExpression();
|
||||
}
|
||||
|
||||
values.Add(new EnumValueSyntax(GetTokens(valueStartIndex), valueName, valueValue));
|
||||
}
|
||||
|
||||
return new EnumSyntax(GetTokens(startIndex), name.Value, exported, type, values);
|
||||
}
|
||||
|
||||
private StatementSyntax ParseStatement()
|
||||
{
|
||||
var startIndex = _tokenIndex;
|
||||
|
||||
@@ -16,4 +16,8 @@ public record StructFuncSyntax(List<Token> Tokens, string Name, string? Hook, Fu
|
||||
|
||||
public record StructSyntax(List<Token> Tokens, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);
|
||||
|
||||
public record StructTemplateSyntax(List<Token> Tokens, List<string> TemplateArguments, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);
|
||||
public record StructTemplateSyntax(List<Token> Tokens, List<string> TemplateArguments, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);
|
||||
|
||||
public record EnumValueSyntax(List<Token> Tokens, string Name, ExpressionSyntax? Value) : SyntaxNode(Tokens);
|
||||
|
||||
public record EnumSyntax(List<Token> Tokens, string Name, bool Exported, TypeSyntax? Type, List<EnumValueSyntax> Values) : DefinitionSyntax(Tokens, Name, Exported);
|
||||
@@ -82,4 +82,5 @@ public enum Symbol
|
||||
Export,
|
||||
Defer,
|
||||
At,
|
||||
Enum,
|
||||
}
|
||||
@@ -24,6 +24,7 @@ public sealed class Tokenizer
|
||||
["export"] = Symbol.Export,
|
||||
["import"] = Symbol.Import,
|
||||
["defer"] = Symbol.Defer,
|
||||
["enum"] = Symbol.Enum,
|
||||
};
|
||||
|
||||
private static readonly Dictionary<char[], Symbol> Symbols = new()
|
||||
|
||||
@@ -3,6 +3,13 @@ import "raylib"
|
||||
|
||||
module "main"
|
||||
|
||||
enum State
|
||||
{
|
||||
NONE
|
||||
TEST
|
||||
SOMEOTHERVALUE
|
||||
}
|
||||
|
||||
extern "main" func main(args: []cstring): i64
|
||||
{
|
||||
raylib::InitWindow(1600, 900, "Hi from nub-lang")
|
||||
|
||||
Reference in New Issue
Block a user