diff options
| author | 2022-08-03 20:24:47 +0200 | |
|---|---|---|
| committer | 2022-08-03 20:24:47 +0200 | |
| commit | 1625c50f0c0e4b1c7ba01a5df5713efaf6dce606 (patch) | |
| tree | bc5f24811413749b776964c1bbdec13a46dd9768 /bootstrap/test.cff | |
initial
Diffstat (limited to 'bootstrap/test.cff')
| -rw-r--r-- | bootstrap/test.cff | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/bootstrap/test.cff b/bootstrap/test.cff new file mode 100644 index 0000000..7749345 --- /dev/null +++ b/bootstrap/test.cff @@ -0,0 +1,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); +} |