feat: in/out 参数 — out 关键字引用传递
fn swap(out x: i64, out y: i64) 声明 out 参数,codegen 层面 函数签名变为 T* 指针,调用点自动传 &variable 地址。 in 是默认行为(值传递),无需显式标注。 Token → Parser → Sema → Codegen 全流水线: - TOK_OUT + "out" 关键字注册 - AST parameter.is_out 字段 - parse_function 解析 out 前缀 - Sema: out 参数注册为 SYM_VARIABLE+is_mut(可赋值) - Codegen: LLVM 函数签名使用 T*,调用点传 alloca Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
// out 参数测试 — 引用传递
|
||||
fn swap(out x: i64, out y: i64) -> void {
|
||||
let t = x;
|
||||
x = y;
|
||||
y = t;
|
||||
}
|
||||
|
||||
fn increment(out x: i64) -> void {
|
||||
x = x + 1;
|
||||
}
|
||||
|
||||
fn main() -> void {
|
||||
let a = 10;
|
||||
let b = 20;
|
||||
print_i64(a);
|
||||
print_i64(b);
|
||||
swap(a, b);
|
||||
print_i64(a);
|
||||
print_i64(b);
|
||||
increment(a);
|
||||
print_i64(a);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// out 参数 + 结构体测试
|
||||
struct Point { x: i64, y: i64 }
|
||||
|
||||
fn init_point(out p: Point) -> void {
|
||||
p = Point { x: 100, y: 200 };
|
||||
}
|
||||
|
||||
fn offset_point(out p: Point) -> void {
|
||||
p = Point { x: p.x + 50, y: p.y + 100 };
|
||||
}
|
||||
|
||||
fn main() -> void {
|
||||
let p = Point { x: 0, y: 0 };
|
||||
print_i64(p.x);
|
||||
print_i64(p.y);
|
||||
init_point(p);
|
||||
print_i64(p.x);
|
||||
print_i64(p.y);
|
||||
offset_point(p);
|
||||
print_i64(p.x);
|
||||
print_i64(p.y);
|
||||
}
|
||||
Reference in New Issue
Block a user