蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验算术/赋值、双轨比较、位/逻辑陷阱、引号/杂项运算符与优先级

PYTHONWindows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。explore-operators.py
explore-operators.py190 行
"""Offline Perl 5 semantic verification lab for Chapter 12 (operators)."""
import json

VERSION = "CPython 3.12.14 offline Perl 5 semantic verifier (perl absent)"


def cmp_spaceship(a, b):
    return (a > b) - (a < b)


def case_arithmetic_and_assignment_ops():
    a, b = 10, 20
    arith = {
        "add": a + b,
        "sub": a - b,
        "mul": a * b,
        "div": a / b,
        "mod": a % b,
        "pow": 2 ** 4,
    }
    c = a + b
    c += a
    after_add = c
    c -= a
    after_sub = c
    c *= a
    after_mul = c
    c = c / a
    after_div = int(c)
    c = int(c) % a
    after_mod = c
    c2 = 2
    c2 **= 4
    expected = {
        "arith": {"add": 30, "sub": -10, "mul": 200, "div": 0.5, "mod": 10, "pow": 16},
        "assign_trace": [40, 30, 300, 30, 0, 16],
    }
    actual = {
        "arith": arith,
        "assign_trace": [after_add, after_sub, after_mul, after_div, after_mod, c2],
    }
    return {
        "name": "arithmetic_and_assignment_ops",
        "summary": "验证算术运算 (+ - * / % **) 与复合赋值运算 (= += -= *= /= %= **=) 逐步状态",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_numeric_vs_string_comparisons():
    a_num, b_num = 10, 20
    a_str, b_str = "abc", "xyz"
    expected = {
        "num_eq": False,
        "num_ne": True,
        "spaceship": -1,
        "num_lt_le": [True, True],
        "num_gt_ge": [False, False],
        "str_lt_le": [True, True],
        "str_gt_ge": [False, False],
        "str_eq_ne": [False, True],
        "str_cmp": -1,
        "pitfall_01_eq_1_numeric": True,
        "pitfall_01_eq_1_string": False,
    }
    actual = {
        "num_eq": a_num == b_num,
        "num_ne": a_num != b_num,
        "spaceship": cmp_spaceship(a_num, b_num),
        "num_lt_le": [a_num < b_num, a_num <= b_num],
        "num_gt_ge": [a_num > b_num, a_num >= b_num],
        "str_lt_le": [a_str < b_str, a_str <= b_str],
        "str_gt_ge": [a_str > b_str, a_str >= b_str],
        "str_eq_ne": [a_str == b_str, a_str != b_str],
        "str_cmp": cmp_spaceship(a_str, b_str),
        "pitfall_01_eq_1_numeric": int("01") == 1,
        "pitfall_01_eq_1_string": "01" == "1",
    }
    return {
        "name": "numeric_vs_string_comparisons",
        "summary": "对比数值比较 (== != <=> < <= > >=) 与字符串字典序比较 (eq ne cmp lt le gt ge) 及 '01' 差异",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_bitwise_and_logical_ops():
    a, b = 60, 13
    bitwise = {
        "and": a & b,
        "or": a | b,
        "xor": a ^ b,
        "not_signed": ~a,
        "lshift": a << 2,
        "rshift": a >> 2,
    }

    def perl_bool(v):
        return not (v is None or v == 0 or v == "" or v == "0")

    def perl_and(x, y):
        return y if perl_bool(x) else x

    def perl_or(x, y):
        return x if perl_bool(x) else y

    # Under Perl 5 without builtin true/false, bareword 'false' is a truthy non-empty string
    bareword_and = perl_and("true", "false")
    numeric_and = perl_and(1, 0)
    numeric_or = perl_or(1, 0)
    not_zero = 1 if not perl_bool(0) else ""
    expected = {
        "bitwise": {"and": 12, "or": 61, "xor": 49, "not_signed": -61, "lshift": 240, "rshift": 15},
        "bareword_false_is_truthy_pitfall": True,
        "numeric_and": 0,
        "numeric_or": 1,
        "not_zero": 1,
    }
    actual = {
        "bitwise": bitwise,
        "bareword_false_is_truthy_pitfall": perl_bool(bareword_and),
        "numeric_and": numeric_and,
        "numeric_or": numeric_or,
        "not_zero": not_zero,
    }
    return {
        "name": "bitwise_and_logical_ops",
        "summary": "验证 use integer 下位运算结果、短路逻辑返回值及裸词 false 在 Perl 5 中为真的常见陷阱",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_quote_misc_and_precedence():
    val_a = 10
    q_res = "a = $a"
    qq_res = f"a = {val_a}"
    concat_res = "blue" + "sky"
    repeat_res = "-" * 3
    range_res = list(range(2, 6))
    inc_val = 10 + 1
    dec_val = 15 - 1
    a, b, c, d = 20, 10, 15, 5
    e1 = (a + b) * c / d
    e2 = ((a + b) * c) / d
    e3 = (a + b) * (c / d)
    e4 = a + (b * c) / d
    expected = {
        "q_literal": "a = $a",
        "qq_interp": "a = 10",
        "concat": "bluesky",
        "repeat": "---",
        "range": [2, 3, 4, 5],
        "inc_dec": [11, 14],
        "precedence_results": [90.0, 90.0, 90.0, 50.0],
    }
    actual = {
        "q_literal": q_res,
        "qq_interp": qq_res,
        "concat": concat_res,
        "repeat": repeat_res,
        "range": range_res,
        "inc_dec": [inc_val, dec_val],
        "precedence_results": [e1, e2, e3, e4],
    }
    return {
        "name": "quote_misc_and_precedence",
        "summary": "验证 q/qq/qx 引号语义、. / x / .. / ++ / -- 杂项运算符以及括号与乘除优先级结果",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_arithmetic_and_assignment_ops(),
        case_numeric_vs_string_comparisons(),
        case_bitwise_and_logical_ops(),
        case_quote_misc_and_precedence(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。

入口文件:projects/perl-operators-lab/explore-operators.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-operators.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Perl

课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a2480.zip

下载课程关联工程包:Perl

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2496.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习