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

离线校验 Swift 首程序语法、SE 提案编号与本机工具链探测

PYTHONWindows x64、CPython 3.12.14 离线 Swift 5/6 语义验证器(本机未安装 swiftc/Xcode)。explore-tutorial.py
explore-tutorial.py105 行
"""Offline Swift 5/6 semantic verification lab for Chapter 01 (tutorial)."""
import json
import re
import shutil

VERSION = "CPython 3.12.14 offline Swift 5/6 semantic verifier (swiftc absent)"


def case_toolchain_absence_probe():
    swift_bin = shutil.which("swift")
    swiftc_bin = shutil.which("swiftc")
    expected = {"swift_found": False, "swiftc_found": False, "offline_mode": True}
    actual = {
        "swift_found": swift_bin is not None,
        "swiftc_found": swiftc_bin is not None,
        "offline_mode": swift_bin is None and swiftc_bin is None,
    }
    return {
        "name": "toolchain_absence_probe",
        "summary": "如实探测本机 swift 与 swiftc 可执行文件,确认当前处于离线语义验证环境",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_hello_world_source_and_print():
    src = '/* 我的第一个 Swift 程序 */\nvar myString = "Hello, World!"\nprint(myString)\n'
    decl = re.search(r'\bvar\s+(\w+)\s*=\s*"([^"]*)"', src)
    call = re.search(r"\bprint\(\s*(\w+)\s*\)", src)
    sym_table = {}
    if decl:
        sym_table[decl.group(1)] = {"kind": "var", "type": "String", "value": decl.group(2)}
    output = (sym_table[call.group(1)]["value"] + "\n") if (call and call.group(1) in sym_table) else ""
    expected = {
        "variable": "myString",
        "inferred_type": "String",
        "mutable_binding": True,
        "stdout": "Hello, World!\n",
    }
    actual = {
        "variable": decl.group(1) if decl else "",
        "inferred_type": sym_table.get("myString", {}).get("type", ""),
        "mutable_binding": sym_table.get("myString", {}).get("kind") == "var",
        "stdout": output,
    }
    return {
        "name": "hello_world_source_and_print",
        "summary": "解析首程序中的 var 声明、String 类型推断以及 print(_:) 标准输出语义",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_swift_evolution_proposal_id_rule():
    candidates = ["SE-0046", "SE-0296", "SE-0345", "README.md", "docs/pitch-draft"]
    se_pattern = re.compile(r"^SE-\d{4}$")
    formal = [c for c in candidates if se_pattern.match(c)]
    expected = {"formal_proposals": ["SE-0046", "SE-0296", "SE-0345"], "formal_count": 3}
    actual = {"formal_proposals": formal, "formal_count": len(formal)}
    return {
        "name": "swift_evolution_proposal_id_rule",
        "summary": "校验正式 Swift Evolution 提案编号 SE-xxxx 规则,区分权威提案与普通仓库文档",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_official_reference_catalog():
    refs = [
        ("The Swift Programming Language", "https://docs.swift.org/swift-book/"),
        ("Swift.org", "https://www.swift.org/"),
        ("Swift Evolution", "https://www.swift.org/swift-evolution/"),
        ("Swift 标准库文档", "https://developer.apple.com/documentation/swift"),
    ]
    expected = {"count": 4, "all_https": True, "domains": ["docs.swift.org", "www.swift.org", "www.swift.org", "developer.apple.com"]}
    actual = {
        "count": len(refs),
        "all_https": all(u.startswith("https://") for _, u in refs),
        "domains": [u.split("/")[2] for _, u in refs],
    }
    return {
        "name": "official_reference_catalog",
        "summary": "核对 Swift 官方语言指南、官网、提案库与标准库文档四项权威入口",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_toolchain_absence_probe(),
        case_hello_world_source_and_print(),
        case_swift_evolution_proposal_id_rule(),
        case_official_reference_catalog(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


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

运行环境:Windows x64、CPython 3.12.14 离线 Swift 5/6 语义验证器(本机未安装 swiftc/Xcode)。

入口文件:projects/swift-tutorial-lab/explore-tutorial.py

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

下载课程关联工程包:Swift

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

下载课程关联工程包:Swift

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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