Fixed associativity for parse_sum/parse_factor

This commit is contained in:
bakk 2022-01-18 18:57:54 +01:00 committed by PaddiM8
parent 48c1247b96
commit b8b7a0e257
3 changed files with 13 additions and 15 deletions

View File

@ -122,11 +122,7 @@ fn build_fn_decl(
// Check if all the expressions in the parameter_expr are
// variables. If not, it can't be turned into a function declaration.
let all_are_vars = match &parameter_expr {
Expr::Vector(exprs) => {
exprs
.iter()
.any(|x| matches!(x, Expr::Var(_)))
}
Expr::Vector(exprs) => exprs.iter().any(|x| matches!(x, Expr::Var(_))),
Expr::Group(expr) => {
matches!(&**expr, Expr::Var(_))
}
@ -610,7 +606,7 @@ fn build_fn_call(
context.sum_variable_names = Some(Vec::new());
}
}
// Don't perform equation solving on special functions
if is_integral || is_sum_prod {
context.in_equation = false;
@ -775,7 +771,13 @@ fn build_var(context: &mut Context, name: &str) -> Expr {
}
}
if context.in_sum_prod && context.sum_variable_names.as_ref().unwrap().contains(&name.to_string()) {
if context.in_sum_prod
&& context
.sum_variable_names
.as_ref()
.unwrap()
.contains(&name.to_string())
{
return Expr::Var(Identifier::from_full_name(name));
}

View File

@ -433,7 +433,7 @@ fn parse_sum(context: &mut Context) -> Result<Expr, CalcError> {
while match_token(context, TokenKind::Plus) || match_token(context, TokenKind::Minus) {
let op = peek(context).kind;
advance(context);
let right = parse_factor(context)?;
let right = parse_sum(context)?;
left = Expr::Binary(Box::new(left), op, Box::new(right));
}
@ -447,11 +447,7 @@ fn parse_factor(context: &mut Context) -> Result<Expr, CalcError> {
if let Expr::Unary(TokenKind::Percent, percent_left) = left.clone() {
let try_parse = parse_factor(context);
if try_parse.is_ok() {
left = Expr::Binary(
percent_left,
TokenKind::Percent,
Box::new(try_parse?),
);
left = Expr::Binary(percent_left, TokenKind::Percent, Box::new(try_parse?));
}
}
@ -477,7 +473,7 @@ fn parse_factor(context: &mut Context) -> Result<Expr, CalcError> {
_ => advance(context).kind,
};
let right = parse_unit(context)?;
let right = parse_factor(context)?;
left = Expr::Binary(Box::new(left), op, Box::new(right));
}

View File

@ -2,4 +2,4 @@ x = 2
y = 3
f(x) = 2x(x - 3)(y + 2)
f(f(x) + y) = 3400
2f(f(x) + y) = 6800