Skip to content

Commit c468c20

Browse files
Fix mul/div precedence to parse left to right (#126)
Multiply and divide should apply left to right when not parenthesized
1 parent 9d52012 commit c468c20

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

scrapscript.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ def xp(n: float) -> Prec:
303303
">>": lp(14),
304304
"<<": lp(14),
305305
"^": rp(13),
306-
"*": lp(12),
307-
"/": lp(12),
306+
"*": rp(12),
307+
"/": rp(12),
308308
"//": lp(12),
309309
"%": lp(12),
310310
"+": lp(11),
@@ -1926,6 +1926,12 @@ def test_mul_binds_tighter_than_add_left(self) -> None:
19261926
Binop(BinopKind.ADD, Binop(BinopKind.MUL, Int(1), Int(2)), Int(3)),
19271927
)
19281928

1929+
def test_mul_and_div_bind_left_to_right(self) -> None:
1930+
self.assertEqual(
1931+
parse([IntLit(1), Operator("/"), IntLit(3), Operator("*"), IntLit(3)]),
1932+
Binop(BinopKind.MUL, Binop(BinopKind.DIV, Int(1), Int(3)), Int(3)),
1933+
)
1934+
19291935
def test_exp_binds_tighter_than_mul_right(self) -> None:
19301936
self.assertEqual(
19311937
parse([IntLit(5), Operator("*"), IntLit(2), Operator("^"), IntLit(3)]),
@@ -3930,6 +3936,16 @@ def test_any_short_circuits(self) -> None:
39303936
Variant("true", Hole()),
39313937
)
39323938

3939+
def test_mul_and_div_have_left_to_right_precedence(self) -> None:
3940+
self.assertEqual(
3941+
self._run(
3942+
"""
3943+
1 / 3 * 3
3944+
"""
3945+
),
3946+
Float(1.0),
3947+
)
3948+
39333949

39343950
class BencodeTests(unittest.TestCase):
39353951
def test_bencode_int(self) -> None:

0 commit comments

Comments
 (0)