| 1 | module scheme |
| 2 | |
| 3 | enum ListUtilityKind as u8 { |
| 4 | find |
| 5 | memp |
| 6 | assp |
| 7 | for_all |
| 8 | exists |
| 9 | filter |
| 10 | partition |
| 11 | remp |
| 12 | fold_left |
| 13 | fold_right |
| 14 | } |
| 15 | |
| 16 | struct UtilityCursor { |
| 17 | mut: |
| 18 | value Value |
| 19 | slow Value |
| 20 | } |
| 21 | |
| 22 | fn (mut cursor UtilityCursor) advance(cycles bool, steps int) ! { |
| 23 | if cursor.value.tag != .pair { return failure('list input was shortened during iteration') } |
| 24 | cursor.value = cursor.value.pair_pointer().cdr |
| 25 | if cycles && steps % 2 == 0 { |
| 26 | if cursor.slow.tag != .pair { return failure('list input was modified during iteration') } |
| 27 | cursor.slow = cursor.slow.pair_pointer().cdr |
| 28 | if cursor.value.tag == .pair && cursor.value.pointer() == cursor.slow.pointer() { |
| 29 | return failure('expected a proper list, got a circular list') |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | @[heap] |
| 35 | struct ListUtilityState { |
| 36 | callback Value |
| 37 | kind ListUtilityKind |
| 38 | mut: |
| 39 | cursor UtilityCursor |
| 40 | cursors []UtilityCursor |
| 41 | rows []Value |
| 42 | inputs int |
| 43 | remaining int |
| 44 | steps int |
| 45 | pending Value |
| 46 | accumulator Value |
| 47 | persistent bool |
| 48 | selected Value = nil_value() |
| 49 | selected_tail &Pair = unsafe { nil } |
| 50 | rejected Value = nil_value() |
| 51 | rejected_tail &Pair = unsafe { nil } |
| 52 | selected_suffix Value = nil_value() |
| 53 | rejected_suffix Value = nil_value() |
| 54 | } |
| 55 | |
| 56 | fn list_utility_state(value Value) &ListUtilityState { |
| 57 | return unsafe { &ListUtilityState(value.pointer()) } |
| 58 | } |
| 59 | |
| 60 | const list_utility_native_descriptors = [ |
| 61 | NativeDescriptor{'cons*', utility_cons_star} |
| 62 | NativeDescriptor{'remove', utility_remove} |
| 63 | NativeDescriptor{'remv', utility_remv} |
| 64 | NativeDescriptor{'remq', utility_remq}, |
| 65 | ]! |
| 66 | |
| 67 | fn (mut vm VM) install_list_utility_primitives() { |
| 68 | for i, name in ['find', 'memp', 'assp', 'for-all', 'exists', 'filter', 'partition', 'remp', |
| 69 | 'fold-left', 'fold-right'] { |
| 70 | fold := i >= int(ListUtilityKind.fold_left) |
| 71 | variadic := fold || i in [int(ListUtilityKind.for_all), int(ListUtilityKind.exists)] |
| 72 | function := &Function{ |
| 73 | code: unsafe { nil } |
| 74 | params: if fold { 3 } else { 2 } |
| 75 | rest: variadic |
| 76 | slot_count: 5 |
| 77 | instructions: [bridge_instruction(.list_utility_init, i, 0), |
| 78 | bridge_instruction(.list_utility_step, 0, 0), |
| 79 | bridge_instruction(.list_utility_collect, 0, 0), bridge_instruction(.jump, 1, 0)] |
| 80 | } |
| 81 | vm.bind_global(name, procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: name, function: function })) |
| 82 | } |
| 83 | for index in 0 .. list_utility_native_descriptors.len { |
| 84 | entry := &list_utility_native_descriptors[index] |
| 85 | vm.bind_global(entry.name, procedure_value(&Procedure{ owner: vm.owner, mode: .native, name: entry.name, native: entry.callback })) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @[direct_array_access] |
| 90 | fn (mut m Machine) start_list_utility(kind ListUtilityKind) ! { |
| 91 | callback := m.env.slots[0] |
| 92 | if !callback.is_callable() { return failure('list callback must be a procedure') } |
| 93 | mut state := &ListUtilityState{ callback: callback, kind: kind } |
| 94 | fold := kind in [.fold_left, .fold_right] |
| 95 | if fold || kind in [.for_all, .exists] { |
| 96 | first := m.env.slots[if fold { 2 } else { 1 }] |
| 97 | others := m.env.slots[if fold { 3 } else { 2 }] |
| 98 | mut count := 1 |
| 99 | mut tail := others |
| 100 | for tail.tag == .pair { |
| 101 | count++ |
| 102 | tail = tail.pair_pointer().cdr |
| 103 | } |
| 104 | state.inputs = count |
| 105 | state.cursors = filled_storage(count, UtilityCursor{}) |
| 106 | container_store_validated(mut state.cursors, 0, UtilityCursor{ |
| 107 | value: first |
| 108 | slow: if fold { |
| 109 | nil_value() |
| 110 | } else { |
| 111 | first |
| 112 | } |
| 113 | }) |
| 114 | tail = others |
| 115 | for i in 1 .. count { |
| 116 | pair := tail.pair_pointer() |
| 117 | container_store_validated(mut state.cursors, i, UtilityCursor{ |
| 118 | value: pair.car |
| 119 | slow: if fold { |
| 120 | nil_value() |
| 121 | } else { |
| 122 | pair.car |
| 123 | } |
| 124 | }) |
| 125 | tail = pair.cdr |
| 126 | } |
| 127 | if fold { |
| 128 | state.accumulator = m.env.slots[1] |
| 129 | length := first.list_length()! |
| 130 | for i in 1 .. state.cursors.len { |
| 131 | if state.cursors[i].value.list_length()! != length { |
| 132 | return failure('fold lists must have equal lengths') |
| 133 | } |
| 134 | } |
| 135 | state.remaining = length |
| 136 | if kind == .fold_right { |
| 137 | if length > max_int / count { return failure('fold inputs exceed supported size') } |
| 138 | state.rows = value_storage(length * count) |
| 139 | for row in 0 .. length { |
| 140 | for column in 0 .. count { |
| 141 | pair := state.cursors[column].value.pair_pointer() |
| 142 | container_store_validated(mut state.rows, row * count + column, pair.car) |
| 143 | state.cursors[column].value = pair.cdr |
| 144 | } |
| 145 | } |
| 146 | state.cursors = []UtilityCursor{} |
| 147 | } |
| 148 | } |
| 149 | } else { |
| 150 | first := m.env.slots[1] |
| 151 | state.cursor = UtilityCursor{ |
| 152 | value: first |
| 153 | slow: if kind in [.filter, .partition, .remp] { |
| 154 | nil_value() |
| 155 | } else { |
| 156 | first |
| 157 | } |
| 158 | } |
| 159 | if kind in [.filter, .partition, .remp] { state.remaining = first.list_length()! } |
| 160 | } |
| 161 | m.env.slots[4] = payload_value(.list_utility, state) |
| 162 | for i in 0 .. 4 { m.env.slots[i] = unspecified() } |
| 163 | } |
| 164 | |
| 165 | @[direct_array_access] |
| 166 | fn (mut m Machine) step_list_utility(mut vm VM) ! { |
| 167 | mut state := list_utility_state(m.env.slots[4]) |
| 168 | if state.kind == .fold_right { |
| 169 | if state.remaining == 0 { |
| 170 | m.finish_call(state.accumulator) |
| 171 | return |
| 172 | } |
| 173 | m.push(state.callback) |
| 174 | row := (state.remaining - 1) * state.inputs |
| 175 | for i in 0 .. state.inputs { m.push(state.rows[row + i]) } |
| 176 | m.push(state.accumulator) |
| 177 | m.apply(mut vm, state.inputs + 1, false)! |
| 178 | return |
| 179 | } |
| 180 | if state.kind in [.for_all, .exists, .fold_left] { |
| 181 | mut empty := 0 |
| 182 | mut last := true |
| 183 | for cursor in state.cursors { |
| 184 | if cursor.value.tag == .nil { |
| 185 | empty++ |
| 186 | continue |
| 187 | } |
| 188 | if cursor.value.tag != .pair { return failure('expected a proper list') } |
| 189 | last = last && cursor.value.pair_pointer().cdr.tag == .nil |
| 190 | } |
| 191 | if empty > 0 { |
| 192 | if empty != state.inputs { return failure('list inputs must have equal lengths') } |
| 193 | m.finish_call(if state.kind == .fold_left { |
| 194 | state.accumulator |
| 195 | } else { |
| 196 | boolean(state.kind == .for_all) |
| 197 | }) |
| 198 | return |
| 199 | } |
| 200 | m.push(state.callback) |
| 201 | if state.kind == .fold_left { m.push(state.accumulator) } |
| 202 | for cursor in state.cursors { m.push(cursor.value.pair_pointer().car) } |
| 203 | // The report explicitly requires a proper tail call on the final row. |
| 204 | m.apply(mut vm, state.inputs + int(state.kind == .fold_left), last && state.kind != .fold_left)! |
| 205 | return |
| 206 | } |
| 207 | short := state.kind in [.find, .memp, .assp] |
| 208 | if (short && state.cursor.value.tag == .nil) || (!short && state.remaining == 0) { |
| 209 | if short { |
| 210 | m.finish_call(boolean(false)) |
| 211 | } else { |
| 212 | selected := state.list_utility_result(false) |
| 213 | if state.kind == .partition { |
| 214 | m.finish_call(payload_value(.multiple, &Vector{ |
| 215 | items: [selected, state.list_utility_result(true)] |
| 216 | })) |
| 217 | } else { |
| 218 | m.finish_call(selected) |
| 219 | } |
| 220 | } |
| 221 | return |
| 222 | } |
| 223 | if state.cursor.value.tag != .pair { return failure('expected a proper list') } |
| 224 | item := state.cursor.value.pair_pointer().car |
| 225 | state.pending = item |
| 226 | candidate := if state.kind == .assp { |
| 227 | head, _ := item.pair_parts()! |
| 228 | head |
| 229 | } else { |
| 230 | item |
| 231 | } |
| 232 | m.push(state.callback) |
| 233 | m.push(candidate) |
| 234 | m.apply(mut vm, 1, false)! |
| 235 | } |
| 236 | |
| 237 | @[direct_array_access] |
| 238 | fn (mut m Machine) collect_list_utility() ! { |
| 239 | result := m.pop_value() |
| 240 | if result.tag == .multiple { return failure('list callback must return one value') } |
| 241 | mut state := list_utility_state(m.env.slots[4]) |
| 242 | if state.kind in [.find, .memp, .assp] && result.is_true() { |
| 243 | m.finish_call(if state.kind == .memp { state.cursor.value } else { state.pending }) |
| 244 | return |
| 245 | } |
| 246 | if state.kind == .for_all && !result.is_true() { |
| 247 | m.finish_call(boolean(false)) |
| 248 | return |
| 249 | } |
| 250 | if state.kind == .exists && result.is_true() { |
| 251 | m.finish_call(result) |
| 252 | return |
| 253 | } |
| 254 | if m.env.escaped { |
| 255 | mut scope := m.detach_bridge() |
| 256 | state = &ListUtilityState{ ...state, persistent: true, cursors: state.cursors.clone() } |
| 257 | scope.slots[4] = payload_value(.list_utility, state) |
| 258 | } |
| 259 | state.steps++ |
| 260 | match state.kind { |
| 261 | .fold_right { |
| 262 | state.accumulator = result |
| 263 | state.remaining-- |
| 264 | } |
| 265 | .fold_left { |
| 266 | state.accumulator = result |
| 267 | for mut cursor in state.cursors { cursor.advance(false, state.steps)! } |
| 268 | state.remaining-- |
| 269 | } |
| 270 | .for_all, .exists { |
| 271 | for mut cursor in state.cursors { cursor.advance(true, state.steps)! } |
| 272 | } |
| 273 | .filter, .partition, .remp { |
| 274 | keep := if state.kind == .remp { !result.is_true() } else { result.is_true() } |
| 275 | if keep || state.kind == .partition { state.append_list_utility(state.pending, !keep) } |
| 276 | state.cursor.advance(false, state.steps)! |
| 277 | state.remaining-- |
| 278 | } |
| 279 | else { state.cursor.advance(true, state.steps)! } |
| 280 | } |
| 281 | state.pending = unspecified() |
| 282 | } |
| 283 | |
| 284 | fn (mut state ListUtilityState) append_list_utility(value Value, rejected bool) { |
| 285 | if state.persistent { |
| 286 | if rejected { |
| 287 | state.rejected_suffix = cons(value, state.rejected_suffix) |
| 288 | } else { |
| 289 | state.selected_suffix = cons(value, state.selected_suffix) |
| 290 | } |
| 291 | return |
| 292 | } |
| 293 | node := &Pair{ car: value, cdr: nil_value() } |
| 294 | link := payload_value(.pair, node) |
| 295 | if rejected { |
| 296 | if isnil(state.rejected_tail) { |
| 297 | state.rejected = link |
| 298 | } else { |
| 299 | state.rejected_tail.cdr = link |
| 300 | } |
| 301 | state.rejected_tail = node |
| 302 | } else { |
| 303 | if isnil(state.selected_tail) { |
| 304 | state.selected = link |
| 305 | } else { |
| 306 | state.selected_tail.cdr = link |
| 307 | } |
| 308 | state.selected_tail = node |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | fn (state &ListUtilityState) list_utility_result(rejected bool) Value { |
| 313 | prefix := if rejected { state.rejected } else { state.selected } |
| 314 | if !state.persistent { return prefix } |
| 315 | mut suffix := if rejected { state.rejected_suffix } else { state.selected_suffix } |
| 316 | mut ordered := nil_value() |
| 317 | for suffix.tag == .pair { |
| 318 | pair := suffix.pair_pointer() |
| 319 | ordered = cons(pair.car, ordered) |
| 320 | suffix = pair.cdr |
| 321 | } |
| 322 | mut out := ListBuilder{} |
| 323 | mut cursor := prefix |
| 324 | for cursor.tag == .pair { |
| 325 | pair := cursor.pair_pointer() |
| 326 | out.push(pair.car) |
| 327 | cursor = pair.cdr |
| 328 | } |
| 329 | return out.finish(ordered) |
| 330 | } |
| 331 | |
| 332 | fn utility_cons_star(mut _vm VM, args []Value) !Value { |
| 333 | check_arity('cons*', args.len, 1, -1)! |
| 334 | mut result := args.last() |
| 335 | for i := args.len - 2; i >= 0; i-- { result = cons(args[i], result) } |
| 336 | return result |
| 337 | } |
| 338 | |
| 339 | fn utility_remove(mut _vm VM, args []Value) !Value { |
| 340 | return utility_remove_matching(args, 'remove', true) |
| 341 | } |
| 342 | |
| 343 | fn utility_remv(mut _vm VM, args []Value) !Value { |
| 344 | return utility_remove_matching(args, 'remv', false) |
| 345 | } |
| 346 | |
| 347 | fn utility_remq(mut _vm VM, args []Value) !Value { |
| 348 | return utility_remove_matching(args, 'remq', false) |
| 349 | } |
| 350 | |
| 351 | fn utility_remove_matching(args []Value, name string, structural bool) !Value { |
| 352 | check_arity(name, args.len, 2, 2)! |
| 353 | length := args[1].list_length()! |
| 354 | mut cursor := args[1] |
| 355 | mut output := ListBuilder{} |
| 356 | for _ in 0 .. length { |
| 357 | pair := cursor.pair_pointer() |
| 358 | matches := if structural { equal(args[0], pair.car) } else { eqv(args[0], pair.car) } |
| 359 | if !matches { output.push(pair.car) } |
| 360 | cursor = pair.cdr |
| 361 | } |
| 362 | return output.finish(nil_value()) |
| 363 | } |
| 364 | |