| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | fn test_lookup_context_snapshots_global_locations_and_staged_shadowing() { |
| 7 | mut known := { |
| 8 | 'x': 7 |
| 9 | 'if': 8 |
| 10 | } |
| 11 | mut syntax := &SyntaxBindings{} |
| 12 | syntax.entries['branch'] = SyntaxBinding{ kind: .keyword, name: 'if' } |
| 13 | mut code := &Code{ doc: reader.read_all('') or { panic(err) }, known_globals: &known, syntax_base: syntax, keyword_variables: ['if'] } |
| 14 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 15 | old := builder.capture_syntax_lookup_context() or { panic(err) } |
| 16 | known['x'] = 99 |
| 17 | syntax.entries['branch'] = SyntaxBinding{ kind: .keyword, name: 'begin' } |
| 18 | mut generated := code.syntax_arena() |
| 19 | generated.globals['branch'] = SyntaxBinding{ kind: .global, name: 'branch', global_slot: 12 } |
| 20 | new := builder.capture_syntax_lookup_context() or { panic(err) } |
| 21 | assert old.binding('x').global_slot == 7 && new.binding('x').global_slot == 99 |
| 22 | assert old.binding('branch').kind == .keyword && old.binding('branch').name == 'if' |
| 23 | assert new.binding('branch').kind == .global && new.binding('branch').global_slot == 12 |
| 24 | assert old.binding('if').kind == .global && old.binding('if').global_slot == 8 |
| 25 | assert old.binding('lambda').kind == .keyword |
| 26 | assert old.binding('absent').unbound |
| 27 | } |
| 28 | |
| 29 | fn test_lookup_context_uses_caller_locations_even_when_keyword_is_from_a_provider_library() { |
| 30 | caller := &Namespace{ writable: true } |
| 31 | provider := &Namespace{ writable: true } |
| 32 | mut known := { |
| 33 | 'x': 42 |
| 34 | } |
| 35 | mut syntax := &SyntaxBindings{} |
| 36 | syntax.entries['get-x'] = SyntaxBinding{ kind: .transformer, name: 'get-x', namespace: provider, top_level: true, transformer: &SyntaxTransformer{} } |
| 37 | code := &Code{ doc: reader.read_all('(get-x)') or { panic(err) }, namespace: caller, known_globals: &known, syntax_base: syntax } |
| 38 | function := &Function{ code: code } |
| 39 | mut scope := &Scope{ function: function } |
| 40 | scope.declare('not-in-input') |
| 41 | mut builder := Builder{ code: code, function: function, scope: scope } |
| 42 | context := builder.capture_syntax_lookup_context() or { panic(err) } |
| 43 | assert voidptr(context.binding('get-x').namespace) == voidptr(provider) |
| 44 | assert context.binding('x').global_slot == 42 |
| 45 | assert voidptr(context.binding('x').namespace) == voidptr(caller) |
| 46 | assert context.binding('not-in-input').kind == .lexical |
| 47 | assert isnil(context.binding('not-in-input').function) |
| 48 | assert voidptr(context.binding('missing').namespace) == voidptr(caller) |
| 49 | } |
| 50 | |
| 51 | fn test_lookup_context_respects_explicit_namespace_syntax_and_reserved_lowering() { |
| 52 | code := &Code{ doc: reader.read_all('') or { panic(err) }, namespace: &Namespace{ implicit_syntax: false } } |
| 53 | mut scope := &Scope{ function: &Function{ code: code } } |
| 54 | scope.declare('vile:statement') |
| 55 | mut builder := Builder{ code: code, function: scope.function, scope: scope } |
| 56 | context := builder.capture_syntax_lookup_context() or { panic(err) } |
| 57 | assert context.binding('if').unbound |
| 58 | assert context.binding('vile:statement').kind == .keyword |
| 59 | assert context.binding('vile:statement').name == implicit_statement_name |
| 60 | } |
| 61 | |
| 62 | fn test_lookup_context_retains_no_compiler_storage_and_repeated_lookup_allocates_nothing() { |
| 63 | mut published := arena.new() |
| 64 | published.push() |
| 65 | mut scratch := arena.new() |
| 66 | scratch.push() |
| 67 | mut known := { |
| 68 | 'dynamic-global'.clone(): 17 |
| 69 | } |
| 70 | mut syntax := &SyntaxBindings{} |
| 71 | syntax.entries['branch'.clone()] = SyntaxBinding{ kind: .keyword, name: 'if'.clone() } |
| 72 | code := &Code{ doc: reader.read_all('') or { panic(err) }, known_globals: &known, syntax_base: syntax, keyword_variables: ['lambda'.clone()] } |
| 73 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 74 | scratch.pop() |
| 75 | context := builder.capture_syntax_lookup_context() or { panic(err) } |
| 76 | scratch.free() |
| 77 | gc_collect() |
| 78 | assert context.binding('lambda').unbound |
| 79 | assert published.owns(context.keyword_variables[0].str) |
| 80 | assert context.binding('branch').name == 'if' |
| 81 | assert published.owns(context.binding('branch').name.str) |
| 82 | before := published.used() |
| 83 | for _ in 0 .. 1001 { |
| 84 | assert context.binding('dynamic-global').global_slot == 17 |
| 85 | assert context.binding('branch').kind == .keyword |
| 86 | assert context.binding('absent').unbound |
| 87 | } |
| 88 | assert published.used() == before |
| 89 | published.pop() |
| 90 | published.free() |
| 91 | } |
| 92 | |
| 93 | fn test_lookup_context_preserves_exact_alias_resolution_and_live_capture_updates() { |
| 94 | mut code := &Code{ doc: reader.read_all('') or { panic(err) } } |
| 95 | function := &Function{ code: code } |
| 96 | mut caller := &Scope{ function: function } |
| 97 | slot := caller.declare('x') |
| 98 | mut builder := Builder{ code: code, function: function, scope: caller } |
| 99 | // Captured aliases can have stale original binding metadata; live use-site |
| 100 | // lookup must win, just as it does in the compiler. |
| 101 | capture := code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'stale', unbound: true }, use_site: true, use_scope: caller, use_name: 'x' }) or { panic(err) } |
| 102 | keyword := code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .keyword, name: 'if' } }) or { panic(err) } |
| 103 | introduced := code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .lexical, name: 'x', function: function, slot: slot } }) or { panic(err) } |
| 104 | context := builder.capture_syntax_lookup_context() or { panic(err) } |
| 105 | for key in [capture, keyword, introduced] { |
| 106 | assert same_syntax_binding(context.binding(key), builder.resolve_binding(key)) |
| 107 | assert isnil(context.binding(key).function) |
| 108 | } |
| 109 | assert context.binding(capture).kind == .lexical |
| 110 | assert context.binding(capture).slot == slot |
| 111 | assert context.binding(keyword).name == 'if' |
| 112 | } |
| 113 | |