XXIIVV

An ALGOL for your cat.

The syntax of POP-2 is ALGOL-like, assignments are in the infix notation, but the language design is deeply concatenative:

foo := 123; ( In Pascal )
123 -> foo; ( In POP-2 )

The language has the explicit notion of an operand stack. Thus, the prior assignment can be written as two separate statements where the first one leaves a value on the stack, and the second, consumes it:

123; -> foo;

Interpreter

The language can easily be compiled to Uxntal, in fact, the language is so elegant and small that an entire compiler is less than 800 lines and fits within 3kb of memory.

At first glance, the language looks a little like Pascal. A statement begins with a keyword, followed by expressions, terminated by a semi-colon. Evaluation consists of moving through the program leaving values on the stack as needed.

Comment

The comment statement will block out a length of text until a terminating semi-colon.

comment This is a comment,
        these can span multiple lines;

Vars

Variables are allocated inside the vars statement, these can be of any length, by default in a 16-bit system, a variable is made of two 8-bit cells.

comment The variable foo is 10 bytes:
vars foo:10;

comment The variable foo is 7 bytes, initialized to "abc def":
vars bar:"abc def";

comment Write 0x1234 in cells overlapping two variables:
vars hb:1 lb:2;
0x1234 -> hb;

A variable can store an array of cells, each cell is a byte, the position of a reference in memory(a pointer) can be reached with the # prefix. To store the bytes 0x89 and 0xab in the third and fourth cells of the array:

vars array:10;
0x89ab -> (#array+2);

If/Then/Elseif/Else/Close

At first glance, the language looks awfully unsurprising, but notice how each case pushes a value on the stack instead of doing an assignment:

vars x; 4 -> x;

if x > 4
	then 1;
elseif x = 4
	then 2;
else
	3;
close
2

The close keyword takes no argument and needs no semi-colon.

Function

Functions are declared in the typical ALGOL fashion, recursion simply leaves the arguments on the stack:

function factRec n;
	if n = 0
		then 1;
	else
		n * factRec(n-1);
	close
end

A function can be quoted with the @ prefix, and applied with the diamond operator:

function double n;
	n + n;
end

function apply fn n;
	n <> fn;
end

apply(@double, 5);
10

The end keyword takes no argument and needs no semi-colon.

Labels

Loops are written with GOTOs, making it possible to loop without dirtying the stack with iterators.

comment Loop for 10 times;
vars i;
loop:
	if i < 10
		then i + 1 -> i, goto loop;
	close
i;
10

I/O

The host system being Varvara, this implementation deviates a bit from the original POP-2, that used the big arrow(=>) without a destination, this will use this Console port number(0x17, 0x18) to output the ASCII letter "H":

0x48 => 23; Outputs: H

Putting it all together, we can make a string printing function:

function printString s;
	vars c:1 cells:2 i; 0 -> i;
loop:
	{s+i} -> cells;
	if c
		then c => 23, i + 1 -> i, goto loop;
	close
end

vars text:"Hello World!\n";
printString(#text);

incoming: 2026