aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2022-08-24 14:46:36 +0200
committerlemon <lsof@mailbox.org>2022-08-24 14:46:36 +0200
commit9f320002800b0a8b7601528334f97ba64182bdd6 (patch)
treeeef77343a2ffbd74916d305610ff1e758467840c /examples
parent53fcd5e1647fb56511bbdd98925dd38a84fd7248 (diff)
more llvm
Diffstat (limited to 'examples')
-rw-r--r--examples/life.cff79
1 files changed, 79 insertions, 0 deletions
diff --git a/examples/life.cff b/examples/life.cff
new file mode 100644
index 0000000..cd1cc2e
--- /dev/null
+++ b/examples/life.cff
@@ -0,0 +1,79 @@
+import "libc.hff";
+
+def W = 60, H = 40;
+
+typedef Board [(W * H)/8]u8;
+static board Board = {};
+
+fn get(b *Board, x uint, y uint) bool {
+ x %= W; y %= H;
+ let idx = x + (y * W);
+ return (*b)[idx / 8] & (1 << (idx % 8)) != 0;
+}
+
+fn set(b *Board, x uint, y uint, set bool) void {
+ x %= W; y %= H;
+ let idx = x + (y * W);
+ (*b)[idx / 8] &= ~(1 << (idx % 8));
+ if set {
+ (*b)[idx / 8] |= (1 << (idx % 8));
+ }
+}
+
+fn next(b *Board) void {
+ let temp Board = *b;
+ for let y = 0; y < H; ++y {
+ for let x = 0; x < W; ++x {
+ defmacro C(ox,oy) [ (as(int)get(&temp, x + ox, y + oy)) ]
+ let n = C(-1,-1) + C(0,-1) + C(1,-1)
+ + C(-1, 0) + C(1, 0)
+ + C(-1, 1) + C(0, 1) + C(1, 1);
+
+ set(b, x, y, #f);
+ if get(&temp, x, y) {
+ set(b, x, y, n == 3);
+ } else if n == 2 {
+ set(b, x, y, #t);
+ } else if n == 3 {
+ set(b, x, y, #t);
+ }
+ }
+ }
+}
+
+fn draw(b *Board) void {
+ printf("\x1B[H\n");
+ printf("\n");
+ for let y = 0; y < H; ++y {
+ printf(" ");
+ for let x = 0; x < W; ++x {
+ if get(b, x, y) {
+ printf("W");
+ } else {
+ printf(" ");
+ }
+ }
+ printf(" \n");
+ }
+}
+
+extern fn time(*void) int;
+fn init(b *Board) void {
+ let rnd u32 = time(#null);
+ for let y = 0; y < H; ++y {
+ for let x = 0; x < W; ++x {
+ set(b, x, y, as(bool)((rnd >> 31) & 1));
+ rnd = (rnd * 134775813) + 1;
+ }
+ }
+}
+
+extern fn usleep(u32) void;
+extern fn main() void {
+ init(&board);
+ for ;; {
+ draw(&board);
+ usleep(100);
+ next(&board);
+ }
+}