Skip to content

Instantly share code, notes, and snippets.

@CraigglesO
Last active April 11, 2023 17:30
Show Gist options
  • Save CraigglesO/8a360eb3b42b615b12d73678c2fd84bf to your computer and use it in GitHub Desktop.
Save CraigglesO/8a360eb3b42b615b12d73678c2fd84bf to your computer and use it in GitHub Desktop.
Man Boy Test Written in Zig
const std = @import("std");
const Frame = struct {
f: *const fn (frame: *Frame) i32,
k: *i32 = undefined,
x1: *Frame = undefined,
x2: *Frame = undefined,
x3: *Frame = undefined,
x4: *Frame = undefined,
x5: *Frame = undefined,
pub fn next(f: *Frame, k: *i32, x1: *Frame, x2: *Frame, x3: *Frame, x4: *Frame, x5: *Frame) *Frame {
f.k = k;
f.x1 = x1;
f.x2 = x2;
f.x3 = x3;
f.x4 = x4;
f.x5 = x5;
return f;
}
};
fn F(a: *Frame) i32 { return a.k.*; }
fn eval(a: *Frame) i32 { return a.f(a); }
fn B(a: *Frame) i32 {
a.k.* -= 1;
var k: i32 = a.k.*;
var b = Frame{ .f = B };
return A(Frame.next(&b, &k, a, a.x1, a.x2, a.x3, a.x4));
}
fn A(a: *Frame) i32 {
if (a.k.* <= 0) { return eval(a.x4) + eval(a.x5); }
else { return B(a); }
}
pub fn main() !void {
var k: i32 = 10;
var zero: i32 = 0;
var one: i32 = 1;
var negOne: i32 = -1;
var a = Frame{ .f = B };
var f1 = Frame{ .f = F, .k = &one };
var f0 = Frame{ .f = F, .k = &zero };
var fn1 = Frame{ .f = F, .k = &negOne };
std.debug.print("{}\n", .{ A(Frame.next(&a, &k, &f1, &fn1, &fn1, &f1, &f0)) });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment