21 lines
775 B
Plaintext
21 lines
775 B
Plaintext
import std::io;
|
|
type op = def(i64, i64): i64;
|
|
def add(a: i64, b: i64): i64 { return a + b; }
|
|
def mul(a: i64, b: i64): i64 { return a * b; }
|
|
def twice<T>(x: T): T where T: numeric { return x + x; }
|
|
struct entry { val name: span<u8>; val run: op; }
|
|
const ops: entry[3] = [entry{ name = "add", run = add }, entry{ name = "mul", run = mul },
|
|
entry{ name = "sub", run = def(a: i64, b: i64): i64 { return a - b; } }];
|
|
const dbl: def(i64): i64 = twice::<i64>;
|
|
def made(): span<op> {
|
|
val table := raw_allocate(2 * size_of::<op>()) as *mut op;
|
|
table[0] = mul;
|
|
table[1] = add;
|
|
return table[0:2];
|
|
}
|
|
const kept: span<op> = made();
|
|
def main(): void {
|
|
for &e in ops { print($"{e.name}={e.run(6, 3)} "); }
|
|
println($"{dbl(21)} {kept[0](4, 5)} {kept[1](4, 5)} {ops[1].run}");
|
|
}
|