1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
defmacro MAX(x, y) [ ((x) < (y) ? (y) : (x)) ]
defmacro fmt(fmt, ...args) [ printf(fmt, args) ]
defmacro add {
(x) [ (x) ],
(x, y, ...rest) [ (x) + add(y, rest) ]
}
defmacro swap(x, y) [{
let $x = &(x);
let $y = &(y);
let $z = *($x);
*($x) = *($y);
*($y) = ($z);
}]
fn fact(x usize) usize {
fn f(acc usize, n usize) usize {
return n == 0 ? acc : f(acc * n, n - 1);
}
return f(1, x);
}
extern fn main (argc int, argv **u8) int {
extern fn printf(fmt *const u8, ...) int;
fmt("%d\n", add(1, 2, 3, 4));
let x = 0;
let y = 7;
printf("x: %d; y: %d\n", x, y);
swap(x, y);
printf("x: %d; y: %d\n", x, y);
printf("fact(6) = %zu\n", fact(6));
fn foo(n int) int {
return n + 1;
}
return foo(-1);
}
|