@@ -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
0 commit comments