feat(selfhost): add 'bux fmt' code formatter

Line-based indentation formatter that:
- Adjusts indentation based on { } brace depth (4 spaces)
- Preserves original line structure and comments
- Skips braces inside string/char literals and // comments
- Handles } at line start and mixed brace lines
- 'bux fmt <file>' formats a single file
- 'bux fmt <dir>' formats all .bux files in directory
This commit is contained in:
2026-06-09 17:48:20 +03:00
parent 3ce7ba51d2
commit 079b76ae71
2 changed files with 184 additions and 2 deletions
+46 -2
View File
@@ -609,6 +609,44 @@ func Cli_Init() -> int {
// Test command — build and run tests
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Fmt command — format source files
// ---------------------------------------------------------------------------
func Cli_Fmt(dir: String) -> int {
// If dir is a file, format just that file
if FileExists(dir) {
Print("Formatting ");
PrintLine(dir);
if Fmt_FormatFile(dir) {
PrintLine(" OK");
return 0;
}
return 1;
}
// Otherwise format all .bux files in directory
Print("Formatting ");
PrintLine(dir);
var fileCount: int = 0;
let files: *String = bux_list_dir(dir, ".bux", &fileCount);
if fileCount == 0 {
PrintLine("No .bux files found");
return 1;
}
var i: int = 0;
var okCount: int = 0;
while i < fileCount {
Print(" ");
PrintLine(files[i]);
if Fmt_FormatFile(files[i]) {
okCount = okCount + 1;
}
i = i + 1;
}
Print("Formatted "); PrintInt(okCount as int64); Print("/"); PrintInt(fileCount as int64); PrintLine(" files");
return 0;
}
func Cli_Test(projectDir: String) -> int {
Print("Testing project: ");
PrintLine(projectDir);
@@ -906,7 +944,7 @@ func Cli_Run(args: *String, argCount: int) -> int {
if argCount < 2 {
PrintLine("Bux Self-Hosting Compiler v0.2.0");
PrintLine("Usage: buxc <command> [args]");
PrintLine("Commands: build, check, new, init, test, run, project, help, version");
PrintLine("Commands: build, check, new, init, fmt, test, run, project, help, version");
return 0;
}
@@ -919,7 +957,7 @@ func Cli_Run(args: *String, argCount: int) -> int {
if String_Eq(cmd, "help") || String_Eq(cmd, "--help") || String_Eq(cmd, "-h") {
PrintLine("Bux Self-Hosting Compiler v0.2.0");
PrintLine("Usage: buxc <command> [args]");
PrintLine("Commands: build, check, new, init, test, run, project, help, version");
PrintLine("Commands: build, check, new, init, fmt, test, run, project, help, version");
PrintLine("Pipeline modules:");
PrintLine(" Lexer ✅ 695 lines");
PrintLine(" Parser ✅ 1004 lines");
@@ -942,6 +980,12 @@ func Cli_Run(args: *String, argCount: int) -> int {
return Cli_Init();
}
if String_Eq(cmd, "fmt") {
let dir: String = ".";
if argCount >= 3 { dir = args[2]; }
return Cli_Fmt(dir);
}
if String_Eq(cmd, "check") {
if argCount < 3 {
PrintLine("Usage: buxc check <file.bux>");