module scheme @[heap] struct IterationState { callback Value collect bool mut: persistent bool remaining int tails []Value prefix Value = nil_value() suffix Value = nil_value() tail &Pair = unsafe { nil } } struct MapCallbackEntry {} fn (mut vm VM) install_iteration_primitives() { for name in ['map', 'for-each'] { function := &Function{ code: unsafe { nil } params: 2 rest: true slot_count: 4 instructions: [ bridge_instruction(.map_init, if name == 'map' { 1 } else { 0 }, 0), bridge_instruction(.map_step, 0, 0), bridge_instruction(.map_collect, 0, 0), bridge_instruction(.jump, 1, 0), bridge_instruction(.ret, 0, 0), ] } vm.bind_global(name, procedure_value(&Procedure{ mode: .closure, name: name, owner: vm.owner, function: function })) } } fn iteration_state(value Value) &IterationState { // Only the private iteration bridge reads this slot, after map_init has // installed the matching payload. It is never a user-provided value. return unsafe { &IterationState(value.pointer()) } } fn (mut m Machine) start_iteration(collect bool) ! { callback := m.env.slots[0] if !callback.is_callable() { return failure('map/for-each needs a procedure') } // This rest-argument spine is made by the VM, not supplied by the user. mut others := m.env.slots[2] mut count := 1 for others.tag == .pair { count++ others = others.pair_pointer().cdr } mut tails := value_storage(count) container_store_validated(mut tails, 0, m.env.slots[1]) others = m.env.slots[2] for i in 1 .. count { pair := others.pair_pointer() container_store_validated(mut tails, i, pair.car) others = pair.cdr } mut length := -1 for value in tails { n := list_iteration_limit(value)! if n >= 0 && (length < 0 || n < length) { length = n } } if length < 0 { return failure('map/for-each needs at least one finite list') } m.env.slots[3] = payload_value(.iteration, &IterationState{ callback: callback, collect: collect, remaining: length, tails: tails }) // Keep only the advancing tails, not the original heads or argument spine. for i in 0 .. 3 { m.env.slots[i] = unspecified() } } fn (mut m Machine) step_iteration(mut vm VM) ! { mut state := iteration_state(m.env.slots[3]) if state.remaining == 0 { m.finish_call(if state.collect { state.iteration_result() } else { unspecified() }) return } m.push(state.callback) for i, tail in state.tails { // Callbacks can mutate input data after the initial proper-list check. if tail.tag != .pair { return failure('map/for-each input list was shortened during iteration') } pair := tail.pair_pointer() m.push(pair.car) container_store_validated(mut state.tails, i, pair.cdr) } state.remaining-- // Reuse ordinary closure entry for Scheme callbacks. Its guards preserve // native-backend priority, budgets, frame growth and special procedures. if isnil(vm.owner.bounded_files) && (isnil(vm.system) || isnil(vm.system.evaluation)) && m.enter_cached_closure[MapCallbackEntry](vm, state.tails.len, false) { return } m.apply(mut vm, state.tails.len, false)! } fn (mut m Machine) collect_iteration() { result := m.pop_value() mut state := iteration_state(m.env.slots[3]) if m.env.escaped { mut scope := m.detach_bridge() state = &IterationState{ callback: state.callback collect: state.collect persistent: true remaining: state.remaining tails: state.tails.clone() prefix: state.prefix suffix: state.suffix } scope.slots[3] = payload_value(.iteration, state) } if !state.collect { return } if state.persistent { state.suffix = cons(result, state.suffix) } else { node := &Pair{ car: result, cdr: nil_value() } link := payload_value(.pair, node) if isnil(state.tail) { state.prefix = link } else { state.tail.cdr = link } state.tail = node } } fn (state &IterationState) iteration_result() Value { if !state.persistent { return state.prefix } mut ordered := nil_value() mut suffix := state.suffix for suffix.tag == .pair { pair := suffix.pair_pointer() ordered = cons(pair.car, ordered) suffix = pair.cdr } mut prefix := state.prefix mut out := ListBuilder{} for prefix.tag == .pair { pair := prefix.pair_pointer() out.push(pair.car) prefix = pair.cdr } return out.finish(ordered) } // Private bridge variables hold implementation state, not user bindings. // Detach them after capture; ordinary Scheme environments keep shared locations. fn (mut m Machine) detach_bridge() &Environment { old := m.env count := old.slots.len mut scope := if count < m.spares.len && !isnil(m.spares[count]) { available := m.spares[count] m.spares[count] = available.parent m.cached-- available } else { &Environment{ slots: value_storage(count) } } scope.parent = old.parent scope.tail_parent = false for i in 0 .. count { container_store_validated(mut scope.slots, i, old.slots[i]) } m.env = scope return scope }