diff --git a/src/engine.rs b/src/engine.rs index e732f112..f99a47a3 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,7 @@ use crate::any::{Any, AnyExt, Dynamic, Variant}; use crate::parser::{Expr, FnDef, Position, ReturnType, Stmt, INT}; use crate::result::EvalAltResult; -use crate::scope::{Scope, ScopeSource, VariableType}; +use crate::scope::{EntryRef as ScopeSource, EntryType as ScopeEntryType, Scope}; #[cfg(not(feature = "no_optimize"))] use crate::optimize::OptimizationLevel; @@ -86,9 +86,8 @@ pub struct Engine<'e> { pub(crate) on_debug: Box, } -impl Engine<'_> { - /// Create a new `Engine` - pub fn new() -> Self { +impl Default for Engine<'_> { + fn default() -> Self { // User-friendly names for built-in types let type_names = [ #[cfg(not(feature = "no_index"))] @@ -97,7 +96,7 @@ impl Engine<'_> { (type_name::(), "dynamic"), ] .iter() - .map(|(k, v)| (k.to_string(), v.to_string())) + .map(|(k, v)| ((*k).to_string(), (*v).to_string())) .collect(); // Create the new scripting Engine @@ -125,6 +124,13 @@ impl Engine<'_> { engine } +} + +impl Engine<'_> { + /// Create a new `Engine` + pub fn new() -> Self { + Default::default() + } /// Control whether and how the `Engine` will optimize an AST after compilation #[cfg(not(feature = "no_optimize"))] @@ -178,7 +184,7 @@ impl Engine<'_> { .params .iter() .zip(args.iter().map(|x| (*x).into_dynamic())) - .map(|(name, value)| (name, VariableType::Normal, value)), + .map(|(name, value)| (name, ScopeEntryType::Normal, value)), ); // Evaluate @@ -288,8 +294,9 @@ impl Engine<'_> { } else { // Otherwise, if `src` is `Some`, then it holds a name and index into `scope`; // using `get_mut` on `scope` to retrieve a mutable reference for return. - let ScopeSource { name, idx, .. } = src.expect("expected source in scope"); - scope.get_mut(name, idx).as_mut() + scope + .get_mut(src.expect("expected source in scope")) + .as_mut() } } @@ -419,18 +426,14 @@ impl Engine<'_> { match dot_lhs { // id.??? Expr::Variable(id, pos) => { - let (ScopeSource { idx, var_type, .. }, _) = - Self::search_scope(scope, id, Ok, *pos)?; + let (entry, _) = Self::search_scope(scope, id, Ok, *pos)?; + + // Avoid referencing scope which is used below as mut + let entry = ScopeSource { name: id, ..entry }; // This is a variable property access (potential function call). // Use a direct index into `scope` to directly mutate the variable value. - let src = ScopeSource { - name: id, - idx, - var_type, - }; - - self.get_dot_val_helper(scope, Some(src), None, dot_rhs) + self.get_dot_val_helper(scope, Some(entry), None, dot_rhs) } // idx_lhs[idx_expr].??? @@ -442,14 +445,14 @@ impl Engine<'_> { // In case the expression mutated `target`, we need to update it back into the scope because it is cloned. if let Some(src) = src { - match src.var_type { - VariableType::Constant => { + match src.typ { + ScopeEntryType::Constant => { return Err(EvalAltResult::ErrorAssignmentToConstant( src.name.to_string(), idx_lhs.position(), )); } - VariableType::Normal => { + ScopeEntryType::Normal => { Self::update_indexed_var_in_scope( src_type, scope, @@ -570,8 +573,8 @@ impl Engine<'_> { src_type, Some(ScopeSource { name: &id, - var_type: src.var_type, - idx: src.idx, + typ: src.typ, + index: src.index, }), idx as usize, val, @@ -614,14 +617,14 @@ impl Engine<'_> { match src_type { // array_id[idx] = val IndexSourceType::Array => { - let arr = scope.get_mut_by_type::(src.name, src.idx); + let arr = scope.get_mut_by_type::(src); arr[idx as usize] = new_val.0; Ok(().into_dynamic()) } // string_id[idx] = val IndexSourceType::String => { - let s = scope.get_mut_by_type::(src.name, src.idx); + let s = scope.get_mut_by_type::(src); let pos = new_val.1; // Value must be a character let ch = *new_val @@ -793,19 +796,21 @@ impl Engine<'_> { match dot_lhs { // id.??? Expr::Variable(id, pos) => { - let (ScopeSource { idx, var_type, .. }, mut target) = - Self::search_scope(scope, id, Ok, *pos)?; + let (entry, mut target) = Self::search_scope(scope, id, Ok, *pos)?; - match var_type { - VariableType::Constant => Err(EvalAltResult::ErrorAssignmentToConstant( + match entry.typ { + ScopeEntryType::Constant => Err(EvalAltResult::ErrorAssignmentToConstant( id.to_string(), op_pos, )), _ => { + // Avoid referencing scope which is used below as mut + let entry = ScopeSource { name: id, ..entry }; + let val = self.set_dot_val_helper(scope, target.as_mut(), dot_rhs, new_val); // In case the expression mutated `target`, we need to update it back into the scope because it is cloned. - *scope.get_mut(id, idx) = target; + *scope.get_mut(entry) = target; val } @@ -823,14 +828,14 @@ impl Engine<'_> { // In case the expression mutated `target`, we need to update it back into the scope because it is cloned. if let Some(src) = src { - match src.var_type { - VariableType::Constant => { + match src.typ { + ScopeEntryType::Constant => { return Err(EvalAltResult::ErrorAssignmentToConstant( src.name.to_string(), lhs.position(), )); } - VariableType::Normal => { + ScopeEntryType::Normal => { Self::update_indexed_var_in_scope( src_type, scope, @@ -880,29 +885,30 @@ impl Engine<'_> { match lhs.as_ref() { // name = rhs - Expr::Variable(name, pos) => match scope.get(name) { - Some(( - ScopeSource { - idx, - var_type: VariableType::Normal, - .. - }, - _, - )) => { - *scope.get_mut(name, idx) = rhs_val.clone(); + Expr::Variable(name, pos) => match scope + .get(name) + .ok_or_else(|| EvalAltResult::ErrorVariableNotFound(name.clone(), *pos))? + .0 + { + entry + @ + ScopeSource { + typ: ScopeEntryType::Normal, + .. + } => { + // Avoid referencing scope which is used below as mut + let entry = ScopeSource { name, ..entry }; + + *scope.get_mut(entry) = rhs_val.clone(); Ok(rhs_val) } - Some(( - ScopeSource { - var_type: VariableType::Constant, - .. - }, - _, - )) => Err(EvalAltResult::ErrorAssignmentToConstant( + ScopeSource { + typ: ScopeEntryType::Constant, + .. + } => Err(EvalAltResult::ErrorAssignmentToConstant( name.to_string(), *op_pos, )), - _ => Err(EvalAltResult::ErrorVariableNotFound(name.clone(), *pos)), }, // idx_lhs[idx_expr] = rhs @@ -912,14 +918,14 @@ impl Engine<'_> { self.eval_index_expr(scope, idx_lhs, idx_expr, *op_pos)?; if let Some(src) = src { - match src.var_type { - VariableType::Constant => { + match src.typ { + ScopeEntryType::Constant => { Err(EvalAltResult::ErrorAssignmentToConstant( src.name.to_string(), idx_lhs.position(), )) } - VariableType::Normal => Ok(Self::update_indexed_var_in_scope( + ScopeEntryType::Normal => Ok(Self::update_indexed_var_in_scope( src_type, scope, src, @@ -1203,10 +1209,15 @@ impl Engine<'_> { if let Some(iter_fn) = self.type_iterators.get(&tid) { scope.push(name.clone(), ()); - let idx = scope.len() - 1; + + let entry = ScopeSource { + name, + index: scope.len() - 1, + typ: ScopeEntryType::Normal, + }; for a in iter_fn(&arr) { - *scope.get_mut(name, idx) = a; + *scope.get_mut(entry) = a; match self.eval_stmt(scope, body) { Ok(_) => (), @@ -1214,7 +1225,8 @@ impl Engine<'_> { Err(x) => return Err(x), } } - scope.pop(); + + scope.rewind(scope.len() - 1); Ok(().into_dynamic()) } else { Err(EvalAltResult::ErrorFor(expr.position())) @@ -1253,7 +1265,7 @@ impl Engine<'_> { // Let statement Stmt::Let(name, Some(expr), _) => { let val = self.eval_expr(scope, expr)?; - scope.push_dynamic(name.clone(), VariableType::Normal, val); + scope.push_dynamic_value(name.clone(), ScopeEntryType::Normal, val, false); Ok(().into_dynamic()) } @@ -1265,7 +1277,7 @@ impl Engine<'_> { // Const statement Stmt::Const(name, expr, _) if expr.is_constant() => { let val = self.eval_expr(scope, expr)?; - scope.push_dynamic(name.clone(), VariableType::Constant, val); + scope.push_dynamic_value(name.clone(), ScopeEntryType::Constant, val, true); Ok(().into_dynamic()) } diff --git a/src/optimize.rs b/src/optimize.rs index 7c87f721..819df9c5 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -6,7 +6,7 @@ use crate::engine::{ KEYWORD_TYPE_OF, }; use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST}; -use crate::scope::{Scope, ScopeEntry, VariableType}; +use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::stdlib::{ boxed::Box, @@ -512,9 +512,9 @@ pub(crate) fn optimize<'a>(statements: Vec, engine: &Engine<'a>, scope: &S // Add constants from the scope into the state scope .iter() - .filter(|ScopeEntry { var_type, expr, .. }| { + .filter(|ScopeEntry { typ, expr, .. }| { // Get all the constants with definite constant expressions - *var_type == VariableType::Constant + *typ == ScopeEntryType::Constant && expr.as_ref().map(Expr::is_constant).unwrap_or(false) }) .for_each(|ScopeEntry { name, expr, .. }| { diff --git a/src/parser.rs b/src/parser.rs index ed627e1e..e14a1de1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3,7 +3,7 @@ use crate::any::{Any, AnyExt, Dynamic}; use crate::engine::Engine; use crate::error::{LexError, ParseError, ParseErrorType}; -use crate::scope::{Scope, VariableType}; +use crate::scope::{EntryType as ScopeEntryType, Scope}; #[cfg(not(feature = "no_optimize"))] use crate::optimize::optimize_into_ast; @@ -2024,7 +2024,7 @@ fn parse_for<'a>( /// Parse a variable definition statement. fn parse_let<'a>( input: &mut Peekable>, - var_type: VariableType, + var_type: ScopeEntryType, allow_stmt_expr: bool, ) -> Result { // let/const... (specified in `var_type`) @@ -2049,13 +2049,13 @@ fn parse_let<'a>( match var_type { // let name = expr - VariableType::Normal => Ok(Stmt::Let(name, Some(Box::new(init_value)), pos)), + ScopeEntryType::Normal => Ok(Stmt::Let(name, Some(Box::new(init_value)), pos)), // const name = { expr:constant } - VariableType::Constant if init_value.is_constant() => { + ScopeEntryType::Constant if init_value.is_constant() => { Ok(Stmt::Const(name, Box::new(init_value), pos)) } // const name = expr - error - VariableType::Constant => Err(ParseError( + ScopeEntryType::Constant => Err(ParseError( PERR::ForbiddenConstantExpr(name), init_value.position(), )), @@ -2186,8 +2186,8 @@ fn parse_stmt<'a>( } } (Token::LeftBrace, _) => parse_block(input, breakable, allow_stmt_expr), - (Token::Let, _) => parse_let(input, VariableType::Normal, allow_stmt_expr), - (Token::Const, _) => parse_let(input, VariableType::Constant, allow_stmt_expr), + (Token::Let, _) => parse_let(input, ScopeEntryType::Normal, allow_stmt_expr), + (Token::Const, _) => parse_let(input, ScopeEntryType::Constant, allow_stmt_expr), _ => parse_expr_stmt(input, allow_stmt_expr), } } diff --git a/src/scope.rs b/src/scope.rs index 32cca263..bf66564d 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -10,32 +10,34 @@ use crate::stdlib::{ vec::Vec, }; -/// Type of a variable in the Scope. +/// Type of an entry in the Scope. #[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)] -pub enum VariableType { - /// Normal variable. +pub enum EntryType { + /// Normal value. Normal, /// Immutable constant value. Constant, } /// An entry in the Scope. -pub struct ScopeEntry<'a> { - /// Name of the variable. +#[derive(Debug, Clone)] +pub struct Entry<'a> { + /// Name of the entry. pub name: Cow<'a, str>, - /// Type of the variable. - pub var_type: VariableType, - /// Current value of the variable. + /// Type of the entry. + pub typ: EntryType, + /// Current value of the entry. pub value: Dynamic, /// A constant expression if the initial value matches one of the recognized types. pub expr: Option, } /// Information about a particular entry in the Scope. -pub(crate) struct ScopeSource<'a> { +#[derive(Debug, Hash, Copy, Clone)] +pub(crate) struct EntryRef<'a> { pub name: &'a str, - pub idx: usize, - pub var_type: VariableType, + pub index: usize, + pub typ: EntryType, } /// A type containing information about the current scope. @@ -57,9 +59,9 @@ pub(crate) struct ScopeSource<'a> { /// # } /// ``` /// -/// When searching for variables, newly-added variables are found before similarly-named but older variables, -/// allowing for automatic _shadowing_ of variables. -pub struct Scope<'a>(Vec>); +/// When searching for entries, newly-added entries are found before similarly-named but older entries, +/// allowing for automatic _shadowing_. +pub struct Scope<'a>(Vec>); impl<'a> Scope<'a> { /// Create a new Scope. @@ -72,7 +74,7 @@ impl<'a> Scope<'a> { self.0.clear(); } - /// Get the number of variables inside the Scope. + /// Get the number of entries inside the Scope. pub fn len(&self) -> usize { self.0.len() } @@ -82,19 +84,14 @@ impl<'a> Scope<'a> { self.0.len() == 0 } - /// Add (push) a new variable to the Scope. + /// Add (push) a new entry to the Scope. pub fn push>, T: Any + Clone>(&mut self, name: K, value: T) { - let value = value.into_dynamic(); + self.push_dynamic_value(name, EntryType::Normal, value.into_dynamic(), false); + } - // Map into constant expressions - //let (expr, value) = map_dynamic_to_expr(value, Position::none()); - - self.0.push(ScopeEntry { - name: name.into(), - var_type: VariableType::Normal, - value, - expr: None, - }); + /// Add (push) a new `Dynamic` entry to the Scope. + pub fn push_dynamic>>(&mut self, name: K, value: Dynamic) { + self.push_dynamic_value(name, EntryType::Normal, value, false); } /// Add (push) a new constant to the Scope. @@ -104,84 +101,75 @@ impl<'a> Scope<'a> { /// However, in order to be used for optimization, constants must be in one of the recognized types: /// `INT` (default to `i64`, `i32` if `only_i32`), `f64`, `String`, `char` and `bool`. pub fn push_constant>, T: Any + Clone>(&mut self, name: K, value: T) { - let value = value.into_dynamic(); - - // Map into constant expressions - let (expr, value) = map_dynamic_to_expr(value, Position::none()); - - self.0.push(ScopeEntry { - name: name.into(), - var_type: VariableType::Constant, - value, - expr, - }); + self.push_dynamic_value(name, EntryType::Constant, value.into_dynamic(), true); } - /// Add (push) a new variable with a `Dynamic` value to the Scope. - pub(crate) fn push_dynamic>>( + /// 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 propagation is a technique used to optimize an AST. + /// However, in order to be used for optimization, the `Dynamic` value must be in one of the + /// recognized types: + /// `INT` (default to `i64`, `i32` if `only_i32`), `f64`, `String`, `char` and `bool`. + pub fn push_constant_dynamic>>(&mut self, name: K, value: Dynamic) { + self.push_dynamic_value(name, EntryType::Constant, value, true); + } + + /// Add (push) a new entry with a `Dynamic` value to the Scope. + pub(crate) fn push_dynamic_value>>( &mut self, name: K, - var_type: VariableType, + entry_type: EntryType, value: Dynamic, + map_expr: bool, ) { - let (expr, value) = map_dynamic_to_expr(value, Position::none()); + let (expr, value) = if map_expr { + map_dynamic_to_expr(value, Position::none()) + } else { + (None, value) + }; - self.0.push(ScopeEntry { + self.0.push(Entry { name: name.into(), - var_type, + typ: entry_type, value, expr, }); } - /// Remove (pop) the last variable from the Scope. - pub fn pop(&mut self) -> Option<(String, VariableType, Dynamic)> { - self.0.pop().map( - |ScopeEntry { - name, - var_type, - value, - .. - }| (name.to_string(), var_type, value), - ) - } - /// Truncate (rewind) the Scope to a previous size. pub fn rewind(&mut self, size: usize) { self.0.truncate(size); } - /// Does the scope contain the variable? + /// Does the scope contain the entry? pub fn contains(&self, key: &str) -> bool { self.0 .iter() .enumerate() .rev() // Always search a Scope in reverse order - .any(|(_, ScopeEntry { name, .. })| name == key) + .any(|(_, Entry { name, .. })| name == key) } - /// Find a variable in the Scope, starting from the last. - pub(crate) fn get(&self, key: &str) -> Option<(ScopeSource, Dynamic)> { + /// Find an entry in the Scope, starting from the last. + pub(crate) fn get(&self, key: &str) -> Option<(EntryRef, Dynamic)> { self.0 .iter() .enumerate() .rev() // Always search a Scope in reverse order - .find(|(_, ScopeEntry { name, .. })| name == key) + .find(|(_, Entry { name, .. })| name == key) .map( |( i, - ScopeEntry { - name, - var_type, - value, - .. + Entry { + name, typ, value, .. }, )| { ( - ScopeSource { + EntryRef { name: name.as_ref(), - idx: i, - var_type: *var_type, + index: i, + typ: *typ, }, value.clone(), ) @@ -189,41 +177,41 @@ impl<'a> Scope<'a> { ) } - /// Get the value of a variable in the Scope, starting from the last. + /// Get the value of an entry in the Scope, starting from the last. pub fn get_value(&self, key: &str) -> Option { self.0 .iter() .enumerate() .rev() // Always search a Scope in reverse order - .find(|(_, ScopeEntry { name, .. })| name == key) - .and_then(|(_, ScopeEntry { value, .. })| value.downcast_ref::()) + .find(|(_, Entry { name, .. })| name == key) + .and_then(|(_, Entry { value, .. })| value.downcast_ref::()) .map(T::clone) } - /// Get a mutable reference to a variable in the Scope. - pub(crate) fn get_mut(&mut self, name: &str, index: usize) -> &mut Dynamic { - let entry = self.0.get_mut(index).expect("invalid index in Scope"); + /// Get a mutable reference to an entry in the Scope. + pub(crate) fn get_mut(&mut self, key: EntryRef) -> &mut Dynamic { + let entry = self.0.get_mut(key.index).expect("invalid index in Scope"); + assert_eq!(entry.typ, key.typ, "entry type not matched"); // assert_ne!( - // entry.var_type, - // VariableType::Constant, - // "get mut of constant variable" + // entry.typ, + // EntryType::Constant, + // "get mut of constant entry" // ); - assert_eq!(entry.name, name, "incorrect key at Scope entry"); + assert_eq!(entry.name, key.name, "incorrect key at Scope entry"); &mut entry.value } - /// Get a mutable reference to a variable in the Scope and downcast it to a specific type - #[cfg(not(feature = "no_index"))] - pub(crate) fn get_mut_by_type(&mut self, name: &str, index: usize) -> &mut T { - self.get_mut(name, index) + /// Get a mutable reference to an entry in the Scope and downcast it to a specific type + pub(crate) fn get_mut_by_type(&mut self, key: EntryRef) -> &mut T { + self.get_mut(key) .downcast_mut::() .expect("wrong type cast") } - /// Get an iterator to variables in the Scope. - pub fn iter(&self) -> impl Iterator { + /// Get an iterator to entries in the Scope. + pub fn iter(&self) -> impl Iterator { self.0.iter().rev() // Always search a Scope in reverse order } } @@ -234,15 +222,15 @@ impl Default for Scope<'_> { } } -impl<'a, K> iter::Extend<(K, VariableType, Dynamic)> for Scope<'a> +impl<'a, K> iter::Extend<(K, EntryType, Dynamic)> for Scope<'a> where K: Into>, { - fn extend>(&mut self, iter: T) { + fn extend>(&mut self, iter: T) { self.0 - .extend(iter.into_iter().map(|(name, var_type, value)| ScopeEntry { + .extend(iter.into_iter().map(|(name, typ, value)| Entry { name: name.into(), - var_type, + typ, value, expr: None, }));