Short recipes for common tasks. Each is end-to-end tested under
tests/py_corpus/phase[1-4]/.
Stay fully annotated to avoid the CPython fallback.
def mandelbrot(cx: float, cy: float, n: int) -> int:
x: float = 0.0
y: float = 0.0
i: int = 0
while i < n:
if x * x + y * y > 4.0:
return i
x2: float = x * x - y * y + cx
y = 2.0 * x * y + cy
x = x2
i = i + 1
return n
def main() -> None:
print(mandelbrot(-0.5, 0.5, 100))
main()import sys
def main() -> None:
print(sys.platform)
main()pcc auto-links libpython; any import triggers it.
import math
def main() -> None:
print(math.pow(2.0, 10.0))
print(math.sqrt(16.0))
main()Scalar args (int / float / str) get marshalled to CPython
PyObject *; the result is tagged as a CPython value and
print(...) converts to pcc-native str.
def main() -> None:
try:
raise ValueError("bad input")
except ValueError as e:
print("caught")
print(str(e))
main()class Temperature:
def __init__(self, celsius: int) -> None:
self._celsius = celsius
@property
def celsius(self) -> int:
return self._celsius
@celsius.setter
def celsius(self, v: int) -> None:
self._celsius = v
def main() -> None:
t = Temperature(20)
print(t.celsius)
t.celsius = 30
print(t.celsius)
main()class Money:
def __init__(self, amount: int) -> None:
self.amount = amount
def __add__(self, other: "Money") -> "Money":
return Money(self.amount + other.amount)
def __lt__(self, other: "Money") -> bool:
return self.amount < other.amount
def main() -> None:
a = Money(100)
b = Money(50)
c = a + b
print(c.amount)
print(a < b)
main()Works for CPython-backed context managers (happy-path exit; exception
propagation through __exit__ is a pending gap).
import io
def main() -> None:
with io.StringIO() as buf:
buf.write("hello")
print(buf.getvalue())
main()import os
def main() -> None:
for entry in os.listdir("."):
print(entry)
main()PI: float = 3.14159
MAX: int = 100
NAME: str = "pcc"
def main() -> None:
print(PI)
print(MAX)
print(NAME)
main()python -m pcc myfile.py --emit-llvm -o myfile.ll
cat myfile.ll | head -40For benchmark + acceptance of the whole corpus:
python tests/py_corpus/run_pcc.py --bench