Fix bugs in cli

This commit is contained in:
nub31
2025-08-18 16:29:41 +02:00
parent b8c69b5583
commit 860e1fd0e4
4 changed files with 53 additions and 47 deletions

View File

@@ -1,34 +1,49 @@
// c
extern func puts(text: cstring) 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() r: u8
func test() g: u8
b: u8
a: u8
} }
struct Human : Test struct Vector2
{ {
name: cstring x: f32
y: f32
func print()
{
puts(this.name)
} }
func test() struct Vector3
{ {
puts("test") x: f32
} y: f32
z: f32
} }
func main(args: []cstring): i64 func main(args: []cstring): i64
{ {
let human: Human = struct { InitWindow(800, 600, "Hello from nub!")
name = "oliver"
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() CloseWindow()
human.test()
return 0 return 0
} }

View File

@@ -4,19 +4,18 @@ namespace NubLang.CLI;
public static class GCC public static class GCC
{ {
public static async Task<bool> Assemble(string asm, string objPath) public static async Task<bool> Assemble(string asmPath, string outPath)
{ {
var dir = Path.GetDirectoryName(objPath); var dir = Path.GetDirectoryName(outPath);
if (dir != null) if (dir != null)
{ {
Directory.CreateDirectory(dir); Directory.CreateDirectory(dir);
} }
using var process = new Process(); 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, UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true, RedirectStandardOutput = true,
RedirectStandardError = true, RedirectStandardError = true,
CreateNoWindow = true CreateNoWindow = true
@@ -24,9 +23,6 @@ public static class GCC
process.Start(); process.Start();
await process.StandardInput.WriteAsync(asm);
process.StandardInput.Close();
await process.WaitForExitAsync(); await process.WaitForExitAsync();
var errors = await process.StandardError.ReadToEndAsync(); var errors = await process.StandardError.ReadToEndAsync();
@@ -50,7 +46,6 @@ public static class GCC
process.StartInfo = new ProcessStartInfo("gcc", ["-nostartfiles", "-o", executablePath, ..objectFiles]) process.StartInfo = new ProcessStartInfo("gcc", ["-nostartfiles", "-o", executablePath, ..objectFiles])
{ {
UseShellExecute = false, UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true, RedirectStandardOutput = true,
RedirectStandardError = true, RedirectStandardError = true,
CreateNoWindow = true CreateNoWindow = true

View File

@@ -14,7 +14,8 @@ const string BIN_DIR = "bin";
const string INT_DIR = "bin-int"; const string INT_DIR = "bin-int";
var INT_BUILTIN_DIR = Path.Join(INT_DIR, "builtin"); var INT_BUILTIN_DIR = Path.Join(INT_DIR, "builtin");
var INT_OBJECT_DIR = Path.Join(INT_DIR, "obj"); 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)) if (Directory.Exists(INT_DIR))
{ {
@@ -24,7 +25,8 @@ if (Directory.Exists(INT_DIR))
Directory.CreateDirectory(BIN_DIR); Directory.CreateDirectory(BIN_DIR);
Directory.CreateDirectory(INT_BUILTIN_DIR); Directory.CreateDirectory(INT_BUILTIN_DIR);
Directory.CreateDirectory(INT_OBJECT_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(); var options = new Options();
@@ -55,7 +57,6 @@ for (var i = 0; i < args.Length; i++)
} }
else if (arg == "-c") else if (arg == "-c")
{ {
i++;
options.Link = false; options.Link = false;
} }
else else
@@ -123,24 +124,25 @@ for (var i = 0; i < typedSyntaxTrees.Count; i++)
var generator = new QBEGenerator(syntaxTree, typedDefinitionTable); var generator = new QBEGenerator(syntaxTree, typedDefinitionTable);
var ssa = generator.Emit(); var ssa = generator.Emit();
File.WriteAllText(Path.Join(INT_DEBUG_DIR, $"{outFileName}.ssa"), ssa);
var asm = await QBE.Invoke(ssa); var ssaPath = Path.Join(INT_SSA_DIR, $"{outFileName}.ssa");
if (asm == null) File.WriteAllText(ssaPath, ssa);
var asmFilePath = Path.Join(INT_ASM_DIR, $"{outFileName}.s");
var qbeSuccess = await QBE.Invoke(ssaPath, asmFilePath);
if (!qbeSuccess)
{ {
return 1; return 1;
} }
File.WriteAllText(Path.Join(INT_DEBUG_DIR, $"{outFileName}.s"), asm); var objFilePath = Path.Join(INT_OBJECT_DIR, $"{outFileName}.o");
var asmSuccess = await GCC.Assemble(asmFilePath, objFilePath);
var fileName = Path.Join(INT_OBJECT_DIR, $"{outFileName}.o");
var asmSuccess = await GCC.Assemble(asm, fileName);
if (!asmSuccess) if (!asmSuccess)
{ {
return 1; return 1;
} }
objectFiles.Add(fileName); objectFiles.Add(objFilePath);
} }
if (options.CustomRuntime == null) if (options.CustomRuntime == null)

View File

@@ -4,13 +4,12 @@ namespace NubLang.CLI;
public static class QBE public static class QBE
{ {
public static async Task<string?> Invoke(string ssa) public static async Task<bool> Invoke(string ssaPath, string outPath)
{ {
using var process = new Process(); using var process = new Process();
process.StartInfo = new ProcessStartInfo("qbe") process.StartInfo = new ProcessStartInfo("qbe", ["-o", outPath, ssaPath])
{ {
UseShellExecute = false, UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true, RedirectStandardOutput = true,
RedirectStandardError = true, RedirectStandardError = true,
CreateNoWindow = true CreateNoWindow = true
@@ -18,9 +17,6 @@ public static class QBE
process.Start(); process.Start();
await process.StandardInput.WriteAsync(ssa);
process.StandardInput.Close();
await process.WaitForExitAsync(); await process.WaitForExitAsync();
var errors = await process.StandardError.ReadToEndAsync(); var errors = await process.StandardError.ReadToEndAsync();
@@ -29,8 +25,6 @@ public static class QBE
await Console.Error.WriteLineAsync("qbe error:\n" + errors); await Console.Error.WriteLineAsync("qbe error:\n" + errors);
} }
var asm = await process.StandardOutput.ReadToEndAsync(); return process.ExitCode == 0;
return process.ExitCode == 0 ? asm : null;
} }
} }