| 1 | module scheme |
| 2 | |
| 3 | @[heap] |
| 4 | struct IterationState { |
| 5 | callback Value |
| 6 | collect bool |
| 7 | mut: |
| 8 | persistent bool |
| 9 | remaining int |
| 10 | tails []Value |
| 11 | prefix Value = nil_value() |
| 12 | suffix Value = nil_value() |
| 13 | tail &Pair = unsafe { nil } |
| 14 | } |
| 15 | |
| 16 | struct MapCallbackEntry {} |
| 17 | |
| 18 | fn (mut vm VM) install_iteration_primitives() { |
| 19 | for name in ['map', 'for-each'] { |
| 20 | function := &Function{ |
| 21 | code: unsafe { nil } |
| 22 | params: 2 |
| 23 | rest: true |
| 24 | slot_count: 4 |
| 25 | instructions: [ |
| 26 | bridge_instruction(.map_init, if name == 'map' { 1 } else { 0 }, 0), |
| 27 | bridge_instruction(.map_step, 0, 0), |
| 28 | bridge_instruction(.map_collect, 0, 0), |
| 29 | bridge_instruction(.jump, 1, 0), |
| 30 | bridge_instruction(.ret, 0, 0), |
| 31 | ] |
| 32 | } |
| 33 | vm.bind_global(name, procedure_value(&Procedure{ mode: .closure, name: name, owner: vm.owner, function: function })) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn iteration_state(value Value) &IterationState { |
| 38 | // Only the private iteration bridge reads this slot, after map_init has |
| 39 | // installed the matching payload. It is never a user-provided value. |
| 40 | return unsafe { &IterationState(value.pointer()) } |
| 41 | } |
| 42 | |
| 43 | fn (mut m Machine) start_iteration(collect bool) ! { |
| 44 | callback := m.env.slots[0] |
| 45 | if !callback.is_callable() { return failure('map/for-each needs a procedure') } |
| 46 | // This rest-argument spine is made by the VM, not supplied by the user. |
| 47 | mut others := m.env.slots[2] |
| 48 | mut count := 1 |
| 49 | for others.tag == .pair { |
| 50 | count++ |
| 51 | others = others.pair_pointer().cdr |
| 52 | } |
| 53 | mut tails := value_storage(count) |
| 54 | container_store_validated(mut tails, 0, m.env.slots[1]) |
| 55 | others = m.env.slots[2] |
| 56 | for i in 1 .. count { |
| 57 | pair := others.pair_pointer() |
| 58 | container_store_validated(mut tails, i, pair.car) |
| 59 | others = pair.cdr |
| 60 | } |
| 61 | mut length := -1 |
| 62 | for value in tails { |
| 63 | n := list_iteration_limit(value)! |
| 64 | if n >= 0 && (length < 0 || n < length) { length = n } |
| 65 | } |
| 66 | if length < 0 { return failure('map/for-each needs at least one finite list') } |
| 67 | m.env.slots[3] = payload_value(.iteration, &IterationState{ callback: callback, collect: collect, remaining: length, tails: tails }) |
| 68 | // Keep only the advancing tails, not the original heads or argument spine. |
| 69 | for i in 0 .. 3 { m.env.slots[i] = unspecified() } |
| 70 | } |
| 71 | |
| 72 | fn (mut m Machine) step_iteration(mut vm VM) ! { |
| 73 | mut state := iteration_state(m.env.slots[3]) |
| 74 | if state.remaining == 0 { |
| 75 | m.finish_call(if state.collect { state.iteration_result() } else { unspecified() }) |
| 76 | return |
| 77 | } |
| 78 | m.push(state.callback) |
| 79 | for i, tail in state.tails { |
| 80 | // Callbacks can mutate input data after the initial proper-list check. |
| 81 | if tail.tag != .pair { |
| 82 | return failure('map/for-each input list was shortened during iteration') |
| 83 | } |
| 84 | pair := tail.pair_pointer() |
| 85 | m.push(pair.car) |
| 86 | container_store_validated(mut state.tails, i, pair.cdr) |
| 87 | } |
| 88 | state.remaining-- |
| 89 | // Reuse ordinary closure entry for Scheme callbacks. Its guards preserve |
| 90 | // native-backend priority, budgets, frame growth and special procedures. |
| 91 | if isnil(vm.owner.bounded_files) && (isnil(vm.system) || isnil(vm.system.evaluation)) |
| 92 | && m.enter_cached_closure[MapCallbackEntry](vm, state.tails.len, false) { |
| 93 | return |
| 94 | } |
| 95 | m.apply(mut vm, state.tails.len, false)! |
| 96 | } |
| 97 | |
| 98 | fn (mut m Machine) collect_iteration() { |
| 99 | result := m.pop_value() |
| 100 | mut state := iteration_state(m.env.slots[3]) |
| 101 | if m.env.escaped { |
| 102 | mut scope := m.detach_bridge() |
| 103 | state = &IterationState{ |
| 104 | callback: state.callback |
| 105 | collect: state.collect |
| 106 | persistent: true |
| 107 | remaining: state.remaining |
| 108 | tails: state.tails.clone() |
| 109 | prefix: state.prefix |
| 110 | suffix: state.suffix |
| 111 | } |
| 112 | scope.slots[3] = payload_value(.iteration, state) |
| 113 | } |
| 114 | if !state.collect { return } |
| 115 | if state.persistent { |
| 116 | state.suffix = cons(result, state.suffix) |
| 117 | } else { |
| 118 | node := &Pair{ car: result, cdr: nil_value() } |
| 119 | link := payload_value(.pair, node) |
| 120 | if isnil(state.tail) { state.prefix = link } else { state.tail.cdr = link } |
| 121 | state.tail = node |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | fn (state &IterationState) iteration_result() Value { |
| 126 | if !state.persistent { return state.prefix } |
| 127 | mut ordered := nil_value() |
| 128 | mut suffix := state.suffix |
| 129 | for suffix.tag == .pair { |
| 130 | pair := suffix.pair_pointer() |
| 131 | ordered = cons(pair.car, ordered) |
| 132 | suffix = pair.cdr |
| 133 | } |
| 134 | mut prefix := state.prefix |
| 135 | mut out := ListBuilder{} |
| 136 | for prefix.tag == .pair { |
| 137 | pair := prefix.pair_pointer() |
| 138 | out.push(pair.car) |
| 139 | prefix = pair.cdr |
| 140 | } |
| 141 | return out.finish(ordered) |
| 142 | } |
| 143 | |
| 144 | // Private bridge variables hold implementation state, not user bindings. |
| 145 | // Detach them after capture; ordinary Scheme environments keep shared locations. |
| 146 | fn (mut m Machine) detach_bridge() &Environment { |
| 147 | old := m.env |
| 148 | count := old.slots.len |
| 149 | mut scope := if count < m.spares.len && !isnil(m.spares[count]) { |
| 150 | available := m.spares[count] |
| 151 | m.spares[count] = available.parent |
| 152 | m.cached-- |
| 153 | available |
| 154 | } else { |
| 155 | &Environment{ slots: value_storage(count) } |
| 156 | } |
| 157 | scope.parent = old.parent |
| 158 | scope.tail_parent = false |
| 159 | for i in 0 .. count { container_store_validated(mut scope.slots, i, old.slots[i]) } |
| 160 | m.env = scope |
| 161 | return scope |
| 162 | } |
| 163 | |