From 4e115d2bc2241f4f5be41aa1cad0fc88d519216e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 29 Oct 2020 11:37:51 +0800 Subject: [PATCH] Code structure refactor. --- RELEASES.md | 4 +- src/ast.rs | 804 +++++++++++++++++++++++++++++++++- src/dynamic.rs | 5 +- src/engine.rs | 13 +- src/engine_api.rs | 4 +- src/fn_call.rs | 15 +- src/fn_native.rs | 2 +- src/fn_register.rs | 2 +- src/lib.rs | 29 +- src/module/mod.rs | 4 +- src/optimize.rs | 6 +- src/packages/arithmetic.rs | 4 +- src/packages/array_basic.rs | 9 +- src/packages/eval.rs | 2 +- src/packages/iter_basic.rs | 2 +- src/packages/map_basic.rs | 3 +- src/packages/math_basic.rs | 8 +- src/packages/string_basic.rs | 3 +- src/packages/string_more.rs | 3 +- src/packages/time_basic.rs | 4 +- src/parser.rs | 817 +---------------------------------- src/plugin.rs | 2 +- src/result.rs | 2 +- src/scope.rs | 3 +- src/syntax.rs | 2 +- src/token.rs | 4 +- src/utils.rs | 10 +- 27 files changed, 899 insertions(+), 867 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 4fb86c9e..38e49c8c 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -5,7 +5,9 @@ Rhai Release Notes Version 0.19.4 ============== -This version adds a low-level API for more flexibility when defining custom syntax. +This version basically cleans up the code structure in preparation for a potential `1.0` release in the future. + +This version also adds a low-level API for more flexibility when defining custom syntax. Bug fixes --------- diff --git a/src/ast.rs b/src/ast.rs index 7b282c24..3d67d832 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,15 +1,124 @@ //! Module defining the AST (abstract syntax tree). -use crate::fn_native::Shared; -use crate::module::Module; -use crate::parser::{FnAccess, ScriptFnDef, Stmt}; +use crate::dynamic::{Dynamic, Union}; +use crate::fn_native::{FnPtr, Shared}; +use crate::module::{Module, ModuleRef}; +use crate::syntax::FnCustomSyntaxEval; +use crate::token::{Position, Token}; +use crate::utils::ImmutableString; +use crate::StaticVec; +use crate::INT; + +#[cfg(not(feature = "no_float"))] +use crate::FLOAT; + +#[cfg(not(feature = "no_index"))] +use crate::engine::Array; + +#[cfg(not(feature = "no_object"))] +use crate::engine::{make_getter, make_setter, Map}; use crate::stdlib::{ + any::TypeId, + borrow::Cow, + fmt, + hash::{Hash, Hasher}, + num::NonZeroUsize, ops::{Add, AddAssign}, + string::String, vec, vec::Vec, }; +#[cfg(not(feature = "no_float"))] +use crate::stdlib::ops::Neg; + +#[cfg(not(feature = "no_closure"))] +use crate::stdlib::collections::HashSet; + +/// A type representing the access mode of a scripted function. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +pub enum FnAccess { + /// Public function. + Public, + /// Private function. + Private, +} + +impl fmt::Display for FnAccess { + #[inline(always)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Private => write!(f, "private"), + Self::Public => write!(f, "public"), + } + } +} + +impl FnAccess { + /// Is this access mode private? + #[inline(always)] + pub fn is_private(self) -> bool { + match self { + Self::Public => false, + Self::Private => true, + } + } + /// Is this access mode public? + #[inline(always)] + pub fn is_public(self) -> bool { + match self { + Self::Public => true, + Self::Private => false, + } + } +} + +/// _[INTERNALS]_ A type containing information on a scripted function. +/// Exported under the `internals` feature only. +/// +/// ## WARNING +/// +/// This type is volatile and may change. +#[derive(Debug, Clone)] +pub struct ScriptFnDef { + /// Function name. + pub name: ImmutableString, + /// Function access mode. + pub access: FnAccess, + /// Names of function parameters. + pub params: StaticVec, + /// Access to external variables. + #[cfg(not(feature = "no_closure"))] + pub externals: HashSet, + /// Function body. + pub body: Stmt, + /// Position of the function definition. + pub pos: Position, + /// Encapsulated running environment, if any. + pub lib: Option>, +} + +impl fmt::Display for ScriptFnDef { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}{}({})", + if self.access.is_private() { + "private " + } else { + "" + }, + self.name, + self.params + .iter() + .map(|s| s.as_str()) + .collect::>() + .join(",") + ) + } +} + /// Compiled AST (abstract syntax tree) of a Rhai script. /// /// # Thread Safety @@ -73,6 +182,7 @@ impl AST { /// No statements are cloned. /// /// This operation is cheap because functions are shared. + #[cfg(not(feature = "no_function"))] #[inline(always)] pub fn clone_functions_only(&self) -> Self { self.clone_functions_only_filtered(|_, _, _| true) @@ -82,6 +192,7 @@ impl AST { /// No statements are cloned. /// /// This operation is cheap because functions are shared. + #[cfg(not(feature = "no_function"))] #[inline(always)] pub fn clone_functions_only_filtered( &self, @@ -425,3 +536,690 @@ impl AsRef for AST { self.lib() } } + +/// An identifier containing a string name and a position. +#[derive(Debug, Clone, Hash)] +pub struct Ident { + pub name: String, + pub pos: Position, +} + +impl Ident { + /// Create a new `Identifier`. + pub fn new(name: String, pos: Position) -> Self { + Self { name, pos } + } +} + +/// An identifier containing an immutable name and a position. +#[derive(Debug, Clone, Hash)] +pub struct IdentX { + pub name: ImmutableString, + pub pos: Position, +} + +impl IdentX { + /// Create a new `Identifier`. + pub fn new(name: impl Into, pos: Position) -> Self { + Self { + name: name.into(), + pos, + } + } +} + +/// _[INTERNALS]_ A type encapsulating the mode of a `return`/`throw` statement. +/// Exported under the `internals` feature only. +/// +/// ## WARNING +/// +/// This type is volatile and may change. +#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)] +pub enum ReturnType { + /// `return` statement. + Return, + /// `throw` statement. + Exception, +} + +/// _[INTERNALS]_ A Rhai statement. +/// 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. +#[derive(Debug, Clone, Hash)] +pub enum Stmt { + /// No-op. + Noop(Position), + /// if expr { stmt } else { stmt } + IfThenElse(Expr, Box<(Stmt, Option)>, Position), + /// while expr { stmt } + While(Expr, Box, Position), + /// loop { stmt } + Loop(Box, Position), + /// for id in expr { stmt } + For(Expr, Box<(String, Stmt)>, Position), + /// let id = expr + Let(Box, Option, Position), + /// const id = expr + Const(Box, Option, Position), + /// expr op= expr + Assignment(Box<(Expr, Cow<'static, str>, Expr)>, Position), + /// { stmt; ... } + Block(Vec, Position), + /// try { stmt; ... } catch ( var ) { stmt; ... } + TryCatch(Box<(Stmt, Option, Stmt, (Position, Position))>), + /// expr + Expr(Expr), + /// continue + Continue(Position), + /// break + Break(Position), + /// return/throw + ReturnWithVal((ReturnType, Position), Option, Position), + /// import expr as var + #[cfg(not(feature = "no_module"))] + Import(Expr, Option>, Position), + /// export var as var, ... + #[cfg(not(feature = "no_module"))] + Export(Vec<(Ident, Option)>, Position), + /// Convert a variable to shared. + #[cfg(not(feature = "no_closure"))] + Share(Ident), +} + +impl Default for Stmt { + #[inline(always)] + fn default() -> Self { + Self::Noop(Default::default()) + } +} + +impl Stmt { + /// Is this statement `Noop`? + pub fn is_noop(&self) -> bool { + match self { + Self::Noop(_) => true, + _ => false, + } + } + + /// Get the `Position` of this statement. + pub fn position(&self) -> Position { + match self { + Self::Noop(pos) + | Self::Continue(pos) + | Self::Break(pos) + | Self::Block(_, pos) + | Self::Assignment(_, pos) + | Self::IfThenElse(_, _, pos) + | Self::While(_, _, pos) + | Self::Loop(_, pos) + | Self::For(_, _, pos) + | Self::ReturnWithVal((_, pos), _, _) => *pos, + + Self::Let(x, _, _) | Self::Const(x, _, _) => x.pos, + Self::TryCatch(x) => (x.3).0, + + Self::Expr(x) => x.position(), + + #[cfg(not(feature = "no_module"))] + Self::Import(_, _, pos) => *pos, + #[cfg(not(feature = "no_module"))] + Self::Export(_, pos) => *pos, + + #[cfg(not(feature = "no_closure"))] + Self::Share(Ident { pos, .. }) => *pos, + } + } + + /// Override the `Position` of this statement. + pub fn set_position(&mut self, new_pos: Position) -> &mut Self { + match self { + Self::Noop(pos) + | Self::Continue(pos) + | Self::Break(pos) + | Self::Block(_, pos) + | Self::Assignment(_, pos) + | Self::IfThenElse(_, _, pos) + | Self::While(_, _, pos) + | Self::Loop(_, pos) + | Self::For(_, _, pos) + | Self::ReturnWithVal((_, pos), _, _) => *pos = new_pos, + + Self::Let(x, _, _) | Self::Const(x, _, _) => x.pos = new_pos, + Self::TryCatch(x) => (x.3).0 = new_pos, + + Self::Expr(x) => { + x.set_position(new_pos); + } + + #[cfg(not(feature = "no_module"))] + Self::Import(_, _, pos) => *pos = new_pos, + #[cfg(not(feature = "no_module"))] + Self::Export(_, pos) => *pos = new_pos, + + #[cfg(not(feature = "no_closure"))] + Self::Share(Ident { pos, .. }) => *pos = new_pos, + } + + self + } + + /// Is this statement self-terminated (i.e. no need for a semicolon terminator)? + pub fn is_self_terminated(&self) -> bool { + match self { + Self::IfThenElse(_, _, _) + | Self::While(_, _, _) + | Self::Loop(_, _) + | Self::For(_, _, _) + | Self::Block(_, _) + | Self::TryCatch(_) => true, + + // A No-op requires a semicolon in order to know it is an empty statement! + Self::Noop(_) => false, + + Self::Let(_, _, _) + | Self::Const(_, _, _) + | Self::Assignment(_, _) + | Self::Expr(_) + | Self::Continue(_) + | Self::Break(_) + | Self::ReturnWithVal(_, _, _) => false, + + #[cfg(not(feature = "no_module"))] + Self::Import(_, _, _) | Self::Export(_, _) => false, + + #[cfg(not(feature = "no_closure"))] + Self::Share(_) => false, + } + } + + /// Is this statement _pure_? + pub fn is_pure(&self) -> bool { + match self { + Self::Noop(_) => true, + Self::Expr(expr) => expr.is_pure(), + Self::IfThenElse(condition, x, _) if x.1.is_some() => { + condition.is_pure() && x.0.is_pure() && x.1.as_ref().unwrap().is_pure() + } + Self::IfThenElse(condition, x, _) => condition.is_pure() && x.0.is_pure(), + Self::While(condition, block, _) => condition.is_pure() && block.is_pure(), + Self::Loop(block, _) => block.is_pure(), + Self::For(iterable, x, _) => iterable.is_pure() && x.1.is_pure(), + Self::Let(_, _, _) | Self::Const(_, _, _) | Self::Assignment(_, _) => false, + Self::Block(block, _) => block.iter().all(|stmt| stmt.is_pure()), + Self::Continue(_) | Self::Break(_) | Self::ReturnWithVal(_, _, _) => false, + Self::TryCatch(x) => x.0.is_pure() && x.2.is_pure(), + + #[cfg(not(feature = "no_module"))] + Self::Import(_, _, _) => false, + #[cfg(not(feature = "no_module"))] + Self::Export(_, _) => false, + + #[cfg(not(feature = "no_closure"))] + Self::Share(_) => false, + } + } +} + +/// _[INTERNALS]_ A type wrapping a custom syntax definition. +/// Exported under the `internals` feature only. +/// +/// ## WARNING +/// +/// This type is volatile and may change. +#[derive(Clone)] +pub struct CustomExpr { + pub(crate) keywords: StaticVec, + pub(crate) func: Shared, + pub(crate) pos: Position, +} + +impl fmt::Debug for CustomExpr { + #[inline(always)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.keywords, f) + } +} + +impl Hash for CustomExpr { + #[inline(always)] + fn hash(&self, state: &mut H) { + self.keywords.hash(state); + } +} + +impl CustomExpr { + /// Get the keywords for this `CustomExpr`. + #[inline(always)] + pub fn keywords(&self) -> &[Expr] { + &self.keywords + } + /// Get the implementation function for this `CustomExpr`. + #[inline(always)] + pub fn func(&self) -> &FnCustomSyntaxEval { + self.func.as_ref() + } + /// Get the position of this `CustomExpr`. + #[inline(always)] + pub fn position(&self) -> Position { + self.pos + } +} + +/// _[INTERNALS]_ A type wrapping a floating-point number. +/// Exported under the `internals` feature only. +/// +/// This type is mainly used to provide a standard `Hash` implementation +/// to floating-point numbers, allowing `Expr` to derive `Hash` automatically. +/// +/// ## WARNING +/// +/// This type is volatile and may change. +#[cfg(not(feature = "no_float"))] +#[derive(Debug, PartialEq, PartialOrd, Clone)] +pub struct FloatWrapper(pub FLOAT, pub Position); + +#[cfg(not(feature = "no_float"))] +impl Hash for FloatWrapper { + #[inline(always)] + fn hash(&self, state: &mut H) { + state.write(&self.0.to_le_bytes()); + self.1.hash(state); + } +} + +#[cfg(not(feature = "no_float"))] +impl Neg for FloatWrapper { + type Output = Self; + + fn neg(self) -> Self::Output { + Self(-self.0, self.1) + } +} + +#[cfg(not(feature = "no_float"))] +impl From<(INT, Position)> for FloatWrapper { + fn from((value, pos): (INT, Position)) -> Self { + Self(value as FLOAT, pos) + } +} + +/// A binary expression structure. +#[derive(Debug, Clone, Hash)] +pub struct BinaryExpr { + pub lhs: Expr, + pub rhs: Expr, + pub pos: Position, +} + +/// _[INTERNALS]_ An expression sub-tree. +/// 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 +/// +/// This type is volatile and may change. +#[derive(Debug, Clone, Hash)] +pub enum Expr { + /// Integer constant. + IntegerConstant(Box<(INT, Position)>), + /// Floating-point constant. + #[cfg(not(feature = "no_float"))] + FloatConstant(Box), + /// Character constant. + CharConstant(Box<(char, Position)>), + /// String constant. + StringConstant(Box), + /// FnPtr constant. + FnPointer(Box), + /// Variable access - ((variable name, position), optional modules, hash, optional index) + Variable(Box<(Ident, Option>, u64, Option)>), + /// Property access. + Property(Box<((ImmutableString, String, String), Position)>), + /// { stmt } + Stmt(Box<(Stmt, Position)>), + /// Wrapped expression - should not be optimized away. + Expr(Box), + /// func(expr, ... ) - ((function name, native_only, capture, position), optional modules, hash, arguments, optional default value) + /// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls + /// and the function names are predictable, so no need to allocate a new `String`. + FnCall( + Box<( + (Cow<'static, str>, bool, bool, Position), + Option>, + u64, + StaticVec, + Option, // Default value is `bool` in order for `Expr` to be `Hash`. + )>, + ), + /// lhs.rhs + Dot(Box), + /// expr[expr] + Index(Box), + /// [ expr, ... ] + Array(Box<(StaticVec, Position)>), + /// #{ name:expr, ... } + Map(Box<(StaticVec<(IdentX, Expr)>, Position)>), + /// lhs in rhs + In(Box), + /// lhs && rhs + And(Box), + /// lhs || rhs + Or(Box), + /// true + True(Position), + /// false + False(Position), + /// () + Unit(Position), + /// Custom syntax + Custom(Box), +} + +impl Default for Expr { + #[inline(always)] + fn default() -> Self { + Self::Unit(Default::default()) + } +} + +impl Expr { + /// Get the type of an expression. + /// + /// Returns `None` if the expression's result type is not constant. + pub fn get_type_id(&self) -> Option { + Some(match self { + Self::Expr(x) => return x.get_type_id(), + + Self::IntegerConstant(_) => TypeId::of::(), + #[cfg(not(feature = "no_float"))] + Self::FloatConstant(_) => TypeId::of::(), + Self::CharConstant(_) => TypeId::of::(), + Self::StringConstant(_) => TypeId::of::(), + Self::FnPointer(_) => TypeId::of::(), + Self::True(_) | Self::False(_) | Self::In(_) | Self::And(_) | Self::Or(_) => { + TypeId::of::() + } + Self::Unit(_) => TypeId::of::<()>(), + + #[cfg(not(feature = "no_index"))] + Self::Array(_) => TypeId::of::(), + + #[cfg(not(feature = "no_object"))] + Self::Map(_) => TypeId::of::(), + + _ => return None, + }) + } + + /// Get the `Dynamic` value of a constant expression. + /// + /// Returns `None` if the expression is not constant. + pub fn get_constant_value(&self) -> Option { + Some(match self { + Self::Expr(x) => return x.get_constant_value(), + + Self::IntegerConstant(x) => x.0.into(), + #[cfg(not(feature = "no_float"))] + Self::FloatConstant(x) => x.0.into(), + Self::CharConstant(x) => x.0.into(), + Self::StringConstant(x) => x.name.clone().into(), + Self::FnPointer(x) => Dynamic(Union::FnPtr(Box::new(FnPtr::new_unchecked( + x.name.clone(), + Default::default(), + )))), + Self::True(_) => true.into(), + Self::False(_) => false.into(), + Self::Unit(_) => ().into(), + + #[cfg(not(feature = "no_index"))] + Self::Array(x) if x.0.iter().all(Self::is_constant) => Dynamic(Union::Array(Box::new( + x.0.iter() + .map(|v| v.get_constant_value().unwrap()) + .collect(), + ))), + + #[cfg(not(feature = "no_object"))] + Self::Map(x) if x.0.iter().all(|(_, v)| v.is_constant()) => { + Dynamic(Union::Map(Box::new( + x.0.iter() + .map(|(k, v)| (k.name.clone(), v.get_constant_value().unwrap())) + .collect(), + ))) + } + + _ => return None, + }) + } + + /// Is the expression a simple variable access? + pub(crate) fn get_variable_access(&self, non_qualified: bool) -> Option<&str> { + match self { + Self::Variable(x) if !non_qualified || x.1.is_none() => Some((x.0).name.as_str()), + _ => None, + } + } + + /// Get the `Position` of the expression. + pub fn position(&self) -> Position { + match self { + Self::Expr(x) => x.position(), + + #[cfg(not(feature = "no_float"))] + Self::FloatConstant(x) => x.1, + + Self::IntegerConstant(x) => x.1, + Self::CharConstant(x) => x.1, + Self::StringConstant(x) => x.pos, + Self::FnPointer(x) => x.pos, + Self::Array(x) => x.1, + Self::Map(x) => x.1, + Self::Property(x) => x.1, + Self::Stmt(x) => x.1, + Self::Variable(x) => (x.0).pos, + Self::FnCall(x) => (x.0).3, + + Self::And(x) | Self::Or(x) | Self::In(x) => x.pos, + + Self::True(pos) | Self::False(pos) | Self::Unit(pos) => *pos, + + Self::Dot(x) | Self::Index(x) => x.lhs.position(), + + Self::Custom(x) => x.pos, + } + } + + /// Override the `Position` of the expression. + pub fn set_position(&mut self, new_pos: Position) -> &mut Self { + match self { + Self::Expr(x) => { + x.set_position(new_pos); + } + + #[cfg(not(feature = "no_float"))] + Self::FloatConstant(x) => x.1 = new_pos, + + Self::IntegerConstant(x) => x.1 = new_pos, + Self::CharConstant(x) => x.1 = new_pos, + Self::StringConstant(x) => x.pos = new_pos, + Self::FnPointer(x) => x.pos = new_pos, + Self::Array(x) => x.1 = new_pos, + Self::Map(x) => x.1 = new_pos, + Self::Variable(x) => (x.0).pos = new_pos, + Self::Property(x) => x.1 = new_pos, + Self::Stmt(x) => x.1 = new_pos, + Self::FnCall(x) => (x.0).3 = new_pos, + Self::And(x) | Self::Or(x) | Self::In(x) => x.pos = new_pos, + Self::True(pos) | Self::False(pos) | Self::Unit(pos) => *pos = new_pos, + Self::Dot(x) | Self::Index(x) => x.pos = new_pos, + Self::Custom(x) => x.pos = new_pos, + } + + self + } + + /// Is the expression pure? + /// + /// A pure expression has no side effects. + pub fn is_pure(&self) -> bool { + match self { + Self::Expr(x) => x.is_pure(), + + Self::Array(x) => x.0.iter().all(Self::is_pure), + + Self::Index(x) | Self::And(x) | Self::Or(x) | Self::In(x) => { + x.lhs.is_pure() && x.rhs.is_pure() + } + + Self::Stmt(x) => x.0.is_pure(), + + Self::Variable(_) => true, + + _ => self.is_constant(), + } + } + + /// Is the expression the unit `()` literal? + #[inline(always)] + pub fn is_unit(&self) -> bool { + match self { + Self::Unit(_) => true, + _ => false, + } + } + + /// Is the expression a simple constant literal? + pub fn is_literal(&self) -> bool { + match self { + Self::Expr(x) => x.is_literal(), + + #[cfg(not(feature = "no_float"))] + Self::FloatConstant(_) => true, + + Self::IntegerConstant(_) + | Self::CharConstant(_) + | Self::StringConstant(_) + | Self::FnPointer(_) + | Self::True(_) + | Self::False(_) + | Self::Unit(_) => true, + + // An array literal is literal if all items are literals + Self::Array(x) => x.0.iter().all(Self::is_literal), + + // An map literal is literal if all items are literals + Self::Map(x) => x.0.iter().map(|(_, expr)| expr).all(Self::is_literal), + + // Check in expression + Self::In(x) => match (&x.lhs, &x.rhs) { + (Self::StringConstant(_), Self::StringConstant(_)) + | (Self::CharConstant(_), Self::StringConstant(_)) => true, + _ => false, + }, + + _ => false, + } + } + + /// Is the expression a constant? + pub fn is_constant(&self) -> bool { + match self { + Self::Expr(x) => x.is_constant(), + + #[cfg(not(feature = "no_float"))] + Self::FloatConstant(_) => true, + + Self::IntegerConstant(_) + | Self::CharConstant(_) + | Self::StringConstant(_) + | Self::FnPointer(_) + | Self::True(_) + | Self::False(_) + | Self::Unit(_) => true, + + // An array literal is constant if all items are constant + Self::Array(x) => x.0.iter().all(Self::is_constant), + + // An map literal is constant if all items are constant + Self::Map(x) => x.0.iter().map(|(_, expr)| expr).all(Self::is_constant), + + // Check in expression + Self::In(x) => match (&x.lhs, &x.rhs) { + (Self::StringConstant(_), Self::StringConstant(_)) + | (Self::CharConstant(_), Self::StringConstant(_)) => true, + _ => false, + }, + + _ => false, + } + } + + /// Is a particular token allowed as a postfix operator to this expression? + pub fn is_valid_postfix(&self, token: &Token) -> bool { + match self { + Self::Expr(x) => x.is_valid_postfix(token), + + #[cfg(not(feature = "no_float"))] + Self::FloatConstant(_) => false, + + Self::IntegerConstant(_) + | Self::CharConstant(_) + | Self::FnPointer(_) + | Self::In(_) + | Self::And(_) + | Self::Or(_) + | Self::True(_) + | Self::False(_) + | Self::Unit(_) => false, + + Self::StringConstant(_) + | Self::Stmt(_) + | Self::FnCall(_) + | Self::Dot(_) + | Self::Index(_) + | Self::Array(_) + | Self::Map(_) => match token { + #[cfg(not(feature = "no_index"))] + Token::LeftBracket => true, + _ => false, + }, + + Self::Variable(_) => match token { + #[cfg(not(feature = "no_index"))] + Token::LeftBracket => true, + Token::LeftParen => true, + Token::Bang => true, + Token::DoubleColon => true, + _ => false, + }, + + Self::Property(_) => match token { + #[cfg(not(feature = "no_index"))] + Token::LeftBracket => true, + Token::LeftParen => true, + _ => false, + }, + + Self::Custom(_) => false, + } + } + + /// Convert a `Variable` into a `Property`. All other variants are untouched. + #[cfg(not(feature = "no_object"))] + #[inline] + pub(crate) fn into_property(self) -> Self { + match self { + Self::Variable(x) if x.1.is_none() => { + let Ident { name, pos } = x.0; + let getter = make_getter(&name); + let setter = make_setter(&name); + Self::Property(Box::new(((name.into(), getter, setter), pos))) + } + _ => self, + } + } +} diff --git a/src/dynamic.rs b/src/dynamic.rs index 615637c8..b405fd8a 100644 --- a/src/dynamic.rs +++ b/src/dynamic.rs @@ -1,14 +1,15 @@ //! Helper module which defines the `Any` trait to to allow dynamic value handling. use crate::fn_native::{FnPtr, SendSync}; -use crate::parser::{ImmutableString, INT}; use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast}; +use crate::utils::ImmutableString; +use crate::INT; #[cfg(not(feature = "no_closure"))] use crate::fn_native::{shared_try_take, Locked, Shared}; #[cfg(not(feature = "no_float"))] -use crate::parser::FLOAT; +use crate::FLOAT; #[cfg(not(feature = "no_index"))] use crate::engine::Array; diff --git a/src/engine.rs b/src/engine.rs index 18a5f582..59bd12bc 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,12 +1,12 @@ //! Main module defining the script evaluation `Engine`. +use crate::ast::{BinaryExpr, Expr, Ident, ReturnType, Stmt}; use crate::dynamic::{map_std_type_name, Dynamic, Union, Variant}; use crate::fn_call::run_builtin_op_assignment; use crate::fn_native::{Callback, FnPtr, OnVarCallback}; use crate::module::{Module, ModuleRef}; use crate::optimize::OptimizationLevel; use crate::packages::{Package, PackagesCollection, StandardPackage}; -use crate::parser::{BinaryExpr, Expr, Ident, ReturnType, Stmt}; use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; @@ -15,7 +15,7 @@ use crate::token::Position; use crate::{calc_native_fn_hash, StaticVec}; #[cfg(not(feature = "no_index"))] -use crate::parser::INT; +use crate::INT; #[cfg(not(feature = "no_module"))] use crate::module::ModuleResolver; @@ -389,11 +389,6 @@ pub struct State { } impl State { - /// Create a new `State`. - #[inline(always)] - pub fn new() -> Self { - Default::default() - } /// Is the state currently at global (root) level? #[inline(always)] pub fn is_global(&self) -> bool { @@ -461,8 +456,8 @@ impl<'e, 'x, 'px, 'a, 's, 'm, 'pm, 't, 'pt> EvalContext<'e, 'x, 'px, 'a, 's, 'm, #[cfg(feature = "internals")] #[cfg(not(feature = "no_module"))] #[inline(always)] - pub fn imports(&self) -> &'a Imports { - self.mods.as_ref() + pub fn imports<'z: 'a>(&'z self) -> &'a Imports { + self.mods } /// Get an iterator over the namespaces containing definition of all script-defined functions. #[inline(always)] diff --git a/src/engine_api.rs b/src/engine_api.rs index 729a7105..36e664dc 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -2,7 +2,7 @@ use crate::ast::AST; use crate::dynamic::{Dynamic, Variant}; -use crate::engine::{Engine, EvalContext, Imports, State}; +use crate::engine::{Engine, EvalContext, Imports}; use crate::fn_native::{FnCallArgs, NativeCallContext, SendSync}; use crate::optimize::OptimizationLevel; use crate::parse_error::ParseError; @@ -1619,7 +1619,7 @@ impl Engine { .get_script_fn(name, args.len(), true) .ok_or_else(|| EvalAltResult::ErrorFunctionNotFound(name.into(), Position::none()))?; - let mut state = State::new(); + let mut state = Default::default(); let mut mods = Default::default(); // Check for data race. diff --git a/src/fn_call.rs b/src/fn_call.rs index 5cb9d7d5..78c381a3 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -1,5 +1,6 @@ //! Implement function-calling mechanism for `Engine`. +use crate::ast::{Expr, Stmt}; use crate::dynamic::Dynamic; use crate::engine::{ search_imports, Engine, Imports, State, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, @@ -10,21 +11,21 @@ use crate::fn_native::{FnCallArgs, FnPtr}; use crate::module::{Module, ModuleRef}; use crate::optimize::OptimizationLevel; use crate::parse_error::ParseErrorType; -use crate::parser::{Expr, ImmutableString, Stmt, INT}; use crate::result::EvalAltResult; use crate::scope::Scope; use crate::stdlib::ops::Deref; use crate::token::Position; -use crate::{calc_native_fn_hash, calc_script_fn_hash, StaticVec}; +use crate::utils::ImmutableString; +use crate::{calc_native_fn_hash, calc_script_fn_hash, StaticVec, INT}; #[cfg(not(feature = "no_function"))] use crate::{ - parser::ScriptFnDef, r#unsafe::unsafe_cast_var_name_to_lifetime, + ast::ScriptFnDef, r#unsafe::unsafe_cast_var_name_to_lifetime, scope::EntryType as ScopeEntryType, }; #[cfg(not(feature = "no_float"))] -use crate::parser::FLOAT; +use crate::FLOAT; #[cfg(not(feature = "no_index"))] use crate::engine::{FN_IDX_GET, FN_IDX_SET}; @@ -41,7 +42,6 @@ use crate::scope::Entry as ScopeEntry; use crate::stdlib::{ any::{type_name, TypeId}, - borrow::Cow, boxed::Box, convert::TryFrom, format, @@ -51,6 +51,9 @@ use crate::stdlib::{ vec::Vec, }; +#[cfg(not(feature = "no_function"))] +use crate::stdlib::borrow::Cow; + #[cfg(feature = "no_std")] #[cfg(not(feature = "no_float"))] use num_traits::float::Float; @@ -628,7 +631,7 @@ impl Engine { statements: impl IntoIterator, lib: &[&Module], ) -> Result<(Dynamic, u64), Box> { - let mut state = State::new(); + let mut state = Default::default(); statements .into_iter() diff --git a/src/fn_native.rs b/src/fn_native.rs index 9eebffbe..db95c6b3 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -1,9 +1,9 @@ //! Module defining interfaces to native-Rust functions. +use crate::ast::{FnAccess, ScriptFnDef}; use crate::dynamic::Dynamic; use crate::engine::{Engine, EvalContext}; use crate::module::Module; -use crate::parser::{FnAccess, ScriptFnDef}; use crate::plugin::PluginFunction; use crate::result::EvalAltResult; use crate::token::{is_valid_identifier, Position}; diff --git a/src/fn_register.rs b/src/fn_register.rs index 2f0684f1..4a8ac472 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -2,10 +2,10 @@ #![allow(non_snake_case)] +use crate::ast::FnAccess; use crate::dynamic::{Dynamic, DynamicWriteLock, Variant}; use crate::engine::Engine; use crate::fn_native::{CallableFunction, FnAny, FnCallArgs, NativeCallContext, SendSync}; -use crate::parser::FnAccess; use crate::r#unsafe::unsafe_cast_box; use crate::result::EvalAltResult; use crate::utils::ImmutableString; diff --git a/src/lib.rs b/src/lib.rs index 018a0138..b2dc3c10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,6 +83,24 @@ mod token; mod r#unsafe; mod utils; +/// The system integer type. +/// +/// If the `only_i32` feature is enabled, this will be `i32` instead. +#[cfg(not(feature = "only_i32"))] +pub type INT = i64; + +/// The system integer type. +/// +/// If the `only_i32` feature is not enabled, this will be `i64` instead. +#[cfg(feature = "only_i32")] +pub type INT = i32; + +/// The system floating-point type. +/// +/// Not available under the `no_float` feature. +#[cfg(not(feature = "no_float"))] +pub type FLOAT = f64; + pub use ast::AST; pub use dynamic::Dynamic; pub use engine::{Engine, EvalContext}; @@ -90,11 +108,11 @@ pub use fn_native::{FnPtr, NativeCallContext}; pub use fn_register::{RegisterFn, RegisterResultFn}; pub use module::Module; pub use parse_error::{ParseError, ParseErrorType}; -pub use parser::{ImmutableString, INT}; pub use result::EvalAltResult; pub use scope::Scope; pub use syntax::Expression; pub use token::Position; +pub use utils::ImmutableString; #[cfg(feature = "internals")] pub use utils::{calc_native_fn_hash, calc_script_fn_hash}; @@ -105,9 +123,7 @@ pub(crate) use utils::{calc_native_fn_hash, calc_script_fn_hash}; pub use rhai_codegen::*; #[cfg(not(feature = "no_function"))] -pub use parser::FnAccess; -#[cfg(feature = "no_function")] -pub use parser::FnAccess; +pub use ast::FnAccess; #[cfg(not(feature = "no_function"))] pub use fn_func::Func; @@ -118,9 +134,6 @@ pub use engine::Array; #[cfg(not(feature = "no_object"))] pub use engine::Map; -#[cfg(not(feature = "no_float"))] -pub use parser::FLOAT; - #[cfg(not(feature = "no_module"))] pub use module::ModuleResolver; @@ -151,7 +164,7 @@ pub use token::{get_next_token, parse_string_literal, InputStream, Token, Tokeni #[cfg(feature = "internals")] #[deprecated(note = "this type is volatile and may change")] -pub use parser::{ +pub use ast::{ BinaryExpr, CustomExpr, Expr, FloatWrapper, Ident, IdentX, ReturnType, ScriptFnDef, Stmt, }; diff --git a/src/module/mod.rs b/src/module/mod.rs index 21050777..863a8643 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -1,16 +1,16 @@ //! Module defining external-loaded modules for Rhai. +use crate::ast::FnAccess; use crate::dynamic::{Dynamic, Variant}; use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn, NativeCallContext, SendSync}; use crate::fn_register::by_value as cast_arg; -use crate::parser::FnAccess; use crate::result::EvalAltResult; use crate::token::{Position, Token}; use crate::utils::{ImmutableString, StraightHasherBuilder}; use crate::{calc_native_fn_hash, calc_script_fn_hash, StaticVec}; #[cfg(not(feature = "no_function"))] -use crate::{fn_native::Shared, parser::ScriptFnDef}; +use crate::{fn_native::Shared, ast::ScriptFnDef}; #[cfg(not(feature = "no_module"))] use crate::{ diff --git a/src/optimize.rs b/src/optimize.rs index a9433a3d..e72dba20 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -1,6 +1,6 @@ //! Module implementing the AST optimizer. -use crate::ast::AST; +use crate::ast::{BinaryExpr, CustomExpr, Expr, ScriptFnDef, Stmt, AST}; use crate::dynamic::Dynamic; use crate::engine::{ Engine, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_IS_DEF_FN, KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, @@ -8,13 +8,13 @@ use crate::engine::{ }; use crate::fn_call::run_builtin_binary_op; use crate::module::Module; -use crate::parser::{map_dynamic_to_expr, BinaryExpr, CustomExpr, Expr, ScriptFnDef, Stmt}; +use crate::parser::map_dynamic_to_expr; use crate::scope::{Entry as ScopeEntry, Scope}; use crate::token::{is_valid_identifier, Position}; use crate::{calc_native_fn_hash, StaticVec}; #[cfg(not(feature = "no_function"))] -use crate::parser::ReturnType; +use crate::ast::ReturnType; use crate::stdlib::{ boxed::Box, diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index c848fbe3..3f04b50d 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -1,13 +1,13 @@ #![allow(non_snake_case)] use crate::def_package; -use crate::parser::INT; use crate::plugin::*; +use crate::INT; use crate::{result::EvalAltResult, token::Position}; #[cfg(not(feature = "no_float"))] -use crate::parser::FLOAT; +use crate::FLOAT; #[cfg(feature = "no_std")] #[cfg(not(feature = "no_float"))] diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index cf194682..9f3179a5 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -5,10 +5,11 @@ use crate::def_package; use crate::dynamic::Dynamic; use crate::engine::Array; use crate::fn_native::{FnPtr, NativeCallContext}; -use crate::parser::{ImmutableString, INT}; use crate::plugin::*; use crate::result::EvalAltResult; use crate::token::Position; +use crate::utils::ImmutableString; +use crate::INT; #[cfg(not(feature = "no_object"))] use crate::engine::Map; @@ -40,12 +41,12 @@ macro_rules! gen_array_functions { } #[rhai_fn(return_raw)] - pub fn pad(context: NativeCallContext, list: &mut Array, len: INT, item: $arg_type) -> Result> { + pub fn pad(_context: NativeCallContext, list: &mut Array, len: INT, item: $arg_type) -> Result> { // Check if array will be over max size limit #[cfg(not(feature = "unchecked"))] - if context.engine().max_array_size() > 0 && len > 0 && (len as usize) > context.engine().max_array_size() { + if _context.engine().max_array_size() > 0 && len > 0 && (len as usize) > _context.engine().max_array_size() { return EvalAltResult::ErrorDataTooLarge( - "Size of array".to_string(), context.engine().max_array_size(), len as usize, Position::none(), + "Size of array".to_string(), _context.engine().max_array_size(), len as usize, Position::none(), ).into(); } diff --git a/src/packages/eval.rs b/src/packages/eval.rs index 36d5b315..68012dc5 100644 --- a/src/packages/eval.rs +++ b/src/packages/eval.rs @@ -1,8 +1,8 @@ use crate::def_package; use crate::dynamic::Dynamic; -use crate::parser::ImmutableString; use crate::plugin::*; use crate::result::EvalAltResult; +use crate::utils::ImmutableString; def_package!(crate:EvalPackage:"Disable 'eval'.", lib, { combine_with_exported_module!(lib, "eval", eval_override); diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index b6f26fff..a9ff0f10 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -1,7 +1,7 @@ use crate::def_package; use crate::dynamic::Variant; -use crate::parser::INT; use crate::result::EvalAltResult; +use crate::INT; use crate::stdlib::{ boxed::Box, diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index ae645fce..045e55c0 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -3,8 +3,9 @@ use crate::def_package; use crate::dynamic::Dynamic; use crate::engine::Map; -use crate::parser::{ImmutableString, INT}; use crate::plugin::*; +use crate::utils::ImmutableString; +use crate::INT; #[cfg(not(feature = "no_index"))] use crate::engine::Array; diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 7b905ca9..67ae918e 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -1,12 +1,12 @@ #![allow(non_snake_case)] use crate::def_package; -use crate::parser::INT; use crate::plugin::*; use crate::token::Position; +use crate::INT; #[cfg(not(feature = "no_float"))] -use crate::parser::FLOAT; +use crate::FLOAT; #[cfg(not(feature = "no_float"))] use crate::result::EvalAltResult; @@ -111,7 +111,7 @@ mod int_functions { #[cfg(not(feature = "no_float"))] #[export_module] mod trig_functions { - use crate::parser::FLOAT; + use crate::FLOAT; pub fn sin(x: FLOAT) -> FLOAT { x.to_radians().sin() @@ -154,7 +154,7 @@ mod trig_functions { #[cfg(not(feature = "no_float"))] #[export_module] mod float_functions { - use crate::parser::FLOAT; + use crate::FLOAT; pub fn sqrt(x: FLOAT) -> FLOAT { x.sqrt() diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index 988e1dac..e02a1a20 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -3,8 +3,9 @@ use crate::def_package; use crate::engine::{FN_TO_STRING, KEYWORD_DEBUG, KEYWORD_PRINT}; use crate::fn_native::FnPtr; -use crate::parser::{ImmutableString, INT}; use crate::plugin::*; +use crate::utils::ImmutableString; +use crate::INT; #[cfg(not(feature = "no_index"))] use crate::engine::Array; diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 9130a9d3..7a76d91e 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -3,9 +3,10 @@ use crate::def_package; use crate::dynamic::Dynamic; use crate::fn_native::FnPtr; -use crate::parser::{ImmutableString, INT}; use crate::plugin::*; +use crate::utils::ImmutableString; use crate::StaticVec; +use crate::INT; #[cfg(not(feature = "unchecked"))] use crate::{result::EvalAltResult, token::Position}; diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index 706233e2..f55288f2 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -4,12 +4,12 @@ use super::{arithmetic::make_err as make_arithmetic_err, math_basic::MAX_INT}; use crate::def_package; use crate::dynamic::Dynamic; -use crate::parser::INT; use crate::plugin::*; use crate::result::EvalAltResult; +use crate::INT; #[cfg(not(feature = "no_float"))] -use crate::parser::FLOAT; +use crate::FLOAT; use crate::stdlib::boxed::Box; diff --git a/src/parser.rs b/src/parser.rs index 828c8c43..d2a9d2f1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,35 +1,36 @@ //! Main module defining the lexer and parser. -use crate::ast::AST; +use crate::ast::{BinaryExpr, CustomExpr, Expr, Ident, IdentX, ReturnType, ScriptFnDef, Stmt, AST}; use crate::dynamic::{Dynamic, Union}; use crate::engine::{Engine, KEYWORD_THIS, MARKER_BLOCK, MARKER_EXPR, MARKER_IDENT}; -use crate::fn_native::{FnPtr, Shared}; -use crate::module::{Module, ModuleRef}; +use crate::module::ModuleRef; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::parse_error::{LexError, ParseError, ParseErrorType}; use crate::scope::{EntryType as ScopeEntryType, Scope}; -use crate::syntax::{CustomSyntax, FnCustomSyntaxEval}; +use crate::syntax::CustomSyntax; use crate::token::{is_keyword_function, is_valid_identifier, Position, Token, TokenStream}; use crate::utils::StraightHasherBuilder; use crate::{calc_script_fn_hash, StaticVec}; -#[cfg(not(feature = "no_index"))] -use crate::engine::Array; +#[cfg(not(feature = "no_float"))] +use crate::ast::FloatWrapper; #[cfg(not(feature = "no_object"))] -use crate::engine::{make_getter, make_setter, Map, KEYWORD_EVAL, KEYWORD_FN_PTR}; +use crate::engine::{make_getter, make_setter, KEYWORD_EVAL, KEYWORD_FN_PTR}; #[cfg(not(feature = "no_function"))] -use crate::engine::{FN_ANONYMOUS, KEYWORD_FN_PTR_CURRY}; +use crate::{ + ast::FnAccess, + engine::{FN_ANONYMOUS, KEYWORD_FN_PTR_CURRY}, + utils::ImmutableString, +}; use crate::stdlib::{ - any::TypeId, borrow::Cow, boxed::Box, - char, collections::HashMap, - fmt, format, - hash::{Hash, Hasher}, + format, + hash::Hash, iter::empty, num::NonZeroUsize, string::{String, ToString}, @@ -37,138 +38,21 @@ use crate::stdlib::{ vec::Vec, }; +#[cfg(not(feature = "no_function"))] +use crate::stdlib::hash::Hasher; + #[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_function"))] use crate::stdlib::collections::hash_map::DefaultHasher; -#[cfg(not(feature = "no_closure"))] -use crate::stdlib::collections::HashSet; - #[cfg(feature = "no_std")] #[cfg(not(feature = "no_function"))] use ahash::AHasher; -/// The system integer type. -/// -/// If the `only_i32` feature is enabled, this will be `i32` instead. -#[cfg(not(feature = "only_i32"))] -pub type INT = i64; - -/// The system integer type. -/// -/// If the `only_i32` feature is not enabled, this will be `i64` instead. -#[cfg(feature = "only_i32")] -pub type INT = i32; - -/// The system floating-point type. -/// -/// Not available under the `no_float` feature. -#[cfg(not(feature = "no_float"))] -pub type FLOAT = f64; - type PERR = ParseErrorType; -pub use crate::utils::ImmutableString; - type FunctionsLib = HashMap; -/// A type representing the access mode of a scripted function. -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -pub enum FnAccess { - /// Public function. - Public, - /// Private function. - Private, -} - -impl fmt::Display for FnAccess { - #[inline(always)] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Private => write!(f, "private"), - Self::Public => write!(f, "public"), - } - } -} - -impl FnAccess { - /// Is this access mode private? - #[inline(always)] - pub fn is_private(self) -> bool { - match self { - Self::Public => false, - Self::Private => true, - } - } - /// Is this access mode public? - #[inline(always)] - pub fn is_public(self) -> bool { - match self { - Self::Public => true, - Self::Private => false, - } - } -} - -/// _[INTERNALS]_ A type containing information on a scripted function. -/// Exported under the `internals` feature only. -/// -/// ## WARNING -/// -/// This type is volatile and may change. -#[derive(Debug, Clone)] -pub struct ScriptFnDef { - /// Function name. - pub name: ImmutableString, - /// Function access mode. - pub access: FnAccess, - /// Names of function parameters. - pub params: StaticVec, - /// Access to external variables. - #[cfg(not(feature = "no_closure"))] - pub externals: HashSet, - /// Function body. - pub body: Stmt, - /// Position of the function definition. - pub pos: Position, - /// Encapsulated running environment, if any. - pub lib: Option>, -} - -impl fmt::Display for ScriptFnDef { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{}{}({})", - if self.access.is_private() { - "private " - } else { - "" - }, - self.name, - self.params - .iter() - .map(|s| s.as_str()) - .collect::>() - .join(",") - ) - } -} - -/// _[INTERNALS]_ A type encapsulating the mode of a `return`/`throw` statement. -/// Exported under the `internals` feature only. -/// -/// ## WARNING -/// -/// This type is volatile and may change. -#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)] -pub enum ReturnType { - /// `return` statement. - Return, - /// `throw` statement. - Exception, -} - #[derive(Clone)] struct ParseState<'e> { /// Reference to the scripting `Engine`. @@ -317,664 +201,6 @@ impl ParseSettings { } } } - -/// An identifier containing a string name and a position. -#[derive(Debug, Clone, Hash)] -pub struct Ident { - pub name: String, - pub pos: Position, -} - -impl Ident { - /// Create a new `Identifier`. - pub fn new(name: String, pos: Position) -> Self { - Self { name, pos } - } -} - -/// An identifier containing an immutable name and a position. -#[derive(Debug, Clone, Hash)] -pub struct IdentX { - pub name: ImmutableString, - pub pos: Position, -} - -impl IdentX { - /// Create a new `Identifier`. - pub fn new(name: impl Into, pos: Position) -> Self { - Self { - name: name.into(), - pos, - } - } -} - -/// _[INTERNALS]_ A Rhai statement. -/// 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. -#[derive(Debug, Clone, Hash)] -pub enum Stmt { - /// No-op. - Noop(Position), - /// if expr { stmt } else { stmt } - IfThenElse(Expr, Box<(Stmt, Option)>, Position), - /// while expr { stmt } - While(Expr, Box, Position), - /// loop { stmt } - Loop(Box, Position), - /// for id in expr { stmt } - For(Expr, Box<(String, Stmt)>, Position), - /// let id = expr - Let(Box, Option, Position), - /// const id = expr - Const(Box, Option, Position), - /// expr op= expr - Assignment(Box<(Expr, Cow<'static, str>, Expr)>, Position), - /// { stmt; ... } - Block(Vec, Position), - /// try { stmt; ... } catch ( var ) { stmt; ... } - TryCatch(Box<(Stmt, Option, Stmt, (Position, Position))>), - /// expr - Expr(Expr), - /// continue - Continue(Position), - /// break - Break(Position), - /// return/throw - ReturnWithVal((ReturnType, Position), Option, Position), - /// import expr as var - #[cfg(not(feature = "no_module"))] - Import(Expr, Option>, Position), - /// export var as var, ... - #[cfg(not(feature = "no_module"))] - Export(Vec<(Ident, Option)>, Position), - /// Convert a variable to shared. - #[cfg(not(feature = "no_closure"))] - Share(Ident), -} - -impl Default for Stmt { - #[inline(always)] - fn default() -> Self { - Self::Noop(Default::default()) - } -} - -impl Stmt { - /// Is this statement `Noop`? - pub fn is_noop(&self) -> bool { - match self { - Self::Noop(_) => true, - _ => false, - } - } - - /// Get the `Position` of this statement. - pub fn position(&self) -> Position { - match self { - Self::Noop(pos) - | Self::Continue(pos) - | Self::Break(pos) - | Self::Block(_, pos) - | Self::Assignment(_, pos) - | Self::IfThenElse(_, _, pos) - | Self::While(_, _, pos) - | Self::Loop(_, pos) - | Self::For(_, _, pos) - | Self::ReturnWithVal((_, pos), _, _) => *pos, - - Self::Let(x, _, _) | Self::Const(x, _, _) => x.pos, - Self::TryCatch(x) => (x.3).0, - - Self::Expr(x) => x.position(), - - #[cfg(not(feature = "no_module"))] - Self::Import(_, _, pos) => *pos, - #[cfg(not(feature = "no_module"))] - Self::Export(_, pos) => *pos, - - #[cfg(not(feature = "no_closure"))] - Self::Share(Ident { pos, .. }) => *pos, - } - } - - /// Override the `Position` of this statement. - pub fn set_position(&mut self, new_pos: Position) -> &mut Self { - match self { - Self::Noop(pos) - | Self::Continue(pos) - | Self::Break(pos) - | Self::Block(_, pos) - | Self::Assignment(_, pos) - | Self::IfThenElse(_, _, pos) - | Self::While(_, _, pos) - | Self::Loop(_, pos) - | Self::For(_, _, pos) - | Self::ReturnWithVal((_, pos), _, _) => *pos = new_pos, - - Self::Let(x, _, _) | Self::Const(x, _, _) => x.pos = new_pos, - Self::TryCatch(x) => (x.3).0 = new_pos, - - Self::Expr(x) => { - x.set_position(new_pos); - } - - #[cfg(not(feature = "no_module"))] - Self::Import(_, _, pos) => *pos = new_pos, - #[cfg(not(feature = "no_module"))] - Self::Export(_, pos) => *pos = new_pos, - - #[cfg(not(feature = "no_closure"))] - Self::Share(Ident { pos, .. }) => *pos = new_pos, - } - - self - } - - /// Is this statement self-terminated (i.e. no need for a semicolon terminator)? - pub fn is_self_terminated(&self) -> bool { - match self { - Self::IfThenElse(_, _, _) - | Self::While(_, _, _) - | Self::Loop(_, _) - | Self::For(_, _, _) - | Self::Block(_, _) - | Self::TryCatch(_) => true, - - // A No-op requires a semicolon in order to know it is an empty statement! - Self::Noop(_) => false, - - Self::Let(_, _, _) - | Self::Const(_, _, _) - | Self::Assignment(_, _) - | Self::Expr(_) - | Self::Continue(_) - | Self::Break(_) - | Self::ReturnWithVal(_, _, _) => false, - - #[cfg(not(feature = "no_module"))] - Self::Import(_, _, _) | Self::Export(_, _) => false, - - #[cfg(not(feature = "no_closure"))] - Self::Share(_) => false, - } - } - - /// Is this statement _pure_? - pub fn is_pure(&self) -> bool { - match self { - Self::Noop(_) => true, - Self::Expr(expr) => expr.is_pure(), - Self::IfThenElse(condition, x, _) if x.1.is_some() => { - condition.is_pure() && x.0.is_pure() && x.1.as_ref().unwrap().is_pure() - } - Self::IfThenElse(condition, x, _) => condition.is_pure() && x.0.is_pure(), - Self::While(condition, block, _) => condition.is_pure() && block.is_pure(), - Self::Loop(block, _) => block.is_pure(), - Self::For(iterable, x, _) => iterable.is_pure() && x.1.is_pure(), - Self::Let(_, _, _) | Self::Const(_, _, _) | Self::Assignment(_, _) => false, - Self::Block(block, _) => block.iter().all(|stmt| stmt.is_pure()), - Self::Continue(_) | Self::Break(_) | Self::ReturnWithVal(_, _, _) => false, - Self::TryCatch(x) => x.0.is_pure() && x.2.is_pure(), - - #[cfg(not(feature = "no_module"))] - Self::Import(_, _, _) => false, - #[cfg(not(feature = "no_module"))] - Self::Export(_, _) => false, - - #[cfg(not(feature = "no_closure"))] - Self::Share(_) => false, - } - } -} - -/// _[INTERNALS]_ A type wrapping a custom syntax definition. -/// Exported under the `internals` feature only. -/// -/// ## WARNING -/// -/// This type is volatile and may change. -#[derive(Clone)] -pub struct CustomExpr { - pub(crate) keywords: StaticVec, - pub(crate) func: Shared, - pub(crate) pos: Position, -} - -impl fmt::Debug for CustomExpr { - #[inline(always)] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&self.keywords, f) - } -} - -impl Hash for CustomExpr { - #[inline(always)] - fn hash(&self, state: &mut H) { - self.keywords.hash(state); - } -} - -impl CustomExpr { - /// Get the keywords for this `CustomExpr`. - #[inline(always)] - pub fn keywords(&self) -> &[Expr] { - &self.keywords - } - /// Get the implementation function for this `CustomExpr`. - #[inline(always)] - pub fn func(&self) -> &FnCustomSyntaxEval { - self.func.as_ref() - } - /// Get the position of this `CustomExpr`. - #[inline(always)] - pub fn position(&self) -> Position { - self.pos - } -} - -/// _[INTERNALS]_ A type wrapping a floating-point number. -/// Exported under the `internals` feature only. -/// -/// This type is mainly used to provide a standard `Hash` implementation -/// to floating-point numbers, allowing `Expr` to derive `Hash` automatically. -/// -/// ## WARNING -/// -/// This type is volatile and may change. -#[cfg(not(feature = "no_float"))] -#[derive(Debug, PartialEq, PartialOrd, Clone)] -pub struct FloatWrapper(pub FLOAT, pub Position); - -#[cfg(not(feature = "no_float"))] -impl Hash for FloatWrapper { - #[inline(always)] - fn hash(&self, state: &mut H) { - state.write(&self.0.to_le_bytes()); - self.1.hash(state); - } -} - -/// A binary expression structure. -#[derive(Debug, Clone, Hash)] -pub struct BinaryExpr { - pub lhs: Expr, - pub rhs: Expr, - pub pos: Position, -} - -/// _[INTERNALS]_ An expression sub-tree. -/// 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 -/// -/// This type is volatile and may change. -#[derive(Debug, Clone, Hash)] -pub enum Expr { - /// Integer constant. - IntegerConstant(Box<(INT, Position)>), - /// Floating-point constant. - #[cfg(not(feature = "no_float"))] - FloatConstant(Box), - /// Character constant. - CharConstant(Box<(char, Position)>), - /// String constant. - StringConstant(Box), - /// FnPtr constant. - FnPointer(Box), - /// Variable access - ((variable name, position), optional modules, hash, optional index) - Variable(Box<(Ident, Option>, u64, Option)>), - /// Property access. - Property(Box<((ImmutableString, String, String), Position)>), - /// { stmt } - Stmt(Box<(Stmt, Position)>), - /// Wrapped expression - should not be optimized away. - Expr(Box), - /// func(expr, ... ) - ((function name, native_only, capture, position), optional modules, hash, arguments, optional default value) - /// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls - /// and the function names are predictable, so no need to allocate a new `String`. - FnCall( - Box<( - (Cow<'static, str>, bool, bool, Position), - Option>, - u64, - StaticVec, - Option, // Default value is `bool` in order for `Expr` to be `Hash`. - )>, - ), - /// lhs.rhs - Dot(Box), - /// expr[expr] - Index(Box), - /// [ expr, ... ] - Array(Box<(StaticVec, Position)>), - /// #{ name:expr, ... } - Map(Box<(StaticVec<(IdentX, Expr)>, Position)>), - /// lhs in rhs - In(Box), - /// lhs && rhs - And(Box), - /// lhs || rhs - Or(Box), - /// true - True(Position), - /// false - False(Position), - /// () - Unit(Position), - /// Custom syntax - Custom(Box), -} - -impl Default for Expr { - #[inline(always)] - fn default() -> Self { - Self::Unit(Default::default()) - } -} - -impl Expr { - /// Get the type of an expression. - /// - /// Returns `None` if the expression's result type is not constant. - pub fn get_type_id(&self) -> Option { - Some(match self { - Self::Expr(x) => return x.get_type_id(), - - Self::IntegerConstant(_) => TypeId::of::(), - #[cfg(not(feature = "no_float"))] - Self::FloatConstant(_) => TypeId::of::(), - Self::CharConstant(_) => TypeId::of::(), - Self::StringConstant(_) => TypeId::of::(), - Self::FnPointer(_) => TypeId::of::(), - Self::True(_) | Self::False(_) | Self::In(_) | Self::And(_) | Self::Or(_) => { - TypeId::of::() - } - Self::Unit(_) => TypeId::of::<()>(), - - #[cfg(not(feature = "no_index"))] - Self::Array(_) => TypeId::of::(), - - #[cfg(not(feature = "no_object"))] - Self::Map(_) => TypeId::of::(), - - _ => return None, - }) - } - - /// Get the `Dynamic` value of a constant expression. - /// - /// Returns `None` if the expression is not constant. - pub fn get_constant_value(&self) -> Option { - Some(match self { - Self::Expr(x) => return x.get_constant_value(), - - Self::IntegerConstant(x) => x.0.into(), - #[cfg(not(feature = "no_float"))] - Self::FloatConstant(x) => x.0.into(), - Self::CharConstant(x) => x.0.into(), - Self::StringConstant(x) => x.name.clone().into(), - Self::FnPointer(x) => Dynamic(Union::FnPtr(Box::new(FnPtr::new_unchecked( - x.name.clone(), - Default::default(), - )))), - Self::True(_) => true.into(), - Self::False(_) => false.into(), - Self::Unit(_) => ().into(), - - #[cfg(not(feature = "no_index"))] - Self::Array(x) if x.0.iter().all(Self::is_constant) => Dynamic(Union::Array(Box::new( - x.0.iter() - .map(|v| v.get_constant_value().unwrap()) - .collect(), - ))), - - #[cfg(not(feature = "no_object"))] - Self::Map(x) if x.0.iter().all(|(_, v)| v.is_constant()) => { - Dynamic(Union::Map(Box::new( - x.0.iter() - .map(|(k, v)| (k.name.clone(), v.get_constant_value().unwrap())) - .collect(), - ))) - } - - _ => return None, - }) - } - - /// Is the expression a simple variable access? - pub(crate) fn get_variable_access(&self, non_qualified: bool) -> Option<&str> { - match self { - Self::Variable(x) if !non_qualified || x.1.is_none() => Some((x.0).name.as_str()), - _ => None, - } - } - - /// Get the `Position` of the expression. - pub fn position(&self) -> Position { - match self { - Self::Expr(x) => x.position(), - - #[cfg(not(feature = "no_float"))] - Self::FloatConstant(x) => x.1, - - Self::IntegerConstant(x) => x.1, - Self::CharConstant(x) => x.1, - Self::StringConstant(x) => x.pos, - Self::FnPointer(x) => x.pos, - Self::Array(x) => x.1, - Self::Map(x) => x.1, - Self::Property(x) => x.1, - Self::Stmt(x) => x.1, - Self::Variable(x) => (x.0).pos, - Self::FnCall(x) => (x.0).3, - - Self::And(x) | Self::Or(x) | Self::In(x) => x.pos, - - Self::True(pos) | Self::False(pos) | Self::Unit(pos) => *pos, - - Self::Dot(x) | Self::Index(x) => x.lhs.position(), - - Self::Custom(x) => x.pos, - } - } - - /// Override the `Position` of the expression. - pub fn set_position(&mut self, new_pos: Position) -> &mut Self { - match self { - Self::Expr(x) => { - x.set_position(new_pos); - } - - #[cfg(not(feature = "no_float"))] - Self::FloatConstant(x) => x.1 = new_pos, - - Self::IntegerConstant(x) => x.1 = new_pos, - Self::CharConstant(x) => x.1 = new_pos, - Self::StringConstant(x) => x.pos = new_pos, - Self::FnPointer(x) => x.pos = new_pos, - Self::Array(x) => x.1 = new_pos, - Self::Map(x) => x.1 = new_pos, - Self::Variable(x) => (x.0).pos = new_pos, - Self::Property(x) => x.1 = new_pos, - Self::Stmt(x) => x.1 = new_pos, - Self::FnCall(x) => (x.0).3 = new_pos, - Self::And(x) | Self::Or(x) | Self::In(x) => x.pos = new_pos, - Self::True(pos) | Self::False(pos) | Self::Unit(pos) => *pos = new_pos, - Self::Dot(x) | Self::Index(x) => x.pos = new_pos, - Self::Custom(x) => x.pos = new_pos, - } - - self - } - - /// Is the expression pure? - /// - /// A pure expression has no side effects. - pub fn is_pure(&self) -> bool { - match self { - Self::Expr(x) => x.is_pure(), - - Self::Array(x) => x.0.iter().all(Self::is_pure), - - Self::Index(x) | Self::And(x) | Self::Or(x) | Self::In(x) => { - x.lhs.is_pure() && x.rhs.is_pure() - } - - Self::Stmt(x) => x.0.is_pure(), - - Self::Variable(_) => true, - - _ => self.is_constant(), - } - } - - /// Is the expression the unit `()` literal? - #[inline(always)] - pub fn is_unit(&self) -> bool { - match self { - Self::Unit(_) => true, - _ => false, - } - } - - /// Is the expression a simple constant literal? - pub fn is_literal(&self) -> bool { - match self { - Self::Expr(x) => x.is_literal(), - - #[cfg(not(feature = "no_float"))] - Self::FloatConstant(_) => true, - - Self::IntegerConstant(_) - | Self::CharConstant(_) - | Self::StringConstant(_) - | Self::FnPointer(_) - | Self::True(_) - | Self::False(_) - | Self::Unit(_) => true, - - // An array literal is literal if all items are literals - Self::Array(x) => x.0.iter().all(Self::is_literal), - - // An map literal is literal if all items are literals - Self::Map(x) => x.0.iter().map(|(_, expr)| expr).all(Self::is_literal), - - // Check in expression - Self::In(x) => match (&x.lhs, &x.rhs) { - (Self::StringConstant(_), Self::StringConstant(_)) - | (Self::CharConstant(_), Self::StringConstant(_)) => true, - _ => false, - }, - - _ => false, - } - } - - /// Is the expression a constant? - pub fn is_constant(&self) -> bool { - match self { - Self::Expr(x) => x.is_constant(), - - #[cfg(not(feature = "no_float"))] - Self::FloatConstant(_) => true, - - Self::IntegerConstant(_) - | Self::CharConstant(_) - | Self::StringConstant(_) - | Self::FnPointer(_) - | Self::True(_) - | Self::False(_) - | Self::Unit(_) => true, - - // An array literal is constant if all items are constant - Self::Array(x) => x.0.iter().all(Self::is_constant), - - // An map literal is constant if all items are constant - Self::Map(x) => x.0.iter().map(|(_, expr)| expr).all(Self::is_constant), - - // Check in expression - Self::In(x) => match (&x.lhs, &x.rhs) { - (Self::StringConstant(_), Self::StringConstant(_)) - | (Self::CharConstant(_), Self::StringConstant(_)) => true, - _ => false, - }, - - _ => false, - } - } - - /// Is a particular token allowed as a postfix operator to this expression? - pub fn is_valid_postfix(&self, token: &Token) -> bool { - match self { - Self::Expr(x) => x.is_valid_postfix(token), - - #[cfg(not(feature = "no_float"))] - Self::FloatConstant(_) => false, - - Self::IntegerConstant(_) - | Self::CharConstant(_) - | Self::FnPointer(_) - | Self::In(_) - | Self::And(_) - | Self::Or(_) - | Self::True(_) - | Self::False(_) - | Self::Unit(_) => false, - - Self::StringConstant(_) - | Self::Stmt(_) - | Self::FnCall(_) - | Self::Dot(_) - | Self::Index(_) - | Self::Array(_) - | Self::Map(_) => match token { - #[cfg(not(feature = "no_index"))] - Token::LeftBracket => true, - _ => false, - }, - - Self::Variable(_) => match token { - #[cfg(not(feature = "no_index"))] - Token::LeftBracket => true, - Token::LeftParen => true, - Token::Bang => true, - Token::DoubleColon => true, - _ => false, - }, - - Self::Property(_) => match token { - #[cfg(not(feature = "no_index"))] - Token::LeftBracket => true, - Token::LeftParen => true, - _ => false, - }, - - Self::Custom(_) => false, - } - } - - /// Convert a `Variable` into a `Property`. All other variants are untouched. - #[cfg(not(feature = "no_object"))] - #[inline] - pub(crate) fn into_property(self) -> Self { - match self { - Self::Variable(x) if x.1.is_none() => { - let Ident { name, pos } = x.0; - let getter = make_getter(&name); - let setter = make_setter(&name); - Self::Property(Box::new(((name.into(), getter, setter), pos))) - } - _ => self, - } - } -} - /// Consume a particular token, checking that it is the expected one. fn eat_token(input: &mut TokenStream, token: Token) -> Position { let (t, pos) = input.next().unwrap(); @@ -1737,10 +963,9 @@ fn parse_unary( .map(|i| Expr::IntegerConstant(Box::new((i, pos)))) .or_else(|| { #[cfg(not(feature = "no_float"))] - return Some(Expr::FloatConstant(Box::new(FloatWrapper( - -(x.0 as FLOAT), - pos, - )))); + return Some(Expr::FloatConstant(Box::new( + -Into::::into(*x), + ))); #[cfg(feature = "no_float")] return None; }) @@ -1749,9 +974,7 @@ fn parse_unary( // Negative float #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(x) => { - Ok(Expr::FloatConstant(Box::new(FloatWrapper(-x.0, x.1)))) - } + Expr::FloatConstant(x) => Ok(Expr::FloatConstant(Box::new(-(*x)))), // Call negative function expr => { diff --git a/src/plugin.rs b/src/plugin.rs index 168de561..7bce9b5c 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,11 +1,11 @@ //! Module defining macros for developing _plugins_. +pub use crate::ast::FnAccess; pub use crate::dynamic::Dynamic; pub use crate::engine::Engine; pub use crate::fn_native::{CallableFunction, FnCallArgs, NativeCallContext}; pub use crate::fn_register::{RegisterFn, RegisterResultFn}; pub use crate::module::Module; -pub use crate::parser::FnAccess; pub use crate::result::EvalAltResult; pub use crate::utils::ImmutableString; diff --git a/src/result.rs b/src/result.rs index f0ec841b..1a42c118 100644 --- a/src/result.rs +++ b/src/result.rs @@ -2,9 +2,9 @@ use crate::dynamic::Dynamic; use crate::parse_error::ParseErrorType; -use crate::parser::INT; use crate::token::Position; use crate::utils::ImmutableString; +use crate::INT; #[cfg(not(feature = "no_function"))] use crate::engine::is_anonymous_fn; diff --git a/src/scope.rs b/src/scope.rs index 05188843..13442a3b 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -1,7 +1,8 @@ //! Module that defines the `Scope` type representing a function call-stack scope. +use crate::ast::Expr; use crate::dynamic::{Dynamic, Variant}; -use crate::parser::{map_dynamic_to_expr, Expr}; +use crate::parser::map_dynamic_to_expr; use crate::token::Position; use crate::stdlib::{borrow::Cow, boxed::Box, iter, string::String, vec::Vec}; diff --git a/src/syntax.rs b/src/syntax.rs index 1aefdc15..4d0a6519 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -1,10 +1,10 @@ //! Module implementing custom syntax for `Engine`. +use crate::ast::Expr; use crate::dynamic::Dynamic; use crate::engine::{Engine, EvalContext, MARKER_BLOCK, MARKER_EXPR, MARKER_IDENT}; use crate::fn_native::{SendSync, Shared}; use crate::parse_error::{LexError, ParseError}; -use crate::parser::Expr; use crate::result::EvalAltResult; use crate::token::{is_valid_identifier, Position, Token}; use crate::utils::ImmutableString; diff --git a/src/token.rs b/src/token.rs index 044201b1..3e4baba9 100644 --- a/src/token.rs +++ b/src/token.rs @@ -9,11 +9,11 @@ use crate::engine::{ use crate::engine::KEYWORD_IS_SHARED; use crate::parse_error::LexError; -use crate::parser::INT; use crate::StaticVec; +use crate::INT; #[cfg(not(feature = "no_float"))] -use crate::parser::FLOAT; +use crate::FLOAT; use crate::stdlib::{ borrow::Cow, diff --git a/src/utils.rs b/src/utils.rs index 451b929a..385c4536 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -42,14 +42,6 @@ impl Hasher for StraightHasher { } } -impl StraightHasher { - /// Create a `StraightHasher`. - #[inline(always)] - pub fn new() -> Self { - Self(0) - } -} - /// A hash builder for `StraightHasher`. #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Default)] pub struct StraightHasherBuilder; @@ -59,7 +51,7 @@ impl BuildHasher for StraightHasherBuilder { #[inline(always)] fn build_hasher(&self) -> Self::Hasher { - StraightHasher::new() + Default::default() } }