| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | struct ValuesBinding { |
| 6 | params []string |
| 7 | rest string |
| 8 | initializer reader.NodeId |
| 9 | origin reader.NodeId |
| 10 | } |
| 11 | |
| 12 | fn (mut b Builder) values_definition(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 13 | check_arity('define-values', nodes.len - 1, 2, 2)! |
| 14 | params, rest := b.code.formals(nodes[1])! |
| 15 | for name in params { b.declare_global_keyword(name) } |
| 16 | if rest != '' { b.declare_global_keyword(rest) } |
| 17 | b.expression(nodes[2], false) |
| 18 | b.store_value_results(params, rest, origin)! |
| 19 | b.constant(unspecified(), origin) |
| 20 | } |
| 21 | |
| 22 | fn (mut b Builder) store_value_results(params []string, rest string, origin reader.NodeId) ! { |
| 23 | b.emit(.unpack_values, params.len, int(rest != ''), origin) |
| 24 | if rest != '' { |
| 25 | b.store(rest, true, origin)! |
| 26 | b.emit(.drop, 0, 0, origin) |
| 27 | } |
| 28 | for i := params.len - 1; i >= 0; i-- { |
| 29 | b.store(params[i], true, origin)! |
| 30 | b.emit(.drop, 0, 0, origin) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn (mut b Builder) values_binding_form(name string, nodes []reader.NodeId, tail bool, origin reader.NodeId) ! { |
| 35 | check_arity(name, nodes.len - 1, 2, -1)! |
| 36 | ids := b.code.nodes(nodes[1])! |
| 37 | mut bindings := []ValuesBinding{cap: ids.len} |
| 38 | mut seen := map[string]bool{} |
| 39 | for id in ids { |
| 40 | parts := b.code.nodes(id)! |
| 41 | if parts.len != 2 { |
| 42 | return failure('each ${name} binding needs formals and an initializer') |
| 43 | } |
| 44 | params, rest := b.code.formals(parts[0])! |
| 45 | if name == 'let-values' { |
| 46 | for key in params { |
| 47 | if b.code.binding_key(key) in seen { return failure('duplicate binding ${key}') } |
| 48 | seen[b.code.binding_key(key)] = true |
| 49 | } |
| 50 | if rest != '' { |
| 51 | if b.code.binding_key(rest) in seen { return failure('duplicate binding ${rest}') } |
| 52 | seen[b.code.binding_key(rest)] = true |
| 53 | } |
| 54 | } |
| 55 | bindings << ValuesBinding{params, rest, parts[1], id} |
| 56 | } |
| 57 | if name == 'let*-values' { |
| 58 | b.sequential_value_bindings(bindings, nodes[2..], tail, origin) |
| 59 | return |
| 60 | } |
| 61 | mut count := 0 |
| 62 | for binding in bindings { count += binding.params.len + int(binding.rest != '') } |
| 63 | // Initializers run in the enclosing region and unpack directly onto the |
| 64 | // argument stack. The callee needs only actual binding slots, no packet slots. |
| 65 | function := &Function{ code: b.code, params: count } |
| 66 | mut scope := new_compile_scope(Scope{ parent: b.scope, function: function }) |
| 67 | for binding in bindings { declare_value_names(mut scope, binding) } |
| 68 | mut inner := Builder{ code: b.code, function: function, scope: scope, return_tail: b.return_tail && tail } |
| 69 | inner.body(nodes[2..]) |
| 70 | inner.emit(.ret, 0, 0, origin) |
| 71 | b.make_closure(function, origin) |
| 72 | for binding in bindings { |
| 73 | b.expression(binding.initializer, false) |
| 74 | b.emit(.unpack_values, binding.params.len, int(binding.rest != ''), binding.origin) |
| 75 | } |
| 76 | b.application(count, tail, origin) |
| 77 | } |
| 78 | |
| 79 | fn declare_value_names(mut scope Scope, binding ValuesBinding) { |
| 80 | for name in binding.params { scope.declare(name) } |
| 81 | if binding.rest != '' { scope.declare(binding.rest) } |
| 82 | } |
| 83 | |
| 84 | fn (mut b Builder) sequential_value_bindings(bindings []ValuesBinding, body []reader.NodeId, tail bool, origin reader.NodeId) { |
| 85 | if bindings.len == 0 { |
| 86 | b.make_closure(b.child([], '', body, tail), origin) |
| 87 | b.application(0, tail, origin) |
| 88 | return |
| 89 | } |
| 90 | // Build flat groups iteratively, as for let*. Each new region captures the |
| 91 | // previous one, preserving distinct locations when names are repeated. |
| 92 | // The first initializer uses the parent's function and its shared name cache. |
| 93 | mut current := Builder{ code: b.code, function: b.function, scope: b.scope, table: b.name_table(), return_tail: b.return_tail } |
| 94 | for i, binding in bindings { |
| 95 | count := binding.params.len + int(binding.rest != '') |
| 96 | function := &Function{ code: b.code, params: count } |
| 97 | mut scope := new_compile_scope(Scope{ parent: current.scope, function: function }) |
| 98 | declare_value_names(mut scope, binding) |
| 99 | current.make_closure(function, origin) |
| 100 | current.expression(binding.initializer, false) |
| 101 | current.emit(.unpack_values, binding.params.len, int(binding.rest != ''), binding.origin) |
| 102 | current.application(count, if i == 0 { tail } else { true }, origin) |
| 103 | if i > 0 { current.emit(.ret, 0, 0, origin) } |
| 104 | current = Builder{ code: b.code, function: function, scope: scope, return_tail: b.return_tail && tail } |
| 105 | } |
| 106 | current.body(body) |
| 107 | current.emit(.ret, 0, 0, origin) |
| 108 | } |
| 109 | |
| 110 | // Only a rest formal constructs a list. Fixed bindings read the existing |
| 111 | // packet directly, without as_values() copying its item container. |
| 112 | fn (mut m Machine) unpack_values(fixed int, rest bool) ! { |
| 113 | packet := m.pop_value() |
| 114 | count := if packet.tag != .multiple { |
| 115 | 1 |
| 116 | } else if packet.pointer() == unsafe { nil } { |
| 117 | 0 |
| 118 | } else { |
| 119 | packet.vector_pointer().items.len |
| 120 | } |
| 121 | check_arity('values binding', count, fixed, if rest { -1 } else { fixed })! |
| 122 | if packet.tag != .multiple { |
| 123 | if fixed == 1 { m.push(packet) } |
| 124 | if rest { m.push(if fixed == 0 { cons(packet, nil_value()) } else { nil_value() }) } |
| 125 | return |
| 126 | } |
| 127 | if count == 0 { |
| 128 | if rest { m.push(nil_value()) } |
| 129 | return |
| 130 | } |
| 131 | items := packet.vector_pointer().items |
| 132 | for i in 0 .. fixed { m.push(items[i]) } |
| 133 | if rest { |
| 134 | mut tail := nil_value() |
| 135 | for i := count - 1; i >= fixed; i-- { tail = cons(items[i], tail) } |
| 136 | m.push(tail) |
| 137 | } |
| 138 | } |
| 139 | |