R1:T | t.CPtr -> T*(40 文件 / 1121 处;含多级 **、强转位 (T*)(x)、下标内 list[AST*]);引号前向引用 'Rect' | t.CPtr 26 处按例外保留,T | t.CPtr | t.State 25 处因 T* 后不能接 | 保持原联合写法。 R12:i = 0 + while i < N: + 尾 i += 1 -> or i in range(N):(10 文件 / 42 处;含 <= -> range(EXPR + 1)、带注解初始化 i: int = 0、尾 i = i + 1)。余 20 处经守卫判定不可安全改写(复合条件 1、尾部非自增 8、体内重绑定/continue 5、while-else 2、循环后仍被读取 2、找不到初始化 2)。
436 lines
11 KiB
Plaintext
436 lines
11 KiB
Plaintext
import config
|
||
import t, c
|
||
|
||
|
||
def ForRangeBasicTest(stop: int):
|
||
i: int = 0
|
||
for i in range(stop):
|
||
print("For range: ", i)
|
||
print("Done")
|
||
|
||
|
||
def ForRangeWithStartStop(start: int, stop: int, step: int):
|
||
i: int = 0
|
||
for i in range(start, stop, step):
|
||
print("Range: ", i)
|
||
print("Done")
|
||
|
||
|
||
def NestedForRangeTest(rows: int, cols: int):
|
||
r: int = 0
|
||
c_val: int = 0
|
||
for r in range(rows):
|
||
for c_val in range(cols):
|
||
print("Cell: ", r, c_val)
|
||
print("Grid done")
|
||
|
||
|
||
def ForWithIfBreakTest(data_size: int):
|
||
i: int = 0
|
||
found: int = 0
|
||
for i in range(data_size):
|
||
if i == 5:
|
||
print("Found at: ", i)
|
||
found = 1
|
||
break
|
||
print("Checking: ", i)
|
||
if found == 0:
|
||
print("Not found")
|
||
|
||
|
||
def ForWithIfContinueTest(n: int):
|
||
i: int = 0
|
||
count: int = 0
|
||
for i in range(n):
|
||
if i % 2 == 0:
|
||
continue
|
||
print("Odd: ", i)
|
||
count += 1
|
||
print("Total odd: ", count)
|
||
|
||
|
||
def ForWithElifBranchTest(value: int):
|
||
i: int = 0
|
||
result: int = 0
|
||
for i in range(value):
|
||
if i < 3:
|
||
result += 1
|
||
elif i < 6:
|
||
result += 2
|
||
elif i < 9:
|
||
result += 3
|
||
else:
|
||
result += 4
|
||
print("Elif branch result: ", result)
|
||
|
||
|
||
def WhileWithIfElifElseTest(iterations: int):
|
||
for counter in range(iterations):
|
||
if counter < 2:
|
||
print("Phase 1: ", counter)
|
||
elif counter < 4:
|
||
print("Phase 2: ", counter)
|
||
elif counter < 6:
|
||
print("Phase 3: ", counter)
|
||
else:
|
||
print("Phase 4: ", counter)
|
||
print("While done")
|
||
|
||
|
||
def WhileWithNestedIfTest(limit: int):
|
||
outer: int = 0
|
||
while outer < limit:
|
||
inner: int = 0
|
||
while inner < limit:
|
||
if inner == 2 and outer == 1:
|
||
print("Target: ", outer, inner)
|
||
inner += 1
|
||
continue
|
||
if inner > 3:
|
||
break
|
||
print("Inner: ", inner)
|
||
inner += 1
|
||
outer += 1
|
||
|
||
|
||
def WhileWithMatchCaseTest(value: int):
|
||
result: int = 0
|
||
match value:
|
||
case 1:
|
||
result = 100
|
||
print("Case 1")
|
||
case 2:
|
||
result = 200
|
||
print("Case 2")
|
||
case 3:
|
||
result = 300
|
||
print("Case 3")
|
||
case _:
|
||
result = 999
|
||
print("Default case")
|
||
print("Match result: ", result)
|
||
|
||
|
||
def WhileWithMatchCaseNoBreakTest(value: int):
|
||
result: int = 0
|
||
match value:
|
||
case 1:
|
||
result = 10
|
||
print("Case 1: ", result)
|
||
case 2:
|
||
result = 20
|
||
print("Case 2: ", result)
|
||
result += 5
|
||
case 3:
|
||
result = 30
|
||
print("Case 3: ", result)
|
||
case _:
|
||
result = 0
|
||
print("No break match result: ", result)
|
||
|
||
|
||
def ComplexForWhileMatchTest(outer_limit: int, inner_limit: int):
|
||
i: int = 0
|
||
j: int = 0
|
||
result: int = 0
|
||
for i in range(outer_limit):
|
||
for j in range(inner_limit):
|
||
match (i, j):
|
||
case (0, 0):
|
||
result += 1
|
||
case (1, 1):
|
||
result += 10
|
||
case (2, _):
|
||
result += 100
|
||
case (_, 0):
|
||
result += 1000
|
||
case _:
|
||
result += 10000
|
||
print("Complex match result: ", result)
|
||
|
||
|
||
def ForWithCaseAndNoBreakTest(n: int):
|
||
i: int = 0
|
||
result: int = 0
|
||
for i in range(n):
|
||
match i:
|
||
case 0:
|
||
result += 1
|
||
case 1:
|
||
result += 2
|
||
case 2:
|
||
result += 3
|
||
result += 10
|
||
case _:
|
||
result += i
|
||
c.NoBreak
|
||
print("NoBreak result: ", result)
|
||
|
||
|
||
def WhileWithBreakConditionTest(limit: int):
|
||
i: int = 0
|
||
early_exit: int = 0
|
||
while i < limit:
|
||
if i == 3:
|
||
early_exit = 1
|
||
break
|
||
if i == 2:
|
||
i += 1
|
||
continue
|
||
print("While i: ", i)
|
||
i += 1
|
||
|
||
|
||
def NestedWhileWithMultipleBreakPoints(depth: int, width: int):
|
||
w: int = 0
|
||
count: int = 0
|
||
for d in range(depth):
|
||
for w in range(width):
|
||
if w == 2:
|
||
break
|
||
if d == 1 and w == 1:
|
||
c.Break
|
||
count += 1
|
||
print("Nested break count: ", count)
|
||
|
||
|
||
def ForWithMatchCaseInLoopTest(iterations: int):
|
||
i: int = 0
|
||
sum_even: int = 0
|
||
sum_odd: int = 0
|
||
for i in range(iterations):
|
||
match i % 3:
|
||
case 0:
|
||
sum_even += i
|
||
case 1:
|
||
sum_odd += i
|
||
case 2:
|
||
sum_even += i * 2
|
||
case _:
|
||
pass
|
||
print("Sum even: ", sum_even)
|
||
print("Sum odd: ", sum_odd)
|
||
|
||
|
||
def ComplexLogicWithElifChains(a: int, b: int, c_val: int):
|
||
result: int = 0
|
||
for i in range(10):
|
||
if i < a:
|
||
if i < b:
|
||
result += 1
|
||
elif i < c_val:
|
||
result += 2
|
||
else:
|
||
result += 3
|
||
elif i < b:
|
||
if i < c_val:
|
||
result += 4
|
||
else:
|
||
result += 5
|
||
elif i < c_val:
|
||
result += 6
|
||
else:
|
||
result += 7
|
||
print("Complex elif chain result: ", result)
|
||
|
||
|
||
def ForRangeWithStepAndCondition(n: int, step: int):
|
||
i: int = 0
|
||
count: int = 0
|
||
for i in range(0, n, step):
|
||
if i % 2 == 0:
|
||
print("Even step: ", i)
|
||
count += 1
|
||
else:
|
||
if i > 5:
|
||
print("Large odd: ", i)
|
||
else:
|
||
print("Small odd: ", i)
|
||
print("Total steps: ", count)
|
||
|
||
|
||
def MatchCaseWithGuardConditions(value: int):
|
||
result: int = 0
|
||
match value:
|
||
case 1 if config.TRUE == 1:
|
||
result = 100
|
||
case 2 if config.FALSE == 0:
|
||
result = 200
|
||
case 3:
|
||
result = 300
|
||
case _:
|
||
result = 0
|
||
print("Guard match result: ", result)
|
||
|
||
|
||
def WhileWithMultipleMatchCases(iterations: int):
|
||
i: int = 0
|
||
state: int = 0
|
||
while i < iterations:
|
||
match state:
|
||
case 0:
|
||
if i > 3:
|
||
state = 1
|
||
case 1:
|
||
if i > 6:
|
||
state = 2
|
||
case 2:
|
||
if i > 9:
|
||
state = 0
|
||
print("State at i=", i, ": ", state)
|
||
i += 1
|
||
|
||
|
||
def ForWhileMixWithBreakAndContinue(n: int):
|
||
i: int = 0
|
||
j: int = 0
|
||
count: int = 0
|
||
for i in range(n):
|
||
j = 0
|
||
while j < n:
|
||
if j == 2:
|
||
j += 1
|
||
continue
|
||
if i == 3 and j == 3:
|
||
break
|
||
count += 1
|
||
j += 1
|
||
print("Mix break continue count: ", count)
|
||
|
||
|
||
def ComplexCaseMatchingWithRanges(start: int, end: int):
|
||
i: int = 0
|
||
bucket0: int = 0
|
||
bucket1: int = 0
|
||
bucket2: int = 0
|
||
bucket3: int = 0
|
||
for i in range(start, end):
|
||
match i:
|
||
case _ if i < 10:
|
||
bucket0 += 1
|
||
case _ if i < 20:
|
||
bucket1 += 1
|
||
case _ if i < 30:
|
||
bucket2 += 1
|
||
case _:
|
||
bucket3 += 1
|
||
print("Bucket0 (<10): ", bucket0)
|
||
print("Bucket1 (10-19): ", bucket1)
|
||
print("Bucket2 (20-29): ", bucket2)
|
||
print("Bucket3 (>=30): ", bucket3)
|
||
|
||
|
||
def WhileWithElifInElifChain(limit: int):
|
||
result: int = 0
|
||
for i in range(limit):
|
||
if i < 2:
|
||
if i == 0:
|
||
result = 1
|
||
else:
|
||
result = 2
|
||
elif i < 4:
|
||
if i == 2:
|
||
result = 3
|
||
else:
|
||
result = 4
|
||
elif i < 6:
|
||
if i == 4:
|
||
result = 5
|
||
else:
|
||
result = 6
|
||
else:
|
||
result = 7
|
||
print("Chain result[", i, "]: ", result)
|
||
|
||
|
||
def ForWithCaseNoBreakNested(inner_limit: int):
|
||
i: int = 0
|
||
j: int = 0
|
||
result: int = 0
|
||
for i in range(3):
|
||
for j in range(inner_limit):
|
||
match j:
|
||
case 0:
|
||
result += 1
|
||
case 1:
|
||
result += 2
|
||
case _:
|
||
result += 3
|
||
c.NoBreak
|
||
print("Nested NoBreak result: ", result)
|
||
|
||
|
||
# ============================================================
|
||
# match case 内 break = 跳出 match(C switch 语义),**不**跳出外层循环
|
||
# 期望 limit=5:i=0 走 _ (+0)、i=1 走 case 1 (+100) 后 break 出 match(循环继续)、
|
||
# i=2 走 case 2 (+10)、i=3 (+3)、i=4 (+4) ⇒ 117
|
||
# ============================================================
|
||
def MatchCaseBreakTest(limit: int) -> int:
|
||
i: int = 0
|
||
result: int = 0
|
||
for i in range(limit):
|
||
match i:
|
||
case 1:
|
||
result += 100
|
||
break
|
||
case 2:
|
||
result += 10
|
||
case _:
|
||
result += i
|
||
return result
|
||
|
||
|
||
# ============================================================
|
||
# switch(C switch 语义):默认不穿透;case 体内写 `fallthrough` 才落到下一个 case。
|
||
# 期望 value=1 → 1+2+3=6;value=2 → 2+3=5;value=3 → 3;其它 → 77
|
||
# ============================================================
|
||
def SwitchCaseFallthroughTest(value: int) -> int:
|
||
result: int = 0
|
||
switch value:
|
||
case 1:
|
||
result += 1
|
||
fallthrough
|
||
case 2:
|
||
result += 2
|
||
fallthrough
|
||
case 3:
|
||
result += 3
|
||
default:
|
||
result = 77
|
||
return result
|
||
|
||
|
||
# ============================================================
|
||
# switch 内 break = 跳出 switch(不跳出外层循环)
|
||
# 期望 limit=5:i=0 走 default (+0)、i=1 走 case 1 (+100) 后 break 出 switch、
|
||
# i=2 走 case 2 (+10)、i=3 (+3)、i=4 (+4) ⇒ 117
|
||
# ============================================================
|
||
def SwitchCaseBreakTest(limit: int) -> int:
|
||
i: int = 0
|
||
result: int = 0
|
||
for i in range(limit):
|
||
switch i:
|
||
case 1:
|
||
result += 100
|
||
break
|
||
case 2:
|
||
result += 10
|
||
default:
|
||
result += i
|
||
return result
|
||
|
||
|
||
# ============================================================
|
||
# switch 多值 case:`case 1 | 2:` 命中同一个臂
|
||
# 期望 value=1 → 10;value=2 → 10;value=3 → 30;其它 → 99
|
||
# ============================================================
|
||
def SwitchMultiValueTest(value: int) -> int:
|
||
result: int = 0
|
||
switch value:
|
||
case 1 | 2:
|
||
result = 10
|
||
case 3:
|
||
result = 30
|
||
default:
|
||
result = 99
|
||
return result |