Optimize data structures.

This commit is contained in:
Stephen Chung 2020-11-06 16:27:40 +08:00
parent c6ef2d1e95
commit 07a8a43298
8 changed files with 88 additions and 95 deletions

View File

@ -4,7 +4,7 @@ use crate::dynamic::{Dynamic, Union};
use crate::fn_native::{FnPtr, Shared}; use crate::fn_native::{FnPtr, Shared};
use crate::module::{Module, ModuleRef}; use crate::module::{Module, ModuleRef};
use crate::syntax::FnCustomSyntaxEval; use crate::syntax::FnCustomSyntaxEval;
use crate::token::{Position, Token}; use crate::token::{Position, Token, NO_POS};
use crate::utils::ImmutableString; use crate::utils::ImmutableString;
use crate::StaticVec; use crate::StaticVec;
use crate::INT; use crate::INT;
@ -93,9 +93,9 @@ pub struct ScriptFnDef {
pub access: FnAccess, pub access: FnAccess,
/// Names of function parameters. /// Names of function parameters.
pub params: StaticVec<String>, pub params: StaticVec<String>,
/// Access to external variables. /// Access to external variables. Boxed because it occurs rarely.
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
pub externals: HashSet<String>, pub externals: Option<Box<HashSet<String>>>,
} }
impl fmt::Display for ScriptFnDef { impl fmt::Display for ScriptFnDef {
@ -590,11 +590,12 @@ pub enum ReturnType {
Exception, Exception,
} }
/// _[INTERNALS]_ A Rhai statement. /// _[INTERNALS]_ A statement.
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.
/// ///
/// Each variant is at most one pointer in size (for speed), /// ## WARNING
/// with everything being allocated together in one single tuple. ///
/// This type is volatile and may change.
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone, Hash)]
pub enum Stmt { pub enum Stmt {
/// No-op. /// No-op.
@ -616,7 +617,7 @@ pub enum Stmt {
/// { stmt; ... } /// { stmt; ... }
Block(Vec<Stmt>, Position), Block(Vec<Stmt>, Position),
/// try { stmt; ... } catch ( var ) { stmt; ... } /// try { stmt; ... } catch ( var ) { stmt; ... }
TryCatch(Box<(Stmt, Option<Ident>, Stmt, (Position, Position))>), TryCatch(Box<(Stmt, Option<Ident>, Stmt)>, Position, Position),
/// expr /// expr
Expr(Expr), Expr(Expr),
/// continue /// continue
@ -639,7 +640,7 @@ pub enum Stmt {
impl Default for Stmt { impl Default for Stmt {
#[inline(always)] #[inline(always)]
fn default() -> Self { fn default() -> Self {
Self::Noop(Default::default()) Self::Noop(NO_POS)
} }
} }
@ -667,7 +668,7 @@ impl Stmt {
| Self::ReturnWithVal((_, pos), _, _) => *pos, | Self::ReturnWithVal((_, pos), _, _) => *pos,
Self::Let(x, _, _) | Self::Const(x, _, _) => x.pos, Self::Let(x, _, _) | Self::Const(x, _, _) => x.pos,
Self::TryCatch(x) => (x.3).0, Self::TryCatch(_, pos, _) => *pos,
Self::Expr(x) => x.position(), Self::Expr(x) => x.position(),
@ -696,7 +697,7 @@ impl Stmt {
| Self::ReturnWithVal((_, pos), _, _) => *pos = new_pos, | Self::ReturnWithVal((_, pos), _, _) => *pos = new_pos,
Self::Let(x, _, _) | Self::Const(x, _, _) => x.pos = new_pos, Self::Let(x, _, _) | Self::Const(x, _, _) => x.pos = new_pos,
Self::TryCatch(x) => (x.3).0 = new_pos, Self::TryCatch(_, pos, _) => *pos = new_pos,
Self::Expr(x) => { Self::Expr(x) => {
x.set_position(new_pos); x.set_position(new_pos);
@ -722,7 +723,7 @@ impl Stmt {
| Self::Loop(_, _) | Self::Loop(_, _)
| Self::For(_, _, _) | Self::For(_, _, _)
| Self::Block(_, _) | Self::Block(_, _)
| Self::TryCatch(_) => true, | Self::TryCatch(_, _, _) => true,
// A No-op requires a semicolon in order to know it is an empty statement! // A No-op requires a semicolon in order to know it is an empty statement!
Self::Noop(_) => false, Self::Noop(_) => false,
@ -758,7 +759,7 @@ impl Stmt {
Self::Let(_, _, _) | Self::Const(_, _, _) | Self::Assignment(_, _) => false, Self::Let(_, _, _) | Self::Const(_, _, _) | Self::Assignment(_, _) => false,
Self::Block(block, _) => block.iter().all(|stmt| stmt.is_pure()), Self::Block(block, _) => block.iter().all(|stmt| stmt.is_pure()),
Self::Continue(_) | Self::Break(_) | Self::ReturnWithVal(_, _, _) => false, Self::Continue(_) | Self::Break(_) | Self::ReturnWithVal(_, _, _) => false,
Self::TryCatch(x) => x.0.is_pure() && x.2.is_pure(), Self::TryCatch(x, _, _) => x.0.is_pure() && x.2.is_pure(),
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Self::Import(_, _, _) => false, Self::Import(_, _, _) => false,
@ -779,7 +780,9 @@ impl Stmt {
/// This type is volatile and may change. /// This type is volatile and may change.
#[derive(Clone)] #[derive(Clone)]
pub struct CustomExpr { pub struct CustomExpr {
/// List of keywords.
pub(crate) keywords: StaticVec<Expr>, pub(crate) keywords: StaticVec<Expr>,
/// Implementation function.
pub(crate) func: Shared<FnCustomSyntaxEval>, pub(crate) func: Shared<FnCustomSyntaxEval>,
} }
@ -848,6 +851,11 @@ impl From<INT> for FloatWrapper {
} }
/// A binary expression structure. /// A binary expression structure.
/// Exported under the `internals` feature only.
///
/// ## WARNING
///
/// This type is volatile and may change.
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone, Hash)]
pub struct BinaryExpr { pub struct BinaryExpr {
/// LHS expression. /// LHS expression.
@ -856,7 +864,12 @@ pub struct BinaryExpr {
pub rhs: Expr, pub rhs: Expr,
} }
/// A function call. /// _[INTERNALS]_ A function call.
/// Exported under the `internals` feature only.
///
/// ## WARNING
///
/// This type is volatile and may change.
#[derive(Debug, Clone, Hash, Default)] #[derive(Debug, Clone, Hash, Default)]
pub struct FnCallInfo { pub struct FnCallInfo {
/// Pre-calculated hash for a script-defined function of the same name and number of parameters. /// Pre-calculated hash for a script-defined function of the same name and number of parameters.
@ -869,7 +882,7 @@ pub struct FnCallInfo {
/// Default value when the function is not found, mostly used to provide a default for comparison functions. /// Default value when the function is not found, mostly used to provide a default for comparison functions.
/// Type is `bool` in order for `FnCallInfo` to be `Hash` /// Type is `bool` in order for `FnCallInfo` to be `Hash`
pub def_value: Option<bool>, pub def_value: Option<bool>,
/// Namespace of the function, if any. /// Namespace of the function, if any. Boxed because it occurs rarely.
pub namespace: Option<Box<ModuleRef>>, pub namespace: Option<Box<ModuleRef>>,
/// Function name. /// Function name.
/// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls /// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls
@ -882,9 +895,6 @@ pub struct FnCallInfo {
/// _[INTERNALS]_ An expression sub-tree. /// _[INTERNALS]_ An expression sub-tree.
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.
/// ///
/// Each variant is at most one pointer in size (for speed),
/// with everything being allocated together in one single tuple.
///
/// ## WARNING /// ## WARNING
/// ///
/// This type is volatile and may change. /// This type is volatile and may change.
@ -938,7 +948,7 @@ pub enum Expr {
impl Default for Expr { impl Default for Expr {
#[inline(always)] #[inline(always)]
fn default() -> Self { fn default() -> Self {
Self::Unit(Default::default()) Self::Unit(NO_POS)
} }
} }
@ -1241,16 +1251,21 @@ impl Expr {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
/// This test is to make sure no code changes increase the sizes of critical data structures. /// This test is to make sure no code changes increase the sizes of critical data structures.
#[test] #[test]
fn check_struct_sizes() { fn check_struct_sizes() {
use std::mem::size_of; use std::mem::size_of;
assert_eq!(size_of::<Dynamic>(), 16); assert_eq!(size_of::<crate::Dynamic>(), 16);
assert_eq!(size_of::<Option<Dynamic>>(), 16); assert_eq!(size_of::<Option<crate::Dynamic>>(), 16);
assert_eq!(size_of::<Expr>(), 16); assert_eq!(size_of::<crate::Position>(), 4);
assert_eq!(size_of::<Stmt>(), 32); assert_eq!(size_of::<crate::ast::Expr>(), 16);
assert_eq!(size_of::<Option<crate::ast::Expr>>(), 16);
assert_eq!(size_of::<crate::ast::Stmt>(), 32);
assert_eq!(size_of::<Option<crate::ast::Stmt>>(), 32);
assert_eq!(size_of::<crate::Scope>(), 72);
assert_eq!(size_of::<crate::LexError>(), 32);
assert_eq!(size_of::<crate::ParseError>(), 16);
assert_eq!(size_of::<crate::EvalAltResult>(), 64);
} }
} }

View File

@ -619,7 +619,6 @@ impl Dynamic {
Self(Union::Variant(Box::new(boxed))) Self(Union::Variant(Box::new(boxed)))
} }
/// Turn the `Dynamic` value into a shared `Dynamic` value backed by an `Rc<RefCell<Dynamic>>` /// Turn the `Dynamic` value into a shared `Dynamic` value backed by an `Rc<RefCell<Dynamic>>`
/// or `Arc<RwLock<Dynamic>>` depending on the `sync` feature. /// or `Arc<RwLock<Dynamic>>` depending on the `sync` feature.
/// ///
@ -644,7 +643,6 @@ impl Dynamic {
#[cfg(feature = "no_closure")] #[cfg(feature = "no_closure")]
panic!("converting into a shared value is not supported under 'no_closure'"); panic!("converting into a shared value is not supported under 'no_closure'");
} }
/// Convert the `Dynamic` value into specific type. /// Convert the `Dynamic` value into specific type.
/// ///
/// Casting to a `Dynamic` just returns as is, but if it contains a shared value, /// Casting to a `Dynamic` just returns as is, but if it contains a shared value,
@ -775,7 +773,6 @@ impl Dynamic {
_ => None, _ => None,
} }
} }
/// Convert the `Dynamic` value into a specific type. /// Convert the `Dynamic` value into a specific type.
/// ///
/// Casting to a `Dynamic` just returns as is, but if it contains a shared value, /// Casting to a `Dynamic` just returns as is, but if it contains a shared value,
@ -819,7 +816,6 @@ impl Dynamic {
) )
}) })
} }
/// Flatten the `Dynamic` and clone it. /// Flatten the `Dynamic` and clone it.
/// ///
/// If the `Dynamic` is not a shared value, it returns a cloned copy. /// If the `Dynamic` is not a shared value, it returns a cloned copy.
@ -839,7 +835,6 @@ impl Dynamic {
_ => self.clone(), _ => self.clone(),
} }
} }
/// Flatten the `Dynamic`. /// Flatten the `Dynamic`.
/// ///
/// If the `Dynamic` is not a shared value, it returns itself. /// If the `Dynamic` is not a shared value, it returns itself.
@ -867,7 +862,6 @@ impl Dynamic {
_ => self, _ => self,
} }
} }
/// Is the `Dynamic` a shared value that is locked? /// Is the `Dynamic` a shared value that is locked?
/// ///
/// ## Note /// ## Note
@ -889,7 +883,6 @@ impl Dynamic {
_ => false, _ => false,
} }
} }
/// Get a reference of a specific type to the `Dynamic`. /// Get a reference of a specific type to the `Dynamic`.
/// Casting to `Dynamic` just returns a reference to it. /// Casting to `Dynamic` just returns a reference to it.
/// ///
@ -922,7 +915,6 @@ impl Dynamic {
.map(|r| DynamicReadLock(DynamicReadLockInner::Reference(r))), .map(|r| DynamicReadLock(DynamicReadLockInner::Reference(r))),
} }
} }
/// Get a mutable reference of a specific type to the `Dynamic`. /// Get a mutable reference of a specific type to the `Dynamic`.
/// Casting to `Dynamic` just returns a mutable reference to it. /// Casting to `Dynamic` just returns a mutable reference to it.
/// ///
@ -955,7 +947,6 @@ impl Dynamic {
.map(|r| DynamicWriteLock(DynamicWriteLockInner::Reference(r))), .map(|r| DynamicWriteLock(DynamicWriteLockInner::Reference(r))),
} }
} }
/// Get a reference of a specific type to the `Dynamic`. /// Get a reference of a specific type to the `Dynamic`.
/// Casting to `Dynamic` just returns a reference to it. /// Casting to `Dynamic` just returns a reference to it.
/// ///
@ -1045,7 +1036,6 @@ impl Dynamic {
_ => None, _ => None,
} }
} }
/// Get a mutable reference of a specific type to the `Dynamic`. /// Get a mutable reference of a specific type to the `Dynamic`.
/// Casting to `Dynamic` just returns a mutable reference to it. /// Casting to `Dynamic` just returns a mutable reference to it.
/// ///
@ -1129,7 +1119,6 @@ impl Dynamic {
_ => None, _ => None,
} }
} }
/// Cast the `Dynamic` as the system integer type `INT` and return it. /// Cast the `Dynamic` as the system integer type `INT` and return it.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline(always)] #[inline(always)]
@ -1141,7 +1130,6 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Cast the `Dynamic` as the system floating-point type `FLOAT` and return it. /// Cast the `Dynamic` as the system floating-point type `FLOAT` and return it.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
@ -1154,7 +1142,6 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Cast the `Dynamic` as a `bool` and return it. /// Cast the `Dynamic` as a `bool` and return it.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline(always)] #[inline(always)]
@ -1166,7 +1153,6 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Cast the `Dynamic` as a `char` and return it. /// Cast the `Dynamic` as a `char` and return it.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline(always)] #[inline(always)]
@ -1178,7 +1164,6 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Cast the `Dynamic` as a string and return the string slice. /// Cast the `Dynamic` as a string and return the string slice.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
/// ///
@ -1191,7 +1176,6 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Convert the `Dynamic` into `String` and return it. /// Convert the `Dynamic` into `String` and return it.
/// If there are other references to the same string, a cloned copy is returned. /// If there are other references to the same string, a cloned copy is returned.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
@ -1200,7 +1184,6 @@ impl Dynamic {
self.take_immutable_string() self.take_immutable_string()
.map(ImmutableString::into_owned) .map(ImmutableString::into_owned)
} }
/// Convert the `Dynamic` into `ImmutableString` and return it. /// Convert the `Dynamic` into `ImmutableString` and return it.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline] #[inline]

