| 1 | module scheme |
| 2 | |
| 3 | import lexer |
| 4 | import reader |
| 5 | |
| 6 | // read consumes one external datum. The reader sees a borrowed, complete frame |
| 7 | // once; decoded strings/symbols and every mutable container own their storage. |
| 8 | // No compiler cache, reversed car array, or second copy of the input is needed. |
| 9 | pub fn (mut vm VM) read(value Value) !Value { |
| 10 | vm.ready()! |
| 11 | if !isnil(custom_input_resource(value)) { |
| 12 | vm.checked_text_input(value)! |
| 13 | return vm.custom_text_input_host_operation(.get_datum, value, unspecified(), 0, 0, unspecified()) |
| 14 | } |
| 15 | return vm.read_input(value) |
| 16 | } |
| 17 | |
| 18 | fn (mut vm VM) read_input(value Value) !Value { |
| 19 | mut port := vm.checked_text_input(value)! |
| 20 | port.busy = true |
| 21 | defer { |
| 22 | port.busy = false |
| 23 | port.mark = -1 |
| 24 | port.release_large_frame() |
| 25 | } |
| 26 | start, end, initial_fold, present := port.frame_datum()! |
| 27 | if !present { return eof_value() } |
| 28 | source := unsafe { tos(&u8(port.buffer.data) + start, end - start) } |
| 29 | result := port.read_input_frame(source, initial_fold) or { return port.absolute_input_error(err, start) } |
| 30 | return result |
| 31 | } |
| 32 | |
| 33 | fn port_read(mut vm VM, args []Value) !Value { |
| 34 | check_arity('read', args.len, 0, 1)! |
| 35 | target := if args.len == 0 { vm.current_input_value()! } else { args[0] } |
| 36 | return vm.read_input(target) or { |
| 37 | // Consumed port errors must never ask a REPL to replay its eval form. |
| 38 | if err is reader.ReadError { |
| 39 | return EvalError{ message: 'read: ${err.message}', offset: err.offset, line: err.line, column: err.column } |
| 40 | } |
| 41 | if err is lexer.LexError { |
| 42 | return EvalError{ message: 'read: ${err.message}', offset: err.offset, line: err.line, column: err.column } |
| 43 | } |
| 44 | return err |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | @[inline] |
| 49 | fn input_space(byte u8) bool { |
| 50 | return byte == ` ` || (byte >= 0x09 && byte <= 0x0d) |
| 51 | } |
| 52 | |
| 53 | @[inline] |
| 54 | fn input_delimiter(byte u8) bool { |
| 55 | return input_space(byte) || byte in [`(`, `)`, `"`, `;`] |
| 56 | } |
| 57 | |
| 58 | // Decoding a completed frame does not require an I/O resource or heap port. |
| 59 | struct InputFrameState { |
| 60 | mut: |
| 61 | no_fold bool |
| 62 | } |
| 63 | |
| 64 | @[inline] |
| 65 | fn (mut port InputPort) read_input_frame(source string, initial_fold bool) !Value { |
| 66 | mut state := InputFrameState{ no_fold: port.no_fold } |
| 67 | defer { port.no_fold = state.no_fold } |
| 68 | return state.read_frame(source, initial_fold) |
| 69 | } |
| 70 | |
| 71 | fn (mut port InputFrameState) read_frame(source string, initial_fold bool) !Value { |
| 72 | first := source[0] |
| 73 | if source.len == 2 && first == `#` && source[1] in [`t`, `T`, `f`, `F`] { |
| 74 | return boolean(source[1] in [`t`, `T`]) |
| 75 | } |
| 76 | if first !in [`(`, `)`, `"`, `'`, `\x60`, `,`] |
| 77 | && !(first == `#` && source.len >= 2 && source[1] in [`(`, `\\`]) { |
| 78 | mut scanner := lexer.new_scanner(source, fold_case: initial_fold)! |
| 79 | token := scanner.next()! |
| 80 | if token.start == 0 && token.end == source.len { |
| 81 | if token.kind == .number { return parse_number(source) } |
| 82 | if token.kind == .boolean { return boolean(source[1] in [`t`, `T`]) } |
| 83 | if token.kind == .identifier && scanner.token_flags() & (lexer.token_identifier_escaped | lexer.token_identifier_barred | lexer.token_identifier_unicode) == 0 { |
| 84 | return text_value(.symbol, if initial_fold { |
| 85 | source.to_lower_ascii() |
| 86 | } else { |
| 87 | source.clone() |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | doc := reader.read_all(source, fold_case: initial_fold)! |
| 93 | port.no_fold = !doc.final_fold_case() |
| 94 | if doc.forms.len != 1 { return failure('input frame must contain one datum') } |
| 95 | if doc.arena.has_labels() { |
| 96 | mut cache := map[reader.NodeId]Value{} |
| 97 | return materialize_datum_graph(&doc, doc.forms[0], mut cache, false) |
| 98 | } |
| 99 | return read_port_datum(&doc, doc.forms[0]) |
| 100 | } |
| 101 | |
| 102 | fn read_port_datum(doc &reader.Document, id reader.NodeId) !Value { |
| 103 | node := doc.arena.node(id)! |
| 104 | return match node.kind { |
| 105 | .empty_list { nil_value() } |
| 106 | .reference { return failure('unresolved reader reference') } |
| 107 | .bytevector { read_bytevector_datum(doc, id, '')! } |
| 108 | .boolean { boolean(doc.arena.boolean_value(id)!) } |
| 109 | .number { |
| 110 | // The parser consumes this lexer-validated span synchronously and |
| 111 | // retains no source view; the port buffer stays pinned throughout. |
| 112 | canonical := doc.arena.node(doc.arena.canonical_id(id)!)! |
| 113 | parse_number(number_view(doc.arena.source_text(), canonical.start, canonical.end)) or { |
| 114 | return EvalError{ message: err.msg(), offset: node.start, line: node.line, column: node.column } |
| 115 | } |
| 116 | } |
| 117 | .character { character(doc.arena.character_value(id)!)! } |
| 118 | .symbol { text_value(.symbol, doc.arena.symbol_name(id)!) } |
| 119 | .keyword { text_value(.keyword, doc.arena.keyword_name(id)!) } |
| 120 | .string_literal { text_value(.string, doc.arena.string_value(id)!) } |
| 121 | .vector { |
| 122 | mut items := []Value{len: node.count} |
| 123 | for i in 0 .. node.count { |
| 124 | items[i] = read_port_datum(doc, doc.arena.vector_at(id, i)!)! |
| 125 | } |
| 126 | payload_value(.vector, &Vector{ items: items }) |
| 127 | } |
| 128 | .pair { |
| 129 | mut root := nil_value() |
| 130 | mut last := &Pair(unsafe { nil }) |
| 131 | mut cursor := id |
| 132 | for { |
| 133 | pair := doc.arena.node(cursor)! |
| 134 | if pair.kind != .pair { break } |
| 135 | car := read_port_datum(doc, pair.car)! |
| 136 | mut cell := &Pair{ car: car, cdr: nil_value() } |
| 137 | value := payload_value(.pair, cell) |
| 138 | if isnil(last) { root = value } else { last.cdr = value } |
| 139 | last = cell |
| 140 | cursor = pair.cdr |
| 141 | } |
| 142 | tail := read_port_datum(doc, cursor)! |
| 143 | last.cdr = tail |
| 144 | root |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | fn (mut port InputPort) release_large_frame() { |
| 150 | // Memory ports retain their single source snapshot so positions stay seekable. |
| 151 | if port.memory_input { return } |
| 152 | remaining := port.buffer.len - port.position |
| 153 | if port.buffer.cap <= 65536 || port.buffer.cap / 4 <= port.read_size || remaining > port.read_size { |
| 154 | return |
| 155 | } |
| 156 | port.checkpoint_input() |
| 157 | mut replacement := []u8{len: remaining, cap: port.read_size} |
| 158 | // Datum conversion has finished and retains no views of the input. Move |
| 159 | // only unread lookahead, so a long string need not pin the high-water buffer. |
| 160 | unsafe { |
| 161 | if remaining > 0 { |
| 162 | vmemcpy(replacement.data, &u8(port.buffer.data) + port.position, remaining) |
| 163 | } |
| 164 | replacement.flags.set(.noslices) |
| 165 | } |
| 166 | port.buffer = replacement |
| 167 | port.position = 0 |
| 168 | } |
| 169 | |