vile / scheme / namespace.v
250 lines · 230 sloc · 9.15 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5// Environments share the VM's stable absolute global slots. Report/null spaces
6// are cached and immutable; explicit mutable spaces support persistent library
7// state and shared-location imports without a second runtime or value format.
8@[heap]
9struct Namespace {
10 implicit_syntax bool = true
11 // Immutable report names initially encode a pristine standard slot as
12 // -slot-2. Only an explicit import materializes a shared absolute location.
13 report_owner &Owner = unsafe { nil }
14mut:
15 writable bool
16 bindings map[string]int
17 syntax &SyntaxBindings = unsafe { nil }
18 keyword_variables []string
19 imported map[string]bool
20 // Allocated lazily from an existing VM-unique compilation coordinate.
21 syntax_identity u64
22 syntax_locations map[string]&SyntaxGlobalLocation
23}
24
25@[heap]
26struct EvalEnvironment {
27 owner &Owner
28 space &Namespace = unsafe { nil }
29}
30
31@[heap]
32struct EvalState {
33mut:
34 interaction Value
35 report Value
36 null_space Value
37}
38
39fn (mut vm VM) eval_state() &EvalState {
40 if isnil(vm.evaluation) { vm.evaluation = &vm.evaluation_storage }
41 return vm.evaluation
42}
43
44// Every assignment targets a resolved binding location, including assignments
45// through aliases. Observe hook changes here so a warm dispatch needs no weak
46// lookups and a resumed method factory sees intervening rebindings.
47fn (mut vm VM) replace_global(index int, value Value) {
48 vm.ensure_goops_slot(index)
49 if index < vm.standard_count && index !in vm.standard_original {
50 vm.standard_original[index] = vm.globals[index]
51 }
52 if !isnil(vm.goops) && vm.goops.protocols_ready
53 && (index == vm.goops.applicability_binding || index == vm.goops.effective_binding
54 || index == vm.goops.specialized_binding) && !eqv(vm.globals[index], value) {
55 vm.goops.applicability_epoch++
56 }
57 container_store(mut vm.globals, index, value)
58 vm.invalidate_native_bindings()
59}
60
61fn (vm &VM) environment_space(value Value) !&Namespace {
62 if value.tag != .environment { return failure('expected an environment specifier') }
63 environment := unsafe { &EvalEnvironment(value.pointer()) }
64 if voidptr(environment.owner) != voidptr(vm.owner) {
65 return failure('environment belongs to another VM')
66 }
67 return environment.space
68}
69
70pub fn (mut vm VM) interaction_environment() !Value {
71 vm.ready()!
72 mut state := vm.eval_state()
73 if state.interaction.tag != .environment {
74 value := payload_value(.environment, &EvalEnvironment{ owner: vm.owner })
75 if v_arena_current() != vm.allocation_owner { return value }
76 state.interaction = value
77 }
78 return state.interaction
79}
80
81pub fn (mut vm VM) report_environment(version int) !Value {
82 vm.ready()!
83 if version != 5 { return failure('unsupported Scheme report version') }
84 mut state := vm.eval_state()
85 if state.report.tag != .environment {
86 mut space := &Namespace{ report_owner: vm.owner }
87 // Before root imports, every original name is still present, including
88 // redefinitions. Avoid a second map-key iteration (and its implicit
89 // string clones) in this common case. Imported aliases need filtering.
90 mut count := vm.standard_count
91 if vm.imported.len > 0 {
92 count = 0
93 for name, index in *vm.bindings {
94 if index < vm.standard_count && !vm.imported[name] { count++ }
95 }
96 }
97 if u64(count) <= u64(~u32(0)) { space.bindings.reserve(u32(count)) }
98 for name, index in *vm.bindings {
99 if index >= vm.standard_count || vm.imported[name] { continue }
100 space.bindings[name] = -index - 2
101 }
102 value := payload_value(.environment, &EvalEnvironment{ owner: vm.owner, space: space })
103 if v_arena_current() != vm.allocation_owner { return value }
104 state.report = value
105 }
106 return state.report
107}
108
109fn report_slot(space &Namespace, slot int) bool {
110 return !isnil(space) && !isnil(space.report_owner) && slot <= -2
111}
112
113fn (vm &VM) pristine_standard(slot int) Value {
114 if vm.globals[slot].tag == .uninitialized { vm.ensure_goops_slot(slot) }
115 return vm.standard_original[slot] or { vm.globals[slot] }
116}
117
118pub fn (mut vm VM) null_environment(version int) !Value {
119 vm.ready()!
120 if version != 5 { return failure('unsupported Scheme report version') }
121 mut state := vm.eval_state()
122 if state.null_space.tag != .environment {
123 value := payload_value(.environment, &EvalEnvironment{ owner: vm.owner, space: &Namespace{} })
124 if v_arena_current() != vm.allocation_owner { return value }
125 state.null_space = value
126 }
127 return state.null_space
128}
129
130// Only a cache miss resolves a name. Keep namespace/map lookup out of the
131// common instruction dispatcher, independently of GCC inlining heuristics.
132@[noinline]
133fn (vm &VM) global_index_in(space &Namespace, name string) !int {
134 if isnil(space) { return vm.global_index(name) }
135 if index := space.bindings[name] { return index }
136 return failure('unbound variable ${binding_diagnostic_name(name)}')
137}
138
139fn (mut vm VM) bind_global_in(space &Namespace, name string, value Value) !int {
140 if !isnil(space) && !space.writable { return failure('environment does not permit global definitions') }
141 if (isnil(space) && name in vm.imported) || (!isnil(space) && name in space.imported) {
142 return failure('cannot redefine imported binding ${name}')
143 }
144 if !isnil(vm.owner.bounded_files) && !special_name(name) {
145 syntax := if isnil(space) { vm.syntax } else { space.syntax }
146 if isnil(syntax) || name !in syntax.entries {
147 return vm.owner.bounded_files.publish_binding(mut vm, space, name, value)
148 }
149 }
150 if isnil(space) { return vm.bind_global(name, value) }
151 mut target := unsafe { &Namespace(space) }
152 if !isnil(target.syntax) && name in target.syntax.entries {
153 target.syntax.entries.delete(name)
154 vm.binding_epoch++
155 }
156 if special_name(name) && name !in target.keyword_variables { target.keyword_variables << name }
157 if index := target.bindings[name] {
158 vm.replace_global(index, value)
159 vm.note_syntax_location(space, name, SyntaxBinding{ kind: .global, name: name, namespace: space, global_slot: index })
160 return index
161 }
162 index := vm.globals.len
163 vm.globals << value
164 target.bindings[name] = index
165 vm.binding_epoch++
166 vm.note_syntax_location(space, name, SyntaxBinding{ kind: .global, name: name, namespace: space, global_slot: index })
167 return index
168}
169
170// The declaration already established the public location. A private alias
171// exposes that same slot to its exact captured syntax context, without another
172// value allocation or an imported/read-only binding.
173fn (mut vm VM) bind_global_alias_in(space &Namespace, name string, slot int) ! {
174 vm.check_assignment_in(space, name)!
175 if isnil(space) {
176 if existing := (*vm.bindings)[name] {
177 if existing == slot { return }
178 }
179 (*vm.bindings)[name] = slot
180 } else {
181 mut target := unsafe { &Namespace(space) }
182 if existing := target.bindings[name] {
183 if existing == slot { return }
184 }
185 target.bindings[name] = slot
186 }
187 vm.binding_epoch++
188 vm.note_syntax_location(space, name, SyntaxBinding{ kind: .global, name: name, namespace: space, global_slot: slot })
189}
190
191fn (vm &VM) check_assignment_in(space &Namespace, name string) ! {
192 if !isnil(space) && !space.writable {
193 return failure('environment does not permit global assignment')
194 }
195 vm.check_imported_assignment(space, name)!
196}
197
198fn (vm &VM) assignment_permitted_in(space &Namespace, name string) bool {
199 return if isnil(space) {
200 name !in vm.imported
201 } else {
202 space.writable && name !in space.imported
203 }
204}
205
206fn (vm &VM) check_imported_assignment(space &Namespace, name string) ! {
207 if isnil(space) {
208 if name in vm.imported { return failure('cannot assign imported binding ${name}') }
209 return
210 }
211 if name in space.imported { return failure('cannot assign imported binding ${name}') }
212}
213
214fn (mut vm VM) bind_syntax_in(space &Namespace, name string, transformer &SyntaxTransformer) ! {
215 if isnil(space) {
216 if name in vm.imported { return failure('cannot redefine imported binding ${name}') }
217 vm.bind_syntax(name, transformer)
218 return
219 }
220 if !space.writable { return failure('environment does not permit global syntax definitions') }
221 if name in space.imported { return failure('cannot redefine imported binding ${name}') }
222 mut target := unsafe { &Namespace(space) }
223 if isnil(target.syntax) { target.syntax = &SyntaxBindings{} }
224 binding := SyntaxBinding{ kind: .transformer, name: name, transformer: transformer, top_level: true, namespace: space }
225 target.syntax.entries[name] = binding
226 vm.binding_epoch++
227 vm.note_syntax_location(space, name, binding)
228}
229
230fn (vm &VM) new_code(doc reader.Document, seed u64, space &Namespace, source_path string) &Code {
231 mut code := &Code{ doc: doc, namespace: space, source_path: source_path, syntax_seed: seed, expansion_limit: vm.options.max_expansions, compiler_vm: vm }
232 if doc.arena.has_labels() { code.enable_datum_graphs() }
233 code.known_globals = if isnil(space) { vm.bindings } else { &space.bindings }
234 if isnil(space) {
235 code.keyword_variables = vm.keyword_variables.clone()
236 if !isnil(vm.syntax) && vm.syntax.entries.len > 0 {
237 // Borrow the stable syntax-table object while compiling. Procedural
238 // transformer calls may update its entries; generated.globals is a private overlay.
239 code.syntax_base = vm.syntax
240 code.has_syntax = true
241 }
242 } else {
243 code.keyword_variables = space.keyword_variables.clone()
244 if !isnil(space.syntax) && space.syntax.entries.len > 0 {
245 code.syntax_base = space.syntax
246 code.has_syntax = true
247 }
248 }
249 return code
250}
251