From 860e1fd0e480ff84176caad47d0a92bf237b93f4 Mon Sep 17 00:00:00 2001 From: nub31 Date: Mon, 18 Aug 2025 16:29:41 +0200 Subject: [PATCH] Fix bugs in cli --- example/src/main.nub | 51 +++++++++++++++++++---------- src/compiler/NubLang.CLI/GCC.cs | 11 ++----- src/compiler/NubLang.CLI/Program.cs | 24 +++++++------- src/compiler/NubLang.CLI/QBE.cs | 14 +++----- 4 files changed, 53 insertions(+), 47 deletions(-) diff --git a/example/src/main.nub b/example/src/main.nub index a9f48e0..5495540 100644 --- a/example/src/main.nub +++ b/example/src/main.nub @@ -1,34 +1,49 @@ +// c extern func puts(text: cstring) -interface Test +// raylib +extern func InitWindow(width: i32, height: i32, title: cstring) +extern func CloseWindow() +extern func WindowShouldClose(): bool +extern func BeginDrawing() +extern func EndDrawing() +extern func ClearBackground(color: Color) +extern func DrawCircle(centerX: i32, centerY: i32, radius: f32, color: Color) + +struct Color { - func print() - func test() + r: u8 + g: u8 + b: u8 + a: u8 } -struct Human : Test +struct Vector2 { - name: cstring + x: f32 + y: f32 +} - func print() - { - puts(this.name) - } - - func test() - { - puts("test") - } +struct Vector3 +{ + x: f32 + y: f32 + z: f32 } func main(args: []cstring): i64 { - let human: Human = struct { - name = "oliver" + InitWindow(800, 600, "Hello from nub!") + + while !WindowShouldClose() + { + BeginDrawing() + ClearBackground(struct { r = 55 g = 55 b = 55 a = 255 }) + DrawCircle(400, 300, 50, struct { r = 255 g = 255 b = 0 a = 0 }) + EndDrawing() } - human.print() - human.test() + CloseWindow() return 0 } diff --git a/src/compiler/NubLang.CLI/GCC.cs b/src/compiler/NubLang.CLI/GCC.cs index 8411cbb..eb12878 100644 --- a/src/compiler/NubLang.CLI/GCC.cs +++ b/src/compiler/NubLang.CLI/GCC.cs @@ -4,19 +4,18 @@ namespace NubLang.CLI; public static class GCC { - public static async Task Assemble(string asm, string objPath) + public static async Task Assemble(string asmPath, string outPath) { - var dir = Path.GetDirectoryName(objPath); + var dir = Path.GetDirectoryName(outPath); if (dir != null) { Directory.CreateDirectory(dir); } using var process = new Process(); - process.StartInfo = new ProcessStartInfo("gcc", ["-xassembler", "-c", "-o", objPath, "-"]) + process.StartInfo = new ProcessStartInfo("gcc", ["-xassembler", "-c", "-o", outPath, asmPath]) { UseShellExecute = false, - RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true @@ -24,9 +23,6 @@ public static class GCC process.Start(); - await process.StandardInput.WriteAsync(asm); - process.StandardInput.Close(); - await process.WaitForExitAsync(); var errors = await process.StandardError.ReadToEndAsync(); @@ -50,7 +46,6 @@ public static class GCC process.StartInfo = new ProcessStartInfo("gcc", ["-nostartfiles", "-o", executablePath, ..objectFiles]) { UseShellExecute = false, - RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true diff --git a/src/compiler/NubLang.CLI/Program.cs b/src/compiler/NubLang.CLI/Program.cs index bbf7e61..64a0fa5 100644 --- a/src/compiler/NubLang.CLI/Program.cs +++ b/src/compiler/NubLang.CLI/Program.cs @@ -14,7 +14,8 @@ const string BIN_DIR = "bin"; const string INT_DIR = "bin-int"; var INT_BUILTIN_DIR = Path.Join(INT_DIR, "builtin"); var INT_OBJECT_DIR = Path.Join(INT_DIR, "obj"); -var INT_DEBUG_DIR = Path.Join(INT_DIR, "debug"); +var INT_SSA_DIR = Path.Join(INT_DIR, "ssa"); +var INT_ASM_DIR = Path.Join(INT_DIR, "asm"); if (Directory.Exists(INT_DIR)) { @@ -24,7 +25,8 @@ if (Directory.Exists(INT_DIR)) Directory.CreateDirectory(BIN_DIR); Directory.CreateDirectory(INT_BUILTIN_DIR); Directory.CreateDirectory(INT_OBJECT_DIR); -Directory.CreateDirectory(INT_DEBUG_DIR); +Directory.CreateDirectory(INT_SSA_DIR); +Directory.CreateDirectory(INT_ASM_DIR); var options = new Options(); @@ -55,7 +57,6 @@ for (var i = 0; i < args.Length; i++) } else if (arg == "-c") { - i++; options.Link = false; } else @@ -123,24 +124,25 @@ for (var i = 0; i < typedSyntaxTrees.Count; i++) var generator = new QBEGenerator(syntaxTree, typedDefinitionTable); var ssa = generator.Emit(); - File.WriteAllText(Path.Join(INT_DEBUG_DIR, $"{outFileName}.ssa"), ssa); - var asm = await QBE.Invoke(ssa); - if (asm == null) + var ssaPath = Path.Join(INT_SSA_DIR, $"{outFileName}.ssa"); + File.WriteAllText(ssaPath, ssa); + + var asmFilePath = Path.Join(INT_ASM_DIR, $"{outFileName}.s"); + var qbeSuccess = await QBE.Invoke(ssaPath, asmFilePath); + if (!qbeSuccess) { return 1; } - File.WriteAllText(Path.Join(INT_DEBUG_DIR, $"{outFileName}.s"), asm); - - var fileName = Path.Join(INT_OBJECT_DIR, $"{outFileName}.o"); - var asmSuccess = await GCC.Assemble(asm, fileName); + var objFilePath = Path.Join(INT_OBJECT_DIR, $"{outFileName}.o"); + var asmSuccess = await GCC.Assemble(asmFilePath, objFilePath); if (!asmSuccess) { return 1; } - objectFiles.Add(fileName); + objectFiles.Add(objFilePath); } if (options.CustomRuntime == null) diff --git a/src/compiler/NubLang.CLI/QBE.cs b/src/compiler/NubLang.CLI/QBE.cs index 9bc3860..52e37c5 100644 --- a/src/compiler/NubLang.CLI/QBE.cs +++ b/src/compiler/NubLang.CLI/QBE.cs @@ -4,13 +4,12 @@ namespace NubLang.CLI; public static class QBE { - public static async Task Invoke(string ssa) + public static async Task Invoke(string ssaPath, string outPath) { using var process = new Process(); - process.StartInfo = new ProcessStartInfo("qbe") + process.StartInfo = new ProcessStartInfo("qbe", ["-o", outPath, ssaPath]) { UseShellExecute = false, - RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true @@ -18,9 +17,6 @@ public static class QBE process.Start(); - await process.StandardInput.WriteAsync(ssa); - process.StandardInput.Close(); - await process.WaitForExitAsync(); var errors = await process.StandardError.ReadToEndAsync(); @@ -29,8 +25,6 @@ public static class QBE await Console.Error.WriteLineAsync("qbe error:\n" + errors); } - var asm = await process.StandardOutput.ReadToEndAsync(); - - return process.ExitCode == 0 ? asm : null; + return process.ExitCode == 0; } -} +} \ No newline at end of file