this repo has no description

Encode const small strings in the compiler (#163)

- Add and fix tests
- Encode small strings in the compiler
- Add string value in comment

authored by bernsteinbear.com and committed by

GitHub 6c1795ae 7e341d54

+24 -1
+12 -1
compiler.py
··· 296 296 assert len(fn.fields) == 0 297 297 return self._const_obj("closure", "TAG_CLOSURE", f".fn={fn.name}, .size=0") 298 298 299 + def _emit_small_string(self, value_str: str) -> str: 300 + value = value_str.encode("utf-8") 301 + length = len(value) 302 + assert length < 8, "small string must be less than 8 bytes" 303 + kImmediateTagBits = 5 304 + kSmallStringTag = 13 305 + tag = (length << kImmediateTagBits) | kSmallStringTag 306 + encoded = value[::-1] + bytes([tag]) 307 + value_int = int.from_bytes(encoded, "big") 308 + return f"(struct object*)({hex(value_int)}ULL /* {value_str!r} */)" 309 + 299 310 def _emit_const(self, exp: Object) -> str: 300 311 assert self._is_const(exp), f"not a constant {exp}" 301 312 if isinstance(exp, Hole): ··· 311 322 return result 312 323 if isinstance(exp, String): 313 324 if len(exp.value) < 8: 314 - raise NotImplementedError("small strings") 325 + return self._emit_small_string(exp.value) 315 326 return self._const_obj( 316 327 "heap_string", "TAG_STRING", f".size={len(exp.value)}, .data={json.dumps(exp.value)}" 317 328 )
+12
compiler_tests.py
··· 36 36 def test_int(self) -> None: 37 37 self.assertEqual(self._run("1"), "1\n") 38 38 39 + def test_small_string(self) -> None: 40 + self.assertEqual(self._run('"hello"'), '"hello"\n') 41 + 42 + def test_heap_string(self) -> None: 43 + self.assertEqual(self._run('"hello world"'), '"hello world"\n') 44 + 45 + def test_const_list(self) -> None: 46 + self.assertEqual( 47 + self._run("""[1, "2", [3, 4], {a=1}, #foo ()]"""), 48 + """[1, "2", [3, 4], {a = 1}, #foo ()]\n""", 49 + ) 50 + 39 51 def test_add(self) -> None: 40 52 self.assertEqual(self._run("1 + 2"), "3\n") 41 53