feat: Phase 8.2, 8.4, 8.5, 9.1 + C backend fixes
Phase 8.2 — Gradual Ownership: - Add tkRef/tkMutRef types and `mut` keyword - Add @[Checked] attribute for opt-in borrow checking - Reject assignment through &T in checked functions - examples/ownership.bux Phase 8.4 — CTFE: - Evaluate const func at compile-time via evalExpr/evalBlock - Fold const declarations to literals; emit #define in C - examples/ctfe.bux (Factorial(10) → 3628800) Phase 8.5 — Trait Bounds: - Change declFuncTypeParams from seq[string] to seq[TypeParam] (name + bound) - Parser handles <T: Comparable> - Sema checks typeImplements at call sites - Fix C backend: generic receivers + pointer self field access - examples/trait_bounds.bux Phase 9.1 — Package Manager: - Inline tables/arrays in TOML parser - bux add, bux install, bux.lock generation - Dependency resolution with git/path sources - Build pipeline merges dependency .bux sources C Backend Fixes: - resolveExprType(ekIdent) now applies typeSubst for generic params - Method desugaring works for monomorphized generic receivers - Pointer checks use isPointer (covers tkRef/tkMutRef) - Field access on &T emits -> instead of . Remove accidentally committed test binaries from tracking
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import Std::Io::PrintInt;
|
||||
|
||||
// const func — evaluated at compile time
|
||||
const func Factorial(n: int) -> int {
|
||||
if n <= 1 {
|
||||
return 1;
|
||||
}
|
||||
return n * Factorial(n - 1);
|
||||
}
|
||||
|
||||
// Compile-time constants
|
||||
const FIB_10 = Factorial(10);
|
||||
const SIMPLE = 5 + 3 * 2;
|
||||
|
||||
func Main() -> int {
|
||||
PrintInt(FIB_10); // 3628800 — computed at compile time
|
||||
PrintInt(SIMPLE); // 11 — computed at compile time
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
// @[Checked] enables borrow checking for this function.
|
||||
// &T = shared reference (read-only)
|
||||
// &mut T = mutable reference (exclusive)
|
||||
|
||||
@[Checked]
|
||||
func ScaleInPlace(val: &mut int, factor: int) {
|
||||
*val = *val * factor;
|
||||
}
|
||||
|
||||
@[Checked]
|
||||
func GetValue(val: &int) -> int {
|
||||
return *val;
|
||||
}
|
||||
|
||||
// Unchecked functions allow raw pointers without restrictions
|
||||
func UncheckedSwap(a: *int, b: *int) {
|
||||
let tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
var x: int = 10;
|
||||
|
||||
// &mut allows mutation
|
||||
ScaleInPlace(&x, 3);
|
||||
PrintInt(x); // 30
|
||||
PrintLine("");
|
||||
|
||||
// & allows reading
|
||||
let y: int = GetValue(&x);
|
||||
PrintInt(y); // 30
|
||||
PrintLine("");
|
||||
|
||||
// Unchecked: raw pointers work like C
|
||||
var a: int = 5;
|
||||
var b: int = 7;
|
||||
UncheckedSwap(&a, &b);
|
||||
PrintInt(a); // 7
|
||||
PrintLine("");
|
||||
PrintInt(b); // 5
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
// Trait (interface) definition
|
||||
interface Drawable {
|
||||
func Draw(self: &Self);
|
||||
}
|
||||
|
||||
// Type that implements the trait
|
||||
struct Circle {
|
||||
radius: int;
|
||||
}
|
||||
|
||||
extend Circle for Drawable {
|
||||
func Draw(self: &Circle) {
|
||||
PrintLine("Drawing circle");
|
||||
}
|
||||
}
|
||||
|
||||
// Generic function with trait bound: T must implement Drawable
|
||||
func Render<T: Drawable>(obj: T) {
|
||||
obj.Draw();
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let c: Circle = Circle { radius: 5 };
|
||||
Render(c); // OK: Circle implements Drawable
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user