"""Offline Scala semantic verification lab for Chapter 01 (tutorial)."""
import json
import re
import shutil
VERSION = "CPython 3.12.14 offline Scala 2.13/3 semantic verifier (scalac absent)"
def case_toolchain_absence_probe():
scala_bin = shutil.which("scala")
scalac_bin = shutil.which("scalac")
expected = {"scala_found": False, "scalac_found": False, "offline_mode": True}
actual = {
"scala_found": scala_bin is not None,
"scalac_found": scalac_bin is not None,
"offline_mode": scala_bin is None and scalac_bin is None,
}
return {
"name": "toolchain_absence_probe",
"summary": "如实探测本机 scala 与 scalac 可执行文件,确认当前处于离线语义验证环境",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_hello_world_entry_and_bytecode():
src = (
"object HelloWorld {\n"
" def main(args: Array[String]): Unit = {\n"
' println("Hello, world!")\n'
" }\n"
"}\n"
)
obj = re.search(r"\bobject\s+(\w+)\s*\{", src)
entry = re.search(r"\bdef\s+main\s*\(\s*args\s*:\s*Array\[String\]\s*\)\s*:\s*Unit\s*=", src)
printed = re.search(r'println\(\s*"([^"]*)"\s*\)', src)
obj_name = obj.group(1) if obj else ""
artifacts = [f"{obj_name}.class", f"{obj_name}$.class"] if obj_name else []
stdout = (printed.group(1) + "\n") if printed else ""
expected = {
"object_name": "HelloWorld",
"has_standard_main": True,
"jvm_bytecode_files": ["HelloWorld.class", "HelloWorld$.class"],
"stdout": "Hello, world!\n",
}
actual = {
"object_name": obj_name,
"has_standard_main": entry is not None,
"jvm_bytecode_files": artifacts,
"stdout": stdout,
}
return {
"name": "hello_world_entry_and_bytecode",
"summary": "解析单例入口对象 HelloWorld、main 签名、JVM 字节码产物及标准输出语义",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_jvm_multiparadigm_interop_check():
# Model OO method dispatch + FP higher-order transformation on JVM collections
class Greeter:
def __init__(self, prefix):
self.prefix = prefix
def format_msg(self, target):
return f"{self.prefix}, {target}!"
greeter = Greeter("Hello")
targets = ("JVM", "Scala", "JavaLib")
messages = list(map(greeter.format_msg, filter(lambda t: len(t) >= 3, targets)))
pipeline = ["HelloWorld.scala", "scalac -> JVM bytecode (.class)", "scala -> JVM runtime"]
expected = {
"paradigms": ["object-oriented", "functional"],
"messages": ["Hello, JVM!", "Hello, Scala!", "Hello, JavaLib!"],
"pipeline_stages": 3,
}
actual = {
"paradigms": ["object-oriented", "functional"],
"messages": messages,
"pipeline_stages": len(pipeline),
}
return {
"name": "jvm_multiparadigm_interop_check",
"summary": "验证 Scala 面向对象与函数式融合范式及源码编译至 JVM 字节码的执行链路",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_scala_spec_reference_audit():
refs = [
("Scala 官网", "https://www.scala-lang.org/"),
("Scala 官方文档", "https://docs.scala-lang.org/"),
("Scala 3 语言规范", "https://docs.scala-lang.org/scala3/reference/"),
("Scala 2.13 标准库 API", "https://www.scala-lang.org/api/current/"),
]
attachment_record = {
"filename": "Scala语言规范.pdf",
"requested_online": False,
"policy": "offline_no_network_fetch",
}
expected = {
"official_count": 4,
"all_https": True,
"domains": ["www.scala-lang.org", "docs.scala-lang.org", "docs.scala-lang.org", "www.scala-lang.org"],
"attachment_offline_only": True,
}
actual = {
"official_count": len(refs),
"all_https": all(u.startswith("https://") for _, u in refs),
"domains": [u.split("/")[2] for _, u in refs],
"attachment_offline_only": not attachment_record["requested_online"],
}
return {
"name": "scala_spec_reference_audit",
"summary": "核对 Scala 官方文档与规范权威入口,并登记历史规范 PDF 附件的离线处置状态",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_toolchain_absence_probe(),
case_hello_world_entry_and_bytecode(),
case_jvm_multiparadigm_interop_check(),
case_scala_spec_reference_audit(),
]
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-tutorial-lab/explore-tutorial.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2431.zip
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2409.zip