vile / scheme / syntax_lexical_context_test.v
112 lines · 106 sloc · 4.51 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4import reader
5
6fn test_lexical_context_finds_arbitrary_names_absent_from_the_input() {
7 code := &Code{ doc: reader.read_all('(lib:get-x)') or { panic(err) } }
8 function := &Function{ code: code }
9 mut caller := &Scope{ function: function }
10 caller.declare('x')
11 caller.declare('not-mentioned')
12 context := capture_lexical_syntax_context(caller) or { panic(err) }
13 for name in ['x', 'not-mentioned'] {
14 binding := lexical_syntax_context_binding(context, name) or { panic('missing ' + name) }
15 assert binding.kind == .lexical && isnil(binding.function)
16 assert syntax_binding_function_matches(binding, function)
17 assert binding.slot == caller.names[name] or { panic('missing original') }
18 }
19 if _ := lexical_syntax_context_binding(context, 'unbound') {
20 assert false
21 }
22}
23
24fn test_lexical_context_preserves_shadowing_across_shared_and_distinct_functions() {
25 code := &Code{ doc: reader.read_all('') or { panic(err) } }
26 outer_function := &Function{ code: code }
27 inner_function := &Function{ code: code }
28 mut outer := &Scope{ function: outer_function }
29 outer_x := outer.declare('x')
30 outer_y := outer.declare('y')
31 mut same_function := &Scope{ parent: outer, function: outer_function }
32 inner_x := same_function.declare('x')
33 mut inner := &Scope{ parent: same_function, function: inner_function }
34 inner_z := inner.declare('z')
35 context := capture_lexical_syntax_context(inner) or { panic(err) }
36 x := lexical_syntax_context_binding(context, 'x') or { panic('missing x') }
37 y := lexical_syntax_context_binding(context, 'y') or { panic('missing y') }
38 z := lexical_syntax_context_binding(context, 'z') or { panic('missing z') }
39 assert x.slot == inner_x && x.slot != outer_x && y.slot == outer_y && z.slot == inner_z
40 assert syntax_binding_function_matches(x, outer_function)
41 assert syntax_binding_function_matches(z, inner_function)
42 builder := Builder{ code: code, function: inner_function, scope: inner }
43 assert builder.syntax_binding_depth(x) or { panic(err) } == 1
44 assert builder.syntax_binding_depth(z) or { panic(err) } == 0
45}
46
47fn test_lexical_context_snapshot_owns_maps_and_canonical_keyword_names() {
48 mut published := arena.new()
49 published.push()
50 domain := &SyntaxLexicalDomain{}
51 mut scratch := arena.new()
52 scratch.push()
53 mut code := &Code{ doc: reader.read_all('') or { panic(err) } }
54 mut generated := code.syntax_arena()
55 generated.lexical_domain = domain
56 function := &Function{ code: code }
57 mut scope := &Scope{ function: function, syntax: &SyntaxBindings{} }
58 name := 'dynamic-local-'.clone()
59 scope.declare(name)
60 scope.syntax.entries['branch'.clone()] = SyntaxBinding{ kind: .keyword, name: 'if'.clone() }
61 scratch.pop()
62 context := capture_lexical_syntax_context(scope) or { panic(err) }
63 scratch.free()
64 gc_collect()
65 assert lexical_syntax_context_binding(context, 'dynamic-local-') or { panic('missing local') }.kind == .lexical
66 keyword := lexical_syntax_context_binding(context, 'branch') or { panic('missing keyword') }
67 assert keyword.kind == .keyword && keyword.name == 'if'
68 assert published.owns(keyword.name.str)
69 published.pop()
70 published.free()
71}
72
73fn test_lexical_context_empty_scopes_and_repeat_lookups_allocate_no_storage() {
74 code := &Code{ doc: reader.read_all('') or { panic(err) } }
75 function := &Function{ code: code }
76 mut scope := &Scope{ function: function }
77 scope.declare('x')
78 context := capture_lexical_syntax_context(scope) or { panic(err) }
79 empty := &Scope{ function: function }
80 mut region := arena.new()
81 region.push()
82 defer {
83 region.pop()
84 region.free()
85 }
86 before := region.used()
87 assert isnil(capture_lexical_syntax_context(empty) or { panic(err) })
88 assert isnil(capture_lexical_syntax_context(unsafe { nil }) or { panic(err) })
89 for _ in 0 .. 1001 {
90 assert lexical_syntax_context_binding(context, 'x') or { panic('missing x') }.slot == 0
91 if _ := lexical_syntax_context_binding(context, 'missing') {
92 assert false
93 }
94 }
95 assert region.used() == before
96}
97
98fn test_lexical_context_snapshot_uses_iterative_scope_traversal_for_long_binding_chains() {
99 code := &Code{ doc: reader.read_all('') or { panic(err) } }
100 function := &Function{ code: code }
101 mut cursor := ScopeRef{unsafe { nil }}
102 for i in 0 .. 5000 {
103 mut scope := &Scope{ parent: cursor.value, function: function }
104 scope.declare('n${i}')
105 cursor = ScopeRef{scope}
106 }
107 context := capture_lexical_syntax_context(cursor.value) or { panic(err) }
108 for i in [0, 100, 4999] {
109 binding := lexical_syntax_context_binding(context, 'n${i}') or { panic('missing binding') }
110 assert binding.slot == i && syntax_binding_function_matches(binding, function)
111 }
112}
113