aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/ack.cff15
-rw-r--r--examples/libc.hff32
2 files changed, 47 insertions, 0 deletions
diff --git a/examples/ack.cff b/examples/ack.cff
new file mode 100644
index 0000000..233abc2
--- /dev/null
+++ b/examples/ack.cff
@@ -0,0 +1,15 @@
+import "libc.hff";
+
+fn ack(m uint, n uint) uint {
+ if m == 0 { return n + 1; }
+ if n == 0 { return ack(m - 1, 1); }
+ return ack(m - 1, ack(m, n - 1));
+}
+
+extern fn main() void {
+ for let m = 0; m <= 4; ++m {
+ for let n = 0; n < 6 - m; ++n {
+ printf("A(%u,%u) = %u\n", m, n, ack(m, n));
+ }
+ }
+}
diff --git a/examples/libc.hff b/examples/libc.hff
new file mode 100644
index 0000000..488a495
--- /dev/null
+++ b/examples/libc.hff
@@ -0,0 +1,32 @@
+// stdio.h
+struct FILE;
+extern static stdin *FILE,
+ stdout *FILE,
+ stderr *FILE;
+extern fn printf(fmt *const u8, ...) int;
+extern fn fprintf(fp *FILE, fmt *const u8, ...) int;
+extern fn sprintf(*u8, fmt *const u8, ...) int;
+extern fn snprintf(*u8, usize, fmt *const u8, ...) int;
+extern fn fopen(path *const u8, mode *const u8) *FILE;
+extern fn fclose(*FILE) int;
+extern fn fgetc(*FILE) int;
+extern fn fputc(int, *FILE) int;
+def EOF = -1;
+
+// stdlib.h
+extern fn abort() void;
+extern fn exit(c int) void;
+extern fn perror(s *const u8) void;
+extern fn malloc(n usize) *void;
+extern fn calloc(n usize, m usize) *void;
+extern fn realloc(p *void, n usize) *void;
+extern fn free(p *void) void;
+
+// string.h
+extern fn strlen(s *const u8) usize;
+extern fn strcmp(a *const u8, b *const u8) int;
+extern fn memcpy(*void, *const void, usize) *void;
+extern fn strcpy(*u8, *const u8) *u8;
+
+//ctype.h
+extern fn tolower(int) int;