Files
ww/examples/mandelbrot/mandelbrot.ww

138 lines
3.8 KiB
Plaintext

// mandelbrot — ANSI-color Mandelbrot set, printed via libc.
//
// What this proves:
// 1. ww does f64 arithmetic (mul, add, sub, div) end to end.
// 2. The @symbol FFI mechanism binds write() from libc.so.6.
// 3. w6l's dynamic linker (PT_INTERP + PT_DYNAMIC + PLT/GOT) reaches
// a real .so at runtime — `ldd mandelbrot` shows libc.so.6 as a
// NEEDED entry.
//
// 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.
@symbol("write") fn c_write(fd: i32, buf: *void, n: u64) i64;
def W: i32 = 78;
def H: i32 = 30;
def MAXI: i32 = 80;
// Number of iterations before |z| escapes |z|>2, capped at MAXI.
fn iterate(cr: f64, ci: f64) i32 = {
let zr: f64 = 0.0;
let zi: f64 = 0.0;
let i: i32 = 0;
for (i < MAXI) {
let zr2: f64 = zr * zr;
let zi2: f64 = zi * zi;
if (zr2 + zi2 > 4.0) { return i; };
let nz: f64 = zr2 - zi2 + cr;
zi = (2.0 * zr) * zi + ci;
zr = nz;
i += 1;
};
return MAXI;
};
// 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 = {
// Classic Mandelbrot window. The y range is squashed to W/H so
// the picture looks roughly proportional in a terminal cell.
let xmin: f64 = -2.5;
let xmax: f64 = 1.0;
let ymin: f64 = -1.1;
let ymax: f64 = 1.1;
let dx: f64 = (xmax - xmin) / (W: f64);
let dy: f64 = (ymax - ymin) / (H: f64);
// 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);
off = emitcell(row.ptr, off, palette(n));
x += 1;
};
off = emitendrow(row.ptr, off);
c_write(1, row.ptr: *void, off: u64);
y += 1;
};
return 0;
};