From 95b31146b58f2bae0e15b057321e4c194174cd92 Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Tue, 11 Jul 2023 14:22:27 +0200 Subject: [PATCH] Add Machine::set_user_input(&mut self, input: String) and get_user_output() -> String. Make read_term_from_user_input() handle Stream::Byte. --- src/machine/machine_state.rs | 4 ++++ src/machine/mod.rs | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/machine/machine_state.rs b/src/machine/machine_state.rs index 6aa51916..50431a8a 100644 --- a/src/machine/machine_state.rs +++ b/src/machine/machine_state.rs @@ -495,6 +495,10 @@ impl MachineState { } } + if let Stream::Byte(_) = stream { + return self.read_term(stream, indices) + } + unreachable!("Stream must be a Stream::Readline(_)") } diff --git a/src/machine/mod.rs b/src/machine/mod.rs index 4cd1fce8..f7beecd3 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -47,6 +47,7 @@ use ordered_float::OrderedFloat; use std::cmp::Ordering; use std::env; +use std::io::Read; use std::path::PathBuf; use std::sync::atomic::AtomicBool; use tokio::runtime::Runtime; @@ -306,6 +307,15 @@ impl Machine { self.run_module_predicate(atom!("$toplevel"), (atom!("$repl"), 1)); } + pub fn set_user_input(&mut self, input: String) { + self.user_input = Stream::from_owned_string(input, &mut self.machine_st.arena); + } + + pub fn get_user_output(&mut self) -> String { + let output_bytes: Vec<_> = self.user_output.bytes().map(|b| b.unwrap()).collect(); + String::from_utf8(output_bytes).unwrap() + } + pub(crate) fn configure_modules(&mut self) { fn update_call_n_indices(loader: &Module, target_code_dir: &mut CodeDir, arena: &mut Arena) { for arity in 1..66 { -- 2.54.0