This commit is contained in:
nub31
2025-05-25 02:13:26 +02:00
parent 6e96c149a7
commit 15a4bd7ea2
5 changed files with 202 additions and 103 deletions

View File

@@ -10,7 +10,7 @@ internal static class Program
{
private static readonly Lexer Lexer = new();
private static readonly Parser Parser = new();
public static int Main(string[] args)
{
if (args.Length != 2)
@@ -19,16 +19,16 @@ internal static class Program
Console.WriteLine("Example: nub src out.asm");
return 1;
}
var input = Path.GetFullPath(args[0]);
var output = Path.GetFullPath(args[1]);
if (!Directory.Exists(input))
{
Console.WriteLine($"Error: Input directory '{input}' does not exist.");
return 1;
}
var outputDir = Path.GetDirectoryName(output);
if (outputDir == null || !Directory.Exists(outputDir))
{
@@ -42,24 +42,25 @@ internal static class Program
return 1;
}
List<Diagnostic> diagnostics = [];
var modules = RunFrontend(input, diagnostics);
foreach (var diagnostic in diagnostics)
List<ModuleNode> modules;
try
{
Console.WriteLine(diagnostic.Format());
modules = RunFrontend(input);
}
if (diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error))
catch (Exception)
{
return 1;
}
var definitions = modules.SelectMany(f => f.Definitions).ToList();
var typeChecker = new TypeChecker(definitions);
typeChecker.TypeCheck();
var typeCheckResult = typeChecker.TypeCheck();
typeCheckResult.PrintAllDiagnostics();
if (typeCheckResult.HasErrors)
{
return 1;
}
var generator = new Generator(definitions);
var result = generator.Generate();
@@ -68,14 +69,14 @@ internal static class Program
return 0;
}
private static List<ModuleNode> RunFrontend(string rootFilePath, List<Diagnostic> diagnostics)
private static List<ModuleNode> RunFrontend(string rootFilePath)
{
List<ModuleNode> modules = [];
RunFrontend(rootFilePath, modules, diagnostics);
RunFrontend(rootFilePath, modules);
return modules;
}
private static void RunFrontend(string rootFilePath, List<ModuleNode> modules, List<Diagnostic> diagnostics)
private static void RunFrontend(string rootFilePath, List<ModuleNode> modules)
{
var filePaths = Directory.EnumerateFiles(rootFilePath, "*.nub", SearchOption.TopDirectoryOnly);
@@ -86,16 +87,21 @@ internal static class Program
tokens.AddRange(Lexer.Lex(src, new SourceFile(filePath, src)));
}
var module = Parser.ParseModule(tokens, rootFilePath);
diagnostics.AddRange(Parser.Diagnostics);
modules.Add(module);
foreach (var import in module.Imports)
var parseResult = Parser.ParseModule(tokens, rootFilePath);
parseResult.PrintAllDiagnostics();
if (parseResult.HasErrors)
{
var importPath = Path.GetFullPath(import, module.Path);
throw new Exception();
}
modules.Add(parseResult.Value);
foreach (var import in parseResult.Value.Imports)
{
var importPath = Path.GetFullPath(import, parseResult.Value.Path);
if (modules.All(m => m.Path != importPath))
{
RunFrontend(importPath, modules, diagnostics);
RunFrontend(importPath, modules);
}
}
}