Phase 7a: bitwise operators & | ^ << >> ~ (retire band/bor/shl/…)

First step of the "stop feeling like C" pass. Bitwise ops were functions
(`band(x, MASK)`, `shl(a, 3)`); they are now real operators:

  band -> &   bor -> |   bxor -> ^   shl -> <<   shr -> >>   bnot -> ~

Precedence is Go-style so the C footgun is gone: `<<`/`>>`/`&` bind like `*`,
`|`/`^` like `+`, both tighter than comparison — `flags & MASK == 0` parses as
`(flags & MASK) == 0`. `>>` is logical (lshr), matching the old `shr`.

Mechanics: lexer tokenizes `<< >> & | ^ ~`; p_mul takes `<< >> &`, p_add takes
`| ^`, p_unary takes `~`; emit_bin routes them through the existing int arith
path (arith_code gains and/or/xor/shl/lshr) and E_UN handles `~`. The six
intrinsics are deleted.

Delivered as two reseeds: (A) add the operators keeping the intrinsics, (B)
migrate every call site to operator form (85 lines across compiler + runtime,
via a balanced-paren call->operator script; two multi-line big-endian reads in
image/truetype done by hand) and delete the intrinsics. Vocabulary drops the six
from LUDIC_INTRINSICS (ludic_syntax.h, grammar, LudicTokens.kt) and the grammar
gains a bitwise-operator rule. LANGUAGE.md precedence table rewritten.

Reseeded (21724 lines); C-free fixpoint holds; goldens byte-identical (the PNG
and TrueType decoders lean on these ops); 17/17; vocab + doc-fences clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-28 00:45:10 +03:00
parent 0e0c58519d
commit fcada23eba
18 changed files with 3417 additions and 3403 deletions

View file

