| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | struct BodyCursor { |
| 6 | items []reader.NodeId |
| 7 | mut: |
| 8 | index int |
| 9 | } |
| 10 | |
| 11 | // Discover generated definitions before compiling any initializer or closure. |
| 12 | // Body-level begins splice into the definition group without recursive walks |
| 13 | // along long lists or allocating a new runtime activation for the group. |
| 14 | fn (mut b Builder) prepare_body(nodes []reader.NodeId) ![]reader.NodeId { |
| 15 | previous_keywords := b.body_keywords |
| 16 | mut keywords := &SyntaxBindings{} |
| 17 | b.body_keywords = keywords |
| 18 | defer { |
| 19 | b.body_keywords = previous_keywords |
| 20 | unsafe { |
| 21 | keywords.entries.free() |
| 22 | free(keywords) |
| 23 | } |
| 24 | } |
| 25 | mut pending := [BodyCursor{ items: nodes }] |
| 26 | mut prepared := []reader.NodeId{cap: nodes.len} |
| 27 | mut definitions := map[int]bool{} |
| 28 | unsafe { |
| 29 | pending.flags.set(.noslices) |
| 30 | prepared.flags.set(.noslices) |
| 31 | } |
| 32 | mut prepared_returned := false |
| 33 | defer { |
| 34 | unsafe { |
| 35 | pending.free() |
| 36 | definitions.free() |
| 37 | } |
| 38 | if !prepared_returned { unsafe { prepared.free() } } |
| 39 | } |
| 40 | group_base := if isnil(b.code.graphs) { 0 } else { b.code.graphs.groups.len } |
| 41 | if !isnil(b.code.graphs) { b.code.graphs.groups << reader.no_node } |
| 42 | defer { |
| 43 | if !isnil(b.code.graphs) { b.code.graphs.groups.trim(group_base) } |
| 44 | } |
| 45 | for pending.len > 0 { |
| 46 | // An include can introduce the first labelled datum after this walk |
| 47 | // started. Earlier, unlabelled cursors have no graph identity to track. |
| 48 | if !isnil(b.code.graphs) { |
| 49 | for b.code.graphs.groups.len < group_base + pending.len { |
| 50 | b.code.graphs.groups << reader.no_node |
| 51 | } |
| 52 | } |
| 53 | last := pending.len - 1 |
| 54 | if pending[last].index == pending[last].items.len { |
| 55 | pending.pop() |
| 56 | if !isnil(b.code.graphs) { b.code.graphs.groups.pop() } |
| 57 | continue |
| 58 | } |
| 59 | source := pending[last].items[pending[last].index] |
| 60 | pending[last].index++ |
| 61 | b.prepare_body_form(source, mut pending, mut prepared, mut definitions) or { |
| 62 | return b.code.source_failure(source, err) |
| 63 | } |
| 64 | } |
| 65 | for name, binding in b.body_keywords.entries { |
| 66 | // Map iteration owns this key copy; only the comparison borrows it. |
| 67 | defer { unsafe { name.free() } } |
| 68 | if !same_syntax_binding(binding, b.resolve_binding(name)) { |
| 69 | return failure('internal definition shadows a keyword needed to expand its body') |
| 70 | } |
| 71 | } |
| 72 | prepared_returned = true |
| 73 | return prepared |
| 74 | } |
| 75 | |
| 76 | fn (mut b Builder) note_body_keyword(name string, binding SyntaxBinding) { |
| 77 | if !isnil(b.body_keywords) && name !in b.body_keywords.entries { |
| 78 | b.body_keywords.entries[name] = binding |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn (mut b Builder) declare_body_name(name string, mut definitions map[int]bool) ! { |
| 83 | if !isnil(b.scope.syntax) && name in b.scope.syntax.entries { |
| 84 | return failure('duplicate internal definition') |
| 85 | } |
| 86 | slot := b.declare_definition(name) |
| 87 | if slot in definitions { return failure('duplicate internal definition') } |
| 88 | definitions[slot] = true |
| 89 | } |
| 90 | |
| 91 | fn (mut b Builder) prepare_body_form(source reader.NodeId, mut pending []BodyCursor, mut prepared []reader.NodeId, mut definitions map[int]bool) ! { |
| 92 | id := b.expand_syntax_head(source)! |
| 93 | node := b.code.node(id)! |
| 94 | mut head := '' |
| 95 | mut parts := []reader.NodeId{} |
| 96 | if node.kind == .pair && b.code.node(node.car)!.kind == .symbol { |
| 97 | head = b.keyword_name(node.car)! |
| 98 | if head in ['begin', 'define', 'define-values', 'define-syntax', 'define-record-type', |
| 99 | 'define-class', 'standard-define-class', 'define-accessor', 'define-extended-generic', |
| 100 | 'define-extended-generics', 'define-generic', 'define-method', 'define-method*', |
| 101 | 'define-enumeration', 'include', 'include-ci', 'cond-expand'] { |
| 102 | key := b.code.name(node.car)! |
| 103 | b.note_body_keyword(key, b.resolve_binding(key)) |
| 104 | parts = b.code.nodes(id)! |
| 105 | } |
| 106 | } |
| 107 | if head in ['include', 'include-ci', 'cond-expand'] { |
| 108 | b.code.enter_syntax_group(id)! |
| 109 | pending << BodyCursor{ items: b.static_forms(head, parts, id)! } |
| 110 | return |
| 111 | } |
| 112 | if head == 'begin' { |
| 113 | b.code.enter_syntax_group(id)! |
| 114 | // Code owns these cached syntax lists for the whole compilation; |
| 115 | // cursors only read them, so retaining a view requires no clone. |
| 116 | pending << BodyCursor{ items: unsafe { parts[1..] } } |
| 117 | return |
| 118 | } |
| 119 | if head == 'define-syntax' { return failure('define-syntax is only valid at top level') } |
| 120 | if head == 'define-enumeration' { b.install_enumeration(parts, id)! } |
| 121 | if head == 'define-record-type' { |
| 122 | declaration := b.record_declaration(parts, id)! |
| 123 | for key in declaration.bindings { b.declare_body_name(key, mut definitions)! } |
| 124 | if declaration.hidden_cd != '' { |
| 125 | b.declare_body_name(declaration.hidden_cd, mut definitions)! |
| 126 | } |
| 127 | b.install_record_annotation(declaration) |
| 128 | } |
| 129 | if head in ['define-class', 'standard-define-class'] { |
| 130 | if parts.len < 3 { return failure('define-class needs a name and superclass list') } |
| 131 | b.declare_body_name(b.code.variable(parts[1])!, mut definitions)! |
| 132 | for slot_id in parts[3..] { |
| 133 | if b.code.node(slot_id)!.kind == .keyword { break } |
| 134 | if b.code.node(slot_id)!.kind == .symbol { continue } |
| 135 | slot_parts := b.code.nodes(slot_id)! |
| 136 | for option_index := 1; option_index + 1 < slot_parts.len; option_index += 2 { |
| 137 | option := b.code.datum(slot_parts[option_index])! |
| 138 | if option.tag == .keyword && option.text() in ['getter', 'setter', 'accessor'] { |
| 139 | name := b.code.variable(slot_parts[option_index + 1])! |
| 140 | if name !in b.scope.names { |
| 141 | slot_index := b.declare_definition(name) |
| 142 | definitions[slot_index] = true |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | if head in ['define-generic', 'define-accessor'] { |
| 149 | if parts.len != 2 { return failure('define-generic needs one name') } |
| 150 | b.declare_body_name(b.code.variable(parts[1])!, mut definitions)! |
| 151 | } |
| 152 | if head == 'define-extended-generic' { |
| 153 | if parts.len != 3 { |
| 154 | return failure('define-extended-generic needs a name and parent expression') |
| 155 | } |
| 156 | b.declare_body_name(b.code.variable(parts[1])!, mut definitions)! |
| 157 | } |
| 158 | if head == 'define-extended-generics' { |
| 159 | names, _ := b.code.goops_extended_prefixes(parts)! |
| 160 | for name in names { b.declare_body_name(b.code.variable(name)!, mut definitions)! } |
| 161 | } |
| 162 | if head in ['define-method', 'define-method*'] { |
| 163 | if parts.len < 3 { return failure('define-method needs a signature and body') } |
| 164 | signature := b.code.node(parts[1])! |
| 165 | if signature.kind != .pair { return failure('define-method needs a generic name') } |
| 166 | name := b.code.variable(signature.car)! |
| 167 | // Several methods may define the same generic in one body. |
| 168 | if name !in b.scope.names { |
| 169 | slot := b.declare_definition(name) |
| 170 | definitions[slot] = true |
| 171 | } |
| 172 | } |
| 173 | if head in ['define', 'define-values'] { |
| 174 | if parts.len < 2 { return failure('define needs a target and initializer') } |
| 175 | if head == 'define-values' { |
| 176 | check_arity(head, parts.len - 1, 2, 2)! |
| 177 | params, rest := b.code.formals(parts[1])! |
| 178 | for key in params { b.declare_body_name(key, mut definitions)! } |
| 179 | if rest != '' { b.declare_body_name(rest, mut definitions)! } |
| 180 | } else { |
| 181 | target := b.code.node(parts[1])! |
| 182 | key := b.code.variable(if target.kind == .pair { target.car } else { parts[1] })! |
| 183 | b.declare_body_name(key, mut definitions)! |
| 184 | } |
| 185 | } |
| 186 | prepared << id |
| 187 | } |
| 188 | |