vile / scheme / eval_datum.v
160 lines · 145 sloc · 6.38 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5const syntax_active_bit = u64(1) << 63
6
7struct DatumCompiler {
8mut:
9 code &Code
10 // A single table records syntax indices and DFS state, preserving sharing
11 // and detecting graph input. Quoted cycles are data; the compiler validates
12 // executable structure after conversion. No reversed flat-list array is needed.
13 indices map[voidptr]u64
14}
15
16fn (mut converter DatumCompiler) convert(value Value, depth int) !reader.NodeId {
17 if depth > reader.max_depth { return failure('eval datum nesting limit exceeded') }
18 if value.tag == .nil { return reader.nil_node }
19 if value.tag !in [.pair, .vector] {
20 return converter.code.syntax_atom(value, reader.no_node)
21 }
22 pointer := value.pointer()
23 if state := converter.indices[pointer] {
24 converter.code.enable_datum_graphs()
25 return reader.NodeId(-2 - int(state & ~syntax_active_bit))
26 }
27 if value.tag == .pair { return converter.pair(value, depth) }
28 items := value.vector_pointer().items
29 mut arena := converter.code.syntax_arena()
30 if items.len > 2147483647 - arena.elements.len {
31 return failure('eval vector exceeds supported range')
32 }
33 start := arena.elements.len
34 id := converter.code.syntax_node(SyntaxNode{ kind: .vector, car: reader.NodeId(start), cdr: reader.NodeId(items.len) })!
35 index := u64(-int(id) - 2)
36 converter.indices[pointer] = index | syntax_active_bit
37 arena.elements.ensure_cap(start + items.len)
38 // Only indices live here. Recursive conversion may grow the element arena;
39 // it cannot inspect these slots, and each is filled before compilation starts.
40 unsafe { arena.elements.len = start + items.len }
41 for i, item in items { arena.elements[start + i] = converter.convert(item, depth + 1)! }
42 converter.indices[pointer] = index
43 return id
44}
45
46fn (mut converter DatumCompiler) pair(root Value, depth int) !reader.NodeId {
47 mut cursor := root
48 mut first := reader.no_node
49 mut last := reader.no_node
50 mut tail := reader.nil_node
51 for cursor.tag == .pair {
52 pointer := cursor.pointer()
53 if state := converter.indices[pointer] {
54 converter.code.enable_datum_graphs()
55 tail = reader.NodeId(-2 - int(state & ~syntax_active_bit))
56 break
57 }
58 id := converter.code.syntax_node(SyntaxNode{ kind: .pair })!
59 converter.indices[pointer] = u64(-int(id) - 2) | syntax_active_bit
60 if first == reader.no_node { first = id }
61 if last != reader.no_node {
62 converter.code.generated.nodes[-int(last) - 2] = SyntaxNode{ ...converter.code.generated.nodes[-int(last) - 2], cdr: id }
63 }
64 last = id
65 pair := cursor.pair_pointer()
66 car := converter.convert(pair.car, depth + 1)!
67 converter.code.generated.nodes[-int(id) - 2] = SyntaxNode{ ...converter.code.generated.nodes[-int(id) - 2], car: car }
68 cursor = pair.cdr
69 if cursor.tag != .pair {
70 tail = converter.convert(cursor, depth + 1)!
71 break
72 }
73 }
74 converter.code.generated.nodes[-int(last) - 2] = SyntaxNode{ ...converter.code.generated.nodes[-int(last) - 2], cdr: tail }
75 // Complete this flat spine after all its cars/tail have been traversed.
76 // Walking existing links avoids retaining a second array of pair pointers.
77 cursor = root
78 for cursor.tag == .pair {
79 pointer := cursor.pointer()
80 state := converter.indices[pointer]
81 if state & syntax_active_bit == 0 { break }
82 converter.indices[pointer] = state & ~syntax_active_bit
83 cursor = cursor.pair_pointer().cdr
84 }
85 return first
86}
87
88fn (mut vm VM) compile_value(expression Value, environment Value) !&Function {
89 return vm.compile_value_at(expression, environment, '')
90}
91
92@[inline]
93fn (mut vm VM) compile_value_at(expression Value, environment Value, source_path string) !&Function {
94 if vm.uses_bounded_operations() {
95 return vm.owner.bounded_files.compile_value(mut vm, expression, environment, source_path)
96 }
97 return vm.compile_value_inner(expression, environment, source_path)
98}
99
100fn (mut vm VM) compile_value_inner(expression Value, environment Value, source_path string) !&Function {
101 space := vm.environment_space(environment)!
102 mut code := vm.new_code(reader.Document{}, vm.next_context(), space, source_path)
103 mut converter := DatumCompiler{ code: code }
104 root := converter.convert(expression, 0)!
105 return source_program(code, [root], false)
106}
107
108// eval_value evaluates a runtime datum in a same-VM environment specifier,
109// without printing or reparsing it. Host entries have the same continuation
110// boundary as eval(source); Scheme eval runs on its caller's existing machine.
111pub fn (mut vm VM) eval_value(expression Value, environment Value) !Value {
112 vm.ready()!
113 vm.check_entry_control()!
114 function := vm.compile_value(expression, environment)!
115 return vm.run_program(function, vm.next_context())
116}
117
118const eval_native_descriptors = [
119 NativeDescriptor{'scheme-report-environment', NativeFn(eval_report_environment)}
120 NativeDescriptor{'null-environment', NativeFn(eval_null_environment)}
121 NativeDescriptor{'interaction-environment', NativeFn(eval_interaction_environment)},
122]!
123
124fn (mut vm VM) install_eval_primitives() {
125 function := &Function{ code: unsafe { nil }, params: 2, slot_count: 2, instructions: [bridge_instruction(.eval_enter, 0, 0)] }
126 vm.bind_global('eval', procedure_value(&Procedure{ mode: .closure, owner: vm.owner, name: 'eval', function: function }))
127 for index in 0 .. eval_native_descriptors.len {
128 entry := &eval_native_descriptors[index]
129 vm.bind_global(entry.name, procedure_value(&Procedure{ mode: .native, owner: vm.owner, name: entry.name, native: entry.callback }))
130 }
131}
132
133fn eval_report_environment(mut vm VM, args []Value) !Value {
134 check_arity('scheme-report-environment', args.len, 1, 1)!
135 return vm.report_environment(int(args[0].as_integer()!))
136}
137
138fn eval_null_environment(mut vm VM, args []Value) !Value {
139 check_arity('null-environment', args.len, 1, 1)!
140 return vm.null_environment(int(args[0].as_integer()!))
141}
142
143fn eval_interaction_environment(mut vm VM, args []Value) !Value {
144 check_arity('interaction-environment', args.len, 0, 0)!
145 return vm.interaction_environment()
146}
147
148// Compilation is a cold procedure mode, kept out of ordinary application.
149@[noinline]
150fn (mut m Machine) enter_eval(mut vm VM) ! {
151 function := vm.compile_value_at(m.env.slots[0], m.env.slots[1], m.source_path())!
152 // The normal closure entry already checked arity/limits, preserved the
153 // caller's continuation and recorded its source site. Tail-replace only
154 // this private bridge; eval introduces no native-entry boundary or frame.
155 scope := m.checked_activation_for(vm, function, unsafe { nil }, true)!
156 m.truncate(m.base)
157 m.function = function
158 m.env = scope
159 m.ip = 0
160}
161