From bc040c3fef8744aaed823d60ac8b58fb67658487 Mon Sep 17 00:00:00 2001 From: nub31 Date: Mon, 26 May 2025 18:29:31 +0200 Subject: [PATCH] ... --- src/compiler/Nub.Lang/Program.cs | 47 ++++++++++++++------------------ 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/compiler/Nub.Lang/Program.cs b/src/compiler/Nub.Lang/Program.cs index 8ce43cc..af76aa9 100644 --- a/src/compiler/Nub.Lang/Program.cs +++ b/src/compiler/Nub.Lang/Program.cs @@ -1,4 +1,6 @@ -using Nub.Lang.Backend; +using System.Diagnostics.CodeAnalysis; +using Nub.Lang.Backend; +using Nub.Lang.Frontend; using Nub.Lang.Frontend.Diagnostics; using Nub.Lang.Frontend.Lexing; using Nub.Lang.Frontend.Parsing; @@ -42,22 +44,7 @@ internal static class Program return 1; } - List modules; - try - { - modules = RunFrontend(input); - } - catch (Exception) - { - return 1; - } - - var definitions = modules.SelectMany(f => f.Definitions).ToList(); - - var typeChecker = new TypeChecker(definitions); - var typeCheckResult = typeChecker.TypeCheck(); - typeCheckResult.PrintAllDiagnostics(); - if (typeCheckResult.HasErrors) + if (TryRunFrontend(input, out var definitions)) { return 1; } @@ -69,15 +56,22 @@ internal static class Program return 0; } - private static List RunFrontend(string rootFilePath) + private static bool TryRunFrontend(string rootFilePath, out List definitions) { List modules = []; - RunFrontend(rootFilePath, modules); - return modules; + var error = TryRunFrontend(rootFilePath, modules); + definitions = modules.SelectMany(f => f.Definitions).ToList(); + + var typeChecker = new TypeChecker(definitions); + var typeCheckResult = typeChecker.TypeCheck(); + typeCheckResult.PrintAllDiagnostics(); + error = error || typeCheckResult.HasErrors; + return error; } - private static void RunFrontend(string rootFilePath, List modules) + private static bool TryRunFrontend(string rootFilePath, List modules) { + var error = false; var filePaths = Directory.EnumerateFiles(rootFilePath, "*.nub", SearchOption.TopDirectoryOnly); List tokens = []; @@ -89,10 +83,6 @@ internal static class Program var parseResult = Parser.ParseModule(tokens, rootFilePath); parseResult.PrintAllDiagnostics(); - if (parseResult.HasErrors) - { - throw new Exception(); - } modules.Add(parseResult.Value); @@ -101,8 +91,13 @@ internal static class Program var importPath = Path.GetFullPath(import, parseResult.Value.Path); if (modules.All(m => m.Path != importPath)) { - RunFrontend(importPath, modules); + if (!TryRunFrontend(importPath, modules)) + { + error = true; + } } } + + return error; } } \ No newline at end of file