Fix bugs in cli
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -4,19 +4,18 @@ namespace NubLang.CLI;
|
||||
|
||||
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)
|
||||
{
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -4,13 +4,12 @@ namespace NubLang.CLI;
|
||||
|
||||
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();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user