Encapsulate structures.

This commit is contained in:
Stephen Chung 2020-12-14 23:05:13 +08:00
parent 5ea6efe6fd
commit 17310ef576
4 changed files with 18 additions and 26 deletions

View File

@ -858,11 +858,11 @@ impl Stmt {
#[derive(Clone)]
pub struct CustomExpr {
/// Implementation function.
pub(crate) func: Shared<FnCustomSyntaxEval>,
pub func: Shared<FnCustomSyntaxEval>,
/// List of keywords.
pub(crate) keywords: StaticVec<Expr>,
pub keywords: StaticVec<Expr>,
/// List of tokens actually parsed.
pub(crate) tokens: Vec<ImmutableString>,
pub tokens: Vec<ImmutableString>,
}
impl fmt::Debug for CustomExpr {
@ -876,24 +876,6 @@ impl fmt::Debug for CustomExpr {
}
}
impl CustomExpr {
/// Get the implementation function for this custom syntax.
#[inline(always)]
pub fn func(&self) -> &FnCustomSyntaxEval {
self.func.as_ref()
}
/// Get the keywords for this custom syntax.
#[inline(always)]
pub fn keywords(&self) -> &[Expr] {
&self.keywords
}
/// Get the actual parsed tokens for this custom syntax.
#[inline(always)]
pub fn tokens(&self) -> &[ImmutableString] {
&self.tokens
}
}
/// _(INTERNALS)_ A binary expression.
/// Exported under the `internals` feature only.
///

View File

@ -532,7 +532,7 @@ pub struct Limits {
#[derive(Debug)]
pub struct EvalContext<'e, 'x, 'px: 'x, 'a, 's, 'm, 'pm: 'm, 't, 'pt: 't> {
pub(crate) engine: &'e Engine,
pub scope: &'x mut Scope<'px>,
pub(crate) scope: &'x mut Scope<'px>,
pub(crate) mods: &'a mut Imports,
pub(crate) state: &'s mut State,
pub(crate) lib: &'m [&'pm Module],
@ -546,6 +546,16 @@ impl<'e, 'x, 'px, 'a, 's, 'm, 'pm, 't, 'pt> EvalContext<'e, 'x, 'px, 'a, 's, 'm,
pub fn engine(&self) -> &'e Engine {
self.engine
}
/// The current [`Scope`].
#[inline(always)]
pub fn scope(&self) -> &Scope<'px> {
self.scope
}
/// Mutable reference to the current [`Scope`].
#[inline(always)]
pub fn scope_mut(&mut self) -> &mut &'x mut Scope<'px> {
&mut self.scope
}
/// _(INTERNALS)_ The current set of modules imported via `import` statements.
/// Available under the `internals` feature only.
#[cfg(feature = "internals")]
@ -1822,7 +1832,7 @@ impl Engine {
Expr::Custom(custom, _) => {
let expressions = custom
.keywords()
.keywords
.iter()
.map(Into::into)
.collect::<StaticVec<_>>();

View File

@ -27,7 +27,7 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
let stmt = inputs.get(1).unwrap();
let condition = inputs.get(2).unwrap();
context.scope.push(var_name, 0 as INT);
context.scope_mut().push(var_name, 0 as INT);
loop {
context.eval_expression_tree(stmt)?;
@ -110,7 +110,7 @@ fn test_custom_syntax_raw() -> Result<(), Box<EvalAltResult>> {
},
1,
|context, inputs| {
context.scope.push("foo", 999 as INT);
context.scope_mut().push("foo", 999 as INT);
Ok(match inputs[0].get_variable_name().unwrap() {
"world" => 123 as INT,

View File

@ -71,7 +71,7 @@ fn test_var_resolver() -> Result<(), Box<EvalAltResult>> {
}
// Silently maps 'chameleon' into 'innocent'.
"chameleon" => context
.scope
.scope()
.get_value("innocent")
.map(Some)
.ok_or_else(|| {