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

@ -571,14 +571,17 @@ whose battle menus dispatch on `KnightAct`/`MageAct` instead of `0..3`.
## Expressions ## Expressions
Precedence: `or → and → compar(< <= > >= == !=) → + - → * / % → unary(- not) → Precedence (high to low): `postfix(. [] ()) → unary(- ~ not) →
postfix(. () )`. Operators are built-in only (no overloading). The boolean * / % << >> & → + - | ^ → compar(< <= > >= == !=) → and → or`.
operators are spelled **`and` / `or` / `not`**; `&&` and `||` are not Ludic operators The bitwise operators bind **tighter than comparison** (Go-style), so
and `!` are rejected with a diagnostic naming the fix (`!=` is unaffected). Bitwise operations are `flags & MASK == 0` means `(flags & MASK) == 0` — no parentheses needed.
functions (`band`, `bor`, `bxor`, `bnot`, `shl`, `shr`), so the symbols are free
— which is why there is only one spelling to remember. `expr with { field: … }` is not implemented; records appear Operators are built-in only (no overloading). The boolean operators are spelled
only in `spawn`. Char literals (`'w'`) are `int` code points; colors are hex **`and` / `or` / `not`**; `&&` and `||` are not Ludic operators, and a bare `!` is
ints (`0xff8800`). rejected with a diagnostic naming the fix (`!=` is unaffected). Bitwise operators
are **`& | ^ << >> ~`** (`>>` is a logical/unsigned shift). `expr with { field: … }`
is not implemented; records appear only in `spawn`. Char literals (`'w'`) are
`int` code points; colors are hex ints (`0xff8800`).
## Builtins (the standard library / runtime surface) ## Builtins (the standard library / runtime surface)

View file

