vile / scheme / namespace_import.v
176 lines · 165 sloc · 7.28 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5// new_environment creates an isolated mutable namespace with the core syntax.
6// Import procedures and macros explicitly; interaction bindings are not copied.
7// This host API is distinct from the report/null environment specifiers.
8pub fn (mut vm VM) new_environment() !Value {
9 vm.ready()!
10 return payload_value(.environment, &EvalEnvironment{ owner: vm.owner, space: &Namespace{ writable: true } })
11}
12
13pub fn (mut vm VM) define_in(environment Value, name string, value Value, options reader.ReadOptions) ! {
14 vm.ready()!
15 space := vm.environment_space(environment)!
16 vm.bind_global_in(space, vm.host_binding_name(name, options)!, value)!
17}
18
19pub fn (vm &VM) lookup_in(environment Value, name string, options reader.ReadOptions) !Value {
20 vm.ready()!
21 space := vm.environment_space(environment)!
22 slot := vm.global_index_in(space, vm.host_binding_name(name, options)!)!
23 return if report_slot(space, slot) { vm.pristine_standard(-slot - 2) } else { vm.read_global(slot) }
24}
25
26fn (vm &VM) namespace_has_binding(space &Namespace, name string) bool {
27 if isnil(space) {
28 return name in *vm.bindings || (!isnil(vm.syntax) && name in vm.syntax.entries)
29 }
30 return name in space.bindings || (!isnil(space.syntax) && name in space.syntax.entries)
31}
32
33fn (vm &VM) namespace_binding(space &Namespace, name string) SyntaxBinding {
34 if isnil(space) {
35 if !isnil(vm.syntax) {
36 if binding := vm.syntax.entries[name] {
37 return vm.imported_syntax_binding(space, name, binding)
38 }
39 }
40 if slot := (*vm.bindings)[name] {
41 return SyntaxBinding{ kind: .global, name: name, global_slot: slot, assignable: name !in vm.imported }
42 }
43 if special_name(name) && name !in vm.keyword_variables {
44 return SyntaxBinding{ kind: .keyword, name: name }
45 }
46 } else {
47 if !isnil(space.syntax) {
48 if binding := space.syntax.entries[name] {
49 return vm.imported_syntax_binding(space, name, binding)
50 }
51 }
52 if slot := space.bindings[name] {
53 return SyntaxBinding{ kind: .global, name: name, namespace: space, global_slot: slot, assignable: space.writable && name !in space.imported }
54 }
55 if space.implicit_syntax && special_name(name) && name !in space.keyword_variables {
56 return SyntaxBinding{ kind: .keyword, name: name, namespace: space }
57 }
58 }
59 return SyntaxBinding{ kind: .global, name: name, namespace: space, unbound: true }
60}
61
62fn (vm &VM) imported_syntax_binding(space &Namespace, name string, binding SyntaxBinding) SyntaxBinding {
63 // Imported aliases retain the original transformer location. Re-exporting
64 // or renaming it never turns a definition-site reference into a use-site one.
65 if binding.kind == .transformer && binding.top_level
66 && (voidptr(binding.namespace) != voidptr(space) || binding.name != name) {
67 return vm.namespace_binding(binding.namespace, binding.name)
68 }
69 return binding
70}
71
72struct NamespaceImport {
73 // Canonical external binding key, including the empty/leading-NUL encoding.
74 // Prefix transforms concatenate decoded public spellings and encode once.
75 name string
76 binding SyntaxBinding
77}
78
79// import_bindings maps source names to destination names. Variable aliases share
80// their absolute slots; syntax aliases preserve binding identity and hygiene.
81// Imported names cannot be assigned or redefined. Validate the complete batch
82// before changing the destination, including collisions caused by case folding.
83pub fn (mut vm VM) import_bindings(into Value, from Value, names map[string]string, options reader.ReadOptions) ! {
84 vm.ready()!
85 target := vm.environment_space(into)!
86 source := vm.environment_space(from)!
87 if !isnil(target) && !target.writable { return failure('environment does not permit imports') }
88 mut plan := []NamespaceImport{cap: names.len}
89 mut destinations := map[string]int{}
90 for external, local in names {
91 key := vm.host_binding_name(external, options)!
92 name := vm.host_binding_name(local, options)!
93 binding := vm.namespace_binding(source, key)
94 if binding.kind == .global && binding.global_slot < 0 && !report_slot(binding.namespace, binding.global_slot) {
95 return failure('cannot import unbound identifier ${binding_diagnostic_name(key)}')
96 }
97 if previous := destinations[name] {
98 if !same_syntax_binding(plan[previous].binding, binding) {
99 return failure('conflicting imports for ${name}')
100 }
101 continue
102 }
103 if vm.namespace_has_binding(target, name) && !same_syntax_binding(vm.namespace_binding(target, name), binding) {
104 return failure('import conflicts with existing binding ${name}')
105 }
106 destinations[name] = plan.len
107 plan << NamespaceImport{name, binding}
108 }
109 for item in plan { vm.install_import(target, item) }
110}
111
112fn (mut vm VM) install_import(space &Namespace, input NamespaceImport) {
113 if isnil(space) { vm.prepare_deferred_root_import(input.name, input.binding) }
114 item := NamespaceImport{ ...input, binding: vm.materialize_report_import(input.binding) }
115 // Imports can change syntax identity or write policy without moving a slot.
116 vm.binding_epoch++
117 if isnil(space) {
118 // Values distinguish aliases introduced by imports from initial names.
119 // Presence protects writes; provenance keeps report snapshots pristine.
120 if item.name !in vm.imported {
121 vm.imported[item.name] = !vm.namespace_has_binding(space, item.name)
122 }
123 if item.binding.kind == .global {
124 if item.name !in *vm.bindings || (*vm.bindings)[item.name] != item.binding.global_slot {
125 vm.binding_epoch++
126 }
127 if !isnil(vm.syntax) { vm.syntax.entries.delete(item.name) }
128 (*vm.bindings)[item.name] = item.binding.global_slot
129 if special_name(item.name) && item.name !in vm.keyword_variables {
130 vm.keyword_variables << item.name
131 }
132 } else {
133 if isnil(vm.syntax) { vm.syntax = &SyntaxBindings{} }
134 vm.syntax.entries[item.name] = item.binding
135 }
136 } else {
137 mut target := unsafe { &Namespace(space) }
138 if item.binding.kind == .global {
139 if item.name !in target.bindings { vm.binding_epoch++ }
140 target.bindings[item.name] = item.binding.global_slot
141 if special_name(item.name) && item.name !in target.keyword_variables {
142 target.keyword_variables << item.name
143 }
144 } else {
145 if isnil(target.syntax) { target.syntax = &SyntaxBindings{} }
146 target.syntax.entries[item.name] = item.binding
147 }
148 target.imported[item.name] = true
149 }
150 vm.note_syntax_import(space, item.name, item.binding)
151}
152
153// Import is an explicit namespace effect. Give only the imported report name
154// an absolute shared location; a pure report query never grows VM storage.
155fn (mut vm VM) materialize_report_import(binding SyntaxBinding) SyntaxBinding {
156 if binding.kind != .global || !report_slot(binding.namespace, binding.global_slot) {
157 return binding
158 }
159 index := -binding.global_slot - 2
160 mut owner := vm.owner
161 if current := owner.report_slots[index] {
162 return SyntaxBinding{ ...binding, global_slot: current }
163 }
164 // Preserve a deferred builtin's placeholder. Its canonical and imported
165 // locations are populated together on first use, without a proxy Value.
166 value := if vm.globals[index].tag == .uninitialized {
167 vm.globals[index]
168 } else { vm.pristine_standard(index) }
169 slot := vm.globals.len
170 vm.globals << value
171 owner.report_slots[index] = slot
172 // The immutable report location already has this identity through
173 // owner.report_slots. Only the actual import target needs a syntax-location
174 // record; materialization never declares a new name in the report space.
175 return SyntaxBinding{ ...binding, global_slot: slot }
176}
177