蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验六类循环、next/redo/last 与 continue 块交互及受控无限循环

PYTHONWindows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。explore-loops.py
explore-loops.py159 行
"""Offline Perl 5 semantic verification lab for Chapter 11 (loops)."""
import json

VERSION = "CPython 3.12.14 offline Perl 5 semantic verifier (perl absent)"


def case_while_until_and_do_while():
    w_trace = []
    i = 0
    while i < 3:
        w_trace.append(i)
        i += 1

    u_trace = []
    j = 0
    while not (j >= 3):
        u_trace.append(j)
        j += 1

    dw_trace = []
    k = 10
    while True:
        dw_trace.append(k)
        k += 1
        if not (k < 5):
            break

    expected = {
        "while_trace": [0, 1, 2],
        "until_trace": [0, 1, 2],
        "do_while_runs_once": [10],
    }
    actual = {
        "while_trace": w_trace,
        "until_trace": u_trace,
        "do_while_runs_once": dw_trace,
    }
    return {
        "name": "while_until_and_do_while",
        "summary": "对比 while(先检真)、until(先检假)与 do...while(至少执行一次)三类条件循环",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_for_foreach_and_nested():
    for_sum = 0
    for idx in range(1, 6):
        for_sum += idx
    items = ["a", "b", "c"]
    foreach_out = [x.upper() for x in items]
    pairs = []
    for row in range(1, 3):
        for col in range(1, 3):
            pairs.append(f"{row}x{col}")
    expected = {
        "for_sum": 15,
        "foreach_out": ["A", "B", "C"],
        "nested_pairs": ["1x1", "1x2", "2x1", "2x2"],
    }
    actual = {
        "for_sum": for_sum,
        "foreach_out": foreach_out,
        "nested_pairs": pairs,
    }
    return {
        "name": "for_foreach_and_nested",
        "summary": "验证 C 风格 for、列表迭代 foreach 与嵌套循环矩阵遍历语义",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_next_last_redo_continue():
    seen = []
    continue_ticks = 0
    x = 0
    retried = False
    while x < 6:
        if x == 2:
            # next jumps to continue block before condition re-check
            continue_ticks += 1
            x += 1
            continue
        if x == 3 and not retried:
            # redo restarts current iteration without running continue block
            retried = True
            seen.append("redo_trigger")
            continue
        if x == 5:
            # last exits loop immediately without running continue block
            break
        seen.append(x)
        continue_ticks += 1
        x += 1

    expected = {
        "seen": [0, 1, "redo_trigger", 3, 4],
        "continue_ticks": 5,
        "stopped_at": 5,
    }
    actual = {
        "seen": seen,
        "continue_ticks": continue_ticks,
        "stopped_at": x,
    }
    return {
        "name": "next_last_redo_continue",
        "summary": "精确模拟 Perl next(经 continue)、redo(跳过 continue 重跑本轮)与 last(立即退出)差异",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_infinite_loop_and_goto_forms():
    emitted = []
    limit = 3
    count = 0
    while True:
        emitted.append("循环会无限执行。")
        count += 1
        if count >= limit:
            break
    goto_forms = ["goto LABEL", "goto EXPR", "goto &NAME"]
    expected = {
        "guarded_iterations": 3,
        "goto_form_count": 3,
        "has_tail_call_goto": True,
    }
    actual = {
        "guarded_iterations": len(emitted),
        "goto_form_count": len(goto_forms),
        "has_tail_call_goto": "goto &NAME" in goto_forms,
    }
    return {
        "name": "infinite_loop_and_goto_forms",
        "summary": "校验 for(;;) 无限循环受控退出模型以及 Perl 三种 goto 形式",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_while_until_and_do_while(),
        case_for_foreach_and_nested(),
        case_next_last_redo_continue(),
        case_infinite_loop_and_goto_forms(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。

入口文件:projects/perl-loops-lab/explore-loops.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-loops.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Perl

课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a2480.zip

下载课程关联工程包:Perl

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2494.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习