Skip to content

Commit 78cc682

Browse files
committed
Add string
1 parent 99b9c3d commit 78cc682

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

ir.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ def op(idx: int) -> str:
441441
return _handle(f"mksmallint({value.value})")
442442
if isinstance(value, Hole):
443443
return _decl("struct object*", "hole()")
444+
if isinstance(value, String):
445+
string_repr = json.dumps(value.value)
446+
return _handle(f"mkstring(heap, {string_repr}, {len(value.value)})")
444447
if isinstance(instr, IntAdd):
445448
operands = ", ".join(gvn.name(op) for op in instr.operands)
446449
return _handle(f"num_add({operands})")
@@ -1578,6 +1581,20 @@ def test_hole(self) -> None:
15781581
}""",
15791582
)
15801583

1584+
def test_string(self) -> None:
1585+
compiler = Compiler()
1586+
compiler.compile_body({}, _parse('"hello"'))
1587+
self.assertEqual(
1588+
compiler.fns[0].to_string(InstrId()),
1589+
"""\
1590+
fn0 {
1591+
bb0 {
1592+
v0 = Const<"hello">
1593+
Return v0
1594+
}
1595+
}""",
1596+
)
1597+
15811598

15821599
class RPOTests(unittest.TestCase):
15831600
def test_one_block(self) -> None:
@@ -1926,6 +1943,9 @@ def test_record_builder_access(self) -> None:
19261943
def test_hole(self) -> None:
19271944
self.assertEqual(_run("()"), "()\n")
19281945

1946+
def test_string(self) -> None:
1947+
self.assertEqual(_run('"hello"'), '"hello"\n')
1948+
19291949

19301950
if __name__ == "__main__":
19311951
__import__("sys").modules["unittest.util"]._MAX_LENGTH = 999999999

0 commit comments

Comments
 (0)