| 1 | module scheme |
| 2 | |
| 3 | // A complete lexical lookup frame supports names absent from a macro input. |
| 4 | // It owns its name maps and retains detached binding identities, never Scope, |
| 5 | // Function or Code. Transformer/namespace metadata retain their VM owner. |
| 6 | @[heap] |
| 7 | struct SyntaxLexicalFrame { |
| 8 | names map[string]int |
| 9 | syntax map[string]SyntaxBinding |
| 10 | domain &SyntaxLexicalDomain = unsafe { nil } |
| 11 | function_id u32 |
| 12 | mut: |
| 13 | parent &SyntaxLexicalFrame = unsafe { nil } |
| 14 | } |
| 15 | |
| 16 | struct SyntaxLexicalFrameRef { |
| 17 | value &SyntaxLexicalFrame |
| 18 | } |
| 19 | |
| 20 | fn capture_lexical_syntax_context(scope &Scope) !&SyntaxLexicalFrame { |
| 21 | mut cursor := ScopeRef{scope} |
| 22 | mut first := SyntaxLexicalFrameRef{unsafe { nil }} |
| 23 | mut last := SyntaxLexicalFrameRef{unsafe { nil }} |
| 24 | for !isnil(cursor.value) { |
| 25 | current := cursor.value |
| 26 | if current.names.len == 0 && (isnil(current.syntax) || current.syntax.entries.len == 0) { |
| 27 | cursor = ScopeRef{current.parent} |
| 28 | continue |
| 29 | } |
| 30 | identity := if current.names.len > 0 { |
| 31 | freeze_syntax_binding(SyntaxBinding{ kind: .lexical, function: current.function })! |
| 32 | } else { |
| 33 | SyntaxBinding{} |
| 34 | } |
| 35 | mut syntax := if isnil(current.syntax) { |
| 36 | map[string]SyntaxBinding{} |
| 37 | } else { |
| 38 | current.syntax.entries.clone() |
| 39 | } |
| 40 | // Clone keys once through the stdlib. Mutable value iteration avoids a |
| 41 | // second authored string-key iteration/copy while detaching the values. |
| 42 | for _, mut binding in syntax { |
| 43 | detached := freeze_syntax_binding(binding)! |
| 44 | binding = SyntaxBinding{ |
| 45 | ...detached |
| 46 | name: if detached.name.len > 0 { detached.name.clone() } else { '' } |
| 47 | introduced: if detached.introduced.len > 0 { |
| 48 | detached.introduced.clone() |
| 49 | } else { |
| 50 | '' |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | frame := &SyntaxLexicalFrame{ |
| 55 | names: current.names.clone() |
| 56 | syntax: syntax |
| 57 | domain: identity.lexical_domain |
| 58 | function_id: identity.lexical_id |
| 59 | } |
| 60 | if isnil(first.value) { |
| 61 | first = SyntaxLexicalFrameRef{frame} |
| 62 | } else { |
| 63 | mut previous := last.value |
| 64 | previous.parent = frame |
| 65 | } |
| 66 | last = SyntaxLexicalFrameRef{frame} |
| 67 | cursor = ScopeRef{current.parent} |
| 68 | } |
| 69 | return first.value |
| 70 | } |
| 71 | |
| 72 | fn lexical_syntax_context_binding(context &SyntaxLexicalFrame, name string) ?SyntaxBinding { |
| 73 | mut cursor := SyntaxLexicalFrameRef{context} |
| 74 | for !isnil(cursor.value) { |
| 75 | frame := cursor.value |
| 76 | if slot := frame.names[name] { |
| 77 | return SyntaxBinding{ kind: .lexical, name: name, slot: slot, lexical_domain: frame.domain, lexical_id: frame.function_id } |
| 78 | } |
| 79 | if binding := frame.syntax[name] { return binding } |
| 80 | cursor = SyntaxLexicalFrameRef{frame.parent} |
| 81 | } |
| 82 | return none |
| 83 | } |
| 84 | |