]> Repositorios git - scryer-prolog.git/commitdiff
Machine and stream config rework
authorbakaq <[email protected]>
Mon, 30 Sep 2024 00:30:27 +0000 (21:30 -0300)
committerbakaq <[email protected]>
Sun, 8 Dec 2024 23:18:05 +0000 (20:18 -0300)
src/machine/config.rs
src/machine/lib_machine.rs
src/machine/mock_wam.rs
src/machine/mod.rs

index 6268c8d813a96b0b40720046ed788a20840d9b1d..79752f31790a76d193a77852b802edbdee810deb 100644 (file)
@@ -1,32 +1,74 @@
-pub struct MachineConfig {
-    pub streams: StreamConfig,
-    pub toplevel: &'static str,
+/// Describes how the streams of a `crate::Machine` will be handled.
+///
+/// Defaults to using standard IO.
+pub struct StreamConfig {
+    pub(crate) inner: StreamConfigInner,
+}
+
+impl Default for StreamConfig {
+    fn default() -> Self {
+        Self::stdio()
+    }
+}
+
+impl StreamConfig {
+    /// Binds the input, output and error streams to stdin, stdout and stderr.
+    pub fn stdio() -> Self {
+        StreamConfig {
+            inner: StreamConfigInner::Stdio,
+        }
+    }
+
+    /// Binds the output stream to a memory buffer, and the error stream to stderr.
+    ///
+    /// The input stream is ignored.
+    pub fn in_memory() -> Self {
+        StreamConfig {
+            inner: StreamConfigInner::Memory,
+        }
+    }
 }
 
-pub enum StreamConfig {
+pub(crate) enum StreamConfigInner {
     Stdio,
     Memory,
 }
 
+/// Describes how a `crate::Machine` will be configured.
+pub struct MachineConfig {
+    pub(crate) streams: StreamConfig,
+    pub(crate) toplevel: &'static str,
+}
+
 impl Default for MachineConfig {
     fn default() -> Self {
         MachineConfig {
-            streams: StreamConfig::Stdio,
-            toplevel: include_str!("../toplevel.pl"),
+            streams: Default::default(),
+            toplevel: default_toplevel(),
         }
     }
 }
 
 impl MachineConfig {
-    pub fn in_memory() -> Self {
-        MachineConfig {
-            streams: StreamConfig::Memory,
-            ..Default::default()
-        }
+    /// Creates a default configuration.
+    pub fn new() -> Self {
+        Default::default()
+    }
+
+    /// Uses the given `crate::StreamConfig` in this configuration. 
+    pub fn with_streams(mut self, streams: StreamConfig) -> Self {
+        self.streams = streams;
+        self
     }
 
+    /// Uses the given toplevel in this configuration.
     pub fn with_toplevel(mut self, toplevel: &'static str) -> Self {
         self.toplevel = toplevel;
         self
     }
 }
+
+/// Returns a static string slice to the default toplevel
+pub fn default_toplevel() -> &'static str {
+    include_str!("../toplevel.pl")
+}
index 52fb3e020ec4964a8e9af9bf11b52b9a18bbffd5..abfe87bf1195d77594dc5ad2d4d21644e24c5b2e 100644 (file)
@@ -1,6 +1,6 @@
 use std::collections::BTreeMap;
 
-use crate::atom_table;
+use crate::{atom_table, StreamConfig};
 use crate::machine::machine_indices::VarKey;
 use crate::machine::mock_wam::CompositeOpDir;
 use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS};
@@ -144,7 +144,7 @@ impl Iterator for QueryState<'_> {
 
 impl Machine {
     pub fn new_lib() -> Self {
-        Machine::new(MachineConfig::in_memory())
+        Machine::new(MachineConfig::default().with_streams(StreamConfig::in_memory()))
     }
 
     pub fn load_module_string(&mut self, module_name: &str, program: String) {
index 90aefa0e7cd74309c48066f8c656a03a4276ee58..2c7e37d6b3a9ee0c5629cea69bdc4296e3bfa860 100644 (file)
@@ -6,6 +6,7 @@ pub use crate::machine::*;
 pub use crate::parser::ast::*;
 use crate::read::*;
 pub use crate::types::*;
+use crate::StreamConfig;
 
 use std::sync::Arc;
 
@@ -232,7 +233,7 @@ pub(crate) fn parse_and_write_parsed_term_to_heap(
 
 impl Machine {
     pub fn with_test_streams() -> Self {
-        Machine::new(MachineConfig::in_memory())
+        Machine::new(MachineConfig::default().with_streams(StreamConfig::in_memory()))
     }
 
     pub fn test_load_file(&mut self, file: &str) -> Vec<u8> {
index f35288926df64b64fe4edf3813a38360b28ce9f1..94f2842f804a9aeb6e766f6f84ed22820a15f892 100644 (file)
@@ -485,13 +485,13 @@ impl Machine {
         let args = MachineArgs::new();
         let mut machine_st = MachineState::new();
 
-        let (user_input, user_output, user_error) = match config.streams {
-            config::StreamConfig::Stdio => (
+        let (user_input, user_output, user_error) = match config.streams.inner {
+            config::StreamConfigInner::Stdio => (
                 Stream::stdin(&mut machine_st.arena, args.add_history),
                 Stream::stdout(&mut machine_st.arena),
                 Stream::stderr(&mut machine_st.arena),
             ),
-            config::StreamConfig::Memory => (
+            config::StreamConfigInner::Memory => (
                 Stream::Null(StreamOptions::default()),
                 Stream::from_owned_string("".to_owned(), &mut machine_st.arena),
                 Stream::stderr(&mut machine_st.arena),