vile / scheme / syntax_object.v
85 lines · 73 sloc · 3.04 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// A document is allocated once for one frozen syntax graph. Individual
4// wrapped subforms are Value handles, not separately allocated objects.
5// Binding owners distinguish locations from different embedding VMs.
6@[heap]
7struct SyntaxObjectData {
8 tree &SyntaxTree
9 owner &Owner = unsafe { nil }
10 context &SyntaxLookupContext = unsafe { nil }
11}
12
13struct SyntaxObjectRef {
14 data &SyntaxObjectData
15 index i32
16}
17
18fn wrapped_syntax(data &SyntaxObjectData, index int) !Value {
19 if index < 0 || index >= data.tree.nodes.len || index > 2147483647 {
20 return failure('invalid syntax node index')
21 }
22 return Value{ tag: .syntax, syntax_index: i32(index), bits: ValueBits{ payload: data } }
23}
24
25fn syntax_object_ref(value Value) !SyntaxObjectRef {
26 if value.tag != .syntax { return failure('expected a syntax object') }
27 return SyntaxObjectRef{unsafe { &SyntaxObjectData(value.pointer()) }, value.syntax_index}
28}
29
30fn syntax_identifier(value Value) bool {
31 if value.tag != .syntax { return false }
32 object := unsafe { &SyntaxObjectData(value.pointer()) }
33 return object.tree.nodes[value.syntax_index].kind == .symbol
34}
35
36fn syntax_identifier_info(value Value) !&SyntaxIdentifier {
37 object := syntax_object_ref(value)!
38 node := object.data.tree.nodes[object.index]
39 if node.kind != .symbol { return failure('expected a syntax identifier') }
40 return &object.data.tree.identifiers[node.car]
41}
42
43fn bound_syntax_identifier_equal(left Value, right Value) !bool {
44 a := syntax_identifier_info(left)!
45 b := syntax_identifier_info(right)!
46 return a.raw_name == b.raw_name && same_syntax_marks(a.wrap, b.wrap)
47}
48
49fn free_syntax_identifier_equal(left Value, right Value) !bool {
50 a := syntax_identifier_info(left)!
51 b := syntax_identifier_info(right)!
52 x := syntax_object_ref(left)!
53 y := syntax_object_ref(right)!
54 return runtime_syntax_binding_equal(a.binding, b.binding, x.data.owner, y.data.owner)
55}
56
57fn syntax_car(value Value) !Value {
58 object := syntax_object_ref(value)!
59 node := object.data.tree.nodes[object.index]
60 if node.kind != .pair { return failure('expected list-structured syntax') }
61 return wrapped_syntax(object.data, int(node.car))
62}
63
64fn syntax_cdr(value Value) !Value {
65 object := syntax_object_ref(value)!
66 node := object.data.tree.nodes[object.index]
67 if node.kind != .pair { return failure('expected list-structured syntax') }
68 return wrapped_syntax(object.data, int(node.cdr))
69}
70
71fn syntax_vector_ref(value Value, index int) !Value {
72 object := syntax_object_ref(value)!
73 node := object.data.tree.nodes[object.index]
74 if node.kind != .vector { return failure('expected vector-structured syntax') }
75 if index < 0 || index >= int(node.cdr) { return failure('syntax vector index out of range') }
76 return wrapped_syntax(object.data, int(object.data.tree.elements[int(node.car) + index]))
77}
78
79fn syntax_identifier_context(value Value) !&SyntaxLookupContext {
80 info := syntax_identifier_info(value)!
81 if !isnil(info.context) { return info.context }
82 object := syntax_object_ref(value)!
83 if !isnil(object.data.context) { return object.data.context }
84 return object.data.tree.context
85}
86