event pump

○ Planned This feature is planned but not yet implemented.

Optional branches feature not fully implemented - see 918_optional_branches/IMPLEMENTATION_ROADMAP.md

Code

// Test 918h: Optional Branches - Event Pump Loop Pattern (THE USE CASE)
//
// This test demonstrates:
// 1. Event pump with multiple optional event types
// 2. Loop using labels/jumps that repeatedly calls pump
// 3. Handler selectively handles some events with |? for rest
// 4. Loop continues even when unhandled optional events fire
//
// This is THE motivating use case: wrapping WIN32/SDL/etc event systems

const std = @import("std");

// Simulated event pump with multiple optional event types
// In real world: this might be WIN32 events, SDL events, etc.
~event pump { iteration: u32 }
| ?mouse_event { code: i32 }
| ?keyboard_event { code: i32 }
| ?window_event { code: i32 }
| ?timer_event { code: i32 }
| ?quit {}

// Proc deterministically returns different events based on iteration
// This simulates a real event pump that returns different events
~proc pump {
    const i = iteration;

    // After 8 iterations, quit
    if (i >= 8) {
        return .{ .quit = .{} };
    }

    // Pattern: keyboard, mouse, window, timer, keyboard, ...
    const event_type = i % 4;

    if (event_type == 0) {
        return .{ .keyboard_event = .{ .code = @intCast(i) } };
    } else if (event_type == 1) {
        return .{ .mouse_event = .{ .code = @intCast(i) } };
    } else if (event_type == 2) {
        return .{ .window_event = .{ .code = @intCast(i) } };
    } else {
        return .{ .timer_event = .{ .code = @intCast(i) } };
    }
}

// Main event loop using label/jump - THE USE CASE
// We only care about keyboard and mouse events
// We use |? to catch window, timer, and other events
// Loop MUST continue even when unhandled events fire

~#pump_loop pump(iteration: 0)
| keyboard_event k |> std.debug.print("Keyboard: {}\n", .{k.code}) |> @pump_loop(iteration: iteration + 1)
| mouse_event m |> std.debug.print("Mouse: {}\n", .{m.code}) |> @pump_loop(iteration: iteration + 1)
| quit |> _
|? |> std.debug.print("Other event (ignored)\n", .{}) |> @pump_loop(iteration: iteration + 1)

// CRITICAL: All 8 iterations complete successfully
// Iterations 2, 3, 6, 7 return window_event or timer_event
// These are caught by |? and execution continues with jump to @pump_loop
// Without |?, execution would stop on iteration 2
input.kz

Expected Output

Keyboard: 0
Mouse: 1
Other event (ignored)
Other event (ignored)
Keyboard: 4
Mouse: 5
Other event (ignored)
Other event (ignored)

Test Configuration

MUST_RUN