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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
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) [
(do
let $x = &(x);
let $y = &(y);
let $z = *$x;
*$x = *$y;
*$y = $z;)
]
defmacro map {
(f, x) [ f(x) ],
(f, x, ...rest) [ f(x) map(f, rest) ]
}
defmacro printints_s(x) [ "%d " ## ]
defmacro printints(...rest) [ printf(map(printints_s, rest) "\n", rest) ]
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);
}
defmacro lambda(tys, body) [
(do
fn $lam tys body
&$lam;)
]
fn counter() int {
static xs int = 0;
extern static glob int;
glob;
return xs++;
}
extern static glob int = 42;
extern fn main (argc int, argv **u8) void {
extern fn printf(fmt *const u8, ...) int;
fmt("%d\n", add(1, 2, 3, 4));
let x = 0;
let y = 7;
switch y {
case 0, 1 do
printf("wow\n");
case else
printf("p\n");
}
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));
printints(1, 7, 8 + 9, x, x / (++y ^ 2));
let z = (do
printf("hi");
x + 1;);
let fo = lambda((x int) void, {});
return (*fo)(0);
}
|