修复了大量存在的问题,增加了假鸭子类型等等机制

This commit is contained in:
2026-06-25 14:49:46 +08:00
parent 19f2787db0
commit d88d11b646
827 changed files with 32617 additions and 18316 deletions

View File

@@ -1,11 +1,12 @@
import t
import stdio
import testcheck
# ============================================================
# Test 1: nonlocal 在条件分支中修改
# ============================================================
def test_nonlocal_conditional():
stdio.printf("--- Test 1: nonlocal in conditional branches ---\n")
testcheck.section("Test 1: nonlocal in conditional branches")
x: t.CInt = 0
def modify(flag: t.CInt):
nonlocal x
@@ -16,16 +17,13 @@ def test_nonlocal_conditional():
modify(1)
modify(0)
modify(1)
if x == 120:
stdio.printf("PASS: nonlocal conditional (x=%d)\n", x)
else:
stdio.printf("FAIL: nonlocal conditional (x=%d, expect 120)\n", x)
testcheck.check(x == 120, "nonlocal conditional (x == 120)", "nonlocal conditional (expect 120)")
# ============================================================
# Test 2: 多个nonlocal变量同时修改
# ============================================================
def test_nonlocal_multi_var():
stdio.printf("--- Test 2: multiple nonlocal variables ---\n")
testcheck.section("Test 2: multiple nonlocal variables")
a: t.CInt = 1
b: t.CInt = 2
c_val: t.CInt = 3
@@ -36,10 +34,7 @@ def test_nonlocal_multi_var():
b = c_val
c_val = tmp
swap_add()
if a == 2 and b == 3 and c_val == 1:
stdio.printf("PASS: multi nonlocal (a=%d b=%d c=%d)\n", a, b, c_val)
else:
stdio.printf("FAIL: multi nonlocal (a=%d b=%d c=%d, expect 2 3 1)\n", a, b, c_val)
testcheck.check(a == 2 and b == 3 and c_val == 1, "multi nonlocal (a==2 b==3 c==1)", "multi nonlocal (expect 2 3 1)")
# ============================================================
# Test 3: global 在多个函数中交叉修改
@@ -55,55 +50,46 @@ def add_global(n: t.CInt):
g_counter += n
def test_global_cross_functions():
stdio.printf("--- Test 3: global cross-function modification ---\n")
testcheck.section("Test 3: global cross-function modification")
global g_counter
g_counter = 0
inc_global()
inc_global()
add_global(10)
inc_global()
if g_counter == 13:
stdio.printf("PASS: global cross (g_counter=%d)\n", g_counter)
else:
stdio.printf("FAIL: global cross (g_counter=%d, expect 13)\n", g_counter)
testcheck.check(g_counter == 13, "global cross (g_counter == 13)", "global cross (expect 13)")
# ============================================================
# Test 4: 嵌套函数修改外层局部变量(引用捕获)
# ============================================================
def test_nested_ref_capture():
stdio.printf("--- Test 4: nested function ref capture ---\n")
arr: list[t.CInt, 4] = [0]
testcheck.section("Test 4: nested function ref capture")
arr: t.CArray[t.CInt, 4] = [0]
def fill():
nonlocal arr
for i in range(4):
arr[i] = (i + 1) * 10
fill()
total: t.CInt = arr[0] + arr[1] + arr[2] + arr[3]
if total == 100:
stdio.printf("PASS: ref capture (total=%d)\n", total)
else:
stdio.printf("FAIL: ref capture (total=%d, expect 100)\n", total)
testcheck.check(total == 100, "ref capture (total == 100)", "ref capture (expect 100)")
# ============================================================
# Test 5: lambda捕获后修改原变量
# ============================================================
def test_lambda_capture_then_modify():
stdio.printf("--- Test 5: lambda capture then modify ---\n")
testcheck.section("Test 5: lambda capture then modify")
x: t.CInt = 10
f = lambda: x
r1: t.CInt = f()
x = 20
r2: t.CInt = f()
if r1 == 10 and r2 == 10:
stdio.printf("PASS: lambda value capture (before=%d after=%d)\n", r1, r2)
else:
stdio.printf("FAIL: lambda value capture (before=%d after=%d, expect 10 10)\n", r1, r2)
testcheck.check(r1 == 10 and r2 == 10, "lambda value capture (before==10 after==10)", "lambda value capture (expect 10 10)")
# ============================================================
# Test 6: 嵌套函数中nonlocal与循环结合
# ============================================================
def test_nonlocal_loop_accumulate():
stdio.printf("--- Test 6: nonlocal with loop accumulate ---\n")
testcheck.section("Test 6: nonlocal with loop accumulate")
sum_val: t.CInt = 0
count: t.CInt = 0
def add_numbers(n: t.CInt):
@@ -113,16 +99,13 @@ def test_nonlocal_loop_accumulate():
count += 1
add_numbers(5) # 1+2+3+4+5=15
add_numbers(3) # 1+2+3=6
if sum_val == 21 and count == 8:
stdio.printf("PASS: loop accumulate (sum=%d count=%d)\n", sum_val, count)
else:
stdio.printf("FAIL: loop accumulate (sum=%d count=%d, expect 21 8)\n", sum_val, count)
testcheck.check(sum_val == 21 and count == 8, "loop accumulate (sum==21 count==8)", "loop accumulate (expect 21 8)")
# ============================================================
# Test 7: 三层嵌套中每层修改不同变量
# ============================================================
def test_deep_nested_vars():
stdio.printf("--- Test 7: deep nested different vars ---\n")
testcheck.section("Test 7: deep nested different vars")
a: t.CInt = 0
b: t.CInt = 0
c_val: t.CInt = 0
@@ -139,10 +122,7 @@ def test_deep_nested_vars():
level2()
level1()
level1()
if a == 2 and b == 20 and c_val == 200:
stdio.printf("PASS: deep nested (a=%d b=%d c=%d)\n", a, b, c_val)
else:
stdio.printf("FAIL: deep nested (a=%d b=%d c=%d, expect 2 20 200)\n", a, b, c_val)
testcheck.check(a == 2 and b == 20 and c_val == 200, "deep nested (a==2 b==20 c==200)", "deep nested (expect 2 20 200)")
# ============================================================
# Test 8: global + nonlocal 在同一嵌套函数中
@@ -150,7 +130,7 @@ def test_deep_nested_vars():
g_state: t.CInt = 0
def test_global_nonlocal_same_func():
stdio.printf("--- Test 8: global+nonlocal in same nested func ---\n")
testcheck.section("Test 8: global+nonlocal in same nested func")
global g_state
g_state = 0
local_state: t.CInt = 0
@@ -161,30 +141,24 @@ def test_global_nonlocal_same_func():
g_state += 1
local_state += 2
worker(5)
if g_state == 5 and local_state == 10:
stdio.printf("PASS: global+nonlocal same (g=%d l=%d)\n", g_state, local_state)
else:
stdio.printf("FAIL: global+nonlocal same (g=%d l=%d, expect 5 10)\n", g_state, local_state)
testcheck.check(g_state == 5 and local_state == 10, "global+nonlocal same (g==5 l==10)", "global+nonlocal same (expect 5 10)")
# ============================================================
# Test 9: lambda带默认参数与捕获
# ============================================================
def test_lambda_default_and_capture():
stdio.printf("--- Test 9: lambda with arg and capture ---\n")
testcheck.section("Test 9: lambda with arg and capture")
base: t.CInt = 100
multiply = lambda x: base * x
r1: t.CInt = multiply(3)
r2: t.CInt = multiply(5)
if r1 == 300 and r2 == 500:
stdio.printf("PASS: lambda arg+capture (100*3=%d 100*5=%d)\n", r1, r2)
else:
stdio.printf("FAIL: lambda arg+capture (100*3=%d 100*5=%d, expect 300 500)\n", r1, r2)
testcheck.check(r1 == 300 and r2 == 500, "lambda arg+capture (100*3==300 100*5==500)", "lambda arg+capture (expect 300 500)")
# ============================================================
# Test 10: 嵌套函数多次调用累积
# ============================================================
def test_nested_accumulate():
stdio.printf("--- Test 10: nested function accumulate ---\n")
testcheck.section("Test 10: nested function accumulate")
total: t.CInt = 0
def add_val(n: t.CInt):
nonlocal total
@@ -192,16 +166,13 @@ def test_nested_accumulate():
add_val(10)
add_val(20)
add_val(30)
if total == 60:
stdio.printf("PASS: nested accumulate (total=%d)\n", total)
else:
stdio.printf("FAIL: nested accumulate (total=%d, expect 60)\n", total)
testcheck.check(total == 60, "nested accumulate (total == 60)", "nested accumulate (expect 60)")
# ============================================================
# Test 11: nonlocal在while循环中
# ============================================================
def test_nonlocal_while():
stdio.printf("--- Test 11: nonlocal in while loop ---\n")
testcheck.section("Test 11: nonlocal in while loop")
result: t.CInt = 0
def countdown(n: t.CInt):
nonlocal result
@@ -210,10 +181,7 @@ def test_nonlocal_while():
result += i
i -= 1
countdown(5) # 5+4+3+2+1=15
if result == 15:
stdio.printf("PASS: nonlocal while (result=%d)\n", result)
else:
stdio.printf("FAIL: nonlocal while (result=%d, expect 15)\n", result)
testcheck.check(result == 15, "nonlocal while (result == 15)", "nonlocal while (expect 15)")
# ============================================================
# Test 12: 嵌套函数修改结构体字段
@@ -223,7 +191,7 @@ class Point:
y: t.CInt
def test_nested_modify_struct():
stdio.printf("--- Test 12: nested modify struct ---\n")
testcheck.section("Test 12: nested modify struct")
p: Point = Point()
p.x = 0
p.y = 0
@@ -233,13 +201,10 @@ def test_nested_modify_struct():
p.y += dy
translate(10, 20)
translate(5, 15)
if p.x == 15 and p.y == 35:
stdio.printf("PASS: nested struct (x=%d y=%d)\n", p.x, p.y)
else:
stdio.printf("FAIL: nested struct (x=%d y=%d, expect 15 35)\n", p.x, p.y)
testcheck.check(p.x == 15 and p.y == 35, "nested struct (x==15 y==35)", "nested struct (expect 15 35)")
def main() -> t.CInt:
stdio.printf("=== ClosureTest2: Advanced Closure/Lambda/Global/Nonlocal ===\n\n")
testcheck.begin("ClosureTest2: Advanced Closure/Lambda/Global/Nonlocal")
test_nonlocal_conditional()
test_nonlocal_multi_var()
test_global_cross_functions()
@@ -252,5 +217,4 @@ def main() -> t.CInt:
test_nested_accumulate()
test_nonlocal_while()
test_nested_modify_struct()
stdio.printf("\n=== ClosureTest2 Complete ===\n")
return 0
return testcheck.end()

