| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | struct SyntaxTemplateRegion { |
| 7 | key u32 |
| 8 | captures bool |
| 9 | } |
| 10 | |
| 11 | struct SyntaxTemplateSharing { |
| 12 | regions []SyntaxTemplateRegion |
| 13 | dynamic bool |
| 14 | } |
| 15 | |
| 16 | fn template_sharing(tree &SyntaxTree, traits []u8, dynamic_only bool) &SyntaxTemplateSharing { |
| 17 | mut count := 0 |
| 18 | mut dynamic := false |
| 19 | for id, node in tree.nodes { |
| 20 | if node.kind !in [.pair, .vector] { continue } |
| 21 | for escaped in 0 .. 2 { |
| 22 | flags := traits[id * 2 + escaped] |
| 23 | if flags & 16 == 0 || (dynamic_only && flags & 2 == 0) { continue } |
| 24 | count++ |
| 25 | if flags & 2 != 0 { dynamic = true } |
| 26 | } |
| 27 | } |
| 28 | if count == 0 { return unsafe { nil } } |
| 29 | mut regions := []SyntaxTemplateRegion{cap: count} |
| 30 | for id, node in tree.nodes { |
| 31 | if node.kind !in [.pair, .vector] { continue } |
| 32 | for escaped in 0 .. 2 { |
| 33 | flags := traits[id * 2 + escaped] |
| 34 | if flags & 16 == 0 || (dynamic_only && flags & 2 == 0) { continue } |
| 35 | regions << SyntaxTemplateRegion{u32(id * 2 + escaped), flags & 2 != 0} |
| 36 | } |
| 37 | } |
| 38 | return &SyntaxTemplateSharing{regions, dynamic} |
| 39 | } |
| 40 | |
| 41 | const no_template_memo_key = ~u64(0) |
| 42 | |
| 43 | struct SyntaxTemplateSpine[T] { |
| 44 | mut: |
| 45 | keys []u64 |
| 46 | previous []T |
| 47 | } |
| 48 | |
| 49 | struct SyntaxTemplateMemo[T] { |
| 50 | storage arena.Arena |
| 51 | sharing &SyntaxTemplateSharing |
| 52 | mut: |
| 53 | completed map[u64]T |
| 54 | paths map[u64]u32 |
| 55 | path u32 |
| 56 | next_path u32 = 1 |
| 57 | spines []&SyntaxTemplateSpine[T] |
| 58 | depth int |
| 59 | } |
| 60 | |
| 61 | fn new_syntax_template_memo[T](storage arena.Arena, sharing &SyntaxTemplateSharing) &SyntaxTemplateMemo[T] { |
| 62 | storage.push() |
| 63 | defer { storage.pop() } |
| 64 | return &SyntaxTemplateMemo[T]{ storage: storage, sharing: sharing } |
| 65 | } |
| 66 | |
| 67 | @[inline] |
| 68 | fn (memo &SyntaxTemplateMemo[T]) key(id i32, escaped bool) u64 { |
| 69 | region := template_region_key(id, escaped) |
| 70 | mut low := 0 |
| 71 | mut high := memo.sharing.regions.len |
| 72 | for low < high { |
| 73 | middle := low + (high - low) / 2 |
| 74 | if memo.sharing.regions[middle].key < u32(region) { low = middle + 1 } else { high = middle } |
| 75 | } |
| 76 | if low == memo.sharing.regions.len || memo.sharing.regions[low].key != u32(region) { return no_template_memo_key } |
| 77 | coordinate := if memo.sharing.regions[low].captures { memo.path } else { u32(0) } |
| 78 | return (region << 32) | u64(coordinate) |
| 79 | } |
| 80 | |
| 81 | fn (mut memo SyntaxTemplateMemo[T]) keep(key u64, value T) { |
| 82 | if key == no_template_memo_key { return } |
| 83 | memo.storage.push() |
| 84 | defer { memo.storage.pop() } |
| 85 | memo.completed[key] = value |
| 86 | } |
| 87 | |
| 88 | fn (mut memo SyntaxTemplateMemo[T]) enter(index int) !u32 { |
| 89 | previous := memo.path |
| 90 | if !memo.sharing.dynamic { return previous } |
| 91 | key := (u64(previous) << 32) | u64(u32(index)) |
| 92 | if path := memo.paths[key] { |
| 93 | memo.path = path |
| 94 | return previous |
| 95 | } |
| 96 | if memo.next_path >= u32(2147483647) { return failure('expanded syntax template exceeds supported range') } |
| 97 | memo.path = memo.next_path |
| 98 | memo.next_path++ |
| 99 | memo.storage.push() |
| 100 | defer { memo.storage.pop() } |
| 101 | memo.paths[key] = memo.path |
| 102 | return previous |
| 103 | } |
| 104 | |
| 105 | fn (mut memo SyntaxTemplateMemo[T]) acquire_spine() &SyntaxTemplateSpine[T] { |
| 106 | if memo.depth == memo.spines.len { |
| 107 | memo.add_spine() |
| 108 | } |
| 109 | mut spine := memo.spines[memo.depth] |
| 110 | memo.depth++ |
| 111 | spine.keys.clear() |
| 112 | spine.previous.clear() |
| 113 | return spine |
| 114 | } |
| 115 | |
| 116 | fn (mut memo SyntaxTemplateMemo[T]) note_spine(mut spine SyntaxTemplateSpine[T], key u64, previous T) { |
| 117 | memo.storage.push() |
| 118 | defer { memo.storage.pop() } |
| 119 | spine.keys << key |
| 120 | spine.previous << previous |
| 121 | } |
| 122 | |
| 123 | // Region traits: ready=1, captures=2, escape removal=4, collected=8, shared=16. |
| 124 | fn (tree &SyntaxTree) template_region_traits(root i32, pattern &SyntaxPattern, storage arena.Arena) !([]u8, []i64) { |
| 125 | mut traits, mut pending := new_template_analysis(storage, tree.nodes.len) |
| 126 | template_analysis_push(mut pending, storage, i64(template_region_key(root, false))) |
| 127 | for pending.len > 0 { |
| 128 | visit := pending.pop() |
| 129 | key := if visit < 0 { ~visit } else { visit } |
| 130 | if traits[key] & 1 != 0 { |
| 131 | if visit >= 0 { traits[key] |= 16 } |
| 132 | continue |
| 133 | } |
| 134 | id := i32(key >> 1) |
| 135 | escaped := key & 1 != 0 |
| 136 | node := tree.nodes[id] |
| 137 | if visit < 0 { |
| 138 | mut flags := u8(1) |
| 139 | if node.kind == .pair { |
| 140 | if !escaped && tree.ellipsis(node.car) { |
| 141 | flags |= (traits[template_region_key(tree.escaped_template(id)!, true)] & 2) | 4 |
| 142 | } else { |
| 143 | after := tree.template_region_tail(node.cdr, escaped) |
| 144 | flags |= (traits[template_region_key(node.car, escaped)] | traits[template_region_key(after, escaped)]) & 6 |
| 145 | } |
| 146 | } else if node.kind == .vector { |
| 147 | for i in 0 .. node.cdr { |
| 148 | flags |= traits[template_region_key(tree.elements[node.car + i], escaped)] & 6 |
| 149 | } |
| 150 | } else if node.kind == .symbol && node.car in pattern.indices { |
| 151 | flags |= 2 |
| 152 | } |
| 153 | traits[key] = flags |
| 154 | continue |
| 155 | } |
| 156 | template_analysis_push(mut pending, storage, ~key) |
| 157 | if node.kind == .pair { |
| 158 | if !escaped && tree.ellipsis(node.car) { |
| 159 | template_analysis_push(mut pending, storage, i64(template_region_key(tree.escaped_template(id)!, true))) |
| 160 | } else { |
| 161 | template_analysis_push(mut pending, storage, i64(template_region_key(tree.template_region_tail(node.cdr, escaped), escaped))) |
| 162 | template_analysis_push(mut pending, storage, i64(template_region_key(node.car, escaped))) |
| 163 | } |
| 164 | } else if node.kind == .vector { |
| 165 | for i in 0 .. node.cdr { |
| 166 | template_analysis_push(mut pending, storage, i64(template_region_key(tree.elements[node.car + i], escaped))) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | return traits, pending |
| 171 | } |
| 172 | |
| 173 | fn (mut memo SyntaxTemplateMemo[T]) add_spine() { |
| 174 | memo.storage.push() |
| 175 | defer { memo.storage.pop() } |
| 176 | memo.spines << &SyntaxTemplateSpine[T]{} |
| 177 | } |
| 178 | |