examples/mandelbrot: ANSI 256-color background per pixel

This commit is contained in:
2026-05-11 17:41:59 +09:00
parent 647d3ee05e
commit e50849d5ec

View File

@@ -1,4 +1,4 @@
// mandelbrot — ASCII Mandelbrot set, printed via libc.
// mandelbrot — ANSI-color Mandelbrot set, printed via libc.
//
// What this proves:
// 1. ww does f64 arithmetic (mul, add, sub, div) end to end.
@@ -7,9 +7,11 @@
// a real .so at runtime — `ldd mandelbrot` shows libc.so.6 as a
// NEEDED entry.
//
// We use libc's `write` (a thin syscall wrapper) rather than `putchar`
// because the runtime here doesn't call __libc_start_main, so stdio
// state stays uninitialised and putchar's buffer is never flushed.
// Each pixel is a colored space rendered via xterm 256-color background
// escapes (\e[48;5;Nm). We use libc's `write` (a thin syscall wrapper)
// rather than `putchar` because the runtime here doesn't call
// __libc_start_main, so stdio state stays uninitialised and putchar's
// buffer is never flushed.
//
// Build: see ./Makefile. No cc — pure ww toolchain.
@@ -36,14 +38,70 @@ fn iterate(cr: f64, ci: f64) i32 = {
return MAXI;
};
// Pick an ASCII glyph by escape iteration count. Inside the set
// (n == MAXI) renders as space — the classic look.
fn shade(n: i32) i32 = {
if (n >= MAXI) { return ' ': i32; };
let chars: str = " .:-=+*#%@";
let idx: i32 = (n * chars.len) / MAXI;
if (idx >= chars.len) { idx = chars.len - 1; };
return chars[idx]: i32;
// Pick an xterm-256 color for an escape count. Inside the set is black;
// outside cycles blue → cyan → green → yellow → red.
fn palette(n: i32) i32 = {
if (n >= MAXI) { return 16; }; // black, inside set
let q: i32 = (n * 12) / MAXI;
if (q == 0) { return 17; }; // dark blue
if (q == 1) { return 18; };
if (q == 2) { return 19; };
if (q == 3) { return 21; }; // bright blue
if (q == 4) { return 33; };
if (q == 5) { return 45; };
if (q == 6) { return 51; }; // cyan
if (q == 7) { return 50; };
if (q == 8) { return 46; }; // green
if (q == 9) { return 154; };
if (q == 10) { return 226; }; // yellow
if (q == 11) { return 208; }; // orange
return 196; // red
};
// Write `n` as decimal into buf at off; return new offset.
fn writedec(buf: *u8, off: i32, n: i32) i32 = {
if (n == 0) { buf[off] = '0': u8; return off + 1; };
let tmp: [4]u8;
let v: i32 = n;
let i: i32 = 0;
for (v > 0) {
tmp[i] = ((v % 10) + 48): u8;
v = v / 10;
i += 1;
};
let cnt: i32 = i;
let j: i32 = 0;
for (j < cnt) {
i -= 1;
buf[off + j] = tmp[i];
j += 1;
};
return off + cnt;
};
// Emit "\e[48;5;<color>m " (set background + the pixel) into buf.
fn emitcell(buf: *u8, off: i32, color: i32) i32 = {
buf[off + 0] = 27u8;
buf[off + 1] = '[': u8;
buf[off + 2] = '4': u8;
buf[off + 3] = '8': u8;
buf[off + 4] = ';': u8;
buf[off + 5] = '5': u8;
buf[off + 6] = ';': u8;
let p: i32 = writedec(buf, off + 7, color);
buf[p + 0] = 'm': u8;
buf[p + 1] = ' ': u8;
return p + 2;
};
// Emit "\e[0m\n" — restore default colors, end the row.
fn emitendrow(buf: *u8, off: i32) i32 = {
buf[off + 0] = 27u8;
buf[off + 1] = '[': u8;
buf[off + 2] = '0': u8;
buf[off + 3] = 'm': u8;
buf[off + 4] = 10u8; // '\n'
return off + 5;
};
export fn main() i32 = {
@@ -61,21 +119,23 @@ export fn main() i32 = {
let dx: f64 = (xmax - xmin) / (W: f64);
let dy: f64 = (ymax - ymin) / (H: f64);
// Compose the picture into a row buffer, then flush per row to
// avoid one libc call per character.
let row: [128]u8;
// Each pixel is ~12 bytes (\e[48;5;NNNm ); plus 5 for the reset
// + newline. 2048 bytes per row is comfortably above the worst
// case of W=78.
let row: [2048]u8;
let y: i32 = 0;
for (y < H) {
let ci: f64 = ymin + (y: f64) * dy;
let off: i32 = 0;
let x: i32 = 0;
for (x < W) {
let cr: f64 = xmin + (x: f64) * dx;
let n: i32 = iterate(cr, ci);
row[x] = (shade(n)): u8;
off = emitcell(row.ptr, off, palette(n));
x += 1;
};
row[W] = 10u8; // '\n'
c_write(1, row.ptr: *void, (W + 1): u64);
off = emitendrow(row.ptr, off);
c_write(1, row.ptr: *void, off: u64);
y += 1;
};
return 0;