feat: add support for structs and member access
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
[code]
|
||||
foreign fn putchar(c: i32) -> i32;
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32
|
||||
}
|
||||
|
||||
fn print_num(n: i32) {
|
||||
// Simple hack to print a 2-digit number for testing
|
||||
putchar(48 + (n / 10));
|
||||
putchar(48 + (n % 10));
|
||||
putchar(10); // newline
|
||||
}
|
||||
|
||||
fn main() -> i32 {
|
||||
let p = Point { x: 40, y: 2 };
|
||||
|
||||
// 40 + 2 = 42
|
||||
print_num(p.x + p.y);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
[expected_return_code]
|
||||
0
|
||||
|
||||
[expected_output]
|
||||
42
|
||||
@@ -0,0 +1,38 @@
|
||||
[code]
|
||||
foreign fn putchar(c: i32) -> i32;
|
||||
|
||||
struct Rect {
|
||||
width: i32,
|
||||
height: i32
|
||||
}
|
||||
|
||||
fn modify_rect(r: *Rect) {
|
||||
let temp: Rect = *r;
|
||||
temp.width = temp.width + 10;
|
||||
temp.height = temp.height + 20;
|
||||
*r = temp;
|
||||
}
|
||||
|
||||
fn print_num(n: i32) {
|
||||
putchar(48 + (n / 10));
|
||||
putchar(48 + (n % 10));
|
||||
putchar(10);
|
||||
}
|
||||
|
||||
fn main() -> i32 {
|
||||
let r = Rect { width: 15, height: 25 };
|
||||
|
||||
modify_rect(&r);
|
||||
|
||||
print_num(r.width);
|
||||
print_num(r.height);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
[expected_return_code]
|
||||
0
|
||||
|
||||
[expected_output]
|
||||
25
|
||||
45
|
||||
Reference in New Issue
Block a user