writeonce/docs/examples/operators/main.wo
shoney.arickathil ed6bfeea3d feat: iteration 36 task 5 — operators sample, docs closeout
- docs/examples/operators: manual-test workload (ok/FAIL lines per
  expression; trap mode proves WO_T_SHIFT); NO test fixtures by
  developer directive — acceptance is the manual pass
- 00-wob-format.md: v6 section (opcodes 42-46, T_SHIFT, header v6)
- CODE-LOGIC.md both sides: precedence-into-existing-rungs, Lua not,
  rewind-and-reparse compound assigns, trap-not-mask, 63-bit hex limit
- story 36 refine -> in-progress with landing blockquote; board updated
- gates: woc-test 543/0, wovm-test ASan, oop-accept ALL MET,
  deps-accept 8/0, web-app 26/0

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-22 21:42:08 +02:00

74 lines
2.5 KiB
Text

-- Operator parity — iteration 36's manual-test workload. Every section
-- prints `label expected actual`: eyeball that the two numbers agree.
-- Build & run: woc . && ./target/operators
-- Trap demo: ./target/operators trap (shift count 64 at run time
-- must die with `trap 12 ... shift count out of range`)
fn expect(label: Text, expected: Int, actual: Int) {
if expected == actual {
print("ok ${label} = ${actual}")
} else {
print("FAIL ${label} expected ${expected} got ${actual}")
}
}
fn shift_by(n: Int) -> Int {
-- variable count: the compiler cannot see 64 here, so an out-of-range
-- value reaches the VM and traps WO_T_SHIFT (the DIV0 precedent)
return 1 << n
}
fn main(args: multi Text) -> Int {
if len(args) >= 1 and args[0] == "trap" {
print("about to shift by 64 — expect trap 12, not a number:")
print_int(shift_by(64))
return 1
}
-- literals: hex, binary, underscore separators
expect("0xFF", 255, 0xFF)
expect("0b1010_1010", 170, 0b1010_1010)
expect("1_000_000", 1000000, 1_000_000)
-- the five bitwise operators, Int-only
expect("12 & 10", 8, 12 & 10)
expect("12 | 3", 15, 12 | 3)
expect("12 ^ 10", 6, 12 ^ 10)
expect("1 << 6", 64, 1 << 6)
expect("-16 >> 2 (arithmetic)", -4, -16 >> 2)
expect("complement -1 ^ 0xF0", -241, -1 ^ 0xF0)
-- precedence: Go's C-trap fix — & << >> bind with * / %, | ^ with + -
expect("1 << 4 + 1 groups (1<<4)+1", 17, 1 << 4 + 1)
expect("2 | 1 * 4 groups 2|(1*4)", 6, 2 | 1 * 4)
if 0b1010_1010 & 0xFF == 0 {
print("FAIL grouping: & lost to ==")
} else {
print("ok x & mask == 0 groups (x & mask) == 0")
}
-- not: boolean negation, a word like and/or
if not false { print("ok not false") }
if not (1 > 2) and not false or false { print("ok not chains with and/or") }
-- Lua placement: not binds tighter than ==
if (not true) == false { print("ok not a == b groups (not a) == b") }
-- compound assigns (+= -= existed as dead tokens; all five live now)
let acc: Int = 10
acc += 5
acc -= 1
acc *= 3
acc /= 2
acc %= 7
expect("10 +=5 -=1 *=3 /=2 %=7", 0, acc)
-- the consumer that motivated the iteration (story 34): HMAC's
-- ipad/opad step is byte ^ constant — RFC 2104's pads, on the byte
-- values of "key"
let k: Text = "key"
expect("'k' ^ 0x36 (ipad)", 93, byte_at(k, 0) ^ 0x36)
expect("'k' ^ 0x5c (opad)", 55, byte_at(k, 0) ^ 0x5C)
print("done — compare every line above by eye; any FAIL is a defect")
return 0
}