module scheme import reader struct BodyCursor { items []reader.NodeId mut: index int } // Discover generated definitions before compiling any initializer or closure. // Body-level begins splice into the definition group without recursive walks // along long lists or allocating a new runtime activation for the group. fn (mut b Builder) prepare_body(nodes []reader.NodeId) ![]reader.NodeId { previous_keywords := b.body_keywords mut keywords := &SyntaxBindings{} b.body_keywords = keywords defer { b.body_keywords = previous_keywords unsafe { keywords.entries.free() free(keywords) } } mut pending := [BodyCursor{ items: nodes }] mut prepared := []reader.NodeId{cap: nodes.len} mut definitions := map[int]bool{} unsafe { pending.flags.set(.noslices) prepared.flags.set(.noslices) } mut prepared_returned := false defer { unsafe { pending.free() definitions.free() } if !prepared_returned { unsafe { prepared.free() } } } group_base := if isnil(b.code.graphs) { 0 } else { b.code.graphs.groups.len } if !isnil(b.code.graphs) { b.code.graphs.groups << reader.no_node } defer { if !isnil(b.code.graphs) { b.code.graphs.groups.trim(group_base) } } for pending.len > 0 { // An include can introduce the first labelled datum after this walk // started. Earlier, unlabelled cursors have no graph identity to track. if !isnil(b.code.graphs) { for b.code.graphs.groups.len < group_base + pending.len { b.code.graphs.groups << reader.no_node } } last := pending.len - 1 if pending[last].index == pending[last].items.len { pending.pop() if !isnil(b.code.graphs) { b.code.graphs.groups.pop() } continue } source := pending[last].items[pending[last].index] pending[last].index++ b.prepare_body_form(source, mut pending, mut prepared, mut definitions) or { return b.code.source_failure(source, err) } } for name, binding in b.body_keywords.entries { // Map iteration owns this key copy; only the comparison borrows it. defer { unsafe { name.free() } } if !same_syntax_binding(binding, b.resolve_binding(name)) { return failure('internal definition shadows a keyword needed to expand its body') } } prepared_returned = true return prepared } fn (mut b Builder) note_body_keyword(name string, binding SyntaxBinding) { if !isnil(b.body_keywords) && name !in b.body_keywords.entries { b.body_keywords.entries[name] = binding } } fn (mut b Builder) declare_body_name(name string, mut definitions map[int]bool) ! { if !isnil(b.scope.syntax) && name in b.scope.syntax.entries { return failure('duplicate internal definition') } slot := b.declare_definition(name) if slot in definitions { return failure('duplicate internal definition') } definitions[slot] = true } fn (mut b Builder) prepare_body_form(source reader.NodeId, mut pending []BodyCursor, mut prepared []reader.NodeId, mut definitions map[int]bool) ! { id := b.expand_syntax_head(source)! node := b.code.node(id)! mut head := '' mut parts := []reader.NodeId{} if node.kind == .pair && b.code.node(node.car)!.kind == .symbol { head = b.keyword_name(node.car)! if head in ['begin', 'define', 'define-values', 'define-syntax', 'define-record-type', 'define-class', 'standard-define-class', 'define-accessor', 'define-extended-generic', 'define-extended-generics', 'define-generic', 'define-method', 'define-method*', 'define-enumeration', 'include', 'include-ci', 'cond-expand'] { key := b.code.name(node.car)! b.note_body_keyword(key, b.resolve_binding(key)) parts = b.code.nodes(id)! } } if head in ['include', 'include-ci', 'cond-expand'] { b.code.enter_syntax_group(id)! pending << BodyCursor{ items: b.static_forms(head, parts, id)! } return } if head == 'begin' { b.code.enter_syntax_group(id)! // Code owns these cached syntax lists for the whole compilation; // cursors only read them, so retaining a view requires no clone. pending << BodyCursor{ items: unsafe { parts[1..] } } return } if head == 'define-syntax' { return failure('define-syntax is only valid at top level') } if head == 'define-enumeration' { b.install_enumeration(parts, id)! } if head == 'define-record-type' { declaration := b.record_declaration(parts, id)! for key in declaration.bindings { b.declare_body_name(key, mut definitions)! } if declaration.hidden_cd != '' { b.declare_body_name(declaration.hidden_cd, mut definitions)! } b.install_record_annotation(declaration) } if head in ['define-class', 'standard-define-class'] { if parts.len < 3 { return failure('define-class needs a name and superclass list') } b.declare_body_name(b.code.variable(parts[1])!, mut definitions)! for slot_id in parts[3..] { if b.code.node(slot_id)!.kind == .keyword { break } if b.code.node(slot_id)!.kind == .symbol { continue } slot_parts := b.code.nodes(slot_id)! for option_index := 1; option_index + 1 < slot_parts.len; option_index += 2 { option := b.code.datum(slot_parts[option_index])! if option.tag == .keyword && option.text() in ['getter', 'setter', 'accessor'] { name := b.code.variable(slot_parts[option_index + 1])! if name !in b.scope.names { slot_index := b.declare_definition(name) definitions[slot_index] = true } } } } } if head in ['define-generic', 'define-accessor'] { if parts.len != 2 { return failure('define-generic needs one name') } b.declare_body_name(b.code.variable(parts[1])!, mut definitions)! } if head == 'define-extended-generic' { if parts.len != 3 { return failure('define-extended-generic needs a name and parent expression') } b.declare_body_name(b.code.variable(parts[1])!, mut definitions)! } if head == 'define-extended-generics' { names, _ := b.code.goops_extended_prefixes(parts)! for name in names { b.declare_body_name(b.code.variable(name)!, mut definitions)! } } if head in ['define-method', 'define-method*'] { if parts.len < 3 { return failure('define-method needs a signature and body') } signature := b.code.node(parts[1])! if signature.kind != .pair { return failure('define-method needs a generic name') } name := b.code.variable(signature.car)! // Several methods may define the same generic in one body. if name !in b.scope.names { slot := b.declare_definition(name) definitions[slot] = true } } if head in ['define', 'define-values'] { if parts.len < 2 { return failure('define needs a target and initializer') } if head == 'define-values' { check_arity(head, parts.len - 1, 2, 2)! params, rest := b.code.formals(parts[1])! for key in params { b.declare_body_name(key, mut definitions)! } if rest != '' { b.declare_body_name(rest, mut definitions)! } } else { target := b.code.node(parts[1])! key := b.code.variable(if target.kind == .pair { target.car } else { parts[1] })! b.declare_body_name(key, mut definitions)! } } prepared << id }