feat: capture-less anonymous functions (closures)

Implement MVP closures — anonymous functions without captures.

Syntax:
  |a: int, b: int| -> int { return a + b; }

Changes:
- ast.bux + ast.nim: add ekClosure AST node
- parser.bux + parser.nim: parse |params| -> Ret { body }
- sema.bux + sema.nim: type-check closure params/body, return tyFunc
- hir_lower.bux + hir_lower.nim: generate __closure_N function + hAddrOf
- lir_c_backend.nim: fix function-pointer variable declaration (cParamDecl)
- C backend: closures compile to global functions with unique names

Test: _test_closure/src/Main.bux
- Closure as variable
- Closure passed to higher-order function
- Address of named function as function pointer

Both bootstrap and selfhost compilers build and pass the test.
This commit is contained in:
2026-06-09 20:24:10 +03:00
parent 34504d1647
commit c83f6d5994
16 changed files with 2290 additions and 8 deletions
+67
View File
@@ -0,0 +1,67 @@
// Generated by Bux C Backend v2
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef const char* String;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
typedef signed char int8;
typedef short int16;
typedef long long int64;
typedef float float32;
typedef double float64;
typedef char char8;
void* bux_alloc(unsigned int size);
void bux_free(void* ptr);
int Apply(int x, int (*op)(int));
int Double(int x);
int __closure_2(int a, int b);
int __closure_3(int x);
int main();
void PrintInt(int x);
void PrintLine(String msg);
int Apply(int x, int (*op)(int)) {
return op(x);
}
int Double(int x) {
return x * 2;
}
int __closure_2(int a, int b) {
return a + b;
}
int __closure_3(int x) {
return x * 3;
}
int main() {
int (*add)(int, int) = &__closure_2;
int sum = add(3, 4);
PrintInt(sum);
PrintLine("");
int result = Apply(5, &__closure_3);
PrintInt(result);
PrintLine("");
int (*d)(int) = &Double;
PrintInt(d(7));
PrintLine("");
return 0;
}