Skip to content

Commit 9d46e0e

Browse files
committed
Compile int sub
1 parent 78cc682 commit 9d46e0e

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

ir.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,12 @@ def run(self) -> dict[Instr, ConstantLattice]:
798798
new_type = CInt(l + r)
799799
case (CInt(_), CInt(_)):
800800
new_type = CInt()
801+
elif isinstance(instr, IntSub):
802+
match (self.type_of(instr.operands[0]), self.type_of(instr.operands[1])):
803+
case (CInt(int(l)), CInt(int(r))):
804+
new_type = CInt(l - r)
805+
case (CInt(_), CInt(_)):
806+
new_type = CInt()
801807
elif isinstance(instr, ListCons):
802808
if isinstance(self.type_of(instr.operands[1]), CList):
803809
new_type = CList()
@@ -1898,6 +1904,9 @@ def test_int(self) -> None:
18981904
def test_int_add(self) -> None:
18991905
self.assertEqual(_run("1 + 2"), "3\n")
19001906

1907+
def test_int_sub(self) -> None:
1908+
self.assertEqual(_run("1 - 2"), "-1\n")
1909+
19011910
def test_fun_id(self) -> None:
19021911
self.assertEqual(_run("a -> a"), "<closure>\n")
19031912

0 commit comments

Comments
 (0)