CompilerFun.DD 2

so i wrote another compiler, but a proper one this time!!

it compiles to (crappy) x86_64 assembly and can call into C functions

the language is almost, but not quite, entirely unlike T3X9

heres an example that draws the mandelbrot set using raylib

const WHITE = 4294967295, BLACK = 4278190080, GREY = 4286019447; ! ARGB constants
const WIDTH = 80*8, HEIGHT = 24*16;

! Denominator for fixed-point computations
const   SCALE = 100;

! ASCII renditions of filled rectangles:
struct COLORS = FULL, UPPER, LOWER, EMPTY;

! Compute the color of a point x+yi
function f(x, y) do
    var cr, ci, zr, zi, ir;
    var i;

    zr := 0;            ! z = 0+0i
    zi := 0;
    cr := x*SCALE/25;   ! c = x+yi
    ci := y*SCALE/20;
    ! try 100 iterations
    for (i=0, 100) do
        ! z := z^2+c
        ir := zr*zr/SCALE - zi*zi/SCALE;
        zi := zr*zi/SCALE + zi*zr/SCALE + ci;
        zr := ir + cr;
        ! if |z| > 2+2i, x+yi is not a member
        ! of the Mandelbrot set M
        if (zi > 2*SCALE \/ zr > 2*SCALE \/
            zi < -2*SCALE\/ zr < -2*SCALE
        )
            return 0;
    end
    ! |z| <= 2+2i after 100 iterations,
    ! so x+yi is probably in M
    return %1;
end

do
    var x, y, r;    ! coordinates, point color
    var even;       ! even line flag
    var line{80};   ! line buffer
    var i, ro, col;

    InitWindow(WIDTH, HEIGHT, "T3X9 Raylib");
    SetTargetFPS(60);

    while (\WindowShouldClose()) do
        ro := 0;
        BeginDrawing();

            ClearBackground(WHITE);

            even := 0;
            for (y=-24, 25) do      ! lines
                for (x=-59, 20) do  ! columns
                    r := \f(x,y);
                    ! When drawing an even line,
                    ! merge the color of r into the
                    ! current rectangle ...
                    ie (even)
                        line{x+59} :=
                            line{x+59}->
                                ! odd point was on
                                r-> FULL: UPPER:
                                ! odd point was off
                                r-> LOWER: EMPTY;
                    ! ... else just memorize the color
                    else
                        line{x+59} := r;
                end
                ! Even line completed, print it
                if (even) do
                    for (i=0, 80) do
                        ie (line{i} = FULL) col := BLACK;
                        else ie (line{i} = UPPER \/ line{i} = LOWER) col := GREY;
                        else col := WHITE;
                        DrawRectangle(i * 8, ro, 8, 16, col);
                    end
                    ro := ro + 16;
                end
                even := \even;
            end

        EndDrawing();
    end

    CloseWindow();
end

mandelbrot

Nasty compiler source is here, im pretty sure theres some bugs that ive missed

." GOODNIGHT."