From a8be700b9f6ae93c431fe69db0a8a49962ca8542 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 7 Mar 2020 13:39:28 +0800 Subject: [PATCH] Simplify op-assignment. --- src/parser.rs | 137 ++++++++++++++++++-------------------------------- 1 file changed, 49 insertions(+), 88 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 328b496e..f6fa43df 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1310,6 +1310,44 @@ fn parse_assignment(lhs: Expr, rhs: Expr, pos: Position) -> Result Result { + let lhs_copy = lhs.clone(); + + parse_assignment( + lhs, + Expr::FunctionCall(function.into(), vec![lhs_copy, rhs], None, pos), + pos, + ) + + /* + const LHS_VALUE: &'static str = "@LHS_VALUE@"; + + let lhs_pos = lhs.position(); + + Ok(Expr::Block( + Box::new(Stmt::Block(vec![ + Stmt::Let(LHS_VALUE.to_string(), Some(Box::new(lhs)), lhs_pos), + Stmt::Expr(Box::new(parse_assignment( + lhs, + Expr::FunctionCall( + function.into(), + vec![Expr::Identifier(LHS_VALUE.to_string(), lhs_pos), rhs], + None, + pos, + ), + pos, + )?)), + ])), + pos, + )) + */ +} + fn parse_binary_op<'a>( input: &mut Peekable>, precedence: i8, @@ -1355,22 +1393,8 @@ fn parse_binary_op<'a>( Token::Divide => Expr::FunctionCall("/".into(), vec![current_lhs, rhs], None, pos), Token::Equals => parse_assignment(current_lhs, rhs, pos)?, - Token::PlusAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall("+".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } - Token::MinusAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall("-".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } + Token::PlusAssign => parse_op_assignment("+", current_lhs, rhs, pos)?, + Token::MinusAssign => parse_op_assignment("-", current_lhs, rhs, pos)?, Token::Period => Expr::Dot(Box::new(current_lhs), Box::new(rhs), pos), // Comparison operators default to false when passed invalid operands @@ -1414,46 +1438,11 @@ fn parse_binary_op<'a>( Token::Or => Expr::Or(Box::new(current_lhs), Box::new(rhs)), Token::And => Expr::And(Box::new(current_lhs), Box::new(rhs)), Token::XOr => Expr::FunctionCall("^".into(), vec![current_lhs, rhs], None, pos), - Token::OrAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall("|".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } - Token::AndAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall("&".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } - Token::XOrAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall("^".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } - Token::MultiplyAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall("*".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } - Token::DivideAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall("/".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } + Token::OrAssign => parse_op_assignment("|", current_lhs, rhs, pos)?, + Token::AndAssign => parse_op_assignment("&", current_lhs, rhs, pos)?, + Token::XOrAssign => parse_op_assignment("^", current_lhs, rhs, pos)?, + Token::MultiplyAssign => parse_op_assignment("*", current_lhs, rhs, pos)?, + Token::DivideAssign => parse_op_assignment("/", current_lhs, rhs, pos)?, Token::Pipe => Expr::FunctionCall("|".into(), vec![current_lhs, rhs], None, pos), Token::LeftShift => { Expr::FunctionCall("<<".into(), vec![current_lhs, rhs], None, pos) @@ -1461,43 +1450,15 @@ fn parse_binary_op<'a>( Token::RightShift => { Expr::FunctionCall(">>".into(), vec![current_lhs, rhs], None, pos) } - Token::LeftShiftAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall("<<".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } - Token::RightShiftAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall(">>".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } + Token::LeftShiftAssign => parse_op_assignment("<<", current_lhs, rhs, pos)?, + Token::RightShiftAssign => parse_op_assignment(">>", current_lhs, rhs, pos)?, Token::Ampersand => { Expr::FunctionCall("&".into(), vec![current_lhs, rhs], None, pos) } Token::Modulo => Expr::FunctionCall("%".into(), vec![current_lhs, rhs], None, pos), - Token::ModuloAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall("%".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } + Token::ModuloAssign => parse_op_assignment("%", current_lhs, rhs, pos)?, Token::PowerOf => Expr::FunctionCall("~".into(), vec![current_lhs, rhs], None, pos), - Token::PowerOfAssign => { - let lhs_copy = current_lhs.clone(); - parse_assignment( - current_lhs, - Expr::FunctionCall("~".into(), vec![lhs_copy, rhs], None, pos), - pos, - )? - } + Token::PowerOfAssign => parse_op_assignment("~", current_lhs, rhs, pos)?, token => { return Err(ParseError::new( PERR::UnknownOperator(token.syntax().into()),