| 1 | module scheme |
| 2 | |
| 3 | import lexer |
| 4 | import reader |
| 5 | |
| 6 | // SourceReadState belongs to the caller. Reuse it for successive pieces of one |
| 7 | // input stream; ordinary eval/eval_file entries keep independent initial states. |
| 8 | pub struct SourceReadState { |
| 9 | pub mut: |
| 10 | fold_case bool = true |
| 11 | } |
| 12 | |
| 13 | // Incomplete source will be reparsed with more input, so it keeps its initial |
| 14 | // mode. A fatal parse failure commits only the scanner state actually consumed, |
| 15 | // including directives encountered before a late label-validation failure. |
| 16 | fn commit_read_failure(mut state SourceReadState, failure IError) { |
| 17 | cause := reader.failure_cause(failure) |
| 18 | if cause is reader.ReadError { |
| 19 | if cause.incomplete { return } |
| 20 | } |
| 21 | if cause is lexer.LexError { |
| 22 | if cause.incomplete { return } |
| 23 | } |
| 24 | if progress := reader.failure_state(failure) { state.fold_case = progress.fold_case } |
| 25 | } |
| 26 | |
| 27 | // eval_with_read_state parses once and commits reader directives before running |
| 28 | // any forms. Execution failures do not roll back consumed source. Source-only |
| 29 | // .auto uses Scheme; filenames still select the frontend in eval_file_as. |
| 30 | pub fn (mut vm VM) eval_with_read_state(source string, syntax SourceSyntax, mut state SourceReadState) !Value { |
| 31 | vm.ready()! |
| 32 | vm.check_entry_control()! |
| 33 | doc := read_source_input(vm, text: source, syntax: syntax, fold_case: state.fold_case) or { |
| 34 | commit_read_failure(mut state, err) |
| 35 | return err |
| 36 | } |
| 37 | state.fold_case = doc.final_fold_case() |
| 38 | return vm.eval_document(doc, unsafe { nil }, '') |
| 39 | } |
| 40 | |