View File

@@ -1 +0,0 @@
{"stdio": "73edbcf76e32d00b"}

View File

@@ -0,0 +1 @@
{"stdio": "73edbcf76e32d00b", "testcheck": "9dbecd0942a39782"}

View File

@@ -0,0 +1 @@
{"main": "440f18f9dce85a95", "stdio": "73edbcf76e32d00b", "testcheck": "9dbecd0942a39782"}

View File

@@ -8,6 +8,7 @@ import c
import t
import stdio
import testcheck
def test_nonlocal_conditional() -> t.CInt: pass

View File

@@ -0,0 +1,25 @@
"""
Auto-generated Python stub file from testcheck.py
Module: testcheck
"""
import t, c
import stdio
_pass_count: t.CExtern | t.CInt
_fail_count: t.CExtern | t.CInt
def begin(name: str) -> t.CInt: pass
def section(name: str) -> t.CInt: pass
def ok(msg: str) -> t.CInt: pass
def fail(msg: str) -> t.CInt: pass
def check(cond: t.CInt, ok_msg: str, fail_msg: str) -> t.CInt: pass
def info(msg: str) -> t.CInt: pass
def end() -> t.CInt: pass

View File

@@ -1,2 +1,3 @@
052c7f96b9eefa2c:main.py
440f18f9dce85a95:main.py
73edbcf76e32d00b:includes/stdio.py
9dbecd0942a39782:includes/testcheck.py