module scheme import reader struct ValuesBinding { params []string rest string initializer reader.NodeId origin reader.NodeId } fn (mut b Builder) values_definition(nodes []reader.NodeId, origin reader.NodeId) ! { check_arity('define-values', nodes.len - 1, 2, 2)! params, rest := b.code.formals(nodes[1])! for name in params { b.declare_global_keyword(name) } if rest != '' { b.declare_global_keyword(rest) } b.expression(nodes[2], false) b.store_value_results(params, rest, origin)! b.constant(unspecified(), origin) } fn (mut b Builder) store_value_results(params []string, rest string, origin reader.NodeId) ! { b.emit(.unpack_values, params.len, int(rest != ''), origin) if rest != '' { b.store(rest, true, origin)! b.emit(.drop, 0, 0, origin) } for i := params.len - 1; i >= 0; i-- { b.store(params[i], true, origin)! b.emit(.drop, 0, 0, origin) } } fn (mut b Builder) values_binding_form(name string, nodes []reader.NodeId, tail bool, origin reader.NodeId) ! { check_arity(name, nodes.len - 1, 2, -1)! ids := b.code.nodes(nodes[1])! mut bindings := []ValuesBinding{cap: ids.len} mut seen := map[string]bool{} for id in ids { parts := b.code.nodes(id)! if parts.len != 2 { return failure('each ${name} binding needs formals and an initializer') } params, rest := b.code.formals(parts[0])! if name == 'let-values' { for key in params { if b.code.binding_key(key) in seen { return failure('duplicate binding ${key}') } seen[b.code.binding_key(key)] = true } if rest != '' { if b.code.binding_key(rest) in seen { return failure('duplicate binding ${rest}') } seen[b.code.binding_key(rest)] = true } } bindings << ValuesBinding{params, rest, parts[1], id} } if name == 'let*-values' { b.sequential_value_bindings(bindings, nodes[2..], tail, origin) return } mut count := 0 for binding in bindings { count += binding.params.len + int(binding.rest != '') } // Initializers run in the enclosing region and unpack directly onto the // argument stack. The callee needs only actual binding slots, no packet slots. function := &Function{ code: b.code, params: count } mut scope := new_compile_scope(Scope{ parent: b.scope, function: function }) for binding in bindings { declare_value_names(mut scope, binding) } mut inner := Builder{ code: b.code, function: function, scope: scope, return_tail: b.return_tail && tail } inner.body(nodes[2..]) inner.emit(.ret, 0, 0, origin) b.make_closure(function, origin) for binding in bindings { b.expression(binding.initializer, false) b.emit(.unpack_values, binding.params.len, int(binding.rest != ''), binding.origin) } b.application(count, tail, origin) } fn declare_value_names(mut scope Scope, binding ValuesBinding) { for name in binding.params { scope.declare(name) } if binding.rest != '' { scope.declare(binding.rest) } } fn (mut b Builder) sequential_value_bindings(bindings []ValuesBinding, body []reader.NodeId, tail bool, origin reader.NodeId) { if bindings.len == 0 { b.make_closure(b.child([], '', body, tail), origin) b.application(0, tail, origin) return } // Build flat groups iteratively, as for let*. Each new region captures the // previous one, preserving distinct locations when names are repeated. // The first initializer uses the parent's function and its shared name cache. mut current := Builder{ code: b.code, function: b.function, scope: b.scope, table: b.name_table(), return_tail: b.return_tail } for i, binding in bindings { count := binding.params.len + int(binding.rest != '') function := &Function{ code: b.code, params: count } mut scope := new_compile_scope(Scope{ parent: current.scope, function: function }) declare_value_names(mut scope, binding) current.make_closure(function, origin) current.expression(binding.initializer, false) current.emit(.unpack_values, binding.params.len, int(binding.rest != ''), binding.origin) current.application(count, if i == 0 { tail } else { true }, origin) if i > 0 { current.emit(.ret, 0, 0, origin) } current = Builder{ code: b.code, function: function, scope: scope, return_tail: b.return_tail && tail } } current.body(body) current.emit(.ret, 0, 0, origin) } // Only a rest formal constructs a list. Fixed bindings read the existing // packet directly, without as_values() copying its item container. fn (mut m Machine) unpack_values(fixed int, rest bool) ! { packet := m.pop_value() count := if packet.tag != .multiple { 1 } else if packet.pointer() == unsafe { nil } { 0 } else { packet.vector_pointer().items.len } check_arity('values binding', count, fixed, if rest { -1 } else { fixed })! if packet.tag != .multiple { if fixed == 1 { m.push(packet) } if rest { m.push(if fixed == 0 { cons(packet, nil_value()) } else { nil_value() }) } return } if count == 0 { if rest { m.push(nil_value()) } return } items := packet.vector_pointer().items for i in 0 .. fixed { m.push(items[i]) } if rest { mut tail := nil_value() for i := count - 1; i >= fixed; i-- { tail = cons(items[i], tail) } m.push(tail) } }