module scheme import arena // Wrapped inputs share one immutable document and keep indexed captures. // Partially unwrapped inputs retain original Values in a lazy sidecar. Both // paths reuse the same repetition coordinates without copying subtrees. struct SyntaxCaseCaptures { pattern &SyntaxPattern mut: input &SyntaxObjectData = unsafe { nil } coordinates SyntaxCaptures values &SyntaxCaseValues = unsafe { nil } } fn (captures &SyntaxCaseCaptures) value(variable int, indices []int) !Value { return captures.value_at(variable, indices, 0) } fn (captures &SyntaxCaseCaptures) value_at(variable int, indices []int, offset int) !Value { if offset < 0 || offset > indices.len { return failure('syntax-case capture rank mismatch') } if variable < 0 || variable >= captures.pattern.variables.len { return failure('invalid syntax-case capture variable') } if indices.len - offset != captures.pattern.variables[variable].depth { return failure('syntax-case capture rank mismatch') } mut path := i32(0) for i in offset .. indices.len { index := indices[i] count := captures.coordinates.items[syntax_capture_key(variable, path)] or { return failure('missing syntax-case capture dimension') } if index < 0 || index >= count { return failure('syntax-case capture index out of range') } path = captures.coordinates.paths[(u64(u32(path)) << 32) | u64(u32(index))] or { return failure('missing syntax-case capture coordinate') } } if !isnil(captures.values) { return captures.values.items[syntax_capture_key(variable, path)] or { return failure('missing syntax-case capture') } } index := captures.coordinates.items[syntax_capture_key(variable, path)] or { return failure('missing syntax-case capture') } return wrapped_syntax(captures.input, int(index)) } struct SyntaxCaseMatcher { tree &SyntaxTree pattern &SyntaxPattern literals map[i32]SyntaxBinding owner &Owner = unsafe { nil } input &SyntaxObjectData = unsafe { nil } } fn (matcher &SyntaxCaseMatcher) matches(mut captures SyntaxCaseCaptures, input i32) !bool { if voidptr(captures.input) != voidptr(matcher.input) || voidptr(captures.pattern) != voidptr(matcher.pattern) { return failure('syntax-case capture document mismatch') } return match_case_node(matcher, matcher.input, matcher.pattern.root, input, 0, mut captures) } fn match_case_node[T](matcher &SyntaxCaseMatcher, source &T, id i32, input i32, path i32, mut captures SyntaxCaseCaptures) !bool { if isnil(matcher.pattern.constants) { return match_case_body[T, PlainSyntaxMatch](matcher, source, id, input, path, mut captures, unsafe { nil }) } mut memo := SyntaxMatchMemo{} defer { unsafe { memo.completed.free() } } return match_case_cached[T, SharedSyntaxMatch](matcher, source, id, input, path, mut captures, &memo) } @[inline] fn match_case_cached[T, M](matcher &SyntaxCaseMatcher, source &T, id i32, input i32, path i32, mut captures SyntaxCaseCaptures, cache &SyntaxMatchMemo) !bool { $if M is SharedSyntaxMatch { mut memo := unsafe { cache } if u64(u32(id)) in matcher.pattern.constants.nodes { key := syntax_match_key(id, input) if result := memo.completed[key] { return result } result := match_case_body[T, M](matcher, source, id, input, path, mut captures, cache)! memo.completed[key] = result return result } } return match_case_body[T, M](matcher, source, id, input, path, mut captures, cache) } fn match_case_body[T, M](matcher &SyntaxCaseMatcher, source &T, id i32, input i32, path i32, mut captures SyntaxCaseCaptures, cache &SyntaxMatchMemo) !bool { pattern := matcher.tree.nodes[id] node := case_input_node(source, input)! match pattern.kind { .symbol { if literal := matcher.literals[pattern.car] { if node.kind != .symbol { return false } return case_input_literal(matcher, source, input, literal) } if matcher.tree.wildcard(id) { return true } variable := matcher.pattern.indices[pattern.car] or { return failure('invalid syntax-case pattern variable') } capture_case_value(source, input, variable, path, mut captures)! return true } .pair { mut cursor := id mut next := input for matcher.tree.nodes[cursor].kind == .pair { pair := matcher.tree.nodes[cursor] tail := matcher.tree.nodes[pair.cdr] if tail.kind == .pair && matcher.tree.ellipsis(tail.car) { variables := matcher.pattern.repeats[pair.car] mut suffix := 0 mut rest := tail.cdr for matcher.tree.nodes[rest].kind == .pair { suffix++ rest = matcher.tree.nodes[rest].cdr } mut remaining := -1 if suffix > 0 { mut available := 0 mut probe := next mut guard := SyntaxCaseSpine{ checkpoint: next } for case_input_node(source, probe)!.kind == .pair { available++ probe = case_input_node(source, probe)!.cdr guard.step(probe)! } if available < suffix { return false } remaining = available - suffix } mut count := 0 mut guard := SyntaxCaseSpine{ checkpoint: next } for remaining != 0 && case_input_node(source, next)!.kind == .pair { item := case_input_node(source, next)! child := if variables.len > 0 { captures.coordinates.child_path(path, count)! } else { path } if !match_case_cached[T, M](matcher, source, pair.car, item.car, child, mut captures, cache)! { return false } next = item.cdr if remaining < 0 { guard.step(next)! } count++ if remaining > 0 { remaining-- } } for variable in variables { captures.coordinates.items[syntax_capture_key(variable, path)] = i32(count) } cursor = tail.cdr continue } item := case_input_node(source, next)! if item.kind != .pair { return false } if !match_case_cached[T, M](matcher, source, pair.car, item.car, path, mut captures, cache)! { return false } cursor = pair.cdr next = item.cdr } return match_case_cached[T, M](matcher, source, cursor, next, path, mut captures, cache) } .vector { if node.kind != .vector { return false } mut repeated_at := -1 for position in 1 .. int(pattern.cdr) { if matcher.tree.ellipsis(matcher.tree.elements[pattern.car + position]) { repeated_at = position - 1 break } } fixed := int(pattern.cdr) - if repeated_at >= 0 { 2 } else { 0 } if node.count < fixed || (repeated_at < 0 && node.count != fixed) { return false } width := node.count - fixed mut index := 0 for index < int(pattern.cdr) { body := matcher.tree.elements[pattern.car + index] if index == repeated_at { variables := matcher.pattern.repeats[body] for offset in 0 .. width { child := if variables.len > 0 { captures.coordinates.child_path(path, offset)! } else { path } if !match_case_cached[T, M](matcher, source, body, case_input_vector_at(source, input, index + offset)!, child, mut captures, cache)! { return false } } for variable in variables { captures.coordinates.items[syntax_capture_key(variable, path)] = i32(width) } index += 2 } else { position := if repeated_at >= 0 && index > repeated_at { index - 2 + width } else { index } if !match_case_cached[T, M](matcher, source, body, case_input_vector_at(source, input, position)!, path, mut captures, cache)! { return false } index++ } } return true } .empty_list { return node.kind == .empty_list } else { if node.kind != pattern.kind { return false } expected := matcher.tree.values[pattern.car] actual := case_input_atom(source, input)! return syntax_pattern_atom_equal(expected, actual) } } } // Brent cycle detection follows the same next index as the streaming match. // Trailing repetitions use one pass and no auxiliary set or preliminary walk. struct SyntaxCaseSpine { mut: checkpoint i32 distance int power int = 1 } @[inline] fn (mut guard SyntaxCaseSpine) step(next i32) ! { guard.distance++ if next == guard.checkpoint { return failure('cyclic syntax-case repetition input') } if guard.distance == guard.power { guard.checkpoint = next guard.distance = 0 guard.power *= 2 } } // Scalar inputs need neither a registry nor a work arena. Literal identifiers // only match wrapped identifiers; this path handles unwrapped scalar values. fn (matcher &SyntaxCaseMatcher) match_scalar(value Value, mut captures SyntaxCaseCaptures) !bool { node := matcher.tree.nodes[matcher.pattern.root] if node.kind == .symbol { if node.car in matcher.literals { return false } if matcher.tree.wildcard(matcher.pattern.root) { return true } variable := matcher.pattern.indices[node.car] or { return failure('invalid syntax-case pattern variable') } capture_case_raw(value, variable, 0, mut captures) return true } if node.kind == .empty_list { return value.tag == .nil } if node.kind in [.pair, .vector] { return false } return syntax_pattern_atom_equal(matcher.tree.values[node.car], value) } // Raw pair/vector syntax needs a temporary structural view. Captures retain // original Values in their own sidecar, never indexes into the freed view. // Fully wrapped inputs retain the original single-document capture path. fn (matcher &SyntaxCaseMatcher) matches_value(value Value, mut captures SyntaxCaseCaptures) !bool { if voidptr(captures.pattern) != voidptr(matcher.pattern) { return failure('syntax-case capture pattern mismatch') } if !isnil(captures.input) || !isnil(captures.values) || captures.coordinates.items.len != 0 || captures.coordinates.paths.len != 0 { return failure('syntax-case needs a fresh capture frame') } if value.tag == .syntax { object := syntax_object_ref(value)! captures.input = object.data current := SyntaxCaseMatcher{ ...matcher, input: object.data } return current.matches(mut captures, object.index) } if value.tag !in [.pair, .vector, .nil] { return matcher.match_scalar(value, mut captures) } mut storage := arena.new(chunk_size: 1024) defer { storage.free() } source := new_syntax_match_source(value, storage) return match_case_node(matcher, source, matcher.pattern.root, 0, 0, mut captures) } fn new_syntax_match_source(value Value, storage arena.Arena) &SyntaxValueSource { storage.push() defer { storage.pop() } source := &SyntaxValueSource{ root: value, storage: storage } unsafe { source.nodes.flags.set(.noslices) } return source }