From 6b7cd0132dc4b944e1b3e14a878bb6ff46eb8a4e Mon Sep 17 00:00:00 2001 From: nub31 Date: Thu, 16 Oct 2025 23:36:06 +0200 Subject: [PATCH] Begin to add support for header file generation --- compiler/NubLang.CLI/Program.cs | 72 ++++++++++++++++++++++++++++++++- examples/raylib/makefile | 7 +++- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/compiler/NubLang.CLI/Program.cs b/compiler/NubLang.CLI/Program.cs index 94ca549..b952f2a 100644 --- a/compiler/NubLang.CLI/Program.cs +++ b/compiler/NubLang.CLI/Program.cs @@ -5,10 +5,78 @@ using NubLang.Generation; using NubLang.Modules; using NubLang.Syntax; -var diagnostics = new List(); +List files = []; +List headerFiles = []; +for (var i = 0; i < args.Length; i++) +{ + if (args[i] == "-h") + { + i += 1; + if (i > args.Length - 1) + { + Console.Error.WriteLine("Expected argument after -h"); + } + + headerFiles.Add(args[i]); + } + else + { + files.Add(args[i]); + } +} + +var diagnostics = new List(); var syntaxTrees = new List(); -foreach (var file in args) + +foreach (var headerFile in headerFiles) +{ + using var bindingsGenerateProc = new Process(); + // todo(nub31): Embed python file to remove absolute path + bindingsGenerateProc.StartInfo = new ProcessStartInfo("python3", ["/home/oliste/repos/nub-lang/bindings/generate.py", headerFile]) + { + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true, + UseShellExecute = false, + }; + + bindingsGenerateProc.Start(); + + var output = await bindingsGenerateProc.StandardOutput.ReadToEndAsync(); + var error = await bindingsGenerateProc.StandardError.ReadToEndAsync(); + + await bindingsGenerateProc.WaitForExitAsync(); + + if (bindingsGenerateProc.ExitCode != 0) + { + Console.Error.WriteLine($"Failed to automatically generate bindings for header file {headerFile}\n{error}"); + return 1; + } + + var tokenizer = new Tokenizer(headerFile, output); + tokenizer.Tokenize(); + diagnostics.AddRange(tokenizer.Diagnostics); + + if (tokenizer.Diagnostics.Any(x => x.Severity == DiagnosticSeverity.Error)) + { + Console.Error.WriteLine($"Binding generator produced invalid syntax for header file {headerFile}"); + return 1; + } + + var parser = new Parser(); + var syntaxTree = parser.Parse(tokenizer.Tokens); + + if (parser.Diagnostics.Any(x => x.Severity == DiagnosticSeverity.Error)) + { + Console.Error.WriteLine($"Binding generator produced invalid syntax tree for header file {headerFile}"); + return 1; + } + + syntaxTrees.Add(syntaxTree); +} + +foreach (var file in files) { var tokenizer = new Tokenizer(file, File.ReadAllText(file)); tokenizer.Tokenize(); diff --git a/examples/raylib/makefile b/examples/raylib/makefile index 88c9aa2..213489f 100644 --- a/examples/raylib/makefile +++ b/examples/raylib/makefile @@ -3,8 +3,11 @@ NUBC = ../../compiler/NubLang.CLI/bin/Debug/net9.0/nubc out: .build/out.o gcc -nostartfiles -lm -o out x86_64.s .build/out.o raylib-5.5_linux_amd64/lib/libraylib.a -.build/out.o: $(NUBC) main.nub raylib.nub - $(NUBC) main.nub raylib.nub +# .build/out.o: $(NUBC) main.nub raylib.nub +# $(NUBC) main.nub raylib.nub + +.build/out.o: $(NUBC) main.nub + $(NUBC) main.nub -h raylib-5.5_linux_amd64/include/raylib.h .PHONY: $(NUBC) $(NUBC):