feat: add HIR, C backend, and end-to-end compilation

- Phase 3: High-Level IR (HIR) with lowering from AST
  - Method call desugaring (obj.method() → Type_method(obj))
  - if/else, while, loop, break, continue lowering
  - struct, enum, function lowering
  - 8 HIR tests passing

- Phase 5A: C backend code generation
  - Type mapping (Bux types → C11 types)
  - Expression and statement emission
  - Struct, enum, function generation
  - C main() wrapper for Bux Main()

- Runtime shim (stdlib/runtime.c)
  - bux_alloc, bux_free, bux_print, bux_panic
  - BuxString, BuxSlice types
  - Bounds checking, division by zero

- Build integration
  - bux build: lex → parse → sema → HIR → C → cc
  - bux run: build + execute
  - bux clean: remove build directory

- Parser fixes
  - Newline handling in struct, enum, extend, interface blocks
  - self keyword as expression and parameter name

- Sema improvements
  - Method resolution (extend blocks)
  - Interface conformance checking
  - collectGlobals made public

- All 70 tests passing (25 lexer + 16 parser + 21 sema + 8 HIR)
- End-to-end: Bux programs compile to native ELF64 binaries
This commit is contained in:
2026-05-30 22:40:34 +03:00
parent 8e637c89e7
commit 8e74215378
15 changed files with 2074 additions and 78 deletions
+54
View File
@@ -83,3 +83,57 @@ suite "Sema":
test "slice element type mismatch":
let res = checkSource("func Main() -> int { let arr = [1, c8\"a\"]; return 0; }")
check res.hasErrors
test "method call with extend":
let src = """
struct Point { x: float64; y: float64; }
extend Point {
func Distance(self: Point) -> float64 { return 0.0; }
}
func Main() -> int {
let p = Point { x: 1.0, y: 2.0 };
let d = p.Distance();
return 0;
}
"""
let res = checkSource(src)
check not res.hasErrors
test "method call with wrong arguments":
let src = """
struct Point { x: float64; y: float64; }
extend Point {
func Add(self: Point, other: Point) -> Point { return self; }
}
func Main() -> int {
let p = Point { x: 1.0, y: 2.0 };
let q = p.Add();
return 0;
}
"""
let res = checkSource(src)
check res.hasErrors
check "too few arguments" in res.diagnostics[0].message
test "interface declaration":
let src = """
interface Display {
func ToString(self: Self) -> String;
}
"""
let res = checkSource(src)
check not res.hasErrors
test "extend for interface":
let src = """
struct Point { x: float64; y: float64; }
interface Display {
func ToString(self: Self) -> String;
}
extend Point for Display {
func ToString(self: Point) -> String { return c8"Point"; }
}
func Main() -> int { return 0; }
"""
let res = checkSource(src)
check not res.hasErrors