module scheme struct GoopsPrintPlan { vm &VM generic Value mut: parts []Value } // Prepare the graph before writing the first byte. A retry with datum labels // must never invoke a printer twice. The immutable plan and current index // survive a continuation captured by either a printer or a custom port. fn (mut m Machine) prepare_goops_print(mut vm VM, p &Procedure, operation PortIoOperation, base int, args []Value) !bool { if operation !in [.display_text, .write_datum, .write_simple, .write_shared, .put_datum] { return false } check_custom_port_arity(operation, args.len)! put := operation == .put_datum value := args[if put { 1 } else { 0 }] if value.tag !in [.pair, .vector] && !goops_primitive_object(value) { return false } if value.tag !in [.pair, .vector] && !vm.goops_class_for(value)!.printer.is_callable() { return false } port := if put { args[0] } else if args.len == 2 { args[1] } else { vm.current_output_value()! } vm.checked_text_output(port)! generic := if operation in [.display_text, .write_datum] && !isnil(p.env) && value.tag in [.pair, .vector] { p.env.slots[0] } else { unspecified() } mut plan := GoopsPrintPlan{ vm: vm, generic: generic } mut state := ScalarPrintState{ plan: &plan } displaying := operation == .display_text suffix := if operation == .write_shared { format_shared_parts(value, false, mut state)! } else { format_unshared_parts(value, displaying, mut state) or { if err !is CircularPrint { return err } if operation == .write_simple { return failure(err.msg()) } plan.parts.clear() state.preserve_case = false format_shared_parts(value, displaying, mut state)! } } m.truncate(base) if plan.parts.len == 0 { m.push(vm.goops.print_text) m.push(custom_immutable_text(suffix)) } else { if suffix.len != 0 { plan.parts << custom_immutable_text(suffix) } m.push(vm.goops.print_parts) m.push(payload_value(.vector, &Vector{ items: unsafe { plan.parts } })) } m.push(port) return true }