vile / scheme / quasiquote.v
202 lines · 193 sloc · 5.74 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5struct QQPending {
6 id reader.NodeId
7 depth int
8}
9
10struct QQPart {
11 pair reader.NodeId
12 car reader.NodeId
13 splice bool
14 active bool
15}
16
17struct QQSegment {
18 op Op
19 constant int
20 origin reader.NodeId
21}
22
23// Recognize the complete long-form notation, including reader abbreviations.
24fn (mut b Builder) qq_marker(id reader.NodeId) !(string, reader.NodeId) {
25 n := b.code.node(id)!
26 if n.kind != .pair { return '', reader.no_node }
27 head := b.code.node(n.car)!
28 if head.kind != .symbol { return '', reader.no_node }
29 name := b.keyword_name(n.car)!
30 if name !in ['quasiquote', 'unquote', 'unquote-splicing'] { return '', reader.no_node }
31 parts := b.code.nodes(id)!
32 if parts.len != 2 { return failure('${name} needs one template or expression') }
33 return name, parts[1]
34}
35
36// A static template is a single constant instruction. The scan is iterative so
37// a long flat list never consumes the V call stack. Nesting levels, rather than
38// the spelling of a comma alone, determine whether evaluation is required.
39fn (mut b Builder) qq_active(id reader.NodeId, depth int) !bool {
40 if !isnil(b.code.graphs) { return b.qq_graph_active(id, depth) }
41 mut pending := [QQPending{id, depth}]
42 for pending.len > 0 {
43 item := pending.pop()
44 n := b.code.node(item.id)!
45 if n.kind == .pair {
46 name, argument := b.qq_marker(item.id)!
47 if name != '' {
48 if name != 'quasiquote' && item.depth == 1 { return true }
49 pending << QQPending{argument, item.depth + if name == 'quasiquote' {
50 1
51 } else {
52 -1
53 }}
54 } else {
55 pending << QQPending{n.cdr, item.depth}
56 pending << QQPending{n.car, item.depth}
57 }
58 } else if n.kind == .vector {
59 for i in 0 .. n.count {
60 pending << QQPending{b.code.vector_at(item.id, i)!, item.depth}
61 }
62 }
63 }
64 return false
65}
66
67fn (mut b Builder) quasiquote(id reader.NodeId, depth int) ! {
68 if !b.qq_active(id, depth)! {
69 b.constant(b.code.datum(id)!, id)
70 return
71 }
72 name, argument := b.qq_marker(id)!
73 if name != '' {
74 if depth == 1 && name == 'unquote' {
75 b.expression(argument, false)
76 return
77 }
78 if depth == 1 && name == 'unquote-splicing' {
79 return failure('unquote-splicing needs a list or vector element position')
80 }
81 n := b.code.node(id)!
82 b.constant(b.code.datum(n.car)!, id)
83 b.quasiquote(argument, depth + if name == 'quasiquote' { 1 } else { -1 })!
84 b.constant(nil_value(), id)
85 b.emit(.qq_cons, 0, 0, id)
86 b.emit(.qq_cons, 0, 0, id)
87 return
88 }
89 n := b.code.node(id)!
90 if n.kind == .vector {
91 mut flags := []u8{len: n.count}
92 mut splices := 0
93 for i in 0 .. n.count {
94 item := b.code.vector_at(id, i)!
95 marker, expression := b.qq_marker(item)!
96 if depth == 1 && marker == 'unquote-splicing' {
97 b.expression(expression, false)
98 flags[i] = 1
99 splices++
100 } else {
101 b.quasiquote(item, depth)!
102 }
103 }
104 constant := b.function.constants.len
105 b.function.constants << literal_value(owned_string(flags.bytestr(), flags.len))
106 b.emit(.qq_vector, constant, splices, id)
107 return
108 }
109 // The active template must be a pair here. Plan its cdr chain once and keep
110 // a static suffix as one literal, instead of allocating every suffix pair.
111 mut parts := []QQPart{}
112 mut current := id
113 mut last_active := -1
114 for {
115 node := b.code.node(current)!
116 if node.kind != .pair { break }
117 marker, _ := b.qq_marker(current)!
118 if marker != '' { break }
119 active := b.qq_active(node.car, depth)!
120 if active { last_active = parts.len }
121 head_marker, _ := b.qq_marker(node.car)!
122 parts << QQPart{current, node.car, depth == 1 && head_marker == 'unquote-splicing', active}
123 current = node.cdr
124 }
125 tail_active := b.qq_active(current, depth)!
126 count := if tail_active { parts.len } else { last_active + 1 }
127 mut segments := []QQSegment{}
128 for i := 0; i < count; {
129 if parts[i].active {
130 if parts[i].splice {
131 _, expression := b.qq_marker(parts[i].car)!
132 b.expression(expression, false)
133 } else {
134 b.quasiquote(parts[i].car, depth)!
135 }
136 segments << QQSegment{if parts[i].splice { Op.qq_splice } else { Op.qq_cons }, 0, parts[i].pair}
137 i++
138 } else {
139 start := i
140 for i < count && !parts[i].active { i++ }
141 if i - start == 1 {
142 b.constant(b.code.datum(parts[start].car)!, parts[start].car)
143 segments << QQSegment{Op.qq_cons, 0, parts[start].pair}
144 } else {
145 mut items := []Value{len: i - start}
146 for j in start .. i { items[j - start] = b.code.datum(parts[j].car)! }
147 constant := b.function.constants.len
148 b.function.constants << literal_value(vector(items))
149 segments << QQSegment{Op.qq_prefix, constant, parts[start].pair}
150 }
151 }
152 }
153 if tail_active {
154 b.quasiquote(current, depth)!
155 } else {
156 suffix := if count < parts.len { parts[count].pair } else { current }
157 b.constant(b.code.datum(suffix)!, id)
158 }
159 for i := segments.len - 1; i >= 0; i-- {
160 b.emit(segments[i].op, segments[i].constant, 0, segments[i].origin)
161 }
162}
163
164fn splice_onto(prefix Value, tail Value) !Value {
165 count := prefix.list_length()!
166 mut current := prefix
167 mut builder := ListBuilder{}
168 for _ in 0 .. count {
169 pair := current.pair_pointer()
170 builder.push(pair.car)
171 current = pair.cdr
172 }
173 return builder.finish(tail)
174}
175
176fn splice_vector(values []Value, flags string) !Value {
177 mut count := values.len
178 for i, flag in flags {
179 if flag == 1 {
180 extra := values[i].list_length()!
181 if extra > max_int - count { return failure('vector length exceeds supported range') }
182 count += extra - 1
183 }
184 }
185 mut items := value_storage(count)
186 mut offset := 0
187 for i, value in values {
188 if flags[i] == 0 {
189 container_store_validated(mut items, offset, value)
190 offset++
191 } else {
192 mut current := value
193 for current.tag == .pair {
194 pair := current.pair_pointer()
195 container_store_validated(mut items, offset, pair.car)
196 offset++
197 current = pair.cdr
198 }
199 }
200 }
201 return payload_value(.vector, &Vector{ items: items })
202}
203