Skip to content

Commit b50dd8d

Browse files
committed
fix editor compilation and parser operator precedence
1 parent 96e82f8 commit b50dd8d

2 files changed

Lines changed: 31 additions & 33 deletions

File tree

libraries/math-parser/src/parser.rs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -84,50 +84,48 @@ pub fn chumsky_parser<'a>() -> impl Parser<'a, &'a str, Node, chumsky::extra::Er
8484

8585
let atom = choice((conditional, float, constant, call, parens, var)).boxed();
8686

87-
let unary = choice((just('-').to(UnaryOp::Neg), just("sqrt").to(UnaryOp::Sqrt)))
88-
.padded()
89-
.repeated()
90-
.foldr(atom, |op, expr| Node::UnaryOp { op, expr: Box::new(expr) });
87+
let add_op = choice((just('+').to(BinaryOp::Add), just('-').to(BinaryOp::Sub))).padded();
88+
let mul_op = choice((just('*').to(BinaryOp::Mul), just('/').to(BinaryOp::Div))).padded();
89+
let pow_op = just('^').to(BinaryOp::Pow).padded();
90+
let unary_op = choice((just('-').to(UnaryOp::Neg), just("sqrt").to(UnaryOp::Sqrt))).padded();
91+
let cmp_op = choice((
92+
just("<").to(BinaryOp::Lt),
93+
just("<=").to(BinaryOp::Leq),
94+
just(">").to(BinaryOp::Gt),
95+
just(">=").to(BinaryOp::Geq),
96+
just("==").to(BinaryOp::Eq),
97+
));
98+
99+
let unary = unary_op.repeated().foldr(atom, |op, expr| Node::UnaryOp { op, expr: Box::new(expr) });
91100

92-
let pow = unary.clone().foldl(just('^').to(BinaryOp::Pow).padded().then(unary).repeated(), |lhs, (op, rhs)| Node::BinOp {
101+
let cmp = unary.clone().foldl(cmp_op.padded().then(unary).repeated(), |lhs: Node, (op, rhs)| Node::BinOp {
102+
lhs: Box::new(lhs),
103+
op,
104+
rhs: Box::new(rhs),
105+
});
106+
107+
let pow = cmp.clone().foldl(pow_op.then(cmp).repeated(), |lhs, (op, rhs)| Node::BinOp {
93108
lhs: Box::new(lhs),
94109
op,
95110
rhs: Box::new(rhs),
96111
});
97112

98113
let product = pow
99114
.clone()
100-
.foldl(choice((just('*').to(BinaryOp::Mul), just('/').to(BinaryOp::Div))).padded().then(pow).repeated(), |lhs, (op, rhs)| {
101-
Node::BinOp {
102-
lhs: Box::new(lhs),
103-
op,
104-
rhs: Box::new(rhs),
105-
}
106-
})
107-
.boxed();
108-
109-
let sum = product.clone().foldl(
110-
choice((just('+').to(BinaryOp::Add), just('-').to(BinaryOp::Sub))).padded().then(product).repeated(),
111-
|lhs, (op, rhs)| Node::BinOp {
112-
lhs: Box::new(lhs),
113-
op,
114-
rhs: Box::new(rhs),
115-
},
116-
);
117-
118-
let cmp = sum.clone().foldl(
119-
choice((just("<").to(BinaryOp::Lt), just(">").to(BinaryOp::Gt), just("==").to(BinaryOp::Eq)))
120-
.padded()
121-
.then(sum)
122-
.repeated(),
123-
|lhs: Node, (op, rhs)| Node::BinOp {
115+
.foldl(mul_op.then(pow).repeated(), |lhs, (op, rhs)| Node::BinOp {
124116
lhs: Box::new(lhs),
125117
op,
126118
rhs: Box::new(rhs),
127-
},
128-
);
119+
})
120+
.boxed();
121+
122+
let sum = product.clone().foldl(add_op.then(product).repeated(), |lhs, (op, rhs)| Node::BinOp {
123+
lhs: Box::new(lhs),
124+
op,
125+
rhs: Box::new(rhs),
126+
});
129127

130-
cmp.padded()
128+
sum.padded()
131129
})
132130
}
133131

node-graph/gcore/src/ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn math<U: num_traits::float::Float>(
5151
#[default(1.)]
5252
operand_b: U,
5353
) -> U {
54-
let (node, _unit) = match ast::Node::try_parse_from_str(&expression) {
54+
let node = match ast::Node::try_parse_from_str(&expression) {
5555
Ok(expr) => expr,
5656
Err(e) => {
5757
warn!("Invalid expression: `{expression}`\n{e:?}");

0 commit comments

Comments
 (0)