From 31878f9f1fe7adb9f7a836e5dd978ab7dc7ac72a Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Mon, 22 Feb 2021 20:27:42 +0100 Subject: [PATCH] Fixed xy^z precedence, from (xy)^2 to x(y^2) --- Cargo.lock | 2 +- kalk/src/parser.rs | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8320459..59f5a1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -139,7 +139,7 @@ dependencies = [ [[package]] name = "kalk_cli" -version = "0.3.12" +version = "0.3.13" dependencies = [ "ansi_term", "kalk", diff --git a/kalk/src/parser.rs b/kalk/src/parser.rs index f062757..450e5ac 100644 --- a/kalk/src/parser.rs +++ b/kalk/src/parser.rs @@ -375,18 +375,7 @@ fn parse_factor(context: &mut Context) -> Result { _ => advance(context).kind, }; - let parse_next = parse_unit(context); - let right = if let Ok(right) = parse_next { - right - /*} else if let Err(CalcError::UnableToParseExpression) = parse_next { - // If it failed to parse further, - // try to parse it as something else. - // Eg. percent unary - break;*/ - } else { - return parse_next; - }; - + let right = parse_unit(context)?; left = Expr::Binary(Box::new(left), op, Box::new(right)); } @@ -536,12 +525,23 @@ fn parse_identifier(context: &mut Context) -> Result { // Turn each individual character into its own variable reference. // This parses eg `xy` as `x*y` instead of *one* variable. - for c in chars { - left = Expr::Binary( - Box::new(left), - TokenKind::Star, - Box::new(Expr::Var(c.to_string())), - ); + let mut right_chars = chars.peekable(); + while let Some(c) = right_chars.next() { + // If last iteration + let right = if right_chars.peek().is_none() { + context.pos -= 1; + context.tokens[context.pos] = Token { + kind: TokenKind::Identifier, + value: c.to_string(), + span: (0, 0), + }; + + parse_exponent(context)? + } else { + Expr::Var(c.to_string()) + }; + + left = Expr::Binary(Box::new(left), TokenKind::Star, Box::new(right)); } Ok(left)