I have reordered time. I have turned the world upside down, and I have done it all for you!
Self-Generating
The following program will emit its own bytecode when run:
( uxncli gen.rom > gen-self.rom ) @q ;&end ;q &l LDAk #18 DEO INC2 GTH2k ?&l &end
The program above compiles to the resulting emitted bytecode:
a001 0fa0 0100 9480 1817 21aa 20ff f7
Self-Printing
The following program will emit a program that emits its own bytecode as hexadecimal ascii characters:
( uxncli pri.rom ) @q ;end ;q &l LDA2k phex #2018 DEO INC2 INC2 GTH2k ?&l BRK @phex ( short* -- ) SWP phex/b &b DUP #04 SFT phex/c &c #0f AND DUP #09 GTH #27 MUL ADD [ LIT "0 ] ADD #18 DEO JMP2r @end
Running the previous program generates the following hexadecimal values which are also valid source code:
a001 32a0 0100 b460 000b a020 1817 2121 aa20 fff2 0004 6000 0006 8004 1f60 0000 800f 1c06 8009 0a80 271a 1880 3018 8018 176c
Maze
A small program to print the iconic Commodore 64 maze:
@maze ( -> ) ( seed ) #c5 DEI2 ,&seed STR2 &w ( -- ) [ LIT2 "/\ ] [ LIT2 &seed &x $1 &y $1 ] ( rand x ) ADDk #50 SFT EOR DUP #03 SFT EOR DUP ,&x STR ( rand y ) SUBk #01 SFT EOR EOR DUP ,&y STR ( emit ) #01 AND [ JMP SWP POP ] #18 DEO !&w
Where Are We?
From anywhere during a program's execution, the location of the program counter can be pushed onto the working stack with the following snippet:
[ LITr 00 ] JSRr