Skip to content

Commit

Permalink
Coalesce minus int/float into negative int/float
Browse files Browse the repository at this point in the history
Don't bother creating a Binop; we can catch this. It makes (testing)
some stuff in the compiler easier.
  • Loading branch information
tekknolagi committed Dec 29, 2024
1 parent 2af3efd commit 419ef9f
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions scrapscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ def parse_unary(tokens: typing.List[Token], p: float) -> "Object":
# Precedence was chosen to be higher than function application so that
# -a b is (-a) b and not -(a b).
r = parse_binary(tokens, HIGHEST_PREC + 1)
if isinstance(r, Int):
assert r.value >= 0, "Tokens should never have negative values"
return Int(-r.value)
if isinstance(r, Float):
assert r.value >= 0, "Tokens should never have negative values"
return Float(-r.value)
return Binop(BinopKind.SUB, Int(0), r)
else:
raise ParseError(f"unexpected token {token!r}")
Expand Down Expand Up @@ -1859,10 +1865,10 @@ def test_parse_digit_returns_int(self) -> None:
def test_parse_digits_returns_int(self) -> None:
self.assertEqual(parse([IntLit(123)]), Int(123))

def test_parse_negative_int_returns_binary_sub_int(self) -> None:
self.assertEqual(parse([Operator("-"), IntLit(123)]), Binop(BinopKind.SUB, Int(0), Int(123)))
def test_parse_negative_int_returns_negative_int(self) -> None:
self.assertEqual(parse([Operator("-"), IntLit(123)]), Int(-123))

def test_parse_negative_var_returns_binary_sub_int(self) -> None:
def test_parse_negative_var_returns_binary_sub_var(self) -> None:
self.assertEqual(parse([Operator("-"), Name("x")]), Binop(BinopKind.SUB, Int(0), Var("x")))

def test_parse_negative_int_binds_tighter_than_plus(self) -> None:
Expand Down Expand Up @@ -1893,7 +1899,7 @@ def test_parse_decimal_returns_float(self) -> None:
self.assertEqual(parse([FloatLit(3.14)]), Float(3.14))

def test_parse_negative_float_returns_binary_sub_float(self) -> None:
self.assertEqual(parse([Operator("-"), FloatLit(3.14)]), Binop(BinopKind.SUB, Int(0), Float(3.14)))
self.assertEqual(parse([Operator("-"), FloatLit(3.14)]), Float(-3.14))

def test_parse_var_returns_var(self) -> None:
self.assertEqual(parse([Name("abc_123")]), Var("abc_123"))
Expand Down

0 comments on commit 419ef9f

Please sign in to comment.