| 1 | module scheme |
| 2 | |
| 3 | import os |
| 4 | import reader |
| 5 | |
| 6 | // Additional sources retain their immutable reader arenas. Positive virtual |
| 7 | // ranges translate links without copying cells or growing the reader's arena. |
| 8 | struct SourceDocument { |
| 9 | doc &reader.Document |
| 10 | path string |
| 11 | identity string |
| 12 | base int |
| 13 | end int |
| 14 | parent int = -1 |
| 15 | } |
| 16 | |
| 17 | @[heap] |
| 18 | struct SourceDocuments { |
| 19 | first int |
| 20 | root_identity string |
| 21 | library_root string |
| 22 | graph_seed u64 |
| 23 | mut: |
| 24 | documents []SourceDocument |
| 25 | next int |
| 26 | } |
| 27 | |
| 28 | fn (sources &SourceDocuments) find(id reader.NodeId) ?int { |
| 29 | mut low := 0 |
| 30 | mut high := sources.documents.len |
| 31 | for low < high { |
| 32 | middle := low + (high - low) / 2 |
| 33 | if int(id) >= sources.documents[middle].end { low = middle + 1 } else { high = middle } |
| 34 | } |
| 35 | if low < sources.documents.len && int(id) >= sources.documents[low].base { return low } |
| 36 | return none |
| 37 | } |
| 38 | |
| 39 | fn (code &Code) source_document(id reader.NodeId) ?int { |
| 40 | if isnil(code.sources) || int(id) < code.sources.first { return none } |
| 41 | return code.sources.find(id) |
| 42 | } |
| 43 | |
| 44 | fn (code &Code) reader_source(id reader.NodeId) !(&reader.Document, reader.NodeId, int) { |
| 45 | if !isnil(code.sources) && int(id) >= code.sources.first { |
| 46 | index := code.sources.find(id) or { return failure('invalid included source node') } |
| 47 | source := code.sources.documents[index] |
| 48 | return source.doc, reader.NodeId(int(id) - source.base), source.base |
| 49 | } |
| 50 | return &code.doc, id, 0 |
| 51 | } |
| 52 | |
| 53 | fn source_node(base int, id reader.NodeId) reader.NodeId { |
| 54 | return if id >= reader.nil_node { reader.NodeId(base + int(id)) } else { id } |
| 55 | } |
| 56 | |
| 57 | fn (code &Code) reader_node(id reader.NodeId) !reader.Node { |
| 58 | doc, local, base := code.reader_source(id)! |
| 59 | node := doc.arena.node(local)! |
| 60 | if base == 0 || node.kind != .pair { return node } |
| 61 | return reader.Node{ ...node, car: source_node(base, node.car), cdr: source_node(base, node.cdr) } |
| 62 | } |
| 63 | |
| 64 | fn (code &Code) source_file(id reader.NodeId) string { |
| 65 | origin := code.source_origin(id) |
| 66 | if index := code.source_document(origin) { return code.sources.documents[index].path } |
| 67 | return code.source_path |
| 68 | } |
| 69 | |
| 70 | fn (mut code Code) add_source(doc reader.Document, path string, including reader.NodeId) ![]reader.NodeId { |
| 71 | if isnil(code.sources) { |
| 72 | code.sources = source_documents(code.doc, code.source_path, '', code.syntax_seed) |
| 73 | } |
| 74 | mut sources := code.sources |
| 75 | parent := code.source_document(code.source_origin(including)) or { -1 } |
| 76 | identity := os.real_path(path) |
| 77 | mut ancestor := parent |
| 78 | for ancestor >= 0 { |
| 79 | if sources.documents[ancestor].identity == identity { |
| 80 | return failure('cyclic source inclusion: ${path}') |
| 81 | } |
| 82 | ancestor = sources.documents[ancestor].parent |
| 83 | } |
| 84 | if sources.root_identity != '' && identity == sources.root_identity { |
| 85 | return failure('cyclic source inclusion: ${path}') |
| 86 | } |
| 87 | if doc.arena.has_labels() { code.enable_datum_graphs() } |
| 88 | count := doc.arena.len() |
| 89 | if count > 2147483647 - sources.next { |
| 90 | return failure('included source exceeds supported node range') |
| 91 | } |
| 92 | base := sources.next |
| 93 | sources.next += count |
| 94 | sources.documents << SourceDocument{ doc: &reader.Document{ ...doc }, path: path, identity: identity, base: base, end: sources.next, parent: parent } |
| 95 | mut forms := filled_storage(doc.forms.len, reader.NodeId(0)) |
| 96 | for i, id in doc.forms { container_store_validated(mut forms, i, source_node(base, id)) } |
| 97 | return forms |
| 98 | } |
| 99 | |
| 100 | fn (code &Code) source_failure(id reader.NodeId, cause IError) IError { |
| 101 | if is_control_transfer(cause) { return cause } |
| 102 | if cause is EvalError { |
| 103 | if cause.line > 0 || cause.source_path != '' { return cause } |
| 104 | } |
| 105 | node := code.reader_node(code.source_origin(id)) or { return cause } |
| 106 | return EvalError{ message: cause.msg(), source_path: code.source_file(id), offset: node.start, line: node.line, column: node.column } |
| 107 | } |
| 108 | |
| 109 | fn source_documents(doc reader.Document, path string, library_root string, graph_seed u64) &SourceDocuments { |
| 110 | first := if doc.arena.len() > 0 { doc.arena.len() } else { 1 } |
| 111 | return &SourceDocuments{ |
| 112 | first: first |
| 113 | next: first |
| 114 | library_root: library_root |
| 115 | graph_seed: graph_seed |
| 116 | root_identity: if path == '' { |
| 117 | '' |
| 118 | } else { |
| 119 | os.real_path(path) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn (code &Code) source_library_root() string { |
| 125 | return if isnil(code.sources) { '' } else { code.sources.library_root } |
| 126 | } |
| 127 | |