@ -42,6 +42,11 @@ fn arith_code(op: ptr) -> ptr {
if streq(op, "-") { return "sub" }
if streq(op, "*") { return "mul" }
if streq(op, "/") { return "sdiv" }
if streq(op, "&") { return "and" }
if streq(op, "|") { return "or" }
if streq(op, "^") { return "xor" }
if streq(op, "<<") { return "shl" }
if streq(op, ">>") { return "lshr" } # logical shift (unsigned)
return "srem"
}
@ -170,6 +175,7 @@ fn emit_expr(e: Node) -> Val {
if e.kind == E_UN {
let a = emit_expr(e.a)
if streq(e.s, "-") { return val(emit_bind(sconcat("sub i32 0, ", a.code)), "int") }
if streq(e.s, "~") { return val(emit_bind(sconcat("xor i32 ", sconcat(a.code, ", -1"))), "int") }
let c = emit_bind(sconcat("icmp eq i32 ", sconcat(a.code, ", 0")))
return val(emit_bind(sconcat("zext i1 ", sconcat(c, " to i32"))), "bool")
}

View file

@ -14,7 +14,6 @@ fn is_intrinsic(name: ptr) -> bool {
if streq(name,"print_str") or streq(name,"print_int") { return true }
if streq(name,"os_argc") or streq(name,"os_arg") or streq(name,"os_exit") or streq(name,"file_stderr") { return true }
if streq(name,"os_system") or streq(name,"os_getenv") { return true }
if streq(name,"shl") or streq(name,"shr") or streq(name,"band") or streq(name,"bor") or streq(name,"bxor") or streq(name,"bnot") { return true }
if streq(name,"peek32") or streq(name,"poke32") { return true }
return false
}
@ -114,16 +113,7 @@ fn emit_intrinsic(name: ptr, e: Node) -> Val {
let n = arg_code(e, 0)
return val(emit_bind(sconcat("call ptr @getenv(ptr ", sconcat(n, ")"))), "str")
}
if streq(name, "bnot") { let a = arg_code(e, 0); return val(emit_bind(sconcat("xor i32 ", sconcat(a, ", -1"))), "int") }
if streq(name,"shl") or streq(name,"shr") or streq(name,"band") or streq(name,"bor") or streq(name,"bxor") {
let a = arg_code(e, 0); let b = arg_code(e, 1)
var opc = "shl"
if streq(name,"shr") { opc = "lshr" }
if streq(name,"band") { opc = "and" }
if streq(name,"bor") { opc = "or" }
if streq(name,"bxor") { opc = "xor" }
return val(emit_bind(sconcat(opc, sconcat(" i32 ", sconcat(a, sconcat(", ", b))))), "int")
}
# bitwise ops are the operators & | ^ << >> ~ now (see emit_bin / p_mul).
if streq(name, "peek32") {
let p = arg_code(e, 0); let i = arg_code(e, 1)
let g = nreg()

View file

@ -32,6 +32,7 @@ fn is_op1(c: int) -> bool {
if c == 40 or c == 41 or c == 123 or c == 125 { return true }
if c == 91 or c == 93 or c == 44 or c == 58 or c == 46 { return true }
if c == 33 or c == 64 { return true }
if c == 38 or c == 124 or c == 94 or c == 126 { return true } # & | ^ ~
return false
}
@ -105,7 +106,7 @@ fn lex(src: ptr) -> void {
i = i + 1
var fnum = 0; var fden = 1
while i < n and char_is_digit(peek8(src, i)) { fnum = fnum * 10 + (peek8(src, i) - 48); fden = fden * 10; i = i + 1 }
let bits = shl(v, 16) + shl(fnum, 16) / fden
let bits = (v << 16) + (fnum << 16) / fden
tok_push(TK_FLOAT, ptr_null(), bits, line)
continue
}
@ -131,6 +132,8 @@ fn lex(src: ptr) -> void {
if two_at(src, i, 42, 61) { tok_push(TK_OP, substr(src, i, 2), 0, line); i = i + 2; continue } # *=
if two_at(src, i, 47, 61) { tok_push(TK_OP, substr(src, i, 2), 0, line); i = i + 2; continue } # /=
if two_at(src, i, 46, 46) { tok_push(TK_OP, substr(src, i, 2), 0, line); i = i + 2; continue } # ..
if two_at(src, i, 60, 60) { tok_push(TK_OP, substr(src, i, 2), 0, line); i = i + 2; continue } # <<
if two_at(src, i, 62, 62) { tok_push(TK_OP, substr(src, i, 2), 0, line); i = i + 2; continue } # >>
if is_op1(c) { tok_push(TK_OP, substr(src, i, 1), 0, line); i = i + 1; continue }
i = i + 1 # skip anything unrecognised
}

File diff suppressed because it is too large Load diff

View file

@ -148,6 +148,6 @@ entry {
# run mode: execute the binary we just built and forward its exit code
if run {
let st = os_system(out)
os_exit(band(shr(st, 8), 255))
os_exit(((st >> 8) & 255))
}
}

View file

@ -82,20 +82,25 @@ fn p_postfix() -> Node {
fn p_unary() -> Node {
if is_op("-") { pi = pi + 1; let n = node(E_UN); n.s = "-"; n.a = p_unary(); return n }
if is_op("~") { pi = pi + 1; let n = node(E_UN); n.s = "~"; n.a = p_unary(); return n } # bitwise not
if is_id("not") { pi = pi + 1; let n = node(E_UN); n.s = "not"; n.a = p_unary(); return n }
return p_postfix()
}
fn mkbin(op: ptr, l: Node, r: Node) -> Node { let b = node(E_BIN); b.s = op; b.a = l; b.b = r; return b }
# precedence (Go-style, so `flags & MASK == 0` needs no parens): shifts and `&`
# bind like `*`; `|` and `^` bind like `+`; both tighter than comparison.
fn p_mul() -> Node {
var l = p_unary()
while is_op("*") or is_op("/") or is_op("%") { let op = toks[pi].text; pi = pi + 1; l = mkbin(op, l, p_unary()) }
while is_op("*") or is_op("/") or is_op("%") or is_op("<<") or is_op(">>") or is_op("&") {
let op = toks[pi].text; pi = pi + 1; l = mkbin(op, l, p_unary()) }
return l
}
fn p_add() -> Node {
var l = p_mul()
while is_op("+") or is_op("-") { let op = toks[pi].text; pi = pi + 1; l = mkbin(op, l, p_mul()) }
while is_op("+") or is_op("-") or is_op("|") or is_op("^") {
let op = toks[pi].text; pi = pi + 1; l = mkbin(op, l, p_mul()) }
return l
}
fn p_cmp() -> Node {

View file

@ -7,8 +7,8 @@ program T {
print_int(classify(65)) # 1
print_int(classify(67)) # 2
print_int(classify(90)) # 9
print_int(shl(1, 4)) # 16
print_int(band(bor(4, 1), 6)) # 4
print_int((1 << 4)) # 16
print_int(((4 | 1) & 6)) # 4
let p = mem_alloc(16)
poke32(p, 1, 9999)
print_int(peek32(p, 1)) # 9999