vile / scheme / quasiquote_graph.v
113 lines · 104 sloc · 3.22 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4import reader
5
6struct QQGraphJob {
7 id reader.NodeId
8 depth int
9 leaving bool
10}
11
12struct QQGraphScratch {
13mut:
14 storage arena.Arena
15 active map[reader.NodeId]bool
16 done map[u64]bool
17 jobs []QQGraphJob
18}
19
20fn new_qq_graph_scratch() &QQGraphScratch {
21 mut storage := arena.new(chunk_size: 1024)
22 mut published := false
23 storage.push()
24 defer {
25 storage.pop()
26 if !published { storage.free() }
27 }
28 mut scratch := &QQGraphScratch{ storage: storage }
29 unsafe { scratch.jobs.flags.set(.noslices) }
30 published = true
31 return scratch
32}
33
34fn (mut scratch QQGraphScratch) add(id reader.NodeId, depth int, leaving bool) {
35 if scratch.jobs.len == scratch.jobs.cap {
36 scratch.reserve_job()
37 }
38 scratch.jobs << QQGraphJob{id, depth, leaving}
39}
40
41fn (mut scratch QQGraphScratch) reserve_job() {
42 scratch.storage.push()
43 defer { scratch.storage.pop() }
44 scratch.jobs.ensure_cap(scratch.jobs.len + 1)
45}
46
47fn (mut scratch QQGraphScratch) mark(id reader.NodeId, key u64, leaving bool) {
48 // Only pointer-free traversal storage is allocated in this scope. Reader,
49 // name caches, errors and emitted values always keep their outer owner.
50 scratch.storage.push()
51 defer { scratch.storage.pop() }
52 if leaving {
53 scratch.active.delete(id)
54 scratch.done[key] = true
55 } else {
56 scratch.active[id] = true
57 }
58}
59
60fn (mut scratch QQGraphScratch) release() {
61 // free clears its receiver after releasing the region containing this header.
62 // Keep that receiver as an independent, stack-local handle for the final call.
63 mut storage := scratch.storage
64 storage.free()
65}
66
67// R7RS-small §2.4 rejects circular quasiquote templates. Shared acyclic nodes
68// remain valid. An active unquote's argument is an expression, so its compiler
69// handles any literal graph inside that expression using the ordinary rules.
70// Completed visits include depth: the same node can have different meaning at
71// different nesting depths. Active-path identity does not need that distinction.
72fn (mut b Builder) qq_graph_active(id reader.NodeId, depth int) !bool {
73 mut scratch := new_qq_graph_scratch()
74 defer { scratch.release() }
75 scratch.add(id, depth, false)
76 mut active := false
77 for scratch.jobs.len > 0 {
78 job := scratch.jobs.pop()
79 if job.depth > 512 { return failure('quasiquote nesting limit exceeded') }
80 canonical := b.code.canonical_syntax_id(job.id)!
81 key := syntax_capture_key(int(canonical), i32(job.depth))
82 if job.leaving {
83 scratch.mark(canonical, key, true)
84 continue
85 }
86 if canonical in scratch.active {
87 return failure('cannot evaluate circular quasiquote syntax')
88 }
89 if key in scratch.done { continue }
90 node := b.code.node(job.id)!
91 if node.kind !in [.pair, .vector] { continue }
92 scratch.mark(canonical, key, false)
93 scratch.add(canonical, job.depth, true)
94 if node.kind == .pair {
95 name, argument := b.qq_marker(job.id)!
96 if name != '' {
97 if name != 'quasiquote' && job.depth == 1 {
98 active = true
99 } else {
100 scratch.add(argument, job.depth + if name == 'quasiquote' { 1 } else { -1 }, false)
101 }
102 } else {
103 scratch.add(node.cdr, job.depth, false)
104 scratch.add(node.car, job.depth, false)
105 }
106 } else {
107 for index in 0 .. node.count {
108 scratch.add(b.code.vector_at(job.id, index)!, job.depth, false)
109 }
110 }
111 }
112 return active
113}
114