added new_with_vars

This commit is contained in:
Erin 2022-02-14 00:08:48 +01:00 committed by ondra05
parent 0134448472
commit ca4a9aba4c

View file

@ -87,6 +87,21 @@ impl ExecEnv {
} }
} }
/// Create a new Scope with predefined variables
pub fn new_with_vars<I>(vars: I) -> Self
where
I: IntoIterator<Item = (String, Variable)>,
{
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 /// 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 /// error if one or more of the Stmts failed to evaluate, or if a
/// `break` or `hopback` statement occurred at the top level. /// `break` or `hopback` statement occurred at the top level.
@ -224,7 +239,9 @@ impl ExecEnv {
instructions: code.to_owned(), instructions: code.to_owned(),
tape_len: tape_len tape_len: tape_len
.as_ref() .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))?, .unwrap_or(Ok(crate::brian::DEFAULT_TAPE_SIZE_LIMIT))?,
}), }),
); );