@ -197,11 +197,11 @@ fn rt_seed(s: int) -> void {
# masked off only when a caller asks for a number. # masked off only when a caller asks for a number.
fn rt_next_rand() -> int { fn rt_next_rand() -> int {
var x = rt_rng var x = rt_rng
x = bxor(x, shl(x, 13)) x = (x ^ (x << 13))
x = bxor(x, shr(x, 17)) x = (x ^ (x >> 17))
x = bxor(x, shl(x, 5)) x = (x ^ (x << 5))
rt_rng = x rt_rng = x
return band(x, 2147483647) return (x & 2147483647)
} }
fn rt_rng_range(lo: int, hi: int) -> int { fn rt_rng_range(lo: int, hi: int) -> int {

View file

@ -31,8 +31,7 @@ fn rt_image_init() -> void {
# ---- decoding ------------------------------------------------------------- # ---- decoding -------------------------------------------------------------
fn png_be32(b: ptr, at: int) -> int { fn png_be32(b: ptr, at: int) -> int {
return bor(bor(shl(peek8(b, at), 24), shl(peek8(b, at + 1), 16)), return (peek8(b, at) << 24) | (peek8(b, at + 1) << 16) | (peek8(b, at + 2) << 8) | peek8(b, at + 3)
bor(shl(peek8(b, at + 2), 8), peek8(b, at + 3)))
} }
fn png_tag(b: ptr, at: int, a: int, c: int, d: int, e: int) -> bool { fn png_tag(b: ptr, at: int, a: int, c: int, d: int, e: int) -> bool {
@ -164,13 +163,13 @@ fn rt_decode_png(path: str) -> bool {
if pa <= pb { if pa <= pc { pick = a } } if pa <= pb { if pa <= pc { pick = a } }
v = v + pick v = v + pick
} }
poke8(raw, cur + x, band(v, 255)) poke8(raw, cur + x, (v & 255))
} }
} }
# expand to 0xAARRGGBB # expand to 0xAARRGGBB
let out = mem_alloc(w * h * 4) let out = mem_alloc(w * h * 4)
let maxv = shl(1, bd) - 1 let maxv = (1 << bd) - 1
for y in 0 .. h { for y in 0 .. h {
let row = y * (stride + 1) + 1 let row = y * (stride + 1) + 1
for x in 0 .. w { for x in 0 .. w {
@ -197,7 +196,7 @@ fn rt_decode_png(path: str) -> bool {
} }
if ct == 3 { if ct == 3 {
let bp = x * bd let bp = x * bd
let idx = band(shr(peek8(raw, row + bp / 8), 8 - bd - bp % 8), maxv) let idx = ((peek8(raw, row + bp / 8) >> (8 - bd - bp % 8)) & maxv)
R = peek8(plte, idx * 3) R = peek8(plte, idx * 3)
G = peek8(plte, idx * 3 + 1) G = peek8(plte, idx * 3 + 1)
B = peek8(plte, idx * 3 + 2) B = peek8(plte, idx * 3 + 2)
@ -205,14 +204,14 @@ fn rt_decode_png(path: str) -> bool {
} }
if ct == 0 { if ct == 0 {
let bp = x * bd let bp = x * bd
let s = band(shr(peek8(raw, row + bp / 8), 8 - bd - bp % 8), maxv) let s = ((peek8(raw, row + bp / 8) >> (8 - bd - bp % 8)) & maxv)
var g = s var g = s
if bd != 8 { g = s * 255 / maxv } if bd != 8 { g = s * 255 / maxv }
R = g R = g
G = g G = g
B = g B = g
} }
poke32(out, y * w + x, bor(bor(shl(A, 24), shl(R, 16)), bor(shl(G, 8), B))) poke32(out, y * w + x, (((A << 24) | (R << 16)) | ((G << 8) | B)))
} }
} }
@ -240,25 +239,25 @@ fn rt_image_load(path: str) -> int {
} }
fn rt_blend_px(x: int, y: int, argb: int) -> void { fn rt_blend_px(x: int, y: int, argb: int) -> void {
let a = band(shr(argb, 24), 255) let a = ((argb >> 24) & 255)
if a == 0 { return } if a == 0 { return }
if x < 0 { return } if x < 0 { return }
if y < 0 { return } if y < 0 { return }
if x >= rt_fbw { return } if x >= rt_fbw { return }
if y >= rt_fbh { return } if y >= rt_fbh { return }
var r = band(shr(argb, 16), 255) var r = ((argb >> 16) & 255)
var g = band(shr(argb, 8), 255) var g = ((argb >> 8) & 255)
var b = band(argb, 255) var b = (argb & 255)
if a != 255 { if a != 255 {
let dst = peek32(rt_fb, y * rt_fbw + x) let dst = peek32(rt_fb, y * rt_fbw + x)
let dr = band(shr(dst, 16), 255) let dr = ((dst >> 16) & 255)
let dg = band(shr(dst, 8), 255) let dg = ((dst >> 8) & 255)
let db = band(dst, 255) let db = (dst & 255)
r = (r * a + dr * (255 - a)) / 255 r = (r * a + dr * (255 - a)) / 255
g = (g * a + dg * (255 - a)) / 255 g = (g * a + dg * (255 - a)) / 255
b = (b * a + db * (255 - a)) / 255 b = (b * a + db * (255 - a)) / 255
} }
poke32(rt_fb, y * rt_fbw + x, bor(bor(shl(r, 16), shl(g, 8)), b)) poke32(rt_fb, y * rt_fbw + x, (((r << 16) | (g << 8)) | b))
} }
fn rt_draw_image(id: int, dx: int, dy: int) -> void { fn rt_draw_image(id: int, dx: int, dy: int) -> void {
@ -328,7 +327,7 @@ fn rt_png_load(path: str) -> int {
if x < png_w { if x < png_w {
if y < png_h { if y < png_h {
let c = peek32(png_px, y * png_w + x) let c = peek32(png_px, y * png_w + x)
if band(shr(c, 24), 255) >= 128 { px = bor(shl(255, 24), band(c, 16777215)) } if ((c >> 24) & 255) >= 128 { px = ((255 << 24) | (c & 16777215)) }
} }
} }
poke32(spr_px, base + y * SPR_SZ + x, px) poke32(spr_px, base + y * SPR_SZ + x, px)
@ -346,8 +345,8 @@ fn rt_draw_sprite(id: int, px: int, py: int) -> void {
for y in 0 .. SPR_SZ { for y in 0 .. SPR_SZ {
for x in 0 .. SPR_SZ { for x in 0 .. SPR_SZ {
let p = peek32(spr_px, base + y * SPR_SZ + x) let p = peek32(spr_px, base + y * SPR_SZ + x)
if band(shr(p, 24), 255) != 0 { if ((p >> 24) & 255) != 0 {
rt_put_px(px + x, py + y, band(p, 16777215)) rt_put_px(px + x, py + y, (p & 16777215))
} }
} }
} }
@ -361,8 +360,8 @@ fn rt_draw_sprite_scaled(id: int, px: int, py: int, sc: int) -> void {
for y in 0 .. SPR_SZ { for y in 0 .. SPR_SZ {
for x in 0 .. SPR_SZ { for x in 0 .. SPR_SZ {
let p = peek32(spr_px, base + y * SPR_SZ + x) let p = peek32(spr_px, base + y * SPR_SZ + x)
if band(shr(p, 24), 255) != 0 { if ((p >> 24) & 255) != 0 {
rt_fill_rect(px + x * sc, py + y * sc, sc, sc, band(p, 16777215)) rt_fill_rect(px + x * sc, py + y * sc, sc, sc, (p & 16777215))
} }
} }
} }
@ -401,7 +400,7 @@ fn rt_sprites_load(path: str) -> void {
let hv = rt_hexval(peek8(d, start + 6 + k)) let hv = rt_hexval(peek8(d, start + 6 + k))
if hv >= 0 { v = v * 16 + hv } if hv >= 0 { v = v * 16 + hv }
} }
if ch < 128 { poke32(pal, ch, bor(shl(255, 24), v)) } if ch < 128 { poke32(pal, ch, ((255 << 24) | v)) }
} }
if c0 == 115 { # 's' — "spr" starts a new sprite if c0 == 115 { # 's' — "spr" starts a new sprite
cur = 0 - 1 cur = 0 - 1
@ -420,7 +419,7 @@ fn rt_sprites_load(path: str) -> void {
for x in 0 .. SPR_SZ { for x in 0 .. SPR_SZ {
if x < len { if x < len {
poke32(spr_px, cur * SPR_SZ * SPR_SZ + row * SPR_SZ + x, poke32(spr_px, cur * SPR_SZ * SPR_SZ + row * SPR_SZ + x,
peek32(pal, band(peek8(d, start + x), 127))) peek32(pal, (peek8(d, start + x) & 127)))
} }
} }
row = row + 1 row = row + 1

View file

@ -36,13 +36,13 @@ fn z_bits(need: int) -> int {
z_err = 1 z_err = 1
return 0 return 0
} }
val = bor(val, shl(peek8(z_src, z_pos), z_bitcnt)) val = (val | (peek8(z_src, z_pos) << z_bitcnt))
z_pos = z_pos + 1 z_pos = z_pos + 1
z_bitcnt = z_bitcnt + 8 z_bitcnt = z_bitcnt + 8
} }
z_bitbuf = shr(val, need) z_bitbuf = (val >> need)
z_bitcnt = z_bitcnt - need z_bitcnt = z_bitcnt - need
return band(val, shl(1, need) - 1) return (val & ((1 << need) - 1))
} }
# ---- Huffman tables ------------------------------------------------------- # ---- Huffman tables -------------------------------------------------------
@ -83,14 +83,14 @@ fn z_decode(table: ptr) -> int {
var first = 0 var first = 0
var index = 0 var index = 0
for len in 1 .. 16 { for len in 1 .. 16 {
code = bor(code, z_bits(1)) code = (code | z_bits(1))
let count = peek32(table, len) let count = peek32(table, len)
if code - first < count { if code - first < count {
return peek32(table, 16 + index + (code - first)) return peek32(table, 16 + index + (code - first))
} }
index = index + count index = index + count
first = shl(first + count, 1) first = ((first + count) << 1)
code = shl(code, 1) code = (code << 1)
} }
z_err = 1 z_err = 1
return -1 return -1
@ -101,8 +101,8 @@ fn z_len_base(sym: int) -> int {
if sym < 8 { return 3 + sym } if sym < 8 { return 3 + sym }
if sym == 28 { return 258 } if sym == 28 { return 258 }
let extra = (sym - 4) / 4 let extra = (sym - 4) / 4
let group = shl(1, extra) let group = (1 << extra)
return 3 + shl(group - 1, 2) + 4 + (sym - 4 - extra * 4) * group return 3 + ((group - 1) << 2) + 4 + (sym - 4 - extra * 4) * group
} }
fn z_len_extra(sym: int) -> int { fn z_len_extra(sym: int) -> int {
@ -114,8 +114,8 @@ fn z_len_extra(sym: int) -> int {
fn z_dist_base(sym: int) -> int { fn z_dist_base(sym: int) -> int {
if sym < 4 { return 1 + sym } if sym < 4 { return 1 + sym }
let extra = (sym - 2) / 2 let extra = (sym - 2) / 2
let group = shl(1, extra) let group = (1 << extra)
return 1 + shl(group, 1) + (sym - 2 - extra * 2) * group return 1 + (group << 1) + (sym - 2 - extra * 2) * group
} }
fn z_dist_extra(sym: int) -> int { fn z_dist_extra(sym: int) -> int {
@ -129,7 +129,7 @@ fn z_stored(out: ptr, at: int, cap: int) -> int {
z_bitbuf = 0 z_bitbuf = 0
z_bitcnt = 0 # stored blocks are byte-aligned z_bitcnt = 0 # stored blocks are byte-aligned
if z_pos + 4 > z_len { return -1 } if z_pos + 4 > z_len { return -1 }
let n = peek8(z_src, z_pos) + shl(peek8(z_src, z_pos + 1), 8) let n = peek8(z_src, z_pos) + (peek8(z_src, z_pos + 1) << 8)
z_pos = z_pos + 4 # LEN then its one's complement z_pos = z_pos + 4 # LEN then its one's complement
var w = at var w = at
for i in 0 .. n { for i in 0 .. n {
@ -271,6 +271,6 @@ fn z_inflate(src: ptr, len: int, out: ptr, cap: int) -> int {
fn z_uncompress(src: ptr, len: int, out: ptr, cap: int) -> int { fn z_uncompress(src: ptr, len: int, out: ptr, cap: int) -> int {
if len < 2 { return -1 } if len < 2 { return -1 }
let cmf = peek8(src, 0) let cmf = peek8(src, 0)
if band(cmf, 15) != 8 { return -1 } if (cmf & 15) != 8 { return -1 }
return z_inflate(ptr_add(src, 2), len - 2, out, cap) return z_inflate(ptr_add(src, 2), len - 2, out, cap)
} }

View file

@ -106,7 +106,7 @@ fn rt_tt_init() -> void {
# ---- big-endian readers --------------------------------------------------- # ---- big-endian readers ---------------------------------------------------
fn tt_u16(d: ptr, at: int) -> int { fn tt_u16(d: ptr, at: int) -> int {
return bor(shl(peek8(d, at), 8), peek8(d, at + 1)) return ((peek8(d, at) << 8) | peek8(d, at + 1))
} }
fn tt_i16(d: ptr, at: int) -> int { fn tt_i16(d: ptr, at: int) -> int {
let v = tt_u16(d, at) let v = tt_u16(d, at)
@ -114,8 +114,7 @@ fn tt_i16(d: ptr, at: int) -> int {
return v return v
} }
fn tt_u32(d: ptr, at: int) -> int { fn tt_u32(d: ptr, at: int) -> int {
return bor(bor(shl(peek8(d, at), 24), shl(peek8(d, at + 1), 16)), return (peek8(d, at) << 24) | (peek8(d, at + 1) << 16) | (peek8(d, at + 2) << 8) | peek8(d, at + 3)
bor(shl(peek8(d, at + 2), 8), peek8(d, at + 3)))
} }
fn tt_i8(d: ptr, at: int) -> int { fn tt_i8(d: ptr, at: int) -> int {
let v = peek8(d, at) let v = peek8(d, at)
@ -232,10 +231,10 @@ fn tt_glyph_index(id: int, cp: int) -> int {
let st = tt_u16(d, startp + i * 2) let st = tt_u16(d, startp + i * 2)
if cp < st { return 0 } if cp < st { return 0 }
let ro = tt_u16(d, rangep + i * 2) let ro = tt_u16(d, rangep + i * 2)
if ro == 0 { return band(cp + tt_i16(d, deltap + i * 2), 65535) } if ro == 0 { return ((cp + tt_i16(d, deltap + i * 2)) & 65535) }
let g = tt_u16(d, rangep + i * 2 + ro + (cp - st) * 2) let g = tt_u16(d, rangep + i * 2 + ro + (cp - st) * 2)
if g == 0 { return 0 } if g == 0 { return 0 }
return band(g + tt_i16(d, deltap + i * 2), 65535) return ((g + tt_i16(d, deltap + i * 2)) & 65535)
} }
} }
return 0 return 0
@ -318,7 +317,7 @@ fn tt_load_outline(id: int, gid: int, a: fixed, b: fixed, c: fixed, e: fixed, dx
p = p + 1 p = p + 1
poke8(flags, i, fl) poke8(flags, i, fl)
i = i + 1 i = i + 1
if band(fl, 8) != 0 { # REPEAT if (fl & 8) != 0 { # REPEAT
var r = peek8(d, p) var r = peek8(d, p)
p = p + 1 p = p + 1
while r > 0 { while r > 0 {
@ -333,24 +332,24 @@ fn tt_load_outline(id: int, gid: int, a: fixed, b: fixed, c: fixed, e: fixed, dx
var xv = 0 var xv = 0
for k in 0 .. npts { for k in 0 .. npts {
let fl = peek8(flags, k) let fl = peek8(flags, k)
if band(fl, 2) != 0 { if (fl & 2) != 0 {
let dxv = peek8(d, p) let dxv = peek8(d, p)
p = p + 1 p = p + 1
if band(fl, 16) != 0 { xv = xv + dxv } else { xv = xv - dxv } if (fl & 16) != 0 { xv = xv + dxv } else { xv = xv - dxv }
} else { } else {
if band(fl, 16) == 0 { xv = xv + tt_i16(d, p); p = p + 2 } if (fl & 16) == 0 { xv = xv + tt_i16(d, p); p = p + 2 }
} }
poke32(xs, k, xv) poke32(xs, k, xv)
} }
var yv = 0 var yv = 0
for k in 0 .. npts { for k in 0 .. npts {
let fl = peek8(flags, k) let fl = peek8(flags, k)
if band(fl, 4) != 0 { if (fl & 4) != 0 {
let dyv = peek8(d, p) let dyv = peek8(d, p)
p = p + 1 p = p + 1
if band(fl, 32) != 0 { yv = yv + dyv } else { yv = yv - dyv } if (fl & 32) != 0 { yv = yv + dyv } else { yv = yv - dyv }
} else { } else {
if band(fl, 32) == 0 { yv = yv + tt_i16(d, p); p = p + 2 } if (fl & 32) == 0 { yv = yv + tt_i16(d, p); p = p + 2 }
} }
poke32(ys, k, yv) poke32(ys, k, yv)
} }
@ -361,7 +360,7 @@ fn tt_load_outline(id: int, gid: int, a: fixed, b: fixed, c: fixed, e: fixed, dx
for k in start .. last + 1 { for k in start .. last + 1 {
let X = fx(peek32(xs, k)) let X = fx(peek32(xs, k))
let Y = fx(peek32(ys, k)) let Y = fx(peek32(ys, k))
ol_pt(a * X + c * Y + dx, b * X + e * Y + dy, band(peek8(flags, k), 1)) ol_pt(a * X + c * Y + dx, b * X + e * Y + dy, (peek8(flags, k) & 1))
} }
if ol_ne < 256 { if ol_ne < 256 {
poke32(ol_ends, ol_ne, ol_n) poke32(ol_ends, ol_ne, ol_n)
@ -382,7 +381,7 @@ fn tt_load_outline(id: int, gid: int, a: fixed, b: fixed, c: fixed, e: fixed, dx
p = p + 4 p = p + 4
var arg1 = 0 var arg1 = 0
var arg2 = 0 var arg2 = 0
if band(flags, 1) != 0 { if (flags & 1) != 0 {
arg1 = tt_i16(d, p) arg1 = tt_i16(d, p)
arg2 = tt_i16(d, p + 2) arg2 = tt_i16(d, p + 2)
p = p + 4 p = p + 4
@ -395,32 +394,32 @@ fn tt_load_outline(id: int, gid: int, a: fixed, b: fixed, c: fixed, e: fixed, dx
var cb = 0.0 var cb = 0.0
var cc = 0.0 var cc = 0.0
var ce = 1.0 var ce = 1.0
if band(flags, 8) != 0 { if (flags & 8) != 0 {
ca = as_fixed(shl(tt_i16(d, p), 2)) # F2Dot14 -> Q16.16 ca = as_fixed((tt_i16(d, p) << 2)) # F2Dot14 -> Q16.16
ce = ca ce = ca
p = p + 2 p = p + 2
} }
if band(flags, 64) != 0 { if (flags & 64) != 0 {
ca = as_fixed(shl(tt_i16(d, p), 2)) ca = as_fixed((tt_i16(d, p) << 2))
ce = as_fixed(shl(tt_i16(d, p + 2), 2)) ce = as_fixed((tt_i16(d, p + 2) << 2))
p = p + 4 p = p + 4
} }
if band(flags, 128) != 0 { if (flags & 128) != 0 {
ca = as_fixed(shl(tt_i16(d, p), 2)) ca = as_fixed((tt_i16(d, p) << 2))
cb = as_fixed(shl(tt_i16(d, p + 2), 2)) cb = as_fixed((tt_i16(d, p + 2) << 2))
cc = as_fixed(shl(tt_i16(d, p + 4), 2)) cc = as_fixed((tt_i16(d, p + 4) << 2))
ce = as_fixed(shl(tt_i16(d, p + 6), 2)) ce = as_fixed((tt_i16(d, p + 6) << 2))
p = p + 8 p = p + 8
} }
var odx = dx var odx = dx
var ody = dy var ody = dy
if band(flags, 2) != 0 { if (flags & 2) != 0 {
odx = a * fx(arg1) + c * fx(arg2) + dx odx = a * fx(arg1) + c * fx(arg2) + dx
ody = b * fx(arg1) + e * fx(arg2) + dy ody = b * fx(arg1) + e * fx(arg2) + dy
} }
tt_load_outline(id, cgid, a * ca + c * cb, b * ca + e * cb, a * cc + c * ce, b * cc + e * ce, odx, ody, depth + 1) tt_load_outline(id, cgid, a * ca + c * cb, b * ca + e * cb, a * cc + c * ce, b * cc + e * ce, odx, ody, depth + 1)
more = 0 more = 0
if band(flags, 32) != 0 { more = 1 } if (flags & 32) != 0 { more = 1 }
} }
} }
} }

View file

@ -232,13 +232,13 @@ fn tt_raster(id: int, gid: int, px: int) -> ptr {
# Rasterizing is far too slow to repeat per frame, so a coverage mask is kept # Rasterizing is far too slow to repeat per frame, so a coverage mask is kept
# per (font, codepoint, size). Open addressing, 8 probes, evict on miss. # per (font, codepoint, size). Open addressing, 8 probes, evict on miss.
fn gc_hash(font: int, cp: int, px: int) -> int { fn gc_hash(font: int, cp: int, px: int) -> int {
return band(bxor(bxor(cp * 2654435761, shl(font, 20)), shl(px, 8)), TT_GC - 1) return ((((cp * 2654435761) ^ (font << 20)) ^ (px << 8)) & (TT_GC - 1))
} }
fn tt_glyph_get(font: int, cp: int, px: int) -> int { fn tt_glyph_get(font: int, cp: int, px: int) -> int {
let h = gc_hash(font, cp, px) let h = gc_hash(font, cp, px)
for k in 0 .. 8 { for k in 0 .. 8 {
let s = band(h + k, TT_GC - 1) let s = ((h + k) & (TT_GC - 1))
if peek32(gc_used, s) == 1 { if peek32(gc_used, s) == 1 {
if peek32(gc_font, s) == font { if peek32(gc_font, s) == font {
if peek32(gc_cp, s) == cp { if peek32(gc_cp, s) == cp {
@ -273,9 +273,9 @@ fn tt_utf8(s: str, at: int) -> int {
let c = peek8(s, at) let c = peek8(s, at)
var extra = 0 - 1 var extra = 0 - 1
if c < 128 { extra = 0 } if c < 128 { extra = 0 }
if shr(c, 5) == 6 { extra = 1 } if (c >> 5) == 6 { extra = 1 }
if shr(c, 4) == 14 { extra = 2 } if (c >> 4) == 14 { extra = 2 }
if shr(c, 3) == 30 { extra = 3 } if (c >> 3) == 30 { extra = 3 }
if extra < 0 { if extra < 0 {
u8_next = at + 1 u8_next = at + 1
return 65533 return 65533
@ -284,14 +284,14 @@ fn tt_utf8(s: str, at: int) -> int {
u8_next = at + 1 u8_next = at + 1
return c return c
} }
var cp = band(c, shr(63, extra)) var cp = (c & (63 >> extra))
for i in 0 .. extra { for i in 0 .. extra {
let b = peek8(s, at + 1 + i) let b = peek8(s, at + 1 + i)
if band(b, 192) != 128 { if (b & 192) != 128 {
u8_next = at + 1 + i u8_next = at + 1 + i
return 65533 return 65533
} }
cp = bor(shl(cp, 6), band(b, 63)) cp = ((cp << 6) | (b & 63))
} }
u8_next = at + 1 + extra u8_next = at + 1 + extra
return cp return cp
@ -300,9 +300,9 @@ fn tt_utf8(s: str, at: int) -> int {
# ---- drawing -------------------------------------------------------------- # ---- drawing --------------------------------------------------------------
fn tt_blit(bmp: ptr, w: int, h: int, dx: int, dy: int, colour: int) -> void { fn tt_blit(bmp: ptr, w: int, h: int, dx: int, dy: int, colour: int) -> void {
if ptr_is_null(bmp) { return } if ptr_is_null(bmp) { return }
let cr = band(shr(colour, 16), 255) let cr = ((colour >> 16) & 255)
let cg = band(shr(colour, 8), 255) let cg = ((colour >> 8) & 255)
let cb = band(colour, 255) let cb = (colour & 255)
for y in 0 .. h { for y in 0 .. h {
let py = dy + y let py = dy + y
if py >= 0 { if py >= 0 {
@ -314,10 +314,10 @@ fn tt_blit(bmp: ptr, w: int, h: int, dx: int, dy: int, colour: int) -> void {
if pxx >= 0 { if pxx >= 0 {
if pxx < rt_fbw { if pxx < rt_fbw {
let d = peek32(rt_fb, py * rt_fbw + pxx) let d = peek32(rt_fb, py * rt_fbw + pxx)
let rr = (cr * a + band(shr(d, 16), 255) * (255 - a)) / 255 let rr = (cr * a + ((d >> 16) & 255) * (255 - a)) / 255
let rg = (cg * a + band(shr(d, 8), 255) * (255 - a)) / 255 let rg = (cg * a + ((d >> 8) & 255) * (255 - a)) / 255
let rb = (cb * a + band(d, 255) * (255 - a)) / 255 let rb = (cb * a + (d & 255) * (255 - a)) / 255
poke32(rt_fb, py * rt_fbw + pxx, bor(bor(shl(rr, 16), shl(rg, 8)), rb)) poke32(rt_fb, py * rt_fbw + pxx, (((rr << 16) | (rg << 8)) | rb))
} }
} }
} }

View file

@ -75,13 +75,13 @@ fn ui_layout() -> void {
# ---- drawing -------------------------------------------------------------- # ---- drawing --------------------------------------------------------------
fn ui_lighten(c: int) -> int { fn ui_lighten(c: int) -> int {
var r = band(shr(c, 16), 255) var r = ((c >> 16) & 255)
var g = band(shr(c, 8), 255) var g = ((c >> 8) & 255)
var b = band(c, 255) var b = (c & 255)
r = r + (255 - r) * 4 / 10 r = r + (255 - r) * 4 / 10
g = g + (255 - g) * 4 / 10 g = g + (255 - g) * 4 / 10
b = b + (255 - b) * 4 / 10 b = b + (255 - b) * 4 / 10
return bor(bor(shl(r, 16), shl(g, 8)), b) return (((r << 16) | (g << 8)) | b)
} }
fn ui_draw_node(i: int) -> void { fn ui_draw_node(i: int) -> void {

View file

@ -42,6 +42,11 @@ fn arith_code(op: ptr) -> ptr {
if streq(op, "-") { return "sub" } if streq(op, "-") { return "sub" }
if streq(op, "*") { return "mul" } if streq(op, "*") { return "mul" }
if streq(op, "/") { return "sdiv" } 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" return "srem"
} }
@ -170,6 +175,7 @@ fn emit_expr(e: Node) -> Val {
if e.kind == E_UN { if e.kind == E_UN {
let a = emit_expr(e.a) 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("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"))) let c = emit_bind(sconcat("icmp eq i32 ", sconcat(a.code, ", 0")))
return val(emit_bind(sconcat("zext i1 ", sconcat(c, " to i32"))), "bool") 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,"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_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,"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 } if streq(name,"peek32") or streq(name,"poke32") { return true }
return false return false
} }
@ -114,16 +113,7 @@ fn emit_intrinsic(name: ptr, e: Node) -> Val {
let n = arg_code(e, 0) let n = arg_code(e, 0)
return val(emit_bind(sconcat("call ptr @getenv(ptr ", sconcat(n, ")"))), "str") 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") } # bitwise ops are the operators & | ^ << >> ~ now (see emit_bin / p_mul).
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")
}
if streq(name, "peek32") { if streq(name, "peek32") {
let p = arg_code(e, 0); let i = arg_code(e, 1) let p = arg_code(e, 0); let i = arg_code(e, 1)
let g = nreg() 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 == 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 == 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 == 33 or c == 64 { return true }
if c == 38 or c == 124 or c == 94 or c == 126 { return true } # & | ^ ~
return false return false
} }
@ -105,7 +106,7 @@ fn lex(src: ptr) -> void {
i = i + 1 i = i + 1
var fnum = 0; var fden = 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 } 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) tok_push(TK_FLOAT, ptr_null(), bits, line)
continue 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, 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, 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, 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 } if is_op1(c) { tok_push(TK_OP, substr(src, i, 1), 0, line); i = i + 1; continue }
i = i + 1 # skip anything unrecognised 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 # run mode: execute the binary we just built and forward its exit code
if run { if run {
let st = os_system(out) 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 { 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 }
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 } if is_id("not") { pi = pi + 1; let n = node(E_UN); n.s = "not"; n.a = p_unary(); return n }
return p_postfix() 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 } 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 { fn p_mul() -> Node {
var l = p_unary() 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 return l
} }
fn p_add() -> Node { fn p_add() -> Node {
var l = p_mul() 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 return l
} }
fn p_cmp() -> Node { fn p_cmp() -> Node {

View file

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

View file

@ -71,7 +71,7 @@ object LudicVocabulary {
"poke32", "peekp", "pokep", "peekf", "pokef", "ptr_add", "ptr_null", "poke32", "peekp", "pokep", "peekf", "pokef", "ptr_add", "ptr_null",
"ptr_is_null", "as_fixed", "as_int", "file_open", "file_read", "file_write", "ptr_is_null", "as_fixed", "as_int", "file_open", "file_read", "file_write",
"file_seek", "file_tell", "file_close", "read_byte", "write_byte", "print_str", "file_seek", "file_tell", "file_close", "read_byte", "write_byte", "print_str",
"str_len", "shl", "shr", "band", "bor", "bxor", "bnot", "os_exit", "os_time", "str_len", "os_exit", "os_time",
"is_windowed", "game_title", "win_open", "win_poll", "win_present", "is_windowed", "game_title", "win_open", "win_poll", "win_present",
"win_running", "win_close" "win_running", "win_close"
) )

View file

@ -198,7 +198,7 @@
{ {
"comment": "compiler intrinsics — these lower straight to libc or the OS", "comment": "compiler intrinsics — these lower straight to libc or the OS",
"name": "support.function.intrinsic.ludic", "name": "support.function.intrinsic.ludic",
"match": "\\b(mem_alloc|mem_realloc|mem_free|mem_copy|mem_set|peek8|poke8|peek32|poke32|peekp|pokep|peekf|pokef|ptr_add|ptr_null|ptr_is_null|as_fixed|as_int|file_open|file_read|file_write|file_seek|file_tell|file_close|read_byte|write_byte|print_str|str_len|shl|shr|band|bor|bxor|bnot|os_exit|os_time|os_argc|os_arg|file_stderr|is_windowed|game_title|win_open|win_poll|win_present|win_running|win_close)\\b(?=\\s*\\()" "match": "\\b(mem_alloc|mem_realloc|mem_free|mem_copy|mem_set|peek8|poke8|peek32|poke32|peekp|pokep|peekf|pokef|ptr_add|ptr_null|ptr_is_null|as_fixed|as_int|file_open|file_read|file_write|file_seek|file_tell|file_close|read_byte|write_byte|print_str|str_len|os_exit|os_time|os_argc|os_arg|file_stderr|is_windowed|game_title|win_open|win_poll|win_present|win_running|win_close)\\b(?=\\s*\\()"
} }
] ]
}, },
@ -222,6 +222,7 @@
"patterns": [ "patterns": [
{ "name": "keyword.operator.arrow.ludic", "match": "->|=>" }, { "name": "keyword.operator.arrow.ludic", "match": "->|=>" },
{ "name": "keyword.operator.range.ludic", "match": "\\.\\." }, { "name": "keyword.operator.range.ludic", "match": "\\.\\." },
{ "name": "keyword.operator.bitwise.ludic", "match": "<<|>>|&|\\||\\^|~" },
{ "name": "keyword.operator.comparison.ludic", "match": "==|!=|<=|>=|<|>" }, { "name": "keyword.operator.comparison.ludic", "match": "==|!=|<=|>=|<|>" },
{ "name": "keyword.operator.assignment.ludic", "match": "\\+=|-=|\\*=|/=|=" }, { "name": "keyword.operator.assignment.ludic", "match": "\\+=|-=|\\*=|/=|=" },
{ "name": "keyword.operator.arithmetic.ludic", "match": "[+\\-*/%]" } { "name": "keyword.operator.arithmetic.ludic", "match": "[+\\-*/%]" }

View file

@ -198,7 +198,7 @@
{ {
"comment": "compiler intrinsics — these lower straight to libc or the OS", "comment": "compiler intrinsics — these lower straight to libc or the OS",
"name": "support.function.intrinsic.ludic", "name": "support.function.intrinsic.ludic",
"match": "\\b(mem_alloc|mem_realloc|mem_free|mem_copy|mem_set|peek8|poke8|peek32|poke32|peekp|pokep|peekf|pokef|ptr_add|ptr_null|ptr_is_null|as_fixed|as_int|file_open|file_read|file_write|file_seek|file_tell|file_close|read_byte|write_byte|print_str|str_len|shl|shr|band|bor|bxor|bnot|os_exit|os_time|os_argc|os_arg|file_stderr|is_windowed|game_title|win_open|win_poll|win_present|win_running|win_close)\\b(?=\\s*\\()" "match": "\\b(mem_alloc|mem_realloc|mem_free|mem_copy|mem_set|peek8|poke8|peek32|poke32|peekp|pokep|peekf|pokef|ptr_add|ptr_null|ptr_is_null|as_fixed|as_int|file_open|file_read|file_write|file_seek|file_tell|file_close|read_byte|write_byte|print_str|str_len|os_exit|os_time|os_argc|os_arg|file_stderr|is_windowed|game_title|win_open|win_poll|win_present|win_running|win_close)\\b(?=\\s*\\()"
} }
] ]
}, },
@ -222,6 +222,7 @@
"patterns": [ "patterns": [
{ "name": "keyword.operator.arrow.ludic", "match": "->|=>" }, { "name": "keyword.operator.arrow.ludic", "match": "->|=>" },
{ "name": "keyword.operator.range.ludic", "match": "\\.\\." }, { "name": "keyword.operator.range.ludic", "match": "\\.\\." },
{ "name": "keyword.operator.bitwise.ludic", "match": "<<|>>|&|\\||\\^|~" },
{ "name": "keyword.operator.comparison.ludic", "match": "==|!=|<=|>=|<|>" }, { "name": "keyword.operator.comparison.ludic", "match": "==|!=|<=|>=|<|>" },
{ "name": "keyword.operator.assignment.ludic", "match": "\\+=|-=|\\*=|/=|=" }, { "name": "keyword.operator.assignment.ludic", "match": "\\+=|-=|\\*=|/=|=" },
{ "name": "keyword.operator.arithmetic.ludic", "match": "[+\\-*/%]" } { "name": "keyword.operator.arithmetic.ludic", "match": "[+\\-*/%]" }

View file

@ -175,12 +175,6 @@ static const LBuiltin LUDIC_INTRINSICS[] = {
{"write_byte","write_byte(b: int)","Write one byte to stdout."}, {"write_byte","write_byte(b: int)","Write one byte to stdout."},
{"print_str","print_str(s: str)","Write a string to stdout."}, {"print_str","print_str(s: str)","Write a string to stdout."},
{"str_len","str_len(s: str) -> int","Length of a string in bytes."}, {"str_len","str_len(s: str) -> int","Length of a string in bytes."},
{"shl","shl(v: int, n: int) -> int","Shift left."},
{"shr","shr(v: int, n: int) -> int","Logical shift right."},
{"band","band(a: int, b: int) -> int","Bitwise and."},
{"bor","bor(a: int, b: int) -> int","Bitwise or."},
{"bxor","bxor(a: int, b: int) -> int","Bitwise xor."},
{"bnot","bnot(a: int) -> int","Bitwise not."},
{"os_exit","os_exit(code: int)","Terminate the process."}, {"os_exit","os_exit(code: int)","Terminate the process."},
{"os_time","os_time() -> int","Seconds since the epoch."}, {"os_time","os_time() -> int","Seconds since the epoch."},
{"is_windowed","is_windowed() -> bool","True when the build has a window."}, {"is_windowed","is_windowed() -> bool","True when the build has a window."},