initial vars - &mut self

trunk
ondra05 2022-09-21 00:28:57 +02:00
parent 8970dabcfd
commit 8ef7118dc1
2 changed files with 6 additions and 6 deletions

View File

@ -4,7 +4,7 @@ use std::collections::HashMap;
/// Host Environment Interface /// Host Environment Interface
pub trait HostInterface { pub trait HostInterface {
/// Initial variables for a stack frame /// Initial variables for a stack frame
fn initial_vars() -> HashMap<String, Variable>; fn initial_vars(&mut self) -> HashMap<String, Variable>;
/// Print a string /// Print a string
fn print(&mut self, string: &str, new_line: bool) -> std::io::Result<()>; fn print(&mut self, string: &str, new_line: bool) -> std::io::Result<()>;
@ -23,7 +23,7 @@ pub trait HostInterface {
#[derive(Clone, Copy, Default)] #[derive(Clone, Copy, Default)]
pub struct Standard; pub struct Standard;
impl HostInterface for Standard { impl HostInterface for Standard {
fn initial_vars() -> HashMap<String, Variable> { fn initial_vars(&mut self) -> HashMap<String, Variable> {
HashMap::default() HashMap::default()
} }

View File

@ -89,12 +89,12 @@ pub const READ_BITS: u8 = 3;
impl<H: HostInterface> ExecEnv<H> { impl<H: HostInterface> ExecEnv<H> {
/// Create a new Scope with no predefined variable definitions or /// Create a new Scope with no predefined variable definitions or
/// other information. /// other information.
pub fn with_host_interface(host_interface: H) -> Self { pub fn with_host_interface(mut host_interface: H) -> Self {
Self { Self {
stack: vec![ stack: vec![
Default::default(), Default::default(),
Scope { Scope {
variables: H::initial_vars(), variables: host_interface.initial_vars(),
}, },
], ],
read_buf: Default::default(), read_buf: Default::default(),
@ -104,7 +104,7 @@ impl<H: HostInterface> ExecEnv<H> {
} }
/// Create a new Scope with predefined variables /// Create a new Scope with predefined variables
pub fn new_with_vars<I>(host_interface: H, vars: I) -> Self pub fn new_with_vars<I>(mut host_interface: H, vars: I) -> Self
where where
I: IntoIterator<Item = (String, Variable)>, I: IntoIterator<Item = (String, Variable)>,
{ {
@ -114,7 +114,7 @@ impl<H: HostInterface> ExecEnv<H> {
variables: ablescript_consts().into_iter().chain(vars).collect(), variables: ablescript_consts().into_iter().chain(vars).collect(),
}, },
Scope { Scope {
variables: H::initial_vars(), variables: host_interface.initial_vars(),
}, },
], ],
read_buf: Default::default(), read_buf: Default::default(),