View File

@ -2034,8 +2034,8 @@ impl Engine {
Stmt::Break(pos) => EvalAltResult::LoopBreak(true, *pos).into(), Stmt::Break(pos) => EvalAltResult::LoopBreak(true, *pos).into(),
// Try/Catch statement // Try/Catch statement
Stmt::TryCatch(x) => { Stmt::TryCatch(x, _, _) => {
let (try_body, var_def, catch_body, _) = x.as_ref(); let (try_body, var_def, catch_body) = x.as_ref();
let result = self let result = self
.eval_stmt(scope, mods, state, lib, this_ptr, try_body, level) .eval_stmt(scope, mods, state, lib, this_ptr, try_body, level)

View File

@ -118,7 +118,6 @@ impl<'a> ArgBackup<'a> {
mem::transmute(&mut self.value_copy) mem::transmute(&mut self.value_copy)
})); }));
} }
/// This function restores the first argument that was replaced by `change_first_arg_to_copy`. /// This function restores the first argument that was replaced by `change_first_arg_to_copy`.
/// ///
/// # Safety /// # Safety
@ -541,16 +540,20 @@ impl Engine {
// Move captured variables into scope // Move captured variables into scope
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
if let Some(captured) = _capture_scope { if let Some(captured) = _capture_scope {
captured if let Some(ref externals) = func.externals {
.into_iter() captured
.filter(|(name, _, _, _)| func.externals.contains(name.as_ref())) .into_iter()
.for_each(|(name, typ, value, _)| { .filter(|(name, _, _, _)| externals.contains(name.as_ref()))
// Consume the scope values. .for_each(|(name, typ, value, _)| {
match typ { // Consume the scope values.
ScopeEntryType::Normal => scope.push(name, value), match typ {
ScopeEntryType::Constant => scope.push_constant(name, value), ScopeEntryType::Normal => scope.push(name, value),
}; ScopeEntryType::Constant => {
}); scope.push_constant(name, value)
}
};
});
}
} }
let result = if _is_method { let result = if _is_method {

View File

@ -387,7 +387,7 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt {
} }
} }
// try { block } catch ( var ) { block } // try { block } catch ( var ) { block }
Stmt::TryCatch(x) if x.0.is_pure() => { Stmt::TryCatch(x, _, _) if x.0.is_pure() => {
// If try block is pure, there will never be any exceptions // If try block is pure, there will never be any exceptions
state.set_dirty(); state.set_dirty();
let pos = x.0.position(); let pos = x.0.position();
@ -399,14 +399,17 @@ fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt {
Stmt::Block(statements, pos) Stmt::Block(statements, pos)
} }
// try { block } catch ( var ) { block } // try { block } catch ( var ) { block }
Stmt::TryCatch(x) => { Stmt::TryCatch(x, try_pos, catch_pos) => {
let (try_block, var_name, catch_block, pos) = *x; let (try_block, var_name, catch_block) = *x;
Stmt::TryCatch(Box::new(( Stmt::TryCatch(
optimize_stmt(try_block, state, false), Box::new((
var_name, optimize_stmt(try_block, state, false),
optimize_stmt(catch_block, state, false), var_name,
pos, optimize_stmt(catch_block, state, false),
))) )),
try_pos,
catch_pos,
)
} }
// {} // {}
Stmt::Expr(Expr::Stmt(x, pos)) if x.is_empty() => { Stmt::Expr(Expr::Stmt(x, pos)) if x.is_empty() => {

View File

@ -32,7 +32,7 @@ use crate::{
use crate::stdlib::{ use crate::stdlib::{
borrow::Cow, borrow::Cow,
boxed::Box, boxed::Box,
collections::HashMap, collections::{HashMap, HashSet},
format, format,
hash::Hash, hash::Hash,
iter::empty, iter::empty,
@ -2376,12 +2376,11 @@ fn parse_try_catch(
// try { body } catch ( var ) { catch_block } // try { body } catch ( var ) { catch_block }
let catch_body = parse_block(input, state, lib, settings.level_up())?; let catch_body = parse_block(input, state, lib, settings.level_up())?;
Ok(Stmt::TryCatch(Box::new(( Ok(Stmt::TryCatch(
body, Box::new((body, var_def, catch_body)),
var_def, token_pos,
catch_body, catch_pos,
(token_pos, catch_pos), ))
))))
} }
/// Parse a function definition. /// Parse a function definition.
@ -2470,7 +2469,7 @@ fn parse_fn(
let params: StaticVec<_> = params.into_iter().map(|(p, _)| p).collect(); let params: StaticVec<_> = params.into_iter().map(|(p, _)| p).collect();
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
let externals = state let externals: HashSet<_> = state
.externals .externals
.iter() .iter()
.map(|(name, _)| name) .map(|(name, _)| name)
@ -2483,7 +2482,11 @@ fn parse_fn(
access, access,
params, params,
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
externals, externals: if externals.is_empty() {
None
} else {
Some(Box::new(externals))
},
body, body,
lib: None, lib: None,
}) })

View File

@ -70,8 +70,8 @@ pub struct Scope<'a> {
values: Vec<Dynamic>, values: Vec<Dynamic>,
/// Type of the entry. /// Type of the entry.
types: Vec<EntryType>, types: Vec<EntryType>,
/// (Name, alias) of the entry. /// (Name, alias) of the entry. The alias is Boxed because it occurs rarely.
names: Vec<(Cow<'a, str>, Option<String>)>, names: Vec<(Cow<'a, str>, Option<Box<String>>)>,
} }
impl<'a> Scope<'a> { impl<'a> Scope<'a> {
@ -91,7 +91,6 @@ impl<'a> Scope<'a> {
pub fn new() -> Self { pub fn new() -> Self {
Default::default() Default::default()
} }
/// Empty the Scope. /// Empty the Scope.
/// ///
/// # Example /// # Example
@ -118,7 +117,6 @@ impl<'a> Scope<'a> {
self.values.clear(); self.values.clear();
self self
} }
/// Get the number of entries inside the Scope. /// Get the number of entries inside the Scope.
/// ///
/// # Example /// # Example
@ -136,7 +134,6 @@ impl<'a> Scope<'a> {
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.values.len() self.values.len()
} }
/// Is the Scope empty? /// Is the Scope empty?
/// ///
/// # Example /// # Example
@ -154,7 +151,6 @@ impl<'a> Scope<'a> {
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.values.len() == 0 self.values.len() == 0
} }
/// Add (push) a new entry to the Scope. /// Add (push) a new entry to the Scope.
/// ///
/// # Example /// # Example
@ -175,7 +171,6 @@ impl<'a> Scope<'a> {
) -> &mut Self { ) -> &mut Self {
self.push_dynamic_value(name, EntryType::Normal, Dynamic::from(value)) self.push_dynamic_value(name, EntryType::Normal, Dynamic::from(value))
} }
/// Add (push) a new `Dynamic` entry to the Scope. /// Add (push) a new `Dynamic` entry to the Scope.
/// ///
/// # Example /// # Example
@ -192,7 +187,6 @@ impl<'a> Scope<'a> {
pub fn push_dynamic(&mut self, name: impl Into<Cow<'a, str>>, value: Dynamic) -> &mut Self { pub fn push_dynamic(&mut self, name: impl Into<Cow<'a, str>>, value: Dynamic) -> &mut Self {
self.push_dynamic_value(name, EntryType::Normal, value) self.push_dynamic_value(name, EntryType::Normal, value)
} }
/// Add (push) a new constant to the Scope. /// Add (push) a new constant to the Scope.
/// ///
/// Constants are immutable and cannot be assigned to. Their values never change. /// Constants are immutable and cannot be assigned to. Their values never change.
@ -219,7 +213,6 @@ impl<'a> Scope<'a> {
) -> &mut Self { ) -> &mut Self {
self.push_dynamic_value(name, EntryType::Constant, Dynamic::from(value)) self.push_dynamic_value(name, EntryType::Constant, Dynamic::from(value))
} }
/// Add (push) a new constant with a `Dynamic` value to the Scope. /// Add (push) a new constant with a `Dynamic` value to the Scope.
/// ///
/// Constants are immutable and cannot be assigned to. Their values never change. /// Constants are immutable and cannot be assigned to. Their values never change.
@ -247,7 +240,6 @@ impl<'a> Scope<'a> {
) -> &mut Self { ) -> &mut Self {
self.push_dynamic_value(name, EntryType::Constant, value) self.push_dynamic_value(name, EntryType::Constant, value)
} }
/// Add (push) a new entry with a `Dynamic` value to the Scope. /// Add (push) a new entry with a `Dynamic` value to the Scope.
#[inline] #[inline]
pub(crate) fn push_dynamic_value( pub(crate) fn push_dynamic_value(
@ -261,7 +253,6 @@ impl<'a> Scope<'a> {
self.values.push(value.into()); self.values.push(value.into());
self self
} }
/// Truncate (rewind) the Scope to a previous size. /// Truncate (rewind) the Scope to a previous size.
/// ///
/// # Example /// # Example
@ -295,7 +286,6 @@ impl<'a> Scope<'a> {
self.values.truncate(size); self.values.truncate(size);
self self
} }
/// Does the scope contain the entry? /// Does the scope contain the entry?
/// ///
/// # Example /// # Example
@ -316,7 +306,6 @@ impl<'a> Scope<'a> {
.rev() // Always search a Scope in reverse order .rev() // Always search a Scope in reverse order
.any(|(key, _)| name == key.as_ref()) .any(|(key, _)| name == key.as_ref())
} }
/// Find an entry in the Scope, starting from the last. /// Find an entry in the Scope, starting from the last.
#[inline(always)] #[inline(always)]
pub(crate) fn get_index(&self, name: &str) -> Option<(usize, EntryType)> { pub(crate) fn get_index(&self, name: &str) -> Option<(usize, EntryType)> {
@ -332,7 +321,6 @@ impl<'a> Scope<'a> {
} }
}) })
} }
/// Get the value of an entry in the Scope, starting from the last. /// Get the value of an entry in the Scope, starting from the last.
/// ///
/// # Example /// # Example
@ -354,7 +342,6 @@ impl<'a> Scope<'a> {
.find(|(_, (key, _))| name == key.as_ref()) .find(|(_, (key, _))| name == key.as_ref())
.and_then(|(index, _)| self.values[index].flatten_clone().try_cast()) .and_then(|(index, _)| self.values[index].flatten_clone().try_cast())
} }
/// Update the value of the named entry. /// Update the value of the named entry.
/// Search starts backwards from the last, and only the first entry matching the specified name is updated. /// Search starts backwards from the last, and only the first entry matching the specified name is updated.
/// If no entry matching the specified name is found, a new one is added. /// If no entry matching the specified name is found, a new one is added.
@ -389,7 +376,6 @@ impl<'a> Scope<'a> {
} }
self self
} }
/// Get a mutable reference to an entry in the Scope. /// Get a mutable reference to an entry in the Scope.
#[inline(always)] #[inline(always)]
pub(crate) fn get_mut(&mut self, index: usize) -> (&mut Dynamic, EntryType) { pub(crate) fn get_mut(&mut self, index: usize) -> (&mut Dynamic, EntryType) {
@ -398,16 +384,14 @@ impl<'a> Scope<'a> {
self.types[index], self.types[index],
) )
} }
/// Update the access type of an entry in the Scope. /// Update the access type of an entry in the Scope.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]
pub(crate) fn set_entry_alias(&mut self, index: usize, alias: String) -> &mut Self { pub(crate) fn set_entry_alias(&mut self, index: usize, alias: String) -> &mut Self {
let entry = self.names.get_mut(index).expect("invalid index in Scope"); let entry = self.names.get_mut(index).expect("invalid index in Scope");
entry.1 = Some(alias); entry.1 = Some(Box::new(alias));
self self
} }
/// Clone the Scope, keeping only the last instances of each variable name. /// Clone the Scope, keeping only the last instances of each variable name.
/// Shadowed variables are omitted in the copy. /// Shadowed variables are omitted in the copy.
#[inline] #[inline]
@ -428,7 +412,6 @@ impl<'a> Scope<'a> {
entries entries
} }
/// Get an iterator to entries in the Scope. /// Get an iterator to entries in the Scope.
#[inline(always)] #[inline(always)]
pub(crate) fn into_iter( pub(crate) fn into_iter(
@ -437,9 +420,8 @@ impl<'a> Scope<'a> {
self.names self.names
.into_iter() .into_iter()
.zip(self.types.into_iter().zip(self.values.into_iter())) .zip(self.types.into_iter().zip(self.values.into_iter()))
.map(|((name, alias), (typ, value))| (name, typ, value, alias)) .map(|((name, alias), (typ, value))| (name, typ, value, alias.map(|v| *v)))
} }
/// Get an iterator to entries in the Scope. /// Get an iterator to entries in the Scope.
/// Shared values are flatten-cloned. /// Shared values are flatten-cloned.
/// ///
@ -470,7 +452,6 @@ impl<'a> Scope<'a> {
self.iter_raw() self.iter_raw()
.map(|(name, constant, value)| (name, constant, value.flatten_clone())) .map(|(name, constant, value)| (name, constant, value.flatten_clone()))
} }
/// Get an iterator to entries in the Scope. /// Get an iterator to entries in the Scope.
/// Shared values are not expanded. /// Shared values are not expanded.
#[inline(always)] #[inline(always)]

View File

@ -85,9 +85,14 @@ impl EvalContext<'_, '_, '_, '_, '_, '_, '_, '_, '_> {
} }
} }
/// Definition of a custom syntax definition.
pub struct CustomSyntax { pub struct CustomSyntax {
/// A parsing function to return the next keyword in a custom syntax based on the
/// keywords parsed so far.
pub parse: Box<FnCustomSyntaxParse>, pub parse: Box<FnCustomSyntaxParse>,
/// Custom syntax implementation function.
pub func: Shared<FnCustomSyntaxEval>, pub func: Shared<FnCustomSyntaxEval>,
/// Delta number of variables in the scope.
pub scope_delta: isize, pub scope_delta: isize,
} }