aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-16 15:49:07 +0200
committeruser <user@clank>2026-08-16 15:49:07 +0200
commit7cd9de2c15f5b00b99ea8d974a4c73f0c2d4f972 (patch)
tree476b659e7f5b54b0511b1b5e4dd9a559fa7230bf
parentsl0ppty: zero strength is not a pass (diff)
downloadslosh-7cd9de2c15f5b00b99ea8d974a4c73f0c2d4f972.tar.gz
slosh-7cd9de2c15f5b00b99ea8d974a4c73f0c2d4f972.tar.xz
slosh-7cd9de2c15f5b00b99ea8d974a4c73f0c2d4f972.zip
sl0ppty: bits
`&` `|` `^` `~` `<<` `>>`, on the 32-bit value the VM already used. There was nothing: `x & 7` answered `unexpected trailing input at offset 2` and `(x ^ y) % 16 < 2` answered `expected ) at offset 3`, which is a fair description of a parser that had never heard of either. `^` is the reason to bother. `(x ^ y) & 1` is the cheapest interesting texture there is, and the language could express a sweep, a ramp and a comparison but not one bit of pattern without `%` gymnastics. **Bits bind tighter than comparison, which is not what C does.** `x & 7 == 0` means `x & (7 == 0)` in C -- a precedence Ritchie put on record as a mistake, kept for a decade of compatibility that a shader expression in a config file does not have. Here it asks what it looks like it asks. Among themselves the usual `&` then `^` then `|`; shifts stay where C has them, just under `+ -`. `&&` and `||` still parse as themselves: the bitwise rungs decline the first character when a second one follows it. Semantics are JavaScript's, deliberately and to the letter -- the shift count masked to five bits (`1 << 33` is 2), `>>` keeping the sign (`-8 >> 1` is -4). Not taste: `contrib/shadertoy.html` reimplements this language in JS so the preview can run in a browser, and `tests/test_shadertoy.py` compares the two on every expression it can think of. Anything C leaves implementation-defined had to be pinned to what JS would show, so the signed right shift is written out in the VM rather than left to the compiler. Verified: 22 new cases in `tests/expr_test.c` covering the operators, both precedence questions, the sign, and the masked count; the preview learned the same six operators and the same ladder, and the cross-check now agrees with the compiler on 564 evaluations, including generated ones that reach for bits. Then seven bitwise chains in a real pane -- an XOR texture, a drifting one, a stepped pulse that walks eight shades of `t >> 7 & 7` and needs no `abs`.
-rw-r--r--config/config.kdl8
-rw-r--r--contrib/shadertoy.html48
-rw-r--r--docs/shaders.md1
-rw-r--r--src/expr.c96
-rw-r--r--tests/expr_test.c35
-rw-r--r--tests/test_shadertoy.py30
6 files changed, 212 insertions, 6 deletions
diff --git a/config/config.kdl b/config/config.kdl
index 4ea6261..d9e8c3c 100644
--- a/config/config.kdl
+++ b/config/config.kdl
@@ -456,6 +456,14 @@ states {
// operators + - * / % integer only; division by zero is 0, not a crash
// < > <= >= == != and && || !, which give 0 or 1 -- so
// `(x < 10) * 200` is how you write a rule
+// & | ^ ~ << >> on the 32-bit value. They bind *tighter* than
+// comparison, so `x & 7 == 0` means what it looks
+// like -- C reads that as `x & (7 == 0)`, which
+// Ritchie called a mistake and it is. The shift
+// count is masked to five bits and `>>` keeps the
+// sign, both matching the browser preview exactly.
+// `(x ^ y) & 1` is the cheapest interesting texture
+// there is.
// ? : both sides are evaluated, then one is chosen
// functions min(a,b) max(a,b) abs(a) clamp(v,lo,hi)
// dist(x1,y1,x2,y2) counts a row double, because a cell is
diff --git a/contrib/shadertoy.html b/contrib/shadertoy.html
index f17a984..a094e82 100644
--- a/contrib/shadertoy.html
+++ b/contrib/shadertoy.html
@@ -106,6 +106,8 @@
<code>+ - * / %</code> integer only; division by zero is 0<br>
<code>&lt; &gt; &lt;= &gt;= == != &amp;&amp; || !</code> give 0 or 1, so
<code>(x &lt; 10) * 200</code> is a rule<br>
+ <code>&amp; | ^ ~ &lt;&lt; &gt;&gt;</code> on the 32-bit value, binding tighter
+ than comparison, so <code>x &amp; 7 == 0</code> reads as it looks<br>
<code>a ? b : c</code> &nbsp; <code>min max abs clamp dist</code><br>
<code>dist</code> counts a row double, because a cell is about twice as
tall as it is wide.
@@ -158,6 +160,7 @@ function compile(src) {
* forgetting it here made `!x` throw instead of evaluating, which the
* conformance test caught on its first run. */
if (src[i] === "-") { i++; const a = primary(); return (e) => -a(e); }
+ if (src[i] === "~") { i++; const a = primary(); return (e) => ~a(e); }
if (src[i] === "!") { i++; const a = primary(); return (e) => a(e) ? 0 : 1; }
if (/[0-9]/.test(src[i] || "")) {
let s = i; while (/[0-9]/.test(src[i] || "")) i++;
@@ -219,6 +222,14 @@ function compile(src) {
case "!=": return (e) => (l(e) !== r(e)) | 0;
case "&&": return (e) => (l(e) && r(e)) ? 1 : 0;
case "||": return (e) => (l(e) || r(e)) ? 1 : 0;
+ /* JS's own bitwise operators are int32 with a five-bit shift count, which
+ is exactly what the compiler in src/expr.c does -- deliberately, so that
+ this preview and the real thing cannot disagree. */
+ case "&": return (e) => l(e) & r(e);
+ case "^": return (e) => l(e) ^ r(e);
+ case "|": return (e) => l(e) | r(e);
+ case "<<": return (e) => l(e) << r(e);
+ case ">>": return (e) => l(e) >> r(e);
}
};
@@ -238,14 +249,45 @@ function compile(src) {
i++; l = binop(c, l, mul());
}
}
- function cmp() {
+ /* Bitwise binds tighter than comparison, unlike C: `x & 7 == 0` asks what it
+ looks like. Same ladder as src/expr.c, which is the point. */
+ function shift() {
let l = add();
for (;;) { ws();
+ const two = src.substr(i, 2);
+ if (two !== "<<" && two !== ">>") return l;
+ i += 2; l = binop(two, l, add());
+ }
+ }
+ function band() {
+ let l = shift();
+ for (;;) { ws();
+ if (src[i] !== "&" || src[i + 1] === "&") return l; /* not `&&` */
+ i++; l = binop("&", l, shift());
+ }
+ }
+ function bxor() {
+ let l = band();
+ for (;;) { ws();
+ if (src[i] !== "^") return l;
+ i++; l = binop("^", l, band());
+ }
+ }
+ function bor() {
+ let l = bxor();
+ for (;;) { ws();
+ if (src[i] !== "|" || src[i + 1] === "|") return l; /* not `||` */
+ i++; l = binop("|", l, bxor());
+ }
+ }
+ function cmp() {
+ let l = bor();
+ for (;;) { ws();
/* Longest first, or `<` eats the `<` of `<=` and leaves an `=`. */
const two = src.substr(i, 2);
- if (["<=", ">=", "==", "!="].includes(two)) { i += 2; l = binop(two, l, add()); continue; }
+ if (["<=", ">=", "==", "!="].includes(two)) { i += 2; l = binop(two, l, bor()); continue; }
const c = src[i];
- if (c === "<" || c === ">") { i++; l = binop(c, l, add()); continue; }
+ if (c === "<" || c === ">") { i++; l = binop(c, l, bor()); continue; }
return l;
}
}
diff --git a/docs/shaders.md b/docs/shaders.md
index bb0221d..aa332e5 100644
--- a/docs/shaders.md
+++ b/docs/shaders.md
@@ -61,6 +61,7 @@ shaders {
| | `since` — milliseconds the pane has been in its current state |
| operators | `+ - * / %` (integer; division by zero is 0, not a crash) |
| | `< > <= >= == != && \|\| !` give 0 or 1, so `(x < 10) * 200` is a rule |
+| | `& \| ^ ~ << >>` on the 32-bit value, binding **tighter** than comparison — so `x & 7 == 0` asks what it looks like, unlike C |
| | `a ? b : c` — both sides are evaluated, then one is chosen |
| functions | `min(a,b)` `max(a,b)` `abs(a)` `clamp(v,lo,hi)` |
| | `dist(x1,y1,x2,y2)` — counts a row double, because a cell is about twice as tall as it is wide |
diff --git a/src/expr.c b/src/expr.c
index 677b3b5..5f07b05 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -14,6 +14,11 @@ enum {
OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, OP_NEG,
OP_LT, OP_GT, OP_LE, OP_GE, OP_EQ, OP_NE,
OP_AND, OP_OR, OP_NOT,
+ /* On the 32-bit value, with JavaScript's semantics to the letter: the shift
+ * count is masked to five bits and `>>` keeps the sign. Not a free choice --
+ * contrib/shadertoy.html reimplements this language in JS and a test compares
+ * the two, so `x << 33` has to mean the same thing in both. */
+ OP_BAND, OP_BXOR, OP_BOR, OP_BNOT, OP_SHL, OP_SHR,
OP_MIN, OP_MAX, OP_ABS, OP_CLAMP, OP_DIST,
OP_SELECT, /* cond a b -> a or b, both already evaluated */
};
@@ -140,6 +145,12 @@ static void parse_primary(parser_t *ps, expr_prog_t *pr) {
emit(ps, pr, OP_NEG, 0);
return;
}
+ if (*ps->p == '~') {
+ ps->p++;
+ parse_primary(ps, pr);
+ emit(ps, pr, OP_BNOT, 0);
+ return;
+ }
if (*ps->p == '!') {
ps->p++;
parse_primary(ps, pr);
@@ -251,12 +262,70 @@ static void parse_add(parser_t *ps, expr_prog_t *pr) {
}
}
-static void parse_cmp(parser_t *ps, expr_prog_t *pr) {
+/* Bitwise, between arithmetic and comparison.
+ *
+ * C puts `&` `^` `|` *below* comparison, so `x & 7 == 0` there means
+ * `x & (7 == 0)`. Ritchie called that a mistake and it is one; a small language
+ * copying it would be inheriting a wart for the sake of familiarity, so here the
+ * bits bind tighter and the line asks what it looks like it asks. Shifts sit
+ * where C puts them, just under `+ -`. */
+static void parse_shift(parser_t *ps, expr_prog_t *pr) {
parse_add(ps, pr);
for (;;) {
if (ps->failed) return;
skip_ws(ps);
uint8_t op;
+ if (eat(ps, "<<")) op = OP_SHL;
+ else if (eat(ps, ">>")) op = OP_SHR;
+ else return;
+ parse_add(ps, pr);
+ emit(ps, pr, op, 0);
+ }
+}
+
+static void parse_band(parser_t *ps, expr_prog_t *pr) {
+ parse_shift(ps, pr);
+ for (;;) {
+ if (ps->failed) return;
+ skip_ws(ps);
+ /* Not the `&` of `&&`: that one belongs to the logical rung above. */
+ if (*ps->p != '&' || ps->p[1] == '&') return;
+ ps->p++;
+ parse_shift(ps, pr);
+ emit(ps, pr, OP_BAND, 0);
+ }
+}
+
+static void parse_bxor(parser_t *ps, expr_prog_t *pr) {
+ parse_band(ps, pr);
+ for (;;) {
+ if (ps->failed) return;
+ skip_ws(ps);
+ if (*ps->p != '^') return;
+ ps->p++;
+ parse_band(ps, pr);
+ emit(ps, pr, OP_BXOR, 0);
+ }
+}
+
+static void parse_bor(parser_t *ps, expr_prog_t *pr) {
+ parse_bxor(ps, pr);
+ for (;;) {
+ if (ps->failed) return;
+ skip_ws(ps);
+ if (*ps->p != '|' || ps->p[1] == '|') return;
+ ps->p++;
+ parse_bxor(ps, pr);
+ emit(ps, pr, OP_BOR, 0);
+ }
+}
+
+static void parse_cmp(parser_t *ps, expr_prog_t *pr) {
+ parse_bor(ps, pr);
+ for (;;) {
+ if (ps->failed) return;
+ skip_ws(ps);
+ uint8_t op;
/* Longest first: `<` would otherwise eat the `<` of `<=` and leave an `=`
* that parses as nothing. */
if (eat(ps, "<=")) op = OP_LE;
@@ -266,7 +335,7 @@ static void parse_cmp(parser_t *ps, expr_prog_t *pr) {
else if (*ps->p == '<') { ps->p++; op = OP_LT; }
else if (*ps->p == '>') { ps->p++; op = OP_GT; }
else return;
- parse_add(ps, pr);
+ parse_bor(ps, pr);
emit(ps, pr, op, 0);
}
}
@@ -337,6 +406,7 @@ static int vm_run(const expr_prog_t *pr, const expr_env_t *env) {
case OP_VAR: st[sp++] = vars[in->imm]; break;
case OP_NEG: if (sp) st[sp - 1] = -st[sp - 1]; break;
case OP_NOT: if (sp) st[sp - 1] = !st[sp - 1]; break;
+ case OP_BNOT: if (sp) st[sp - 1] = (int32_t)~(uint32_t)st[sp - 1]; break;
case OP_ABS: if (sp) st[sp - 1] = st[sp - 1] < 0 ? -st[sp - 1] : st[sp - 1]; break;
default: {
int need = in->op == OP_CLAMP ? 3 : in->op == OP_DIST ? 4
@@ -358,6 +428,27 @@ static int vm_run(const expr_prog_t *pr, const expr_env_t *env) {
case OP_EQ: st[sp - 2] = a == b; sp--; break;
case OP_NE: st[sp - 2] = a != b; sp--; break;
case OP_AND: st[sp - 2] = a && b; sp--; break;
+ case OP_BAND: st[sp - 2] = (int32_t)((uint32_t)a & (uint32_t)b); sp--; break;
+ case OP_BXOR: st[sp - 2] = (int32_t)((uint32_t)a ^ (uint32_t)b); sp--; break;
+ case OP_BOR: st[sp - 2] = (int32_t)((uint32_t)a | (uint32_t)b); sp--; break;
+ /* Count masked to five bits, like JS, so a silly shift is defined
+ * rather than undefined. Shifted unsigned because moving a bit into
+ * the sign of a signed value is undefined in C and simply wraps in JS,
+ * which is what the preview will show. */
+ case OP_SHL:
+ st[sp - 2] = (int32_t)((uint32_t)a << (b & 31));
+ sp--;
+ break;
+ /* Arithmetic: the sign is kept, so `-8 >> 1` is -4. Written out rather
+ * than left to the compiler, because a signed right shift is
+ * implementation-defined in C and this one has to match JS. */
+ case OP_SHR: {
+ unsigned k = (unsigned)(b & 31);
+ uint32_t u = (uint32_t)a;
+ st[sp - 2] = (int32_t)(a < 0 ? ~((~u) >> k) : u >> k);
+ sp--;
+ break;
+ }
case OP_OR: st[sp - 2] = a || b; sp--; break;
case OP_MIN: st[sp - 2] = a < b ? a : b; sp--; break;
case OP_MAX: st[sp - 2] = a > b ? a : b; sp--; break;
@@ -413,6 +504,7 @@ static bool stack_fits(const expr_prog_t *pr) {
case OP_VAR: sp++; break;
case OP_NEG:
case OP_NOT:
+ case OP_BNOT:
case OP_ABS: break;
case OP_CLAMP:
case OP_SELECT: sp -= 2; break;
diff --git a/tests/expr_test.c b/tests/expr_test.c
index fb191b8..abb3335 100644
--- a/tests/expr_test.c
+++ b/tests/expr_test.c
@@ -71,6 +71,41 @@ int main(void) {
/* The reason comparisons return numbers: masking is how you write a rule. */
eq("(3 < 4) * 200", 200);
+ printf("\n-- bitwise, on the 32-bit value\n");
+ eq("12 & 10", 8);
+ eq("12 | 10", 14);
+ eq("12 ^ 10", 6);
+ eq("~0", -1);
+ eq("~12", -13);
+ eq("1 << 4", 16);
+ eq("48 >> 2", 12);
+ /* `>>` keeps the sign, so it halves a negative rather than turning it into a
+ * large positive. Written out in the VM because C leaves it
+ * implementation-defined and JavaScript does not -- and the browser preview is
+ * JavaScript. */
+ eq("0 - 8 >> 1", -4);
+ eq("0 - 1 >> 28", -1);
+ /* The count is masked to five bits, which is what JS does with it: a shift
+ * nobody meant is defined rather than undefined. */
+ eq("1 << 33", 2);
+ eq("1 << 32", 1);
+ eq("8 >> 33", 4);
+ /* Bits bind tighter than comparison, unlike C, where `12 & 8 == 8` means
+ * `12 & (8 == 8)` and answers 0. Ritchie's own regret; not worth inheriting. */
+ eq("12 & 8 == 8", 1);
+ eq("5 ^ 3 == 6", 1);
+ /* And among themselves: & then ^ then |, as everywhere else. */
+ eq("1 | 6 & 4", 5);
+ eq("1 ^ 6 & 4", 5);
+ eq("3 & 1 | 4 & 4", 5);
+ /* `&&` and `||` still parse as themselves, one character longer. */
+ eq("1 & 3 && 2 & 1", 0);
+ eq("(1 & 3) && (2 & 2)", 1);
+ eq("0 | 0 || 1 & 1", 1);
+ /* The XOR texture, which is the reason to want any of this. */
+ eq("x ^ y", 7);
+ eq("(x ^ y) & 1", 1);
+
printf("\n-- variables\n");
eq("x", 3);
eq("y", 4);
diff --git a/tests/test_shadertoy.py b/tests/test_shadertoy.py
index 6af076c..62e205e 100644
--- a/tests/test_shadertoy.py
+++ b/tests/test_shadertoy.py
@@ -88,6 +88,32 @@ EXPRS = [
"(x + y) % 7 * 30",
"cols - x",
"rows * 2 - y",
+ # bitwise, where the two implementations agree by construction: int32, a
+ # five-bit shift count, an arithmetic `>>`, and -- unlike C -- bits binding
+ # tighter than comparison.
+ "x & 7",
+ "x | 8",
+ "x ^ y",
+ "~x",
+ "~0",
+ "x << 3",
+ "x >> 1",
+ "-8 >> 1",
+ "-1 >> 28",
+ "1 << 31",
+ "1 << 33",
+ "x << 40",
+ "x >> 40",
+ "0 - 5 & 3",
+ "x & 7 == 5",
+ "x ^ y | 1",
+ "x | y & 1",
+ "x ^ y ^ x",
+ "(x ^ y) % 16 < 2",
+ "(x & 1) ^ (y & 1)",
+ "~x & 255",
+ "t >> 6 & 15",
+ "(x * 7 ^ y * 13) & 31",
]
@@ -100,7 +126,9 @@ def rand_exprs(n, seed=20260814):
for _ in range(n):
a, b, c = (rng.choice(atoms) for _ in range(3))
out.append(rng.choice([
- f"({a} {rng.choice(['+', '-', '*', '/', '%'])} {b}) * 2",
+ f"({a} {rng.choice(['+', '-', '*', '/', '%', '&', '|', '^'])} {b}) * 2",
+ f"{a} {rng.choice(['<<', '>>'])} ({b} % 6)",
+ f"({a} ^ {b}) & {c}",
f"clamp({a} * {rng.choice([2, 3, 9])} - {b}, 0, 255)",
f"({a} {rng.choice(['<', '>', '==', '<=', '!='])} {b}) * {c}",
f"min({a}, {b}) + max({b}, {c})",