Fixed xy^z precedence, from (xy)^2 to x(y^2)

This commit is contained in:
PaddiM8 2021-02-22 20:27:42 +01:00
parent c895ae1eb8
commit 31878f9f1f
2 changed files with 19 additions and 19 deletions

2
Cargo.lock generated
View File

@ -139,7 +139,7 @@ dependencies = [
[[package]] [[package]]
name = "kalk_cli" name = "kalk_cli"
version = "0.3.12" version = "0.3.13"
dependencies = [ dependencies = [
"ansi_term", "ansi_term",
"kalk", "kalk",

View File

@ -375,18 +375,7 @@ fn parse_factor(context: &mut Context) -> Result<Expr, CalcError> {
_ => advance(context).kind, _ => advance(context).kind,
}; };
let parse_next = parse_unit(context); let right = 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;
};
left = Expr::Binary(Box::new(left), op, Box::new(right)); left = Expr::Binary(Box::new(left), op, Box::new(right));
} }
@ -536,12 +525,23 @@ fn parse_identifier(context: &mut Context) -> Result<Expr, CalcError> {
// Turn each individual character into its own variable reference. // Turn each individual character into its own variable reference.
// This parses eg `xy` as `x*y` instead of *one* variable. // This parses eg `xy` as `x*y` instead of *one* variable.
for c in chars { let mut right_chars = chars.peekable();
left = Expr::Binary( while let Some(c) = right_chars.next() {
Box::new(left), // If last iteration
TokenKind::Star, let right = if right_chars.peek().is_none() {
Box::new(Expr::Var(c.to_string())), 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) Ok(left)