Machine and stream config rework

This commit is contained in:
bakaq
2024-09-29 21:30:27 -03:00
parent 29fc55cb28
commit 658e39aae6
4 changed files with 60 additions and 17 deletions

View File

@@ -1,32 +1,74 @@
pub struct MachineConfig { /// Describes how the streams of a `crate::Machine` will be handled.
pub streams: StreamConfig, ///
pub toplevel: &'static str, /// Defaults to using standard IO.
pub struct StreamConfig {
pub(crate) inner: StreamConfigInner,
} }
pub enum StreamConfig { 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(crate) enum StreamConfigInner {
Stdio, Stdio,
Memory, 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 { impl Default for MachineConfig {
fn default() -> Self { fn default() -> Self {
MachineConfig { MachineConfig {
streams: StreamConfig::Stdio, streams: Default::default(),
toplevel: include_str!("../toplevel.pl"), toplevel: default_toplevel(),
} }
} }
} }
impl MachineConfig { impl MachineConfig {
pub fn in_memory() -> Self { /// Creates a default configuration.
MachineConfig { pub fn new() -> Self {
streams: StreamConfig::Memory, Default::default()
..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 { pub fn with_toplevel(mut self, toplevel: &'static str) -> Self {
self.toplevel = toplevel; self.toplevel = toplevel;
self self
} }
} }
/// Returns a static string slice to the default toplevel
pub fn default_toplevel() -> &'static str {
include_str!("../toplevel.pl")
}

View File

@@ -1,6 +1,6 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use crate::atom_table; use crate::{atom_table, StreamConfig};
use crate::machine::machine_indices::VarKey; use crate::machine::machine_indices::VarKey;
use crate::machine::mock_wam::CompositeOpDir; use crate::machine::mock_wam::CompositeOpDir;
use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS}; use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS};
@@ -144,7 +144,7 @@ impl Iterator for QueryState<'_> {
impl Machine { impl Machine {
pub fn new_lib() -> Self { 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) { pub fn load_module_string(&mut self, module_name: &str, program: String) {

View File

@@ -6,6 +6,7 @@ pub use crate::machine::*;
pub use crate::parser::ast::*; pub use crate::parser::ast::*;
use crate::read::*; use crate::read::*;
pub use crate::types::*; pub use crate::types::*;
use crate::StreamConfig;
use std::sync::Arc; use std::sync::Arc;
@@ -232,7 +233,7 @@ pub(crate) fn parse_and_write_parsed_term_to_heap(
impl Machine { impl Machine {
pub fn with_test_streams() -> Self { 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> { pub fn test_load_file(&mut self, file: &str) -> Vec<u8> {

View File

@@ -485,13 +485,13 @@ impl Machine {
let args = MachineArgs::new(); let args = MachineArgs::new();
let mut machine_st = MachineState::new(); let mut machine_st = MachineState::new();
let (user_input, user_output, user_error) = match config.streams { let (user_input, user_output, user_error) = match config.streams.inner {
config::StreamConfig::Stdio => ( config::StreamConfigInner::Stdio => (
Stream::stdin(&mut machine_st.arena, args.add_history), Stream::stdin(&mut machine_st.arena, args.add_history),
Stream::stdout(&mut machine_st.arena), Stream::stdout(&mut machine_st.arena),
Stream::stderr(&mut machine_st.arena), Stream::stderr(&mut machine_st.arena),
), ),
config::StreamConfig::Memory => ( config::StreamConfigInner::Memory => (
Stream::Null(StreamOptions::default()), Stream::Null(StreamOptions::default()),
Stream::from_owned_string("".to_owned(), &mut machine_st.arena), Stream::from_owned_string("".to_owned(), &mut machine_st.arena),
Stream::stderr(&mut machine_st.arena), Stream::stderr(&mut machine_st.arena),