| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | @[heap] |
| 6 | struct SyntaxCaseValues { |
| 7 | mut: |
| 8 | items map[u64]Value |
| 9 | } |
| 10 | |
| 11 | // Read-only singleton: V may heap-promote an address-taken local even for |
| 12 | // an empty struct. Every empty input has the same immutable structural view. |
| 13 | struct SyntaxCaseEmptySource {} |
| 14 | |
| 15 | const syntax_case_empty_source = SyntaxCaseEmptySource{} |
| 16 | |
| 17 | fn (source &SyntaxCaseEmptySource) value(_ reader.NodeId) Value { |
| 18 | return nil_value() |
| 19 | } |
| 20 | |
| 21 | fn case_input_node[T](source &T, id i32) !reader.Node { |
| 22 | $if T is SyntaxCaseEmptySource { |
| 23 | return reader.Node{ kind: .empty_list } |
| 24 | } $else $if T is SyntaxObjectData { |
| 25 | return graph_source_node(source.tree, reader.NodeId(id)) |
| 26 | } $else { |
| 27 | return graph_source_node(source, reader.NodeId(id)) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | fn case_input_vector_at[T](source &T, id i32, index int) !i32 { |
| 32 | $if T is SyntaxCaseEmptySource { |
| 33 | return failure('empty syntax has no vector elements') |
| 34 | } $else $if T is SyntaxObjectData { |
| 35 | return i32(graph_source_vector_at(source.tree, reader.NodeId(id), index)!) |
| 36 | } $else { |
| 37 | return i32(graph_source_vector_at(source, reader.NodeId(id), index)!) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn case_input_atom[T](source &T, id i32) !Value { |
| 42 | $if T is SyntaxObjectData { |
| 43 | return graph_source_atom(source.tree, reader.NodeId(id)) |
| 44 | } $else { |
| 45 | value := source.value(reader.NodeId(id)) |
| 46 | if value.tag != .syntax { return value } |
| 47 | object := syntax_object_ref(value)! |
| 48 | return graph_source_atom(object.data.tree, reader.NodeId(object.index)) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | fn case_literal_binding(literal SyntaxBinding, actual SyntaxBinding, owner &Owner, input_owner &Owner) bool { |
| 53 | return runtime_syntax_binding_equal(literal, actual, owner, input_owner) |
| 54 | } |
| 55 | |
| 56 | fn case_input_literal[T](matcher &SyntaxCaseMatcher, source &T, id i32, literal SyntaxBinding) !bool { |
| 57 | $if T is SyntaxObjectData { |
| 58 | actual := source.tree.identifiers[source.tree.nodes[id].car].binding |
| 59 | return case_literal_binding(literal, actual, matcher.owner, source.owner) |
| 60 | } $else { |
| 61 | value := source.value(reader.NodeId(id)) |
| 62 | if !syntax_identifier(value) { return false } |
| 63 | info := syntax_identifier_info(value)! |
| 64 | object := syntax_object_ref(value)! |
| 65 | return case_literal_binding(literal, info.binding, matcher.owner, object.data.owner) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn capture_case_value[T](source &T, id i32, variable int, path i32, mut captures SyntaxCaseCaptures) ! { |
| 70 | key := syntax_capture_key(variable, path) |
| 71 | $if T is SyntaxObjectData { |
| 72 | captures.coordinates.items[key] = id |
| 73 | } $else { |
| 74 | capture_case_raw(source.value(reader.NodeId(id)), variable, path, mut captures) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | fn capture_case_raw(value Value, variable int, path i32, mut captures SyntaxCaseCaptures) { |
| 79 | if isnil(captures.values) { captures.values = &SyntaxCaseValues{} } |
| 80 | captures.values.items[syntax_capture_key(variable, path)] = value |
| 81 | } |
| 82 | |