| 1 | module scheme |
| 2 | |
| 3 | // Count only the spine. Contents are ignored, including arbitrary nonsyntax |
| 4 | // leaves; wrapped and raw pairs can share a spine without graph materialization. |
| 5 | fn syntax_spine_empty(value Value) bool { |
| 6 | if value.tag == .nil { return true } |
| 7 | if value.tag != .syntax { return false } |
| 8 | data := unsafe { &SyntaxObjectData(value.pointer()) } |
| 9 | return data.tree.nodes[value.syntax_index].kind == .empty_list |
| 10 | } |
| 11 | |
| 12 | fn syntax_spine_next(value Value) !Value { |
| 13 | if value.tag == .pair { return value.pair_pointer().cdr } |
| 14 | if value.tag == .syntax { |
| 15 | return syntax_cdr(value) or { return failure('generate-temporaries expects a proper list or list-structured syntax') } |
| 16 | } |
| 17 | return failure('generate-temporaries expects a proper list or list-structured syntax') |
| 18 | } |
| 19 | |
| 20 | fn syntax_spine_length(input Value) !int { |
| 21 | mut current := input |
| 22 | mut slow := input |
| 23 | mut count := 0 |
| 24 | for !syntax_spine_empty(current) { |
| 25 | current = syntax_spine_next(current)! |
| 26 | if count == 2147483647 { return failure('temporary count exceeds supported range') } |
| 27 | count++ |
| 28 | if count % 2 == 0 { |
| 29 | slow = syntax_spine_next(slow)! |
| 30 | if !syntax_spine_empty(current) && eqv(current, slow) { |
| 31 | return failure('generate-temporaries expects a finite proper list') |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | return count |
| 36 | } |
| 37 | |
| 38 | fn syntax_native_temporaries(mut vm VM, args []Value) !Value { |
| 39 | check_arity('generate-temporaries', args.len, 1, 1)! |
| 40 | count := syntax_spine_length(args[0])! |
| 41 | if count == 0 { return nil_value() } |
| 42 | mut owner := vm.owner |
| 43 | if isnil(owner.syntax_domain) { owner.syntax_domain = &owner.syntax_domain_storage } |
| 44 | mut domain := owner.syntax_domain |
| 45 | if u64(count) > ~u64(0) - domain.next_mark { |
| 46 | return failure('syntax mark count exceeds supported range') |
| 47 | } |
| 48 | |
| 49 | mut tree := &SyntaxTree{ |
| 50 | nodes: []SyntaxTreeNode{len: count} |
| 51 | identifiers: []SyntaxIdentifier{len: count} |
| 52 | } |
| 53 | context := &SyntaxLookupContext{ |
| 54 | temporaries: &SyntaxTemporaryContext{ |
| 55 | domain: domain |
| 56 | first: domain.next_mark + 1 |
| 57 | identifiers: unsafe { tree.identifiers } |
| 58 | } |
| 59 | } |
| 60 | tree.context = context |
| 61 | data := &SyntaxObjectData{ tree: tree, owner: vm.owner, context: context } |
| 62 | for i in 0 .. count { |
| 63 | domain.next_mark++ |
| 64 | name := '\x00~t${u64(domain)}.${domain.next_mark}' |
| 65 | wrap := &SyntaxWrap{ domain: domain, mark: domain.next_mark } |
| 66 | tree.nodes[i] = SyntaxTreeNode{ kind: .symbol, car: i32(i) } |
| 67 | tree.identifiers[i] = SyntaxIdentifier{ |
| 68 | name: name |
| 69 | raw_name: 't' |
| 70 | binding: SyntaxBinding{ kind: .global, name: name, unbound: true } |
| 71 | wrap: wrap |
| 72 | context: context |
| 73 | } |
| 74 | } |
| 75 | mut result := nil_value() |
| 76 | for i := count - 1; i >= 0; i-- { |
| 77 | result = cons(wrapped_syntax(data, i)!, result) |
| 78 | } |
| 79 | return result |
| 80 | } |
| 81 | |
| 82 | // Share the already exact, immutable identifier table. A temporary's context |
| 83 | // can recover its unique free identity without an extra map or per-name copy. |
| 84 | struct SyntaxTemporaryContext { |
| 85 | domain &SyntaxLexicalDomain |
| 86 | first u64 |
| 87 | identifiers []SyntaxIdentifier |
| 88 | } |
| 89 | |
| 90 | fn (context &SyntaxTemporaryContext) binding(name string, history &SyntaxWrap) ?SyntaxBinding { |
| 91 | if external_identifier_spelling(name) != 't' { return none } |
| 92 | mut cursor := SyntaxWrapRef{history} |
| 93 | for !isnil(cursor.value) { |
| 94 | wrap := cursor.value |
| 95 | if !wrap.antimark && voidptr(wrap.domain) == voidptr(context.domain) && wrap.mark >= context.first && wrap.mark - context.first < u64(context.identifiers.len) { |
| 96 | return context.identifiers[int(wrap.mark - context.first)].binding |
| 97 | } |
| 98 | cursor = SyntaxWrapRef{wrap.parent} |
| 99 | } |
| 100 | return none |
| 101 | } |
| 102 | |