module scheme import reader struct LibraryExport { internal string external string origin reader.NodeId = reader.no_node } struct LibraryIdentity { token u8 = 1 } @[heap] struct LibraryDeclaration { identity &LibraryIdentity name LibraryName source_path string imports []ImportSet exports []LibraryExport code &Code forms []reader.NodeId } fn (mut b Builder) library_export_binding(id reader.NodeId) !string { name := b.code.variable(id)! if !isnil(b.code.generated) { if alias := b.code.generated.aliases[name] { if alias.use_site && isnil(alias.use_scope) { return alias.use_name } } } return name } fn (mut b Builder) library_export(id reader.NodeId) !LibraryExport { if b.code.node(id)!.kind == .symbol { return LibraryExport{ internal: b.library_export_binding(id)!, external: external_identifier_key(b.code.datum_name(id)!), origin: b.code.source_origin(id) } } parts := b.code.nodes(id)! if parts.len != 3 || b.library_identifier(parts[0])! != 'rename' { return failure('library export needs an identifier or (rename internal external)') } return LibraryExport{ internal: b.library_export_binding(parts[1])!, external: external_identifier_key(b.library_identifier(parts[2])!), origin: b.code.source_origin(parts[1]) } } fn (mut b Builder) library_declaration(nodes []reader.NodeId) !&LibraryDeclaration { check_arity('define-library', nodes.len - 1, 1, -1)! name := b.library_name(nodes[1])! mut imports := []ImportSet{} mut exports := []LibraryExport{} mut bodies := []reader.NodeId{} mut pending := [BodyCursor{ items: unsafe { nodes[2..] } }] 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 { 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 } declaration := pending[last].items[pending[last].index] pending[last].index++ b.collect_library_declaration(declaration, mut imports, mut exports, mut bodies, mut pending) or { return b.code.source_failure(declaration, err) } } // Retain only the referenced generated subtrees and aliases. The enclosing // compilation may now discard its expansion arena before this library is // ever imported. Original reader nodes remain shared immutable source data. mut code := &Code{ doc: b.code.doc, source_path: b.code.source_path, syntax_seed: b.code.syntax_seed, sources: b.code.sources } if !isnil(b.code.graphs) { code.enable_datum_graphs() code.graphs.literals = b.code.child_graph_literal_owner() } mut importer := &SourceImporter{ parent: b.code, code: code } defer { importer.release_scratch() unsafe { free(importer) } } mut forms := filled_storage(bodies.len, reader.NodeId(0)) for i, id in bodies { container_store_validated(mut forms, i, importer.import_form(id)!) } return &LibraryDeclaration{ identity: &LibraryIdentity{}, name: name, source_path: b.code.source_file(nodes[0]), imports: imports, exports: exports, code: code, forms: forms } } fn (mut b Builder) collect_library_declaration(declaration reader.NodeId, mut imports []ImportSet, mut exports []LibraryExport, mut bodies []reader.NodeId, mut pending []BodyCursor) ! { parts := b.code.nodes(declaration)! if parts.len == 0 { return failure('library declaration must be nonempty') } head := b.library_identifier(parts[0])! match head { 'import' { for id in parts[1..] { imports << b.import_set(id)! } } 'export' { for id in parts[1..] { exports << b.library_export(id)! } } 'include', 'include-ci' { bodies << b.static_forms(head, parts, declaration)! } 'include-library-declarations', 'cond-expand' { b.code.enter_syntax_group(declaration)! pending << BodyCursor{ items: b.static_forms(head, parts, declaration)! } } 'begin' { for id in parts[1..] { bodies << id } } else { return failure('unsupported library declaration ${head}') } } }