From 61fbd3ee283cddbb07f98913a3ded23d95271c03 Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 19 Sep 2022 20:58:32 +0200 Subject: [PATCH] Host Interface initial vars support --- ablescript/src/host_interface.rs | 10 ++++++++++ ablescript/src/interpret.rs | 20 ++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ablescript/src/host_interface.rs b/ablescript/src/host_interface.rs index d8db023..e0920e2 100644 --- a/ablescript/src/host_interface.rs +++ b/ablescript/src/host_interface.rs @@ -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; + /// 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 { + HashMap::default() + } + fn print(&mut self, string: &str, new_line: bool) -> std::io::Result<()> { use std::io::Write; diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index d74a32f..6274e51 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -91,7 +91,12 @@ impl ExecEnv { /// 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 ExecEnv { where I: IntoIterator, { - 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,