From 33d3e3490873d2bb5f4a803d4d67827c0ba25d78 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 26 Apr 2020 18:04:07 +0800 Subject: [PATCH] Deep linking for dot/index chains. --- README.md | 2 +- src/any.rs | 14 +- src/engine.rs | 947 ++++++++++++++++++++------------------------------ src/error.rs | 3 - src/parser.rs | 200 +++++------ src/scope.rs | 32 +- 6 files changed, 482 insertions(+), 716 deletions(-) diff --git a/README.md b/README.md index 3b302577..0e8d4b77 100644 --- a/README.md +++ b/README.md @@ -1947,7 +1947,7 @@ Properties and methods in a Rust custom type registered with the [`Engine`] can ```rust let a = new_ts(); // constructor function a.field = 500; // property access -a.update(); // method call +a.update(); // method call, 'a' can be changed update(a); // this works, but 'a' is unchanged because only // a COPY of 'a' is passed to 'update' by VALUE diff --git a/src/any.rs b/src/any.rs index f718b01c..46ae6044 100644 --- a/src/any.rs +++ b/src/any.rs @@ -212,7 +212,7 @@ impl fmt::Display for Dynamic { #[cfg(not(feature = "no_float"))] Union::Float(value) => write!(f, "{}", value), Union::Array(value) => write!(f, "{:?}", value), - Union::Map(value) => write!(f, "{:?}", value), + Union::Map(value) => write!(f, "#{:?}", value), Union::Variant(_) => write!(f, "?"), } } @@ -229,7 +229,7 @@ impl fmt::Debug for Dynamic { #[cfg(not(feature = "no_float"))] Union::Float(value) => write!(f, "{:?}", value), Union::Array(value) => write!(f, "{:?}", value), - Union::Map(value) => write!(f, "{:?}", value), + Union::Map(value) => write!(f, "#{:?}", value), Union::Variant(_) => write!(f, ""), } } @@ -268,16 +268,6 @@ fn cast_box(item: Box) -> Result> { } impl Dynamic { - /// Get a reference to the inner `Union`. - pub(crate) fn get_ref(&self) -> &Union { - &self.0 - } - - /// Get a mutable reference to the inner `Union`. - pub(crate) fn get_mut(&mut self) -> &mut Union { - &mut self.0 - } - /// Create a `Dynamic` from any type. A `Dynamic` value is simply returned as is. /// /// Beware that you need to pass in an `Array` type for it to be recognized as an `Array`. diff --git a/src/engine.rs b/src/engine.rs index b3ed775d..a0d568ca 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -5,7 +5,7 @@ use crate::calc_fn_hash; use crate::error::ParseErrorType; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, StandardPackage}; -use crate::parser::{Expr, FnDef, ReturnType, Stmt, INT}; +use crate::parser::{Expr, FnDef, ReturnType, Stmt}; use crate::result::EvalAltResult; use crate::scope::{EntryRef as ScopeSource, EntryType as ScopeEntryType, Scope}; use crate::token::Position; @@ -13,10 +13,12 @@ use crate::token::Position; use crate::stdlib::{ any::TypeId, boxed::Box, + cell::RefCell, collections::HashMap, format, hash::{Hash, Hasher}, iter::once, + mem, ops::{Deref, DerefMut}, rc::Rc, string::{String, ToString}, @@ -74,65 +76,75 @@ const FUNCTIONS_COUNT: usize = 512; #[cfg(any(feature = "only_i32", feature = "only_i64"))] const FUNCTIONS_COUNT: usize = 256; -/// A type encapsulating an index value, which may be an integer or a string key. -#[derive(Debug, Eq, PartialEq, Hash, Clone)] -enum IndexValue { - Num(usize), - Str(String), -} - -impl IndexValue { - fn from_num(idx: INT) -> Self { - Self::Num(idx as usize) - } - fn from_str(name: String) -> Self { - Self::Str(name) - } - fn as_num(self) -> usize { - match self { - Self::Num(n) => n, - _ => panic!("index value is numeric"), - } - } - fn as_str(self) -> String { - match self { - Self::Str(s) => s, - _ => panic!("index value is string"), - } - } -} - -/// A type encapsulating the target of a update action. -/// The reason we need this is because we cannot hold a mutable reference to a variable in -/// the current `Scope` while evaluating expressions requiring access to the same `Scope`. -/// So we cannot use a single `&mut Dynamic` everywhere; instead, we hold enough information -/// to find the variable from the `Scope` when we need to update it. -#[derive(Debug)] +/// A type that encapsulates a mutation target for an expression with side effects. enum Target<'a> { - /// The update target is a variable stored in the current `Scope`. - Scope(ScopeSource<'a>), - /// The update target is a `Dynamic` value stored somewhere. - Value(&'a mut Dynamic), + /// The target is a mutable reference to a `Dynamic` value somewhere. + Ref(&'a mut Dynamic), + /// The target is a variable stored in the current `Scope`. + Scope(&'a RefCell), + /// The target is a temporary `Dynamic` value (i.e. the mutation can cause no side effects). + Value(Dynamic), + /// The target is a character inside a String. + StringChar(Box<(&'a mut Dynamic, usize, Dynamic)>), } -impl<'a> Target<'a> { - fn get_mut(self, scope: &'a mut Scope) -> &'a mut Dynamic { +impl Target<'_> { + /// Get the value of the `Target` as a `Dynamic`. + pub fn into_dynamic(self) -> Dynamic { match self { - Self::Value(t) => t, - Self::Scope(src) => scope.get_mut(src), + Target::Ref(r) => r.clone(), + Target::Scope(r) => r.borrow().clone(), + Target::Value(v) => v, + Target::StringChar(s) => s.2, } } -} -impl<'a> From> for Target<'a> { - fn from(src: ScopeSource<'a>) -> Self { - Self::Scope(src) + /// Update the value of the `Target`. + pub fn set_value(&mut self, new_val: Dynamic, pos: Position) -> Result<(), Box> { + match self { + Target::Scope(r) => *r.borrow_mut() = new_val, + Target::Ref(r) => **r = new_val, + Target::Value(_) => { + return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(pos))) + } + Target::StringChar(x) => match x.0 { + Dynamic(Union::Str(s)) => { + // Replace the character at the specified index position + let new_ch = new_val + .as_char() + .map_err(|_| EvalAltResult::ErrorCharMismatch(pos))?; + + let mut chars: Vec = s.chars().collect(); + let ch = *chars.get(x.1).expect("string index out of bounds"); + + // See if changed - if so, update the String + if ch != new_ch { + chars[x.1] = new_ch; + s.clear(); + chars.iter().for_each(|&ch| s.push(ch)); + } + } + _ => panic!("should be String"), + }, + } + + Ok(()) } } +impl<'a> From<&'a RefCell> for Target<'a> { + fn from(value: &'a RefCell) -> Self { + Self::Scope(value) + } +} impl<'a> From<&'a mut Dynamic> for Target<'a> { fn from(value: &'a mut Dynamic) -> Self { - Self::Value(value) + Self::Ref(value) + } +} +impl> From for Target<'_> { + fn from(value: T) -> Self { + Self::Value(value.into()) } } @@ -386,85 +398,12 @@ fn search_scope<'a>( scope: &'a Scope, id: &str, begin: Position, -) -> Result<(ScopeSource<'a>, Dynamic), Box> { - scope +) -> Result<(&'a RefCell, ScopeEntryType), Box> { + let (entry, _) = scope .get(id) - .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(id.into(), begin))) -} + .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(id.into(), begin)))?; -/// Replace a character at an index position in a mutable string -fn str_replace_char(s: &mut String, idx: usize, new_ch: char) { - let mut chars: Vec = s.chars().collect(); - let ch = *chars.get(idx).expect("string index out of bounds"); - - // See if changed - if so, update the String - if ch != new_ch { - chars[idx] = new_ch; - s.clear(); - chars.iter().for_each(|&ch| s.push(ch)); - } -} - -/// Update the value at an index position -fn update_indexed_val( - mut target: Dynamic, - idx: IndexValue, - new_val: Dynamic, - pos: Position, -) -> Result> { - match target.get_mut() { - Union::Array(arr) => { - arr[idx.as_num()] = new_val; - } - Union::Map(map) => { - map.insert(idx.as_str(), new_val); - } - Union::Str(s) => { - // Value must be a character - let ch = new_val - .as_char() - .map_err(|_| EvalAltResult::ErrorCharMismatch(pos))?; - str_replace_char(s, idx.as_num(), ch); - } - // All other variable types should be an error - _ => panic!("invalid type for indexing: {}", target.type_name()), - } - - Ok(target) -} - -/// Update the value at an index position in a variable inside the scope -fn update_indexed_scope_var( - scope: &mut Scope, - src: ScopeSource, - idx: IndexValue, - new_val: Dynamic, - pos: Position, -) -> Result> { - let target = scope.get_mut(src); - - match target.get_mut() { - // array_id[idx] = val - Union::Array(arr) => { - arr[idx.as_num()] = new_val; - } - // map_id[idx] = val - Union::Map(map) => { - map.insert(idx.as_str(), new_val); - } - // string_id[idx] = val - Union::Str(s) => { - // Value must be a character - let ch = new_val - .as_char() - .map_err(|_| EvalAltResult::ErrorCharMismatch(pos))?; - str_replace_char(s, idx.as_num(), ch); - } - // All other variable types should be an error - _ => panic!("invalid type for indexing: {}", target.type_name()), - } - - Ok(().into()) + Ok((&scope.get_ref(entry), entry.typ)) } impl Engine { @@ -692,8 +631,7 @@ impl Engine { || fn_lib.map_or(false, |lib| lib.has_function(name, 1)) } - // Perform an actual function call, taking care of special functions such as `type_of` - // and property getter/setter for maps. + // Perform an actual function call, taking care of special functions fn exec_fn_call( &self, fn_lib: Option<&FunctionsLib>, @@ -717,27 +655,7 @@ impl Engine { ))) } - _ => { - // Map property access? - if let Some(prop) = extract_prop_from_getter(fn_name) { - if let Dynamic(Union::Map(map)) = args[0] { - return Ok(map.get(prop).cloned().unwrap_or_else(|| ().into())); - } - } - - // Map property update - if let Some(prop) = extract_prop_from_setter(fn_name) { - let (arg, value) = args.split_at_mut(1); - - if let Dynamic(Union::Map(map)) = arg[0] { - map.insert(prop.to_string(), value[0].clone()); - return Ok(().into()); - } - } - - // Normal function call - self.call_fn_raw(None, fn_lib, fn_name, args, def_val, pos, level) - } + _ => self.call_fn_raw(None, fn_lib, fn_name, args, def_val, pos, level), } } @@ -784,203 +702,334 @@ impl Engine { .map_err(|err| EvalAltResult::set_position(err, pos)) } - /// Chain-evaluate a dot setter. - fn dot_get_helper( + /// Chain-evaluate a dot/index chain. + fn eval_dot_index_chain_helper( &self, - scope: &mut Scope, fn_lib: Option<&FunctionsLib>, - target: Target, - dot_rhs: &Expr, + mut target: Target, + rhs: &Expr, + idx_list: &mut [Dynamic], + idx_more: &mut Vec, + is_index: bool, + op_pos: Position, level: usize, - ) -> Result> { - match dot_rhs { - // xxx.fn_name(arg_expr_list) - Expr::FunctionCall(fn_name, arg_exprs, def_val, pos) => { - let mut arg_values = arg_exprs - .iter() - .map(|arg_expr| self.eval_expr(scope, fn_lib, arg_expr, level)) - .collect::, _>>()?; - let mut args: Vec<_> = once(target.get_mut(scope)) - .chain(arg_values.iter_mut()) - .collect(); - let def_val = def_val.as_ref(); - self.exec_fn_call(fn_lib, fn_name, &mut args, def_val, *pos, 0) + mut new_val: Option, + ) -> Result<(Dynamic, bool), Box> { + // Store a copy of the RefMut from `borrow_mut` since it is a temporary value + let mut scope_base = match target { + Target::Scope(r) => Some(r.borrow_mut()), + Target::Ref(_) | Target::Value(_) | Target::StringChar(_) => None, + }; + // Get a reference to the mutation target Dynamic + let obj = match target { + Target::Scope(_) => scope_base.as_mut().unwrap().deref_mut(), + Target::Ref(r) => r, + Target::Value(ref mut r) => r, + Target::StringChar(ref mut x) => &mut x.2, + }; + + // Pop the last index value + let mut idx_val; + let mut idx_fixed = idx_list; + + if let Some(val) = idx_more.pop() { + // Values in variable list + idx_val = val; + } else { + // No more value in variable list, pop from fixed list + let len = idx_fixed.len(); + let splits = idx_fixed.split_at_mut(len - 1); + + idx_val = mem::replace(splits.1.get_mut(0).unwrap(), ().into()); + idx_fixed = splits.0; + } + + if is_index { + match rhs { + // xxx[idx].dot_rhs... + Expr::Dot(idx, idx_rhs, pos) | + // xxx[idx][dot_rhs]... + Expr::Index(idx, idx_rhs, pos) => { + let is_index = matches!(rhs, Expr::Index(_,_,_)); + + let indexed_val = self.get_indexed_mut(obj, idx_val, idx.position(), op_pos, false)?; + self.eval_dot_index_chain_helper( + fn_lib, indexed_val, idx_rhs.as_ref(), idx_fixed, idx_more, is_index, *pos, level, new_val + ) + } + // xxx[rhs] = new_val + _ if new_val.is_some() => { + let mut indexed_val = self.get_indexed_mut(obj, idx_val, rhs.position(), op_pos, true)?; + indexed_val.set_value(new_val.unwrap(), rhs.position())?; + Ok((().into(), true)) + } + // xxx[rhs] + _ => self + .get_indexed_mut(obj, idx_val, rhs.position(), op_pos, false) + .map(|v| (v.into_dynamic(), false)) } - - // xxx.id - Expr::Property(id, pos) => { - let fn_name = make_getter(id); - let mut args = [target.get_mut(scope)]; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0) - } - - // xxx.idx_lhs[idx_expr] - Expr::Index(idx_lhs, idx_expr, op_pos) => { - let lhs_value = match idx_lhs.as_ref() { - // xxx.id[idx_expr] - Expr::Property(id, pos) => { - let fn_name = make_getter(id); - let mut args = [target.get_mut(scope)]; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0)? + } else { + match rhs { + // xxx.fn_name(arg_expr_list) + Expr::FunctionCall(fn_name, _, def_val, pos) => { + let mut args = once(obj) + .chain(idx_val.downcast_mut::().unwrap().iter_mut()) + .collect::>(); + let def_val = def_val.as_ref(); + // A function call is assumed to have side effects, so the value is changed + self.exec_fn_call(fn_lib, fn_name, &mut args, def_val, *pos, 0).map(|v| (v, true)) + } + // {xxx:map}.id + Expr::Property(id, pos) if obj.is::() => { + let mut indexed_val = + self.get_indexed_mut(obj, id.to_string().into(), *pos, op_pos, new_val.is_some())?; + if let Some(new_val) = new_val { + indexed_val.set_value(new_val, rhs.position())?; + Ok((().into(), true)) + } else { + Ok((indexed_val.into_dynamic(), false)) } - // xxx.???[???][idx_expr] - Expr::Index(_, _, _) => { - // Chain the indexing - self.dot_get_helper(scope, fn_lib, target, idx_lhs, level)? - } - // Syntax error - _ => { - return Err(Box::new(EvalAltResult::ErrorDotExpr( - "".to_string(), - dot_rhs.position(), - ))) - } - }; - - self.get_indexed_val(scope, fn_lib, &lhs_value, idx_expr, *op_pos, level, false) - .map(|(val, _)| val) - } - - // xxx.dot_lhs.rhs - Expr::Dot(dot_lhs, rhs, _) => match dot_lhs.as_ref() { - // xxx.id.rhs + } + // xxx.id = ??? + Expr::Property(id, pos) if new_val.is_some() => { + let fn_name = make_setter(id); + let mut args = [obj, new_val.as_mut().unwrap()]; + self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0).map(|v| (v, true)) + } + // xxx.id Expr::Property(id, pos) => { let fn_name = make_getter(id); - let mut args = [target.get_mut(scope)]; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0) - .and_then(|mut val| { - self.dot_get_helper(scope, fn_lib, (&mut val).into(), rhs, level) - }) + let mut args = [obj]; + self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0).map(|v| (v, false)) } - // xxx.idx_lhs[idx_expr].rhs - Expr::Index(idx_lhs, idx_expr, op_pos) => { - let val = match idx_lhs.as_ref() { - // xxx.id[idx_expr].rhs - Expr::Property(id, pos) => { - let fn_name = make_getter(id); - let mut args = [target.get_mut(scope)]; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0)? - } - // xxx.???[???][idx_expr].rhs - Expr::Index(_, _, _) => { - self.dot_get_helper(scope, fn_lib, target, idx_lhs, level)? - } - // Syntax error - _ => { - return Err(Box::new(EvalAltResult::ErrorDotExpr( - "".to_string(), - dot_rhs.position(), - ))) - } - }; + // {xxx:map}.idx_lhs[idx_expr] + Expr::Index(dot_lhs, dot_rhs, pos) | + // {xxx:map}.dot_lhs.rhs + Expr::Dot(dot_lhs, dot_rhs, pos) if obj.is::() => { + let is_index = matches!(rhs, Expr::Index(_,_,_)); - self.get_indexed_val(scope, fn_lib, &val, idx_expr, *op_pos, level, false) - .and_then(|(mut val, _)| { - self.dot_get_helper(scope, fn_lib, (&mut val).into(), rhs, level) - }) + let indexed_val = if let Expr::Property(id, pos) = dot_lhs.as_ref() { + self.get_indexed_mut(obj, id.to_string().into(), *pos, op_pos, false)? + } else { + // Syntax error + return Err(Box::new(EvalAltResult::ErrorDotExpr( + "".to_string(), + rhs.position(), + ))); + }; + self.eval_dot_index_chain_helper( + fn_lib, indexed_val, dot_rhs, idx_fixed, idx_more, is_index, *pos, level, new_val + ) + } + // xxx.idx_lhs[idx_expr] + Expr::Index(dot_lhs, dot_rhs, pos) | + // xxx.dot_lhs.rhs + Expr::Dot(dot_lhs, dot_rhs, pos) => { + let is_index = matches!(rhs, Expr::Index(_,_,_)); + let mut buf: Dynamic = ().into(); + let mut args = [obj, &mut buf]; + + let mut indexed_val = if let Expr::Property(id, pos) = dot_lhs.as_ref() { + let fn_name = make_getter(id); + self.exec_fn_call(fn_lib, &fn_name, &mut args[..1], None, *pos, 0)? + } else { + // Syntax error + return Err(Box::new(EvalAltResult::ErrorDotExpr( + "".to_string(), + rhs.position(), + ))); + }; + let (result, changed) = self.eval_dot_index_chain_helper( + fn_lib, (&mut indexed_val).into(), dot_rhs, idx_fixed, idx_more, is_index, *pos, level, new_val + )?; + + // Feed the value back via a setter just in case it has been updated + if changed { + if let Expr::Property(id, pos) = dot_lhs.as_ref() { + let fn_name = make_setter(id); + args[1] = &mut indexed_val; + self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0)?; + } + } + + Ok((result, changed)) } // Syntax error _ => Err(Box::new(EvalAltResult::ErrorDotExpr( "".to_string(), - dot_lhs.position(), + rhs.position(), ))), - }, - - // Syntax error - _ => Err(Box::new(EvalAltResult::ErrorDotExpr( - "".to_string(), - dot_rhs.position(), - ))), + } } } - /// Evaluate a dot chain getter - fn dot_get( + /// Evaluate a dot/index chain + fn eval_dot_index_chain( &self, scope: &mut Scope, fn_lib: Option<&FunctionsLib>, dot_lhs: &Expr, dot_rhs: &Expr, + is_index: bool, + op_pos: Position, level: usize, + new_val: Option, ) -> Result> { + // Keep four levels of index values in fixed array to reduce allocations + let mut idx_list: [Dynamic; 4] = [().into(), ().into(), ().into(), ().into()]; + // Spill over additional levels into a variable list + let idx_more: &mut Vec = &mut Vec::new(); + + let size = + self.eval_indexed_chain(scope, fn_lib, dot_rhs, &mut idx_list, idx_more, 0, level)?; + + let idx_list = if size < idx_list.len() { + &mut idx_list[0..size] + } else { + &mut idx_list + }; + match dot_lhs { - // id.??? + // id.??? or id[???] Expr::Variable(id, pos) => { - let (entry, _) = search_scope(scope, id, *pos)?; + let (target, typ) = search_scope(scope, id, *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. - self.dot_get_helper(scope, fn_lib, entry.into(), dot_rhs, level) - } - - // idx_lhs[idx_expr].??? - Expr::Index(idx_lhs, idx_expr, op_pos) => { - let (src, index, mut val) = - self.eval_index_expr(scope, fn_lib, idx_lhs, idx_expr, *op_pos, level)?; - let value = self.dot_get_helper(scope, fn_lib, (&mut val).into(), dot_rhs, level); - - // 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.typ { - ScopeEntryType::Constant => { - return Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( - src.name.to_string(), - idx_lhs.position(), - ))); - } - - ScopeEntryType::Normal => { - update_indexed_scope_var(scope, src, index, val, dot_rhs.position())?; - } + // Constants cannot be modified + match typ { + ScopeEntryType::Constant if new_val.is_some() => { + return Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( + id.to_string(), + *pos, + ))); } + _ => (), } - value + self.eval_dot_index_chain_helper( + fn_lib, + target.into(), + dot_rhs, + idx_list, + idx_more, + is_index, + op_pos, + level, + new_val, + ) + .map(|(v, _)| v) } - - // {expr}.??? + // {expr}.??? = ??? or {expr}[???] = ??? + expr if new_val.is_some() => { + return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS( + expr.position(), + ))); + } + // {expr}.??? or {expr}[???] expr => { - let mut val = self.eval_expr(scope, fn_lib, expr, level)?; - self.dot_get_helper(scope, fn_lib, (&mut val).into(), dot_rhs, level) + let val = self.eval_expr(scope, fn_lib, expr, level)?; + + self.eval_dot_index_chain_helper( + fn_lib, + val.into(), + dot_rhs, + idx_list, + idx_more, + is_index, + op_pos, + level, + new_val, + ) + .map(|(v, _)| v) } } } - /// Get the value at the indexed position of a base type - fn get_indexed_val( + fn eval_indexed_chain( &self, scope: &mut Scope, fn_lib: Option<&FunctionsLib>, - val: &Dynamic, - idx_expr: &Expr, - op_pos: Position, + expr: &Expr, + list: &mut [Dynamic], + more: &mut Vec, + size: usize, level: usize, - only_index: bool, - ) -> Result<(Dynamic, IndexValue), Box> { - let idx_pos = idx_expr.position(); + ) -> Result> { + let size = match expr { + Expr::FunctionCall(_, arg_exprs, _, _) => { + let arg_values = arg_exprs + .iter() + .map(|arg_expr| self.eval_expr(scope, fn_lib, arg_expr, level)) + .collect::, _>>()?; + + if size < list.len() { + list[size] = arg_values.into(); + } else { + more.push(arg_values.into()); + } + size + 1 + } + Expr::Property(_, _) => { + // Placeholder + if size < list.len() { + list[size] = ().into(); + } else { + more.push(().into()); + } + size + 1 + } + Expr::Index(lhs, rhs, _) | Expr::Dot(lhs, rhs, _) => { + // Evaluate in left-to-right order + let lhs_val = match lhs.as_ref() { + Expr::Property(_, _) => ().into(), // Placeholder + _ => self.eval_expr(scope, fn_lib, lhs, level)?, + }; + + // Push in reverse order + let size = self.eval_indexed_chain(scope, fn_lib, rhs, list, more, size, level)?; + + if size < list.len() { + list[size] = lhs_val; + } else { + more.push(lhs_val); + } + size + 1 + } + _ => { + let val = self.eval_expr(scope, fn_lib, expr, level)?; + if size < list.len() { + list[size] = val; + } else { + more.push(val); + } + size + 1 + } + }; + Ok(size) + } + + /// Get the value at the indexed position of a base type + fn get_indexed_mut<'a>( + &self, + val: &'a mut Dynamic, + idx: Dynamic, + idx_pos: Position, + op_pos: Position, + create: bool, + ) -> Result, Box> { let type_name = self.map_type_name(val.type_name()); - match val.get_ref() { - Union::Array(arr) => { + match val { + Dynamic(Union::Array(arr)) => { // val_array[idx] - let index = self - .eval_expr(scope, fn_lib, idx_expr, level)? + let index = idx .as_int() - .map_err(|_| EvalAltResult::ErrorNumericIndexExpr(idx_expr.position()))?; + .map_err(|_| EvalAltResult::ErrorNumericIndexExpr(idx_pos))?; let arr_len = arr.len(); if index >= 0 { - arr.get(index as usize) - .map(|v| { - ( - if only_index { ().into() } else { v.clone() }, - IndexValue::from_num(index), - ) - }) + arr.get_mut(index as usize) + .map(Target::from) .ok_or_else(|| { Box::new(EvalAltResult::ErrorArrayBounds(arr_len, index, idx_pos)) }) @@ -991,37 +1040,31 @@ impl Engine { } } - Union::Map(map) => { + Dynamic(Union::Map(map)) => { // val_map[idx] - let index = self - .eval_expr(scope, fn_lib, idx_expr, level)? + let index = idx .take_string() - .map_err(|_| EvalAltResult::ErrorStringIndexExpr(idx_expr.position()))?; + .map_err(|_| EvalAltResult::ErrorStringIndexExpr(idx_pos))?; - Ok(( - map.get(&index) - .map(|v| if only_index { ().into() } else { v.clone() }) - .unwrap_or_else(|| ().into()), - IndexValue::from_str(index), - )) + Ok(if create { + map.entry(index).or_insert(().into()).into() + } else { + map.get_mut(&index).map(Target::from).unwrap_or_else(|| Target::from(())) + }) } - Union::Str(s) => { + Dynamic(Union::Str(s)) => { // val_string[idx] - let index = self - .eval_expr(scope, fn_lib, idx_expr, level)? + let index = idx .as_int() - .map_err(|_| EvalAltResult::ErrorNumericIndexExpr(idx_expr.position()))?; + .map_err(|_| EvalAltResult::ErrorNumericIndexExpr(idx_pos))?; let num_chars = s.chars().count(); if index >= 0 { - s.chars() - .nth(index as usize) - .map(|ch| (ch.into(), IndexValue::from_num(index))) - .ok_or_else(|| { - Box::new(EvalAltResult::ErrorStringBounds(num_chars, index, idx_pos)) - }) + let index = index as usize; + let ch = s.chars().nth(index).unwrap(); + Ok(Target::StringChar(Box::new((val, index, ch.into())))) } else { Err(Box::new(EvalAltResult::ErrorStringBounds( num_chars, index, idx_pos, @@ -1037,221 +1080,6 @@ impl Engine { } } - /// Evaluate an index expression - fn eval_index_expr<'a>( - &self, - scope: &mut Scope, - fn_lib: Option<&FunctionsLib>, - lhs: &'a Expr, - idx_expr: &Expr, - op_pos: Position, - level: usize, - ) -> Result<(Option>, IndexValue, Dynamic), Box> { - match lhs { - // id[idx_expr] - Expr::Variable(name, _) => { - let (ScopeSource { typ, index, .. }, val) = - search_scope(scope, &name, lhs.position())?; - let (val, idx) = - self.get_indexed_val(scope, fn_lib, &val, idx_expr, op_pos, level, false)?; - - Ok((Some(ScopeSource { name, typ, index }), idx, val)) - } - - // (expr)[idx_expr] - expr => { - let val = self.eval_expr(scope, fn_lib, expr, level)?; - self.get_indexed_val(scope, fn_lib, &val, idx_expr, op_pos, level, false) - .map(|(val, index)| (None, index, val)) - } - } - } - - /// Chain-evaluate a dot setter - fn dot_set_helper( - &self, - scope: &mut Scope, - fn_lib: Option<&FunctionsLib>, - this_ptr: &mut Dynamic, - dot_rhs: &Expr, - new_val: &mut Dynamic, - val_pos: Position, - level: usize, - ) -> Result> { - match dot_rhs { - // xxx.id - Expr::Property(id, pos) => { - let mut args = [this_ptr, new_val]; - self.exec_fn_call(fn_lib, &make_setter(id), &mut args, None, *pos, 0) - } - - // xxx.lhs[idx_expr] - // TODO - Allow chaining of indexing! - Expr::Index(lhs, idx_expr, op_pos) => match lhs.as_ref() { - // xxx.id[idx_expr] - Expr::Property(id, pos) => { - let fn_name = make_getter(id); - self.exec_fn_call(fn_lib, &fn_name, &mut [this_ptr], None, *pos, 0) - .and_then(|val| { - let (_, index) = self.get_indexed_val( - scope, fn_lib, &val, idx_expr, *op_pos, level, true, - )?; - - update_indexed_val(val, index, new_val.clone(), val_pos) - }) - .and_then(|mut val| { - let fn_name = make_setter(id); - let mut args = [this_ptr, &mut val]; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0) - }) - } - - // All others - syntax error for setters chain - _ => Err(Box::new(EvalAltResult::ErrorDotExpr( - "for assignment".to_string(), - *op_pos, - ))), - }, - - // xxx.lhs.{...} - Expr::Dot(lhs, rhs, _) => match lhs.as_ref() { - // xxx.id.rhs - Expr::Property(id, pos) => { - let fn_name = make_getter(id); - self.exec_fn_call(fn_lib, &fn_name, &mut [this_ptr], None, *pos, 0) - .and_then(|mut val| { - self.dot_set_helper( - scope, fn_lib, &mut val, rhs, new_val, val_pos, level, - )?; - Ok(val) - }) - .and_then(|mut val| { - let fn_name = make_setter(id); - let mut args = [this_ptr, &mut val]; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0) - }) - } - - // xxx.lhs[idx_expr].rhs - // TODO - Allow chaining of indexing! - Expr::Index(lhs, idx_expr, op_pos) => match lhs.as_ref() { - // xxx.id[idx_expr].rhs - Expr::Property(id, pos) => { - let fn_name = make_getter(id); - self.exec_fn_call(fn_lib, &fn_name, &mut [this_ptr], None, *pos, 0) - .and_then(|v| { - let (mut value, index) = self.get_indexed_val( - scope, fn_lib, &v, idx_expr, *op_pos, level, false, - )?; - - self.dot_set_helper( - scope, fn_lib, &mut value, rhs, new_val, val_pos, level, - )?; - - // In case the expression mutated `target`, we need to update it back into the scope because it is cloned. - update_indexed_val(v, index, value, val_pos) - }) - .and_then(|mut v| { - let fn_name = make_setter(id); - let mut args = [this_ptr, &mut v]; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0) - }) - } - - // All others - syntax error for setters chain - _ => Err(Box::new(EvalAltResult::ErrorDotExpr( - "for assignment".to_string(), - *op_pos, - ))), - }, - - // All others - syntax error for setters chain - _ => Err(Box::new(EvalAltResult::ErrorDotExpr( - "for assignment".to_string(), - lhs.position(), - ))), - }, - - // Syntax error - _ => Err(Box::new(EvalAltResult::ErrorDotExpr( - "for assignment".to_string(), - dot_rhs.position(), - ))), - } - } - - // Evaluate a dot chain setter - fn dot_set( - &self, - scope: &mut Scope, - fn_lib: Option<&FunctionsLib>, - dot_lhs: &Expr, - dot_rhs: &Expr, - new_val: &mut Dynamic, - val_pos: Position, - op_pos: Position, - level: usize, - ) -> Result> { - match dot_lhs { - // id.??? - Expr::Variable(id, pos) => { - let (src, mut target) = search_scope(scope, id, *pos)?; - - match src.typ { - ScopeEntryType::Constant => Err(Box::new( - EvalAltResult::ErrorAssignmentToConstant(id.to_string(), op_pos), - )), - _ => { - // Avoid referencing scope which is used below as mut - let entry = ScopeSource { name: id, ..src }; - let this_ptr = &mut target; - let value = self.dot_set_helper( - scope, fn_lib, this_ptr, dot_rhs, new_val, val_pos, level, - ); - - // In case the expression mutated `target`, we need to update it back into the scope because it is cloned. - *scope.get_mut(entry) = target; - - value - } - } - } - - // lhs[idx_expr].??? - // TODO - Allow chaining of indexing! - Expr::Index(lhs, idx_expr, op_pos) => { - let (src, index, mut target) = - self.eval_index_expr(scope, fn_lib, lhs, idx_expr, *op_pos, level)?; - let this_ptr = &mut target; - let value = - self.dot_set_helper(scope, fn_lib, this_ptr, dot_rhs, new_val, val_pos, level); - - // 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.typ { - ScopeEntryType::Constant => { - return Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( - src.name.to_string(), - lhs.position(), - ))); - } - ScopeEntryType::Normal => { - update_indexed_scope_var(scope, src, index, target, val_pos)?; - } - } - } - - value - } - - // Syntax error - _ => Err(Box::new(EvalAltResult::ErrorDotExpr( - "for assignment".to_string(), - dot_lhs.position(), - ))), - } - } - // Evaluate an 'in' expression fn eval_in_expr( &self, @@ -1319,7 +1147,9 @@ impl Engine { Expr::FloatConstant(f, _) => Ok((*f).into()), Expr::StringConstant(s, _) => Ok(s.to_string().into()), Expr::CharConstant(c, _) => Ok((*c).into()), - Expr::Variable(id, pos) => search_scope(scope, id, *pos).map(|(_, val)| val), + Expr::Variable(id, pos) => { + search_scope(scope, id, *pos).map(|(v, _)| v.borrow().clone()) + } Expr::Property(_, _) => panic!("unexpected property."), // Statement block @@ -1327,7 +1157,7 @@ impl Engine { // lhs = rhs Expr::Assignment(lhs, rhs, op_pos) => { - let mut rhs_val = self.eval_expr(scope, fn_lib, rhs, level)?; + let rhs_val = self.eval_expr(scope, fn_lib, rhs, level)?; match lhs.as_ref() { // name = rhs @@ -1350,7 +1180,7 @@ impl Engine { )) => { // Avoid referencing scope which is used below as mut let entry = ScopeSource { name, ..entry }; - *scope.get_mut(entry) = rhs_val.clone(); + *scope.get_ref(entry).borrow_mut() = rhs_val.clone(); Ok(rhs_val) } @@ -1369,39 +1199,19 @@ impl Engine { // idx_lhs[idx_expr] = rhs #[cfg(not(feature = "no_index"))] Expr::Index(idx_lhs, idx_expr, op_pos) => { - let (src, index, _) = - self.eval_index_expr(scope, fn_lib, idx_lhs, idx_expr, *op_pos, level)?; - - if let Some(src) = src { - match src.typ { - ScopeEntryType::Constant => { - Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( - src.name.to_string(), - idx_lhs.position(), - ))) - } - ScopeEntryType::Normal => { - let pos = rhs.position(); - Ok(update_indexed_scope_var(scope, src, index, rhs_val, pos)?) - } - } - } else { - Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS( - idx_lhs.position(), - ))) - } + let new_val = Some(rhs_val); + self.eval_dot_index_chain( + scope, fn_lib, idx_lhs, idx_expr, true, *op_pos, level, new_val, + ) } - // dot_lhs.dot_rhs = rhs #[cfg(not(feature = "no_object"))] Expr::Dot(dot_lhs, dot_rhs, _) => { - let new_val = &mut rhs_val; - let val_pos = rhs.position(); - self.dot_set( - scope, fn_lib, dot_lhs, dot_rhs, new_val, val_pos, *op_pos, level, + let new_val = Some(rhs_val); + self.eval_dot_index_chain( + scope, fn_lib, dot_lhs, dot_rhs, false, *op_pos, level, new_val, ) } - // Error assignment to constant expr if expr.is_constant() => { Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( @@ -1419,12 +1229,15 @@ impl Engine { // lhs[idx_expr] #[cfg(not(feature = "no_index"))] - Expr::Index(lhs, idx_expr, op_pos) => self - .eval_index_expr(scope, fn_lib, lhs, idx_expr, *op_pos, level) - .map(|(_, _, x)| x), + Expr::Index(lhs, idx_expr, op_pos) => { + self.eval_dot_index_chain(scope, fn_lib, lhs, idx_expr, true, *op_pos, level, None) + } + // lhs.dot_rhs #[cfg(not(feature = "no_object"))] - Expr::Dot(lhs, rhs, _) => self.dot_get(scope, fn_lib, lhs, rhs, level), + Expr::Dot(lhs, dot_rhs, op_pos) => { + self.eval_dot_index_chain(scope, fn_lib, lhs, dot_rhs, false, *op_pos, level, None) + } #[cfg(not(feature = "no_index"))] Expr::Array(contents, _) => { @@ -1618,7 +1431,7 @@ impl Engine { }; for a in iter_fn(arr) { - *scope.get_mut(entry) = a; + *scope.get_ref(entry).borrow_mut() = a; match self.eval_stmt(scope, fn_lib, body, level) { Ok(_) => (), diff --git a/src/error.rs b/src/error.rs index d87c3722..b08e5975 100644 --- a/src/error.rs +++ b/src/error.rs @@ -100,8 +100,6 @@ pub enum ParseErrorType { FnMissingBody(String), /// Assignment to an inappropriate LHS (left-hand-side) expression. AssignmentToInvalidLHS, - /// Assignment to a copy of a value. - AssignmentToCopy, /// Assignment to an a constant variable. AssignmentToConstant(String), /// Break statement not inside a loop. @@ -150,7 +148,6 @@ impl ParseError { ParseErrorType::FnMissingBody(_) => "Expecting body statement block for function declaration", ParseErrorType::WrongFnDefinition => "Function definitions must be at global level and cannot be inside a block or another function", ParseErrorType::AssignmentToInvalidLHS => "Cannot assign to this expression", - ParseErrorType::AssignmentToCopy => "Cannot assign to this expression because it will only be changing a copy of the value", ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant variable.", ParseErrorType::LoopBreak => "Break statement should only be used inside a loop" } diff --git a/src/parser.rs b/src/parser.rs index 15343778..a9f79250 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -476,34 +476,41 @@ impl Expr { /// Is a particular token allowed as a postfix operator to this expression? pub fn is_valid_postfix(&self, token: &Token) -> bool { match self { - Expr::IntegerConstant(_, _) - | Expr::FloatConstant(_, _) - | Expr::CharConstant(_, _) - | Expr::In(_, _, _) - | Expr::And(_, _, _) - | Expr::Or(_, _, _) - | Expr::True(_) - | Expr::False(_) - | Expr::Unit(_) => false, + Self::IntegerConstant(_, _) + | Self::FloatConstant(_, _) + | Self::CharConstant(_, _) + | Self::In(_, _, _) + | Self::And(_, _, _) + | Self::Or(_, _, _) + | Self::True(_) + | Self::False(_) + | Self::Unit(_) => false, - Expr::StringConstant(_, _) - | Expr::Stmt(_, _) - | Expr::FunctionCall(_, _, _, _) - | Expr::Assignment(_, _, _) - | Expr::Dot(_, _, _) - | Expr::Index(_, _, _) - | Expr::Array(_, _) - | Expr::Map(_, _) => match token { + Self::StringConstant(_, _) + | Self::Stmt(_, _) + | Self::FunctionCall(_, _, _, _) + | Self::Assignment(_, _, _) + | Self::Dot(_, _, _) + | Self::Index(_, _, _) + | Self::Array(_, _) + | Self::Map(_, _) => match token { Token::LeftBracket => true, _ => false, }, - Expr::Variable(_, _) | Expr::Property(_, _) => match token { + Self::Variable(_, _) | Self::Property(_, _) => match token { Token::LeftBracket | Token::LeftParen => true, _ => false, }, } } + + pub(crate) fn into_property(self) -> Self { + match self { + Self::Variable(id, pos) => Self::Property(id, pos), + _ => self, + } + } } /// Consume a particular token, checking that it is the expected one. @@ -734,7 +741,17 @@ fn parse_index_expr<'a>( match input.peek().unwrap() { (Token::RightBracket, _) => { eat_token(input, Token::RightBracket); - Ok(Expr::Index(lhs, Box::new(idx_expr), pos)) + + let idx_expr = Box::new(idx_expr); + + match input.peek().unwrap() { + (Token::LeftBracket, _) => { + let follow_pos = eat_token(input, Token::LeftBracket); + let follow = parse_index_expr(idx_expr, input, follow_pos, allow_stmt_expr)?; + Ok(Expr::Index(lhs, Box::new(follow), pos)) + } + _ => Ok(Expr::Index(lhs, idx_expr, pos)), + } } (Token::LexError(err), pos) => return Err(PERR::BadInput(err.to_string()).into_err(*pos)), (_, pos) => Err(PERR::MissingToken( @@ -1005,71 +1022,6 @@ fn parse_unary<'a>( } } -/// Parse an assignment. -fn parse_assignment(lhs: Expr, rhs: Expr, pos: Position) -> Result> { - // Is the LHS in a valid format for an assignment target? - fn valid_assignment_chain(expr: &Expr, is_top: bool) -> Option> { - match expr { - // var - Expr::Variable(_, _) => { - assert!(is_top, "property expected but gets variable"); - None - } - // property - Expr::Property(_, _) => { - assert!(!is_top, "variable expected but gets property"); - None - } - - // idx_lhs[...] - Expr::Index(idx_lhs, _, pos) => match idx_lhs.as_ref() { - // var[...] - Expr::Variable(_, _) => { - assert!(is_top, "property expected but gets variable"); - None - } - // property[...] - Expr::Property(_, _) => { - assert!(!is_top, "variable expected but gets property"); - None - } - // ???[...][...] - Expr::Index(_, _, _) => Some(ParseErrorType::AssignmentToCopy.into_err(*pos)), - // idx_lhs[...] - _ => Some(ParseErrorType::AssignmentToInvalidLHS.into_err(*pos)), - }, - - // dot_lhs.dot_rhs - Expr::Dot(dot_lhs, dot_rhs, pos) => match dot_lhs.as_ref() { - // var.dot_rhs - Expr::Variable(_, _) if is_top => valid_assignment_chain(dot_rhs, false), - // property.dot_rhs - Expr::Property(_, _) if !is_top => valid_assignment_chain(dot_rhs, false), - // idx_lhs[...].dot_rhs - Expr::Index(idx_lhs, _, _) => match idx_lhs.as_ref() { - // var[...].dot_rhs - Expr::Variable(_, _) if is_top => valid_assignment_chain(dot_rhs, false), - // property[...].dot_rhs - Expr::Property(_, _) if !is_top => valid_assignment_chain(dot_rhs, false), - // ???[...][...].dot_rhs - Expr::Index(_, _, _) => Some(ParseErrorType::AssignmentToCopy.into_err(*pos)), - // idx_lhs[...].dot_rhs - _ => Some(ParseErrorType::AssignmentToCopy.into_err(idx_lhs.position())), - }, - - expr => panic!("unexpected dot expression {:#?}", expr), - }, - - _ => Some(ParseErrorType::AssignmentToInvalidLHS.into_err(expr.position())), - } - } - - match valid_assignment_chain(&lhs, true) { - None => Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), - Some(err) => Err(err), - } -} - fn parse_assignment_stmt<'a>( input: &mut Peekable>, lhs: Expr, @@ -1077,7 +1029,7 @@ fn parse_assignment_stmt<'a>( ) -> Result> { let pos = eat_token(input, Token::Equals); let rhs = parse_expr(input, allow_stmt_expr)?; - parse_assignment(lhs, rhs, pos) + Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)) } /// Parse an operator-assignment expression. @@ -1108,15 +1060,51 @@ fn parse_op_assignment_stmt<'a>( let rhs = parse_expr(input, allow_stmt_expr)?; // lhs op= rhs -> lhs = op(lhs, rhs) - parse_assignment( - lhs, - Expr::FunctionCall(op.into(), vec![lhs_copy, rhs], None, pos), - pos, - ) + let rhs_expr = Expr::FunctionCall(op.into(), vec![lhs_copy, rhs], None, pos); + Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs_expr), pos)) } -/// Parse an 'in' expression. -fn parse_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result> { +/// Make a dot expression. +fn make_dot_expr(lhs: Expr, rhs: Expr, op_pos: Position, is_index: bool) -> Expr { + match (lhs, rhs) { + // idx_lhs[idx_rhs].rhs + (Expr::Index(idx_lhs, idx_rhs, idx_pos), rhs) => Expr::Index( + idx_lhs, + Box::new(make_dot_expr(*idx_rhs, rhs, op_pos, true)), + idx_pos, + ), + // lhs.id + (lhs, rhs @ Expr::Variable(_, _)) | (lhs, rhs @ Expr::Property(_, _)) => { + let lhs = if is_index { lhs.into_property() } else { lhs }; + Expr::Dot(Box::new(lhs), Box::new(rhs.into_property()), op_pos) + } + // lhs.dot_lhs.dot_rhs + (lhs, Expr::Dot(dot_lhs, dot_rhs, dot_pos)) => Expr::Dot( + Box::new(lhs), + Box::new(Expr::Dot( + Box::new(dot_lhs.into_property()), + Box::new(dot_rhs.into_property()), + dot_pos, + )), + op_pos, + ), + // lhs.idx_lhs[idx_rhs] + (lhs, Expr::Index(idx_lhs, idx_rhs, idx_pos)) => Expr::Dot( + Box::new(lhs), + Box::new(Expr::Index( + Box::new(idx_lhs.into_property()), + Box::new(idx_rhs.into_property()), + idx_pos, + )), + op_pos, + ), + // lhs.rhs + (lhs, rhs) => Expr::Dot(Box::new(lhs), Box::new(rhs.into_property()), op_pos), + } +} + +/// Make an 'in' expression. +fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result> { match (&lhs, &rhs) { (_, Expr::IntegerConstant(_, pos)) | (_, Expr::FloatConstant(_, pos)) @@ -1321,34 +1309,10 @@ fn parse_binary_op<'a>( Token::Pipe => Expr::FunctionCall("|".into(), vec![current_lhs, rhs], None, pos), Token::XOr => Expr::FunctionCall("^".into(), vec![current_lhs, rhs], None, pos), - Token::In => parse_in_expr(current_lhs, rhs, pos)?, + Token::In => make_in_expr(current_lhs, rhs, pos)?, #[cfg(not(feature = "no_object"))] - Token::Period => { - fn check_property(expr: Expr) -> Result> { - match expr { - // xxx.lhs.rhs - Expr::Dot(lhs, rhs, pos) => Ok(Expr::Dot( - Box::new(check_property(*lhs)?), - Box::new(check_property(*rhs)?), - pos, - )), - // xxx.lhs[idx] - Expr::Index(lhs, idx, pos) => { - Ok(Expr::Index(Box::new(check_property(*lhs)?), idx, pos)) - } - // xxx.id - Expr::Variable(id, pos) => Ok(Expr::Property(id, pos)), - // xxx.prop - expr @ Expr::Property(_, _) => Ok(expr), - // xxx.fn() - expr @ Expr::FunctionCall(_, _, _, _) => Ok(expr), - expr => Err(PERR::PropertyExpected.into_err(expr.position())), - } - } - - Expr::Dot(Box::new(current_lhs), Box::new(check_property(rhs)?), pos) - } + Token::Period => make_dot_expr(current_lhs, rhs, pos, false), token => return Err(PERR::UnknownOperator(token.syntax().into()).into_err(pos)), }; diff --git a/src/scope.rs b/src/scope.rs index 6abfb40f..6eeff2bd 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -4,7 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::parser::{map_dynamic_to_expr, Expr}; use crate::token::Position; -use crate::stdlib::{borrow::Cow, iter, vec::Vec}; +use crate::stdlib::{borrow::Cow, cell::RefCell, iter, vec::Vec}; /// Type of an entry in the Scope. #[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)] @@ -23,7 +23,7 @@ pub struct Entry<'a> { /// Type of the entry. pub typ: EntryType, /// Current value of the entry. - pub value: Dynamic, + pub value: RefCell, /// A constant expression if the initial value matches one of the recognized types. pub expr: Option, } @@ -226,15 +226,17 @@ impl<'a> Scope<'a> { value: Dynamic, map_expr: bool, ) { + let expr = if map_expr { + map_dynamic_to_expr(value.clone(), Position::none()) + } else { + None + }; + self.0.push(Entry { name: name.into(), typ: entry_type, - value: value.clone(), - expr: if map_expr { - map_dynamic_to_expr(value, Position::none()) - } else { - None - }, + value: value.into(), + expr, }); } @@ -311,7 +313,7 @@ impl<'a> Scope<'a> { index, typ: *typ, }, - value.clone(), + value.borrow().clone(), )) } else { None @@ -337,7 +339,7 @@ impl<'a> Scope<'a> { .iter() .rev() .find(|Entry { name: key, .. }| name == key) - .and_then(|Entry { value, .. }| value.downcast_ref::().cloned()) + .and_then(|Entry { value, .. }| value.borrow().downcast_ref::().cloned()) } /// Update the value of the named entry. @@ -377,14 +379,14 @@ impl<'a> Scope<'a> { .. }, _, - )) => self.0.get_mut(index).unwrap().value = Dynamic::from(value), + )) => *self.0.get_mut(index).unwrap().value.borrow_mut() = Dynamic::from(value), None => self.push(name, value), } } /// 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"); + pub(crate) fn get_ref(&self, key: EntryRef) -> &RefCell { + let entry = self.0.get(key.index).expect("invalid index in Scope"); assert_eq!(entry.typ, key.typ, "entry type not matched"); // assert_ne!( @@ -394,7 +396,7 @@ impl<'a> Scope<'a> { // ); assert_eq!(entry.name, key.name, "incorrect key at Scope entry"); - &mut entry.value + &entry.value } /// Get an iterator to entries in the Scope. @@ -418,7 +420,7 @@ where .extend(iter.into_iter().map(|(name, typ, value)| Entry { name: name.into(), typ, - value, + value: value.into(), expr: None, })); }