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

离线校验 while/do-while 边界、for-yield 推导与 Breaks 中断控制

PYTHONWindows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。explore-loop-types.py
explore-loop-types.py123 行
"""Offline Scala semantic verification lab for Chapter 12 (loop-types)."""
import json

VERSION = "CPython 3.12.14 offline Scala 2.13/3 semantic verifier (scalac absent)"


def case_while_and_do_while_iteration():
    # while evaluates condition first; do...while executes body at least once
    w_trace = []
    a = 10
    while a < 10:
        w_trace.append(a)
        a += 1

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

    expected = {"while_runs_when_false": [], "do_while_runs_once": [10]}
    actual = {"while_runs_when_false": w_trace, "do_while_runs_once": dw_trace}
    return {
        "name": "while_and_do_while_iteration",
        "summary": "对比 while 先判后执行与 do...while 先执行一次再判条件的边界行为",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_for_range_guard_and_yield():
    to_inclusive = list(range(1, 6))       # 1 to 5
    until_exclusive = list(range(1, 5))    # 1 until 5
    guarded_yield = [x * 2 for x in range(1, 10) if x != 3 and x < 8]
    expected = {
        "one_to_five": [1, 2, 3, 4, 5],
        "one_until_five": [1, 2, 3, 4],
        "for_yield_guarded": [2, 4, 8, 10, 12, 14],
    }
    actual = {
        "one_to_five": to_inclusive,
        "one_until_five": until_exclusive,
        "for_yield_guarded": guarded_yield,
    }
    return {
        "name": "for_range_guard_and_yield",
        "summary": "验证 for 循环中 to(闭区间)、until(半开区间)、守卫过滤与 yield 推导结果",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_breaks_control_interrupt():
    # Model scala.util.control.Breaks: breakable { ... if (a == 4) break() }
    class BreakControl(Exception):
        pass

    visited = []
    try:
        for a in range(1, 11):
            visited.append(a)
            if a == 4:
                raise BreakControl()
    except BreakControl:
        pass

    expected = {"visited_until_break": [1, 2, 3, 4], "stopped_at": 4}
    actual = {"visited_until_break": visited, "stopped_at": visited[-1]}
    return {
        "name": "breaks_control_interrupt",
        "summary": "模拟 Scala 2.8+ scala.util.control.Breaks 的 breakable/break 异常控制流中断机制",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_infinite_loop_guard_semantics():
    # Safely bound the infinite `while (true)` pattern with an explicit step cap
    emitted = []
    a = 10
    steps = 0
    while True:
        emitted.append(f"a 的值为 : {a}")
        steps += 1
        if steps >= 3:
            break
    expected = {
        "constant_condition": True,
        "sample_lines": ["a 的值为 : 10", "a 的值为 : 10", "a 的值为 : 10"],
        "requires_external_interrupt": True,
    }
    actual = {
        "constant_condition": True,
        "sample_lines": emitted,
        "requires_external_interrupt": True,
    }
    return {
        "name": "infinite_loop_guard_semantics",
        "summary": "安全有界模拟 while(true) 无限循环输出模式并验证需外部中断(Ctrl+C)或退出守卫",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_while_and_do_while_iteration(),
        case_for_range_guard_and_yield(),
        case_breaks_control_interrupt(),
        case_infinite_loop_guard_semantics(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


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

运行环境:Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。

入口文件:projects/scala-loop-types-lab/explore-loop-types.py

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

下载课程关联工程包:Scala

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

下载课程关联工程包:Scala

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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