From dc6896fbe2e26134b0558bba8c7c2dcb58a408a4 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 17 Dec 2021 16:55:24 +0800 Subject: [PATCH] Add OpAssignment::new_from_token. --- src/ast/stmt.rs | 22 +++++++++++++++------- src/engine.rs | 10 +++++----- src/func/call.rs | 2 +- src/optimizer.rs | 4 +--- src/parser.rs | 2 +- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/ast/stmt.rs b/src/ast/stmt.rs index 6cb2d6a5..c09c3ff5 100644 --- a/src/ast/stmt.rs +++ b/src/ast/stmt.rs @@ -30,19 +30,27 @@ impl OpAssignment<'_> { /// /// # Panics /// - /// Panics if the operator name is not an op-assignment operator. + /// Panics if the name is not an op-assignment operator. #[must_use] - pub fn new(op: Token) -> Self { + #[inline(always)] + pub fn new(name: &str) -> Self { + Self::new_from_token(Token::lookup_from_syntax(name).expect("operator")) + } + /// Create a new [`OpAssignment`] from a [`Token`]. + /// + /// # Panics + /// + /// Panics if the token is not an op-assignment operator. + #[must_use] + pub fn new_from_token(op: Token) -> Self { let op_raw = op .map_op_assignment() - .expect("op-assignment") + .expect("op-assignment operator") .literal_syntax(); - let op_assignment = op.literal_syntax(); - Self { - hash_op_assign: calc_fn_hash(op_assignment, 2), + hash_op_assign: calc_fn_hash(op.literal_syntax(), 2), hash_op: calc_fn_hash(op_raw, 2), - op: op_assignment, + op: op.literal_syntax(), } } } diff --git a/src/engine.rs b/src/engine.rs index 49ea0512..e88e76e3 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -335,20 +335,20 @@ pub const FN_IDX_SET: &str = "index$set$"; pub const FN_ANONYMOUS: &str = "anon$"; /// Standard equality comparison operator. -pub const OP_EQUALS: &str = "=="; +pub const OP_EQUALS: &str = Token::EqualsTo.literal_syntax(); /// Standard method function for containment testing. /// The `in` operator is implemented as a call to this method. pub const OP_CONTAINS: &str = "contains"; /// Standard exclusive range operator. -pub const OP_EXCLUSIVE_RANGE: &str = ".."; +pub const OP_EXCLUSIVE_RANGE: &str = Token::ExclusiveRange.literal_syntax(); /// Standard inclusive range operator. -pub const OP_INCLUSIVE_RANGE: &str = "..="; +pub const OP_INCLUSIVE_RANGE: &str = Token::InclusiveRange.literal_syntax(); /// Standard concatenation operator token. -pub const TOKEN_OP_CONCAT: Token = Token::PlusAssign; +pub const OP_CONCAT: &str = Token::PlusAssign.literal_syntax(); /// Method of chaining. #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] @@ -2312,7 +2312,7 @@ impl Engine { mods, state, lib, - Some(OpAssignment::new(TOKEN_OP_CONCAT)), + Some(OpAssignment::new(OP_CONCAT)), pos, &mut (&mut result).into(), ("", Position::NONE), diff --git a/src/func/call.rs b/src/func/call.rs index f78dd894..87478c71 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -2,7 +2,7 @@ use super::native::{CallableFunction, FnAny}; use super::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn}; -use crate::api::limits::defaults::MAX_DYNAMIC_PARAMETERS; +use crate::api::default_limits::MAX_DYNAMIC_PARAMETERS; use crate::ast::FnCallHashes; use crate::engine::{ EvalState, FnResolutionCacheEntry, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, diff --git a/src/optimizer.rs b/src/optimizer.rs index 1526da0b..2ad5bc47 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -381,9 +381,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b match x.2 { Expr::FnCall(ref mut x2, _) => { state.set_dirty(); - let op = Token::lookup_from_syntax(&x2.name).expect("operator"); - let op_assignment = op.make_op_assignment().expect("operator"); - x.1 = Some(OpAssignment::new(op_assignment)); + x.1 = Some(OpAssignment::new(&x2.name)); let value = mem::take(&mut x2.args[1]); diff --git a/src/parser.rs b/src/parser.rs index d670caf3..2d0601ca 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1716,7 +1716,7 @@ fn make_assignment_stmt( } } - let op_info = op.map(OpAssignment::new); + let op_info = op.map(OpAssignment::new_from_token); match lhs { // const_expr = rhs