module scheme fn (mut vm VM) comparison_search(association bool) Value { index := int(association) if vm.comparison_searches[index].tag == .unspecified { function := if association { &assoc_comparison_bridge } else { &member_comparison_bridge } value := procedure_value(&Procedure{ mode: .closure owner: vm.owner name: if association { 'assoc' } else { 'member' } function: function }) if v_arena_current() == vm.allocation_owner { vm.comparison_searches[index] = value } return value } return vm.comparison_searches[index] } @[direct_array_access] fn (mut m Machine) start_comparison_search(_association bool) ! { if !m.env.slots[2].is_callable() { return failure('search comparator must be a procedure') } m.env.slots[3] = integer(0) m.env.slots[4] = m.env.slots[1] } @[direct_array_access] fn (mut m Machine) step_comparison_search(mut vm VM, association bool) ! { current := m.env.slots[1] if current.tag == .nil { m.finish_call(boolean(false)) return } if current.tag != .pair { return failure('expected a proper list') } item := current.pair_pointer().car candidate := if association { first, _ := item.pair_parts()! first } else { item } m.env.slots[5] = current.pair_pointer().cdr m.push(m.env.slots[2]) m.push(m.env.slots[0]) m.push(candidate) m.apply(mut vm, 2, false)! } @[direct_array_access] fn (mut m Machine) collect_comparison_search(association bool) ! { result := m.pop_value() if result.tag == .multiple { return failure('search comparator must return one value') } current := m.env.slots[1] if !eqv(current.pair_pointer().cdr, m.env.slots[5]) { return failure('search input was modified during iteration') } if result.is_true() { m.finish_call(if association { current.pair_pointer().car } else { current }) return } if m.env.escaped { m.detach_bridge() } steps := m.env.slots[3].whole() + 1 next := m.env.slots[5] if steps % 2 == 0 { slow := m.env.slots[4] if slow.tag != .pair { return failure('search input was modified during iteration') } next_slow := slow.pair_pointer().cdr if next.tag == .pair && next.pointer() == next_slow.pointer() { return failure('expected a proper list, got a circular list') } m.env.slots[4] = next_slow } m.env.slots[1] = next m.env.slots[3] = integer(steps) m.env.slots[5] = unspecified() }