From ac65856e34e3faee9933b0733dedf341bbd955fd Mon Sep 17 00:00:00 2001 From: Richard Carson Date: Sun, 1 May 2022 01:52:47 -0400 Subject: [PATCH] Fix implied multiplication --- src/grammar.pest | 6 +++--- src/handlers/math.rs | 5 +++++ src/token.rs | 3 +++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/grammar.pest b/src/grammar.pest index f4c2107..ab230b9 100644 --- a/src/grammar.pest +++ b/src/grammar.pest @@ -43,8 +43,7 @@ expression_list = {toplevel_expression ~ (comma ~ toplevel_expression)+} toplevel_expression = {ternary_expression} -ternary_expression = {implied_mul_expression ~ ("?" ~ implied_mul_expression ~ ":" ~ implied_mul_expression)*} -implied_mul_expression = {bool_or_expression ~ bool_or_expression*} +ternary_expression = {bool_or_expression ~ ("?" ~ bool_or_expression ~ ":" ~ bool_or_expression)*} bool_or_expression = {bool_and_expression ~ (bool_or ~ bool_and_expression)*} bool_and_expression = {bool_cmp_expression ~ (bool_and ~ bool_cmp_expression)*} bool_cmp_expression = {or_expression ~ ((lt|gt|eq|ne) ~ or_expression)*} @@ -53,7 +52,8 @@ xor_expression = {and_expression ~ (xor ~ and_expression)*} and_expression = {sh_expression ~ (and ~ sh_expression)*} sh_expression = {as_expression ~ ((lshift|rshift) ~ as_expression)*} as_expression = {md_expression ~ ((plus|minus) ~ md_expression)*} -md_expression = {power_expression ~ ((multiply|divide|modulus) ~ power_expression)*} +md_expression = {implied_mul_expression ~ ((multiply|divide|modulus) ~ implied_mul_expression)*} +implied_mul_expression = {power_expression ~ (!minus ~ power_expression)*} power_expression = {call_expression ~ (power ~ call_expression)*} call_expression = {identifier ~ lparen ~ toplevel_expression ~ rparen | identifier ~ lparen ~ expression_list? ~ rparen | prefix_unary_expression} prefix_unary_expression = {(not|minus)+ ~ postfix_unary_expression | postfix_unary_expression} diff --git a/src/handlers/math.rs b/src/handlers/math.rs index cb66118..c2e683d 100644 --- a/src/handlers/math.rs +++ b/src/handlers/math.rs @@ -236,6 +236,11 @@ pub fn math_expression_handler(token: &mut Token, _state: &mut ParserState) -> O if token.children().len() > 1 { let mut i = 1; while i < token.children().len() { + let next_child = token.child(i).unwrap(); + if next_child.text() == "(" || next_child.text() == ")" { + continue; + } + let ih = IntegerType::checked_mul; let fh = |l: FloatType, r: FloatType| l * r; diff --git a/src/token.rs b/src/token.rs index f0d96cc..27c7976 100644 --- a/src/token.rs +++ b/src/token.rs @@ -462,6 +462,7 @@ mod test_token { token_does_value_equal("4(x)", Value::Integer(64), &mut state); token_does_value_equal("(4)x", Value::Integer(64), &mut state); token_does_value_equal("(2)(2)(2)(2)", Value::Integer(16), &mut state); + token_does_value_equal("(2)(2)(2)(-2)", Value::Integer(-16), &mut state); // add / sub expression token_does_text_equal("2*$2", "$4.00", &mut state); @@ -498,6 +499,8 @@ mod test_token { let t = Token::new("5+5\nfn(x, y) = x * y\n5+5", &mut state).unwrap(); assert_eq!("10\nx * y\n10", t.text); token_does_value_equal("fn(5,5)", Value::Integer(25), &mut state); + let t = Token::new("fn(x, y) = 5x + 10(x * y)\nfn(2, 3)", &mut state).unwrap(); + assert_eq!("5x + 10(x * y)\n70", t.text); assert_eq!(true, Token::new("f(x) = f(x)\nf(0)", &mut state).is_err()); // Help