025 interpreter benchmark real

✓ Passing This code compiles and runs correctly.

Code

// REAL Interpreter Benchmark
// Tests the FULL pipeline: parse Koru flow string → execute → return result
// Uses a realistic multi-step flow with continuations

~import "$std/runtime"
~import "$std/interpreter"

const std = @import("std");

// ============================================================================
// Events that the flow will dispatch to
// ============================================================================

~pub event compute { a: []const u8, b: []const u8, op: []const u8 }
| result { value: []const u8 }
| error { message: []const u8 }

~proc compute {
    const a_val = std.fmt.parseInt(i64, a, 10) catch 0;
    const b_val = std.fmt.parseInt(i64, b, 10) catch 0;

    const value = if (std.mem.eql(u8, op, "add"))
        a_val + b_val
    else if (std.mem.eql(u8, op, "mul"))
        a_val * b_val
    else if (std.mem.eql(u8, op, "sub"))
        a_val - b_val
    else
        0;

    var buf: [32]u8 = undefined;
    const result_str = std.fmt.bufPrint(&buf, "{d}", .{value}) catch "0";
    return .{ .result = .{ .value = result_str } };
}

~pub event print_result { value: []const u8 }
| printed {}

~proc print_result {
    std.debug.print("Result: {s}\n", .{value});
    return .{ .printed = .{} };
}

// Register events for runtime dispatch
~std.runtime:register(scope: "bench") {
    compute
    print_result
}

// ============================================================================
// The flow we'll parse and execute at runtime
// This is a REAL flow with continuations
// ============================================================================

const FLOW_SOURCE =
    \\~compute(a: "42", b: "17", op: "add")
    \\| result r |> print_result(value: r.value)
    \\| error e |> print_result(value: e.message)
;

~import "$std/io"

// ============================================================================
// Benchmark: Parse and execute the flow
// ============================================================================

~std.interpreter:run(source: FLOW_SOURCE, dispatcher: dispatch_bench)
| result r |> std.io:print.ln("Success: branch {{ r.value.branch:s }}")
| exhausted e |> std.io:print.ln("Exhausted: {{ e.last_event }}")
| parse_error e |> std.io:print.ln("Error: {{ e.message }}")
| validation_error e |> std.io:print.ln("Error: {{ e.message }}")
| dispatch_error e |> std.io:print.ln("Error: {{ e.message }}")
input.kz