"""Offline Kotlin 2.x semantic verifier for Chapter 02 (intro)."""
import json
import sys
VERSION = "CPython 3.12.14 offline Kotlin 2.x semantic verifier (kotlinc absent)"
class Site:
"""Models Kotlin `class Site(val name: String, val url: String)`."""
def __init__(self, name: str, url: str) -> None:
self._name = name
self._url = url
@property
def name(self) -> str:
return self._name
@property
def url(self) -> str:
return self._url
def introduce(self) -> str:
return f"欢迎访问 {self.name}({self.url})"
def run_cases() -> list[dict]:
cases: list[dict] = []
# Case 1: Kotlin version timeline milestones (2011 -> 2025 / Kotlin 2.2)
milestones = [
(2011, "Kotlin 首次公开"),
(2016, "Kotlin 1.0"),
(2017, "Google I/O 宣布"),
(2019, "Kotlin-first"),
(2020, "Kotlin 1.4"),
(2021, "Kotlin 1.5"),
(2022, "Kotlin 1.7"),
(2023, "Kotlin 1.9"),
(2024, "Kotlin 2.0"),
(2025, "Kotlin 2.2"),
]
years = [y for y, _ in milestones]
actual_1 = {
"count": len(milestones),
"strictly_increasing_years": years == sorted(set(years)),
"k2_stable_year": dict((v, y) for y, v in milestones)["Kotlin 2.0"],
"target_release": milestones[-1][1],
}
expected_1 = {
"count": 10,
"strictly_increasing_years": True,
"k2_stable_year": 2024,
"target_release": "Kotlin 2.2",
}
cases.append({
"name": "kotlin_release_timeline_model",
"summary": "Verify chronological ordering of 10 Kotlin milestones from 2011 to Kotlin 2.2 (2025).",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Four official compiler backends & multi-target compilation model
backends = {
"Kotlin/JVM": "Java 虚拟机",
"Kotlin/JS": "浏览器与 Node.js",
"Kotlin/Wasm": "WebAssembly",
"Kotlin/Native": "原生机器码",
}
actual_2 = {
"backend_count": len(backends),
"supports_jvm_interop": "Kotlin/JVM" in backends,
"supports_wasm": "Kotlin/Wasm" in backends,
"script_extension": ".kts",
}
expected_2 = {
"backend_count": 4,
"supports_jvm_interop": True,
"supports_wasm": True,
"script_extension": ".kts",
}
cases.append({
"name": "four_compiler_backends_model",
"summary": "Verify the four official Kotlin compiler backends and script extension .kts.",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Parameterless main() output (Kotlin 1.3+)
hello_lines = ["Hello, Kotlin!", "欢迎来到 www.mbsky.com"]
actual_3 = {
"lines": hello_lines,
"line_count": len(hello_lines),
"utf8_roundtrip": all(s.encode("utf-8").decode("utf-8") == s for s in hello_lines),
}
expected_3 = {
"lines": ["Hello, Kotlin!", "欢迎来到 www.mbsky.com"],
"line_count": 2,
"utf8_roundtrip": True,
}
cases.append({
"name": "hello_main_utf8_output",
"summary": "Model Kotlin parameterless main() printing two UTF-8 lines.",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: OO Site class expression-body method & string template expression
site = Site("蓝色天空网", "www.mbsky.com")
actual_4 = {
"introduce": site.introduce(),
"name_length": len(site.name),
"template_line": f"站点名称长度:{len(site.name)}",
}
expected_4 = {
"introduce": "欢迎访问 蓝色天空网(www.mbsky.com)",
"name_length": 5,
"template_line": "站点名称长度:5",
}
cases.append({
"name": "site_class_expression_body_and_length",
"summary": "Model Site(val name, val url), expression-body introduce(), and ${site.name.length} evaluation.",
"expected": expected_4,
"actual": actual_4,
"passed": actual_4 == expected_4,
})
return cases
def main() -> None:
payload = {"version": VERSION, "cases": run_cases()}
sys.stdout.write(json.dumps(payload, ensure_ascii=False, indent=2) + "\n")
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Kotlin 2.x 语义验证器(本地未安装 kotlinc/JVM Kotlin 工具链)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Kotlin 2.x 语义验证器(本地未安装 kotlinc/JVM Kotlin 工具链)。
入口文件:projects/kotlin-intro-lab/explore-intro.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-intro.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Kotlin 2.x 语义验证器(本地未安装 kotlinc/JVM Kotlin 工具链)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联43章) · 含当前章节或实例 · mbskycom202612a2132.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2153.zip