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)] #[derive(Clone)]
pub struct CustomExpr { pub struct CustomExpr {
/// Implementation function. /// Implementation function.
pub(crate) func: Shared<FnCustomSyntaxEval>, pub func: Shared<FnCustomSyntaxEval>,
/// List of keywords. /// List of keywords.
pub(crate) keywords: StaticVec<Expr>, pub keywords: StaticVec<Expr>,
/// List of tokens actually parsed. /// List of tokens actually parsed.
pub(crate) tokens: Vec<ImmutableString>, pub tokens: Vec<ImmutableString>,
} }
impl fmt::Debug for CustomExpr { 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. /// _(INTERNALS)_ A binary expression.
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.
/// ///

View File

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

View File

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

View File

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