"""Offline Lua 5.4 semantic verification lab for Chapter 10 (miscellaneous-operator)."""
import json
VERSION = "CPython 3.12.14 offline Lua 5.4 semantic verifier (lua interpreter absent)"
def lua_truthy(v):
return not (v is False or v is None)
def lua_and(a, b_fn):
return a if not lua_truthy(a) else b_fn()
def lua_or(a, b_fn):
return a if lua_truthy(a) else b_fn()
def case_arithmetic_and_floor_division():
a, b = 21, 10
expected = {
"add": 31,
"sub": 11,
"mul": 210,
"div": 2.1,
"mod": 1,
"pow": 441,
"unm": -21,
"five_div_two": 2.5,
"five_floordiv_two": 2,
}
actual = {
"add": a + b,
"sub": a - b,
"mul": a * b,
"div": a / b,
"mod": a % b,
"pow": a ** 2,
"unm": -a,
"five_div_two": 5 / 2,
"five_floordiv_two": 5 // 2,
}
return {
"name": "arithmetic_and_floor_division",
"summary": "校验算术运算符 + - * / % ^ 负号以及 Lua 5.3+ 整除 // 与浮点除法 / 的区别",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_relational_operators_tilde_eq():
a, b = 21, 10
a2, b2 = 5, 20
expected = {
"eq": False,
"neq_tilde": True,
"lt": False,
"gt": True,
"le_after": True,
"ge_after": True,
"lua_neq_token": "~=",
}
actual = {
"eq": a == b,
"neq_tilde": a != b,
"lt": a < b,
"gt": a > b,
"le_after": a2 <= b2,
"ge_after": b2 >= a2,
"lua_neq_token": "~=",
}
return {
"name": "relational_operators_tilde_eq",
"summary": "校验关系运算符 ==、~=(非 !=)、>、<、>=、<= 的布尔求值结果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_logical_short_circuit_value_return():
side_effect = []
def probe(val):
side_effect.append(val)
return val
r1 = lua_and(False, lambda: probe("skipped_and"))
r2 = lua_or(0, lambda: probe("skipped_or")) # 0 is truthy in Lua!
r3 = lua_or(None, lambda: probe("default_val"))
r4 = not lua_truthy(lua_and(False, lambda: True))
expected = {
"false_and_x": False,
"zero_or_x_short_circuits": 0,
"nil_or_default": "default_val",
"not_false_and_true": True,
"evaluated_probes": ["default_val"],
}
actual = {
"false_and_x": r1,
"zero_or_x_short_circuits": r2,
"nil_or_default": r3,
"not_false_and_true": r4,
"evaluated_probes": side_effect,
}
return {
"name": "logical_short_circuit_value_return",
"summary": "校验 and/or 短路求值并返回实际操作数(含 0 为真短路)及 not 逻辑非",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_concat_length_and_precedence():
a, b = "Hello ", "World"
# Right associativity of ^ : 2^3^2 == 2^(3^2) == 512, -2^2 == -(2^2) == -4
pow_right = 2 ** (3 ** 2)
neg_pow = -(2 ** 2)
x, y, z, w = 20, 10, 15, 5
e1 = (x + y) * z / w
e2 = ((x + y) * z) / w
e3 = (x + y) * (z / w)
e4 = x + (y * z) / w
expected = {
"concat": "Hello World",
"len_world": 5,
"len_test": 4,
"len_mbsky": 13,
"pow_right_assoc": 512,
"unary_minus_below_pow": -4,
"prec_results": [90.0, 90.0, 90.0, 50.0],
}
actual = {
"concat": a + b,
"len_world": len(b),
"len_test": len("Test"),
"len_mbsky": len("www.mbsky.com"),
"pow_right_assoc": pow_right,
"unary_minus_below_pow": neg_pow,
"prec_results": [e1, e2, e3, e4],
}
return {
"name": "concat_length_and_precedence",
"summary": "校验 .. 字符串连接、# 长度运算符、^ 与 .. 右结合性及四则括号优先级",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_arithmetic_and_floor_division(),
case_relational_operators_tilde_eq(),
case_logical_short_circuit_value_return(),
case_concat_length_and_precedence(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Lua 5.4 语义验证器(本机未安装原生 lua/luac 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Lua 5.4 语义验证器(本机未安装原生 lua/luac 解释器)。
入口文件:projects/lua-miscellaneous-operator-lab/explore-miscellaneous-operator.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-miscellaneous-operator.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Lua 5.4 语义验证器(本机未安装原生 lua/luac 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2526.zip
课程关联工程包(登记关联23章) · 含当前章节或实例 · mbskycom202612a2510.zip