module scheme import arena import reader // Authored read-only against source30's API; execution belongs to a successor. fn review_template_spec(pattern_source string, template_source string) SyntaxCaseTemplate { mut known := { 'definition': 11 } code := &Code{ doc: reader.read_all(pattern_source + '\n' + template_source) or { panic(err.msg()) }, known_globals: &known } mut builder := Builder{ code: code, function: &Function{ code: code } } tree, roots := builder.freeze_syntax(code.doc.forms) or { panic(err.msg()) } pattern := tree.compile_case_pattern(roots[0], map[i32]SyntaxBinding{}) or { panic(err.msg()) } data := &SyntaxObjectData{ tree: tree, owner: &Owner{}, context: builder.capture_syntax_lookup_context() or { panic(err.msg()) } } return data.compile_case_template(roots[1], &pattern) or { panic(err.msg()) } } fn review_template_wrapped(source string) Value { code := &Code{ doc: reader.read_all(source) or { panic(err.msg()) } } mut builder := Builder{ code: code, function: &Function{ code: code } } tree, roots := builder.freeze_syntax_input(code.doc.forms) or { panic(err.msg()) } return wrapped_syntax(&SyntaxObjectData{ tree: tree, owner: &Owner{} }, int(roots[0])) or { panic(err.msg()) } } fn review_template_frame(template &SyntaxCaseTemplate, input Value) SyntaxCaseCaptures { matcher := SyntaxCaseMatcher{ tree: template.data.tree, pattern: template.pattern, owner: template.data.owner } mut captures := SyntaxCaseCaptures{ pattern: template.pattern } assert matcher.matches_value(input, mut captures) or { panic(err.msg()) } return captures } fn review_template_text(template &SyntaxCaseTemplate, captures &SyntaxCaseCaptures) string { return write(syntax_to_datum(template.expand(captures) or { panic(err.msg()) }) or { panic(err.msg()) }) or { panic(err.msg()) } } fn test_independent_template_repeat_marker_cells_are_grammar_not_escape_regions() { for source in ['(x ...)', '(x ... definition)', '#(x ... definition)'] { template := review_template_spec('(x ...)', source) captures := review_template_frame(template, list([integer(1), integer(2)])) assert review_template_text(template, captures) == if source == '(x ...)' { '(1 2)' } else if source[0] == `#` { '#(1 2 definition)' } else { '(1 2 definition)' } } } fn test_independent_template_shared_body_keys_distinguish_consecutive_ellipsis_depths() { input := list([list([integer(1), integer(2)]), nil_value(), list([integer(3)])]) for source, expected in { '((#1=x ... #1# ... ...) ...)': '((1 2 1 2 3) (1 2 3) (3 1 2 3))' '#(#(#1=x ... #1# ... ...) ...)': '#(#(1 2 1 2 3) #(1 2 3) #(3 1 2 3))' } { template := review_template_spec('((x ...) ...)', source) captures := review_template_frame(template, input) assert review_template_text(template, captures) == expected } } fn test_independent_template_shared_subgraph_has_separate_escaped_and_normal_modes() { template := review_template_spec('(x ...)', '(#1=(x ...) ((... #1#) ...))') captures := review_template_frame(template, list([integer(1), integer(2)])) assert review_template_text(template, captures) == '((1 2) ((1 ...) (2 ...)))' } fn test_independent_template_consecutive_flattening_handles_empty_ragged_rows_and_replication() { input := list([list([integer(1), integer(2)]), nil_value(), list([integer(3)])]) for source, expected in { '(x ... ...)': '(1 2 3)' '#(x ... ...)': '#(1 2 3)' } { template := review_template_spec('((x ...) ...)', source) captures := review_template_frame(template, input) assert review_template_text(template, captures) == expected empty := review_template_frame(template, nil_value()) assert review_template_text(template, empty) == if source[0] == `#` { '#()' } else { '()' } } product := review_template_spec('((x ...) (y ...))', '((x (y ...)) ...)') frame := review_template_frame(product, list([list([integer(1), integer(2)]), list([integer(3), integer(4), integer(5)])])) assert review_template_text(product, frame) == '((1 (3 4 5)) (2 (3 4 5)))' } fn test_independent_template_substitution_preserves_raw_and_foreign_wrapped_cycles_exactly() { raw := cons(integer(7), nil_value()) mut pair := raw.pair_pointer() pair.cdr = raw foreign := review_template_wrapped('#1=(9 . #1#)') template := review_template_spec('(x y)', '#(x y x)') captures := review_template_frame(template, list([raw, foreign])) output := template.expand(captures) or { panic(err.msg()) } items := output.vector_pointer().items assert eqv(items[0], raw) && eqv(items[2], raw) && eqv(items[1], foreign) assert voidptr((syntax_object_ref(items[1]) or { panic(err.msg()) }).data.owner) == voidptr((syntax_object_ref(foreign) or { panic(err.msg()) }).data.owner) plain := syntax_to_datum(output) or { panic(err.msg()) } values := plain.vector_pointer().items assert eqv(values[0], values[2]) && !eqv(values[0], values[1]) assert eqv(values[0], values[0].pair_pointer().cdr) assert eqv(values[1], values[1].pair_pointer().cdr) } fn test_independent_template_constant_escape_normalization_retains_shared_graph_and_metadata() { template := review_template_spec('_', '#(#1=(definition (... (... kept)) end) #1#)') captures := review_template_frame(template, integer(0)) output := template.expand(captures) or { panic(err.msg()) } assert output.tag == .syntax first := syntax_vector_ref(output, 0) or { panic(err.msg()) } second := syntax_vector_ref(output, 1) or { panic(err.msg()) } assert eqv(first, second) identifier := syntax_car(first) or { panic(err.msg()) } info := syntax_identifier_info(identifier) or { panic(err.msg()) } assert info.binding.global_slot == 11 object := syntax_object_ref(identifier) or { panic(err.msg()) } assert voidptr(object.data.owner) == voidptr(template.data.owner) assert voidptr(object.data.context) == voidptr(template.data.context) for _ in 0 .. 30 { other := review_template_spec('_', '(... (discarded ...))') frame := review_template_frame(other, integer(0)) other.expand(frame) or { panic(err.msg()) } } gc_collect() assert eqv(output, template.expand(captures) or { panic(err.msg()) }) plain := syntax_to_datum(output) or { panic(err.msg()) } assert eqv(plain.vector_pointer().items[0], plain.vector_pointer().items[1]) assert write(plain) or { panic(err.msg()) } == '#((definition (... kept) end) (definition (... kept) end))' } fn test_independent_template_width_failure_restores_arena_and_does_not_poison_reuse() { template := review_template_spec('((x ...) (y ...))', '#((x y) ...)') bad := review_template_frame(template, list([list([integer(1)]), list([integer(2), integer(3)])])) good := review_template_frame(template, list([list([integer(1)]), list([integer(2)])])) mut output := arena.new(chunk_size: 1048576) output.push() if _ := template.expand(bad) { assert false } else { assert err.msg().contains('lengths do not match') } assert output.is_current() assert review_template_text(template, good) == '#((1 2))' assert output.is_current() output.pop() output.free() } fn test_independent_template_rejects_cyclic_structure_before_tree_grammar_walk() { code := &Code{ doc: reader.read_all('x\n#1=(definition . #1#)') or { panic(err.msg()) } } mut builder := Builder{ code: code, function: &Function{ code: code } } tree, roots := builder.freeze_syntax_input(code.doc.forms) or { panic(err.msg()) } pattern := tree.compile_case_pattern(roots[0], map[i32]SyntaxBinding{}) or { panic(err.msg()) } data := &SyntaxObjectData{ tree: tree } if _ := data.compile_case_template(roots[1], pattern) { assert false } else { assert err.msg().contains('circular') } } fn test_independent_template_retained_allocations_equal_direct_result_construction() { for source in ['(x x)', '#(x x)'] { template := review_template_spec('x', source) captures := review_template_frame(template, integer(42)) mut actual := arena.new(chunk_size: 1048576) actual.push() result := template.expand(captures) or { panic(err.msg()) } assert actual.is_current() && actual.owns(result.pointer()) used := actual.used() actual.pop() mut expected := arena.new(chunk_size: 1048576) expected.push() if source[0] == `(` { _ = cons(integer(42), cons(integer(42), nil_value())) } else { _ = payload_value(.vector, &Vector{ items: []Value{len: 2, init: integer(42)} }) } assert used == expected.used(), '${source}: ${used} != ${expected.used()}' expected.pop() expected.free() actual.free() } } fn test_independent_template_scratch_emissions_reset_across_siblings_and_nested_depths() { template := review_template_spec('(x ...)', '#(#(x ...) (x ...) #(x ...) (x ...) ((x x) ...) #((x ...) (x ...)))') for items, expected in { 3: '#(#(1 2 3) (1 2 3) #(1 2 3) (1 2 3) ((1 1) (2 2) (3 3)) #((1 2 3) (1 2 3)))' 0: '#(#() () #() () () #(() ()))' } { input := if items == 0 { nil_value() } else { list([integer(1), integer(2), integer(3)]) } captures := review_template_frame(template, input) assert review_template_text(template, captures) == expected assert review_template_text(template, captures) == expected } }