Skip to content

Commit

Permalink
Fix implied multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed May 1, 2022
1 parent 5db4269 commit ac65856
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -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)*}
Expand All @@ -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}
Expand Down
5 changes: 5 additions & 0 deletions src/handlers/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ac65856

Please sign in to comment.