"""vast vs CPython ast 差分测试(临时脚本)""" import sys import ast sys.path.insert(0, r'd:\Users\TermiNexus\Desktop\TransPyC') import lib.vast as vast def norm_dump(t): # 归一化:对象地址、repr 差异 return t def compare(src, label=''): try: ref = ast.dump(ast.parse(src), indent=2) ref_ok = True except SyntaxError as e: ref = f'' ref_ok = False try: got = vast.dump(vast.parse(src), indent=2) got_ok = True except Exception as e: got = f'<{type(e).__name__}: {e}>' got_ok = False if ref == got: return True, ref return False, (ref, got, ref_ok, got_ok) TESTS = [ 'x = 1', 'x = 1 + 2 * 3', 'x = (1 + 2) * 3', 'def foo(a, b=2):\n return a + b', 'def foo(a: int, b: float = 2.5) -> str:\n return str(a)', 'class Foo:\n x: int = 1\n def __init__(self):\n pass', 'class Foo(Bar, Baz):\n pass', 'if x > 0:\n y = 1\nelif x < 0:\n y = -1\nelse:\n y = 0', 'for i in range(10):\n print(i)', 'while x < 10:\n x += 1', 'try:\n pass\nexcept Exception as e:\n raise\nexcept:\n pass\nfinally:\n pass', 'with open("f") as f:\n data = f.read()', 'import os\nfrom sys import argv, exit as e\nimport a.b.c as abc', 'x = [1, 2, 3]\ny = (1, 2)\nz = {"a": 1, "b": [1,2]}\ns = {1, 2, 3}', 's = f"{name} is {age} years old"', 's = f"{x!r} {y:>10}"', 'x = a.b.c[0].d(1, 2, k=3)', 'x = 0x1F + 0b101 + 1_000_000 + 1.5e10', 'x = True\nif not x:\n pass', 'x = a if b else c', 'l = [x for x in range(10) if x % 2 == 0]', 'g = (x * 2 for x in range(5))', 'd = {k: v for k, v in pairs}', 'f = lambda x, y=1: x + y', 'x = a[1:2]\ny = a[::2]\nz = a[1, 2]', '@decorator\ndef foo():\n pass', '@t.NoVTable\nclass Foo:\n pass', 'global x\nnonlocal y', 'del x[0], y', 'assert x == 1, "msg"', 'raise ValueError("bad")', 'x = -5\ny = ~x\nz = +3', 'a = 5\nb = a\nc = a = b', 'x: int = 5\ny: list[int]\nz: str | None = None', 'def f(x, *args, y=1, **kwargs):\n pass', 'match x:\n case 1:\n pass\n case [a, b]:\n pass\n case {"k": v}:\n pass\n case Point(x=0, y=y):\n pass\n case _:\n pass', 'fn foo():\n return 1', 'func bar(x):\n return x', 'x = "a" "b" "c"', 'x = f"{a}" "suffix"', 'y = b"bytes"', 'r = r"raw\\nstring"', 'x = ...', 'x = (y := 5)', 'def f() -> None:\n return None', 'x = a < b < c', 'x = a is not b\nx = c not in d', 'x = -2 ** 2', 'x = 2 ** -1', 'x = await f()', 'async def g():\n await f()', 'async for i in stream:\n pass', 'async with lock:\n pass', ] def main(): fails = 0 skips = 0 for src in TESTS: ok, res = compare(src) if not ok: ref, got, ref_ok, got_ok = res if not ref_ok: # CPython 也解析失败 → 跳过(vast 应同样报错,但容错) skips += 1 print('SKIP(cpy-err)', repr(src[:50])) continue fails += 1 print('FAIL', repr(src[:50])) print(' ref:', ref[:300]) print(' got:', got[:300]) else: print('OK ', repr(src[:50])) print('=' * 40) print(f'PASS: {len(TESTS) - fails - skips}/{len(TESTS)} FAIL: {fails} SKIP: {skips}') if __name__ == '__main__': main()