Host Interface initial vars support

This commit is contained in:
Erin 2022-09-19 20:58:32 +02:00 committed by ondra05
parent 2690c65c21
commit 61fbd3ee28
2 changed files with 24 additions and 6 deletions

View file

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

View file

@ -91,7 +91,12 @@ impl<H: HostInterface> ExecEnv<H> {
/// other information.
pub fn with_host_interface(host_interface: H) -> Self {
Self {
stack: vec![Default::default()],
stack: vec![
Default::default(),
Scope {
variables: H::initial_vars(),
},
],
read_buf: Default::default(),
finalisers: vec![],
host_interface,
@ -103,12 +108,15 @@ impl<H: HostInterface> ExecEnv<H> {
where
I: IntoIterator<Item = (String, Variable)>,
{
let scope = Scope {
variables: ablescript_consts().into_iter().chain(vars).collect(),
};
Self {
stack: vec![scope],
stack: vec![
Scope {
variables: ablescript_consts().into_iter().chain(vars).collect(),
},
Scope {
variables: H::initial_vars(),
},
],
read_buf: Default::default(),
finalisers: vec![],
host_interface,