From 417b3b53b43465ea88ce5f65c1450ddef7a75853 Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 14 Feb 2022 00:08:48 +0100 Subject: [PATCH] added new_with_vars --- ablescript/src/interpret.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index a2239e2..ba9191f 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -87,6 +87,21 @@ impl ExecEnv { } } + /// Create a new Scope with predefined variables + pub fn new_with_vars(vars: I) -> Self + where + I: IntoIterator, + { + let scope = Scope { + variables: ablescript_consts().into_iter().chain(vars).collect(), + }; + + Self { + stack: vec![scope], + read_buf: Default::default(), + } + } + /// Execute a set of Statements in the root stack frame. Return an /// error if one or more of the Stmts failed to evaluate, or if a /// `break` or `hopback` statement occurred at the top level. @@ -224,7 +239,9 @@ impl ExecEnv { instructions: code.to_owned(), tape_len: tape_len .as_ref() - .map(|tape_len| self.eval_expr(tape_len).map(|v| v.into_isize() as usize)) + .map(|tape_len| { + self.eval_expr(tape_len).map(|v| v.into_isize() as usize) + }) .unwrap_or(Ok(crate::brian::DEFAULT_TAPE_SIZE_LIMIT))?, }), );