Host Interface initial vars support

trunk
ondra05 2022-09-19 20:58:32 +02:00
parent daca854db4
commit 2f5306795f
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 /// Host Environment Interface
pub trait HostInterface { pub trait HostInterface {
/// Initial variables for a stack frame
fn initial_vars() -> 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<()>;
@ -17,6 +23,10 @@ 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> {
HashMap::default()
}
fn print(&mut self, string: &str, new_line: bool) -> std::io::Result<()> { fn print(&mut self, string: &str, new_line: bool) -> std::io::Result<()> {
use std::io::Write; use std::io::Write;

View File

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