vile / scheme / quasisyntax.v
169 lines · 161 sloc · 6.34 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5// Expressions are captured once, outside template repetitions. Private splice
6// variables carry their list unchanged; the renderer copies only output cells.
7struct QuasisyntaxPlan {
8mut:
9 patterns []reader.NodeId
10 inputs []reader.NodeId
11 splices []string
12}
13
14fn (mut b Builder) quasisyntax_marker(id reader.NodeId) !(string, []reader.NodeId) {
15 node := b.code.node(id)!
16 if node.kind != .pair || b.code.node(node.car)!.kind != .symbol { return '', []reader.NodeId{} }
17 name := b.keyword_name(node.car)!
18 if name !in ['quasisyntax', 'unsyntax', 'unsyntax-splicing'] { return '', []reader.NodeId{} }
19 parts := b.code.nodes(id)!
20 if name == 'quasisyntax' && parts.len != 2 { return failure('quasisyntax needs one template') }
21 return name, parts
22}
23
24// Reuse the existing pointer-free quasiquote work arena. Graph paths are checked
25// before rebuilding, including cycles whose nesting depth would otherwise grow.
26// Active operands are expressions and retain the ordinary compiler's graph rules.
27fn (mut b Builder) validate_quasisyntax_graph(id reader.NodeId) ! {
28 mut scratch := new_qq_graph_scratch()
29 defer { scratch.release() }
30 scratch.add(id, 1, false)
31 for scratch.jobs.len > 0 {
32 job := scratch.jobs.pop()
33 if job.depth > 512 { return failure('quasisyntax nesting limit exceeded') }
34 canonical := if isnil(b.code.graphs) { job.id } else { b.code.canonical_syntax_id(job.id)! }
35 key := syntax_capture_key(int(canonical), i32(job.depth))
36 if job.leaving {
37 scratch.mark(canonical, key, true)
38 continue
39 }
40 if !isnil(b.code.graphs) {
41 if canonical in scratch.active {
42 return failure('cannot evaluate circular quasisyntax template')
43 }
44 if key in scratch.done { continue }
45 }
46 node := b.code.node(job.id)!
47 if node.kind !in [.pair, .vector] { continue }
48 if !isnil(b.code.graphs) {
49 scratch.mark(canonical, key, false)
50 scratch.add(canonical, job.depth, true)
51 }
52 if node.kind == .pair {
53 name, parts := b.quasisyntax_marker(job.id)!
54 if name != '' {
55 if name == 'quasisyntax' || job.depth > 1 {
56 for argument in parts[1..] {
57 scratch.add(argument, job.depth + if name == 'quasisyntax' { 1 } else { -1 }, false)
58 }
59 }
60 } else {
61 scratch.add(node.cdr, job.depth, false)
62 scratch.add(node.car, job.depth, false)
63 }
64 } else {
65 for i in 0 .. node.count { scratch.add(b.code.vector_at(job.id, i)!, job.depth, false) }
66 }
67 }
68}
69
70fn (mut b Builder) quasisyntax_form(nodes []reader.NodeId, tail bool, origin reader.NodeId) ! {
71 check_arity('quasisyntax', nodes.len - 1, 1, 1)!
72 if !isnil(b.code.graphs) { b.validate_quasisyntax_graph(nodes[1])! }
73 mut plan := QuasisyntaxPlan{}
74 template := b.quasisyntax_template(nodes[1], 1, 0, false, mut plan)!
75 if plan.inputs.len == 0 {
76 b.syntax_template(template, origin)!
77 return
78 }
79 pattern := b.code.syntax_list(plan.patterns, origin)!
80 clause := b.code.syntax_list([pattern, template], origin)!
81 b.syntax_case_dispatch([], [clause], reader.no_node, plan.inputs, .template, plan.splices, tail, origin)!
82}
83
84fn (mut b Builder) quasisyntax_capture(expression reader.NodeId, splice bool, mut plan QuasisyntaxPlan) !reader.NodeId {
85 mut generated := b.code.syntax_arena()
86 if generated.serial == ~u64(0) {
87 return failure('generated identifier count exceeds supported range')
88 }
89 generated.serial++
90 // External NUL-leading names use a distinct escaped-source key space.
91 name := '\x00~q${b.code.syntax_seed}.${generated.serial}'
92 variable := b.code.syntax_symbol(name, 'quasisyntax-value', expression)!
93 plan.patterns << variable
94 plan.inputs << expression
95 if splice { plan.splices << name }
96 return variable
97}
98
99fn (mut b Builder) quasisyntax_element(id reader.NodeId, depth int, nesting int, mut plan QuasisyntaxPlan, mut output []reader.NodeId) ! {
100 name, parts := b.quasisyntax_marker(id)!
101 if depth == 1 && name in ['unsyntax', 'unsyntax-splicing'] {
102 for expression in parts[1..] {
103 output << b.quasisyntax_capture(expression, name == 'unsyntax-splicing', mut plan)!
104 }
105 } else {
106 output << b.quasisyntax_template(id, depth, nesting, true, mut plan)!
107 }
108}
109
110// Cdr chains iterate; recursion is limited to actual nesting. Unchanged regions
111// keep their original source IDs and definition-site contexts.
112fn (mut b Builder) quasisyntax_template(id reader.NodeId, depth int, nesting int, element bool, mut plan QuasisyntaxPlan) !reader.NodeId {
113 if nesting > 512 || depth > 512 { return failure('quasisyntax nesting limit exceeded') }
114 name, parts := b.quasisyntax_marker(id)!
115 if name != '' {
116 if name != 'quasisyntax' && !element && parts.len != 2 {
117 return failure('${name} with zero or multiple operands needs a list or vector element position')
118 }
119 if depth == 1 && name != 'quasisyntax' {
120 if name == 'unsyntax-splicing' {
121 return failure('unsyntax-splicing needs a list or vector element position')
122 }
123 return b.quasisyntax_capture(parts[1], false, mut plan)
124 }
125 mut output := []reader.NodeId{cap: parts.len}
126 output << parts[0]
127 if name == 'quasisyntax' {
128 output << b.quasisyntax_template(parts[1], depth + 1, nesting + 1, false, mut plan)!
129 } else {
130 for argument in parts[1..] {
131 b.quasisyntax_element(argument, depth - 1, nesting + 1, mut plan, mut output)!
132 }
133 }
134 if output == parts { return id }
135 return b.code.syntax_list(output, id)
136 }
137 node := b.code.node(id)!
138 if node.kind == .vector {
139 mut output := []reader.NodeId{cap: node.count}
140 mut changed := false
141 for i in 0 .. node.count {
142 item := b.code.vector_at(id, i)!
143 before := output.len
144 b.quasisyntax_element(item, depth, nesting + 1, mut plan, mut output)!
145 if output.len != before + 1 || output[before] != item { changed = true }
146 }
147 return if changed { b.code.syntax_vector(output, id)! } else { id }
148 }
149 if node.kind != .pair { return id }
150 mut output := []reader.NodeId{}
151 mut cursor := id
152 mut changed := false
153 for {
154 pair := b.code.node(cursor)!
155 if pair.kind != .pair { break }
156 if cursor != id {
157 marker, _ := b.quasisyntax_marker(cursor)!
158 if marker != '' { break }
159 }
160 before := output.len
161 b.quasisyntax_element(pair.car, depth, nesting + 1, mut plan, mut output)!
162 if output.len != before + 1 || output[before] != pair.car { changed = true }
163 cursor = pair.cdr
164 }
165 mut result := b.quasisyntax_template(cursor, depth, nesting + 1, false, mut plan)!
166 if !changed && result == cursor { return id }
167 for i := output.len - 1; i >= 0; i-- { result = b.code.syntax_pair(output[i], result, id)! }
168 return result
169}
170