"""Offline Scala semantic verification lab for Chapter 10 (operators)."""
import json
VERSION = "CPython 3.12.14 offline Scala 2.13/3 semantic verifier (scalac absent)"
def case_arithmetic_and_relational_ops():
a, b, c = 10, 20, 25
expected = {
"add": 30, "sub": -10, "mul": 200, "div": 2, "mod_ba": 0, "mod_ca": 5,
"eq": False, "ne": True, "gt": False, "lt": True, "ge_ba": True, "le_ba": False,
}
actual = {
"add": a + b, "sub": a - b, "mul": a * b, "div": b // a, "mod_ba": b % a, "mod_ca": c % a,
"eq": a == b, "ne": a != b, "gt": a > b, "lt": a < b, "ge_ba": b >= a, "le_ba": b <= a,
}
return {
"name": "arithmetic_and_relational_ops",
"summary": "校验算术运算符(+ - * / %)与关系运算符(== != > < >= <=)的计算结果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_logical_short_circuit_and_bool_only():
a, b = True, False
evaluated = []
def rhs():
evaluated.append("rhs_called")
return True
short_and = b and rhs()
short_or = a or rhs()
expected = {
"a_and_b": False,
"a_or_b": True,
"not_a_and_b": True,
"short_circuit_prevented_rhs": True,
}
actual = {
"a_and_b": a and b,
"a_or_b": a or b,
"not_a_and_b": not (a and b),
"short_circuit_prevented_rhs": (not short_and) and short_or and len(evaluated) == 0,
}
return {
"name": "logical_short_circuit_and_bool_only",
"summary": "验证布尔逻辑运算符(&& || !)的真值结果与短路求值特性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_bitwise_32bit_and_unsigned_shift():
def to_int32(x):
x &= 0xFFFFFFFF
return x - 0x100000000 if x & 0x80000000 else x
def unsigned_rshift32(x, n):
return (x & 0xFFFFFFFF) >> (n & 31)
a, b = 60, 13
truth_table = [(p, q, p & q, p | q, p ^ q) for p, q in [(0, 0), (0, 1), (1, 1), (1, 0)]]
expected = {
"and": 12, "or": 61, "xor": 49, "not_a": -61,
"shl2": 240, "shr2": 15, "ushr2": 15,
"neg_ushr1": 2147483647,
"truth_rows": 4,
}
actual = {
"and": a & b, "or": a | b, "xor": a ^ b, "not_a": to_int32(~a),
"shl2": to_int32(a << 2), "shr2": a >> 2, "ushr2": unsigned_rshift32(a, 2),
"neg_ushr1": unsigned_rshift32(-1, 1),
"truth_rows": len(truth_table),
}
return {
"name": "bitwise_32bit_and_unsigned_shift",
"summary": "验证 32 位有符号补码按位运算(& | ^ ~ << >>)与无符号右移(>>>)语义",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_compound_assignment_and_precedence():
a, b = 10, 20
c = a + b # 30
trace = [c]
c += a; trace.append(c) # 40
c -= a; trace.append(c) # 30
c *= a; trace.append(c) # 300
a, c = 10, 15
c //= a; trace.append(c) # 1
a, c = 10, 15
c %= a; trace.append(c) # 5
c <<= 2; trace.append(c) # 20
c >>= 2; trace.append(c) # 5
c >>= a; trace.append(c) # 0
c &= a; trace.append(c) # 0
c ^= a; trace.append(c) # 10
c |= a; trace.append(c) # 10
precedence_val = 7 + 3 * 2
right_assoc_cons = [1] + [2] + [3] # 1 :: 2 :: 3 :: Nil associates right-to-left
expected = {
"assignment_trace": [30, 40, 30, 300, 1, 5, 20, 5, 0, 0, 10, 10],
"seven_plus_three_times_two": 13,
"colon_ending_is_right_assoc": True,
"cons_list": [1, 2, 3],
}
actual = {
"assignment_trace": trace,
"seven_plus_three_times_two": precedence_val,
"colon_ending_is_right_assoc": "::".endswith(":"),
"cons_list": right_assoc_cons,
}
return {
"name": "compound_assignment_and_precedence",
"summary": "验证复合赋值链逐步轨迹、乘加优先级(7+3*2=13)以及以冒号结尾方法的右结合规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_arithmetic_and_relational_ops(),
case_logical_short_circuit_and_bool_only(),
case_bitwise_32bit_and_unsigned_shift(),
case_compound_assignment_and_precedence(),
]
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-operators-lab/explore-operators.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-operators.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2426.zip
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2409.zip