"""Offline Scala semantic verification lab for Chapter 06 (literals)."""
import json
import re
VERSION = "CPython 3.12.14 offline Scala 2.13/3 semantic verifier (scalac absent)"
def case_integer_hex_and_octal_removal():
def parse_scala_int_literal(lit):
s = lit.strip()
is_long = s.endswith(("L", "l"))
body = s[:-1] if is_long else s
if re.fullmatch(r"-?0[xX][0-9a-fA-F]+", body):
return ("Long" if is_long else "Int", int(body, 16))
if re.fullmatch(r"-?0[0-9]+", body):
return ("Error", "octal_removed_since_2_10")
if re.fullmatch(r"-?(0|[1-9][0-9]*)", body):
return ("Long" if is_long else "Int", int(body, 10))
return ("Error", "invalid")
expected = {
"decimal": ("Int", 42),
"negative": ("Int", -99),
"hex": ("Int", 42),
"hex_ff": ("Int", 255),
"long_lit": ("Long", 10000000000),
"octal_rejected": ("Error", "octal_removed_since_2_10"),
}
actual = {
"decimal": parse_scala_int_literal("42"),
"negative": parse_scala_int_literal("-99"),
"hex": parse_scala_int_literal("0x2A"),
"hex_ff": parse_scala_int_literal("0XFF"),
"long_lit": parse_scala_int_literal("10000000000L"),
"octal_rejected": parse_scala_int_literal("077"),
}
return {
"name": "integer_hex_and_octal_removal",
"summary": "校验十进制、十六进制、Long 后缀字面量及 Scala 2.10+ 移除八进制字面量的规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_float_double_and_boolean_literals():
def classify_float(lit):
if lit.endswith(("f", "F")):
return ("Float", float(lit[:-1]))
return ("Double", float(lit))
expected = {
"pi_double": ("Double", 3.14),
"sci_double": ("Double", 1e-10),
"pi_float": ("Float", 3.14),
"neg_float": ("Float", -0.001),
"bool_pair": [True, False],
}
actual = {
"pi_double": classify_float("3.14"),
"sci_double": classify_float("1.0e-10"),
"pi_float": classify_float("3.14f"),
"neg_float": classify_float("-0.001F"),
"bool_pair": [True, False],
}
return {
"name": "float_double_and_boolean_literals",
"summary": "校验 Double 默认推断、f/F 后缀 Float 字面量与 true/false 布尔字面量",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_char_symbol_and_multiline_strip_margin():
def strip_margin(text, margin_char="|"):
lines = text.splitlines()
result = []
for line in lines:
stripped = line.lstrip(" \t")
if stripped.startswith(margin_char):
result.append(stripped[1:])
else:
result.append(line)
return "\n".join(result)
raw_multi = "This is\n |a multi-line\n |string"
cleaned = strip_margin(raw_multi)
symbol_repr = "'" + "x"
expected = {
"unicode_char": "A",
"symbol_to_string": "'x",
"multiline_lines": ["This is", "a multi-line", "string"],
}
actual = {
"unicode_char": "\u0041",
"symbol_to_string": symbol_repr,
"multiline_lines": cleaned.splitlines(),
}
return {
"name": "char_symbol_and_multiline_strip_margin",
"summary": "验证 Unicode 字符字面量、Symbol 字符串表示与多行字符串 stripMargin 对齐裁剪",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_null_and_unit_special_literals():
any_ref_types = {"String", "List", "Object"}
any_val_types = {"Int", "Double", "Boolean", "Char", "Unit"}
expected = {
"null_assignable_to_string": True,
"null_assignable_to_int": False,
"unit_singleton_value": "()",
}
actual = {
"null_assignable_to_string": "String" in any_ref_types,
"null_assignable_to_int": "Int" in any_ref_types and "Int" not in any_val_types,
"unit_singleton_value": "()",
}
return {
"name": "null_and_unit_special_literals",
"summary": "校验 null 仅兼容引用类型(AnyRef)而不兼容值类型(AnyVal)及 Unit 字面量 ()",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_integer_hex_and_octal_removal(),
case_float_double_and_boolean_literals(),
case_char_symbol_and_multiline_strip_margin(),
case_null_and_unit_special_literals(),
]
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-literals-lab/explore-literals.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-literals.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2409.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2424.zip