159 lines
4.1 KiB
Python
159 lines
4.1 KiB
Python
from stdint import *
|
|
import w32.win32console
|
|
import t, c
|
|
|
|
|
|
def add_inline(a: t.CInt, b: t.CInt) -> t.CDefine:
|
|
return a + b
|
|
|
|
def mul_inline(a: t.CInt, b: t.CInt) -> t.CDefine:
|
|
return a * b
|
|
|
|
|
|
class AppError(Exception):
|
|
pass
|
|
|
|
class DiskError(AppError):
|
|
pass
|
|
|
|
class NetworkError(AppError):
|
|
pass
|
|
|
|
class TimeoutError(NetworkError):
|
|
pass
|
|
|
|
|
|
def raise_disk():
|
|
raise DiskError("disk read failed")
|
|
|
|
def raise_network():
|
|
raise NetworkError("connection refused")
|
|
|
|
def raise_timeout():
|
|
raise TimeoutError("operation timed out")
|
|
|
|
|
|
def main() -> t.CInt | t.CExport:
|
|
w32.win32console.SetConsoleCP(65001)
|
|
w32.win32console.SetConsoleOutputCP(65001)
|
|
|
|
print("=== Test 1: Raise and catch exact custom exception ===")
|
|
try:
|
|
raise DiskError("disk error")
|
|
except DiskError as e:
|
|
print("PASS 1: caught DiskError:", e)
|
|
except:
|
|
print("FAIL 1: caught by generic except")
|
|
print("")
|
|
|
|
print("=== Test 2: Catch parent catches child ===")
|
|
try:
|
|
raise_disk()
|
|
except AppError as e:
|
|
print("PASS 2: caught AppError (child DiskError):", e)
|
|
except:
|
|
print("FAIL 2: caught by generic except")
|
|
print("")
|
|
|
|
print("=== Test 3: Catch parent catches nested child ===")
|
|
try:
|
|
raise_timeout()
|
|
except AppError as e:
|
|
print("PASS 3: caught AppError (grandchild TimeoutError):", e)
|
|
except:
|
|
print("FAIL 3: caught by generic except")
|
|
print("")
|
|
|
|
print("=== Test 4: Catch mid-level parent ===")
|
|
try:
|
|
raise_timeout()
|
|
except NetworkError as e:
|
|
print("PASS 4: caught NetworkError (child TimeoutError):", e)
|
|
except:
|
|
print("FAIL 4: caught by generic except")
|
|
print("")
|
|
|
|
print("=== Test 5: Exact catch before parent ===")
|
|
try:
|
|
raise_network()
|
|
except NetworkError as e:
|
|
print("PASS 5: caught NetworkError:", e)
|
|
except AppError as e:
|
|
print("FAIL 5: caught AppError (should be NetworkError first)")
|
|
except:
|
|
print("FAIL 5: caught by generic except")
|
|
print("")
|
|
|
|
print("=== Test 6: Parent catch when exact not present ===")
|
|
try:
|
|
raise_network()
|
|
except DiskError as e:
|
|
print("FAIL 6: caught DiskError (wrong)")
|
|
except AppError as e:
|
|
print("PASS 6: caught AppError (no NetworkError handler):", e)
|
|
except:
|
|
print("FAIL 6: caught by generic except")
|
|
print("")
|
|
|
|
print("=== Test 7: Generic except fallback ===")
|
|
try:
|
|
raise_disk()
|
|
except RuntimeError as e:
|
|
print("FAIL 7: caught RuntimeError (wrong)")
|
|
except:
|
|
print("PASS 7: caught by generic except")
|
|
print("")
|
|
|
|
print("=== Test 8: Cross-function with inheritance ===")
|
|
try:
|
|
raise_timeout()
|
|
except NetworkError as e:
|
|
print("PASS 8: caught NetworkError (cross-func TimeoutError):", e)
|
|
except:
|
|
print("FAIL 8: caught by generic except")
|
|
print("")
|
|
|
|
print("=== Test 9: Built-in exception still works ===")
|
|
try:
|
|
raise ValueError("test")
|
|
except ValueError as e:
|
|
print("PASS 9: caught ValueError:", e)
|
|
except:
|
|
print("FAIL 9: caught by generic except")
|
|
print("")
|
|
|
|
print("=== Test 10: Mixed built-in and custom ===")
|
|
try:
|
|
raise RuntimeError("test")
|
|
except DiskError as e:
|
|
print("FAIL 10: caught DiskError (wrong)")
|
|
except RuntimeError as e:
|
|
print("PASS 10: caught RuntimeError:", e)
|
|
except:
|
|
print("FAIL 10: caught by generic except")
|
|
print("")
|
|
|
|
print("=== All Custom Exception Tests Complete ===")
|
|
|
|
print("")
|
|
print("=== Test LLVMIR: inline LLVM IR add ===")
|
|
x: t.CInt = 10
|
|
y: t.CInt = 20
|
|
r: t.CInt = c.LLVMIR(f"add i32 {c.LInp(x)}, {c.LInp(y)}", t.CInt)
|
|
print("LLVMIR add result:", r)
|
|
|
|
print("")
|
|
print("=== Test CDefine function macro ===")
|
|
s: t.CInt = add_inline(3, 4)
|
|
print("add_inline(3, 4) =", s)
|
|
m: t.CInt = mul_inline(5, 6)
|
|
print("mul_inline(5, 6) =", m)
|
|
|
|
print("")
|
|
print("=== Test LLVMIR with LOut ===")
|
|
out_val: t.CInt = 0
|
|
c.LLVMIR(f"{c.LOut(out_val)} = add i32 {c.LInp(x)}, {c.LInp(y)}", t.CInt)
|
|
print("LLVMIR LOut result:", out_val)
|
|
|
|
return 0
|