include(joinpath(@__DIR__, "observations.jl"))
properties(text) = (Base.operator_precedence(Symbol(text)), Base.operator_associativity(Symbol(text)))
x, y = -11, 4
probe("op_arith_0", (x,y), +x, -11)
probe("op_arith_1", (x,y), -x, 11)
probe("op_arith_2", (x,y), x+y, -7)
probe("op_arith_3", (x,y), x-y, -15)
probe("op_arith_4", (x,y), x*y, -44)
probe("op_arith_5", (x,y), (x/y,(x//1)/(y//1),1/0), (-2.75,-11//4,Inf))
probe("op_arith_6", (x,y), (x÷y,outcome(() -> x÷0)), (-2,"DivideError"))
probe("op_arith_7", (x,y), (x\y) == y/x, true)
probe("op_arith_8", (x,y), x^y, 14641)
probe("op_arith_9", (x,y), (x%y,rem(x,y)), (-3,-3))
probe("op_bool_0", raw"!false", !false, true)
probe("op_bool_1", raw"(true && (7,11), false && error())", (true && (7,11), false && error()), ((7,11),false))
probe("op_bool_2", raw"(false || (7,11), true || error())", (false || (7,11), true || error()), ((7,11),true))
probe("op_cmp_0", raw"(7==7.0,7==8)", (7==7.0,7==8), (true,false))
probe("op_cmp_1", raw"(7!=8,7≠8)", (7!=8,7≠8), (true,true))
probe("op_cmp_2", raw"7<11", 7<11, true)
probe("op_cmp_3", raw"(7<=7.0,-7≤-11)", (7<=7.0,-7≤-11), (true,false))
probe("op_cmp_4", raw"7>11", 7>11, false)
probe("op_cmp_5", raw"(11>=7,7≥7.0)", (11>=7,7≥7.0), (true,true))
probe("op_bit_0", raw"(~Int8(5),~UInt8(5),~UInt32(5))", (~Int8(5),~UInt8(5),~UInt32(5)), (Int8(-6),0xfa,0xfffffffa))
probe("op_bit_1", raw"0x05&0x03", 0x05&0x03, 0x01)
probe("op_bit_2", raw"0x05|0x03", 0x05|0x03, 0x07)
probe("op_bit_3", raw"(0x05⊻0x03,xor(0x05,0x03))", (0x05⊻0x03,xor(0x05,0x03)), (0x06,0x06))
probe("op_bit_4", raw"(0x05⊼0x03,nand(0x05,0x03))", (0x05⊼0x03,nand(0x05,0x03)), (0xfe,0xfe))
probe("op_bit_5", raw"(0x05⊽0x03,nor(0x05,0x03))", (0x05⊽0x03,nor(0x05,0x03)), (0xf8,0xf8))
probe("op_bit_6", raw"Int8(-8) >>> 2", Int8(-8) >>> 2, Int8(62))
probe("op_bit_7", raw"Int8(-8) >> 2", Int8(-8) >> 2, Int8(-2))
probe("op_bit_8", raw"UInt8(0x45) << 2", UInt8(0x45) << 2, 0x14)
probe("op_update_0", (7,7 + 11), (let x=7; x = 7 + 11; x end), 18)
probe("op_update_1", (7,3), (let x=7; x += 3; x end), 10)
probe("op_update_2", (7,3), (let x=7; x -= 3; x end), 4)
probe("op_update_3", (7,3), (let x=7; x *= 3; x end), 21)
probe("op_update_4", (7,2), (let x=7; x /= 2; x end), 3.5)
probe("op_update_5", (7,14), (let x=7; x \= 14; x end), 2.0)
probe("op_update_6", (7,3), (let x=7; x ÷= 3; x end), 2)
probe("op_update_7", (7,3), (let x=7; x %= 3; x end), 1)
probe("op_update_8", (7,2), (let x=7; x ^= 2; x end), 49)
probe("op_update_9", (7,3), (let x=7; x //= 3; x end), 7//3)
probe("op_update_10", (7,2), (let x=7; x <<= 2; x end), 28)
probe("op_update_11", (7,2), (let x=7; x >>= 2; x end), 1)
probe("op_update_12", (7,2), (let x=7; x >>>= 2; x end), 1)
probe("op_update_13", (7,3), (let x=7; x &= 3; x end), 3)
probe("op_update_14", (7,8), (let x=7; x |= 8; x end), 15)
probe("op_update_15", (7,3), (let x=7; x ⊻= 3; x end), 4)
probe("op_order_0", first((".",(inner=(value=7,),).inner.value)), (properties(first((".",(inner=(value=7,),).inner.value))),last((".",(inner=(value=7,),).inner.value))), ((17,:left),7))
probe("op_order_1", first(("^",2^3^2)), (properties(first(("^",2^3^2))),last(("^",2^3^2))), ((15,:right),512))
probe("op_order_2", first(("+",(+7,-2^2,!!false,~UInt8(5),√9,∛8))), (properties(first(("+",(+7,-2^2,!!false,~UInt8(5),√9,∛8)))),last(("+",(+7,-2^2,!!false,~UInt8(5),√9,∛8)))), ((11,:none),(7,-4,false,0xfa,3.0,2.0)))
probe("op_order_3", first(("<<",8 >> 1 >> 1)), (properties(first(("<<",8 >> 1 >> 1))),last(("<<",8 >> 1 >> 1))), ((14,:left),2))
probe("op_order_4", first(("//",12//3//2)), (properties(first(("//",12//3//2))),last(("//",12//3//2))), ((13,:left),2//1))
probe("op_order_5", first(("*",7+3*2)), (properties(first(("*",7+3*2))),last(("*",7+3*2))), ((12,:none),13))
probe("op_order_6", first(("+",11-3-2)), (properties(first(("+",11-3-2))),last(("+",11-3-2))), ((11,:none),6))
probe("op_order_7", first((":",(collect(2:4),isdefined(Base,Symbol("..")),(2:4...,)))), (properties(first((":",(collect(2:4),isdefined(Base,Symbol("..")),(2:4...,))))),last((":",(collect(2:4),isdefined(Base,Symbol("..")),(2:4...,))))), ((10,:left),([2,3,4],false,(2,3,4))))
probe("op_order_8", first(("|>",(properties("<|"),3 |> abs2 |> sqrt,outcome(() -> sqrt <| abs2 <| 3)))), (properties(first(("|>",(properties("<|"),3 |> abs2 |> sqrt,outcome(() -> sqrt <| abs2 <| 3))))),last(("|>",(properties("<|"),3 |> abs2 |> sqrt,outcome(() -> sqrt <| abs2 <| 3))))), ((9,:left),((8,:right),3.0,"UndefVarError")))
probe("op_order_9", first(("<",(2<3<=3,Int<:Integer,properties("<:")))), (properties(first(("<",(2<3<=3,Int<:Integer,properties("<:"))))),last(("<",(2<3<=3,Int<:Integer,properties("<:"))))), ((7,:none),(true,true,(7,:right))))
probe("op_order_10", first(("&&",(properties("||"),true || false && false))), (properties(first(("&&",(properties("||"),true || false && false)))),last(("&&",(properties("||"),true || false && false)))), ((6,:right),((5,:right),true)))
probe("op_order_11", first(("?",false ? 7 : true ? 11 : 13)), (properties(first(("?",false ? 7 : true ? 11 : 13))),last(("?",false ? 7 : true ? 11 : 13))), ((3,:left),11))
probe("op_order_12", first(("=>",(:a=>:b=>7).second.first)), (properties(first(("=>",(:a=>:b=>7).second.first))),last(("=>",(:a=>:b=>7).second.first))), ((2,:right),:b))
probe("op_order_13", first(("=",(let a=b=7; (a,b) end))), (properties(first(("=",(let a=b=7; (a,b) end)))),last(("=",(let a=b=7; (a,b) end)))), ((1,:right),(7,7)))
probe("op_arithmetic_walk", (11,3,7),
(11+3+7,3-7,3*2/12,2+20-5,50*2/10,23%2,2^4,7÷2,1/0), (21,-4,.5,17,10.0,1,16,3,Inf))
probe("op_coefficient", 3, (let v=3; (2v,6/2v,2v^2) end), (6,1.0,18))
probe("op_boolean_walk", (true,false), (!true,!false,true&&(7,11),false&&(7,11),false||(7,11)),
(false,true,(7,11),false,(7,11)))
probe("op_comparison_walk", (7,8,11), (7==7,7==8,7!=8,7==7.0,7<11,7>11,7>=7.0,-7<=-11),
(true,false,true,true,true,false,true,false))
samples = [.25,1.5,.75]
probe("op_chains", samples, (1<2<=2<3==3>2>=1==1<3!=5,0 .< samples .< 1),
(true,BitVector([true,false,true])))
seen = Int[]
mark(n) = (push!(seen,n); n)
forward = mark(2) < mark(3) <= mark(5)
first_order = copy(seen)
empty!(seen)
early = mark(2) > mark(3) <= mark(5)
probe("op_chain_side_effect", (2,3,5), (forward,first_order,early,seen), (true,[3,2,5],false,[3,2]))
probe("op_bits_walk", (UInt8(5),UInt8(3)), (~UInt8(5),0x05&0x03,0x05|0x03,0x05⊻0x03,
nand(0x05,0x05),nor(0x05,0x03),~UInt32(5)), (0xfa,0x01,0x07,0x06,0xfa,0xf8,0xfffffffa))
v = 2
v += 3
v ^= 2
probe("op_update_walk", (2,3,2), v, 25)
u = 0x01
before = typeof(u)
u *= 2
probe("op_update_type", (0x01,2), (before,typeof(u),u), (UInt8,Int,2))
data = [2.0,3.0,4.0]
formula = 2 .* data.^2 .+ sin.(data)
expanded = @. 2data^2 + sin(data)
assigned = copy(data)
peer = assigned
assigned .+= 2
probe("op_dot_fusion", data,
(formula ≈ expanded,formula ≈ map(t -> 2t^2+sin(t), data),assigned===peer,peer),
(true,true,true,[4.0,5.0,6.0]))
probe("op_dot_power", [2,3], (([2,3].^3),outcome(() -> [2,3]^3),(.√ [4,9]),(.! [false,true])),
([8,27],"MethodError",[2.0,3.0],[true,false]))
probe("op_dot_whitespace", "1.+[2,3] vs 1 .+ [2,3] vs 1. + [2,3]",
(outcome(() -> Meta.parse("1.+[2,3]")),1 .+ [2,3],outcome(() -> 1. + [2,3])),
("ParseError",[3,4],"MethodError"))
probe("op_query_precedence", ("+","*",".","sin","+=","="),
map(text -> first(properties(text)),("+","*",".","sin","+=","=")), (11,12,17,0,1,1))
probe("op_query_associativity", ("-","+","^","⊗","sin","→"),
map(text -> last(properties(text)),("-","+","^","⊗","sin","→")), (:left,:none,:right,:left,:none,:right))
本例需在 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Julia 1.10.13 官方便携版,Windows x64,本机隔离目录
入口文件:projects/julia-number-operators/operators.jl
本地运行步骤快速上手
- 点击上方“保存源码”保存为
operators.jl,或下载右侧课程工程包。 - 确认本地已准备 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a205.zip
课程关联工程包(登记关联2章) · 含当前章节或实例 · mbskycom202612a213.zip