Add OpAssignment::new_from_token.

This commit is contained in:
Stephen Chung 2021-12-17 16:55:24 +08:00
parent 8d67c70294
commit dc6896fbe2
5 changed files with 23 additions and 17 deletions

View File

@ -30,19 +30,27 @@ impl OpAssignment<'_> {
/// ///
/// # Panics /// # Panics
/// ///
/// Panics if the operator name is not an op-assignment operator. /// Panics if the name is not an op-assignment operator.
#[must_use] #[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 let op_raw = op
.map_op_assignment() .map_op_assignment()
.expect("op-assignment") .expect("op-assignment operator")
.literal_syntax(); .literal_syntax();
let op_assignment = op.literal_syntax();
Self { 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), hash_op: calc_fn_hash(op_raw, 2),
op: op_assignment, op: op.literal_syntax(),
} }
} }
} }

View File

@ -335,20 +335,20 @@ pub const FN_IDX_SET: &str = "index$set$";
pub const FN_ANONYMOUS: &str = "anon$"; pub const FN_ANONYMOUS: &str = "anon$";
/// Standard equality comparison operator. /// Standard equality comparison operator.
pub const OP_EQUALS: &str = "=="; pub const OP_EQUALS: &str = Token::EqualsTo.literal_syntax();
/// Standard method function for containment testing. /// Standard method function for containment testing.
/// The `in` operator is implemented as a call to this method. /// The `in` operator is implemented as a call to this method.
pub const OP_CONTAINS: &str = "contains"; pub const OP_CONTAINS: &str = "contains";
/// Standard exclusive range operator. /// Standard exclusive range operator.
pub const OP_EXCLUSIVE_RANGE: &str = ".."; pub const OP_EXCLUSIVE_RANGE: &str = Token::ExclusiveRange.literal_syntax();
/// Standard inclusive range operator. /// Standard inclusive range operator.
pub const OP_INCLUSIVE_RANGE: &str = "..="; pub const OP_INCLUSIVE_RANGE: &str = Token::InclusiveRange.literal_syntax();
/// Standard concatenation operator token. /// Standard concatenation operator token.
pub const TOKEN_OP_CONCAT: Token = Token::PlusAssign; pub const OP_CONCAT: &str = Token::PlusAssign.literal_syntax();
/// Method of chaining. /// Method of chaining.
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
@ -2312,7 +2312,7 @@ impl Engine {
mods, mods,
state, state,
lib, lib,
Some(OpAssignment::new(TOKEN_OP_CONCAT)), Some(OpAssignment::new(OP_CONCAT)),
pos, pos,
&mut (&mut result).into(), &mut (&mut result).into(),
("", Position::NONE), ("", Position::NONE),

View File

@ -2,7 +2,7 @@
use super::native::{CallableFunction, FnAny}; use super::native::{CallableFunction, FnAny};
use super::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn}; 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::ast::FnCallHashes;
use crate::engine::{ use crate::engine::{
EvalState, FnResolutionCacheEntry, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, EvalState, FnResolutionCacheEntry, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR,

View File

@ -381,9 +381,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b
match x.2 { match x.2 {
Expr::FnCall(ref mut x2, _) => { Expr::FnCall(ref mut x2, _) => {
state.set_dirty(); state.set_dirty();
let op = Token::lookup_from_syntax(&x2.name).expect("operator"); x.1 = Some(OpAssignment::new(&x2.name));
let op_assignment = op.make_op_assignment().expect("operator");
x.1 = Some(OpAssignment::new(op_assignment));
let value = mem::take(&mut x2.args[1]); let value = mem::take(&mut x2.args[1]);

View File

@ -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 { match lhs {
// const_expr = rhs // const_expr = rhs