uxn/projects/examples/exercises/brainfuck.tal

75 lines
1.6 KiB
Tal

( brainfuck interpreter )
%DEC { #01 SUB }
%DEC2 { #0001 SUB2 }
%DECr { LITr 01 SUBr }
%HALT { #0101 #0e DEO2 }
%EMIT { #18 DEO }
|0100 ( -> )
;memory
;program
&while
( Move the pointer to the right )
LDAk LIT '> NEQ ,&movr JCN [ SWP2 INC2 SWP2 ] &movr
( Move the pointer to the left )
LDAk LIT '< NEQ ,&movl JCN [ SWP2 DEC2 SWP2 ] &movl
( Increment the memory cell at the pointer )
LDAk LIT '+ NEQ ,&incr JCN [ OVR2 STH2k LDA INC STH2r STA ] &incr
( Decrement the memory cell at the pointer )
LDAk LIT '- NEQ ,&decr JCN [ OVR2 STH2k LDA DEC STH2r STA ] &decr
( Output the character signified by the cell at the pointer )
LDAk LIT '. NEQ ,&emit JCN [ OVR2 LDA EMIT ] &emit
( Jump past the matching ] if the cell at the pointer is 0 )
LDAk LIT '[ NEQ ,&next JCN [ ,goto-next JSR ] &next
( Jump back to the matching [ if the cell at the pointer is nonzero )
LDAk LIT '] NEQ ,&prev JCN [ ,goto-back JSR ] &prev
INC2 LDAk ,&while JCN
POP2
HALT
BRK
@goto-next ( -- )
OVR2 LDA #00 EQU JMP JMP2r
( depth ) LITr 00
INC2
&loop
LDAk LIT '[ NEQ JMP INCr
LDAk LIT '] NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
DECr
&no-end
INC2 LDAk ,&loop JCN
&end
( depth ) POPr
JMP2r
@goto-back ( -- )
OVR2 LDA #00 NEQ JMP JMP2r
( depth ) LITr 00
DEC2
&loop
LDAk LIT '] NEQ JMP INCr
LDAk LIT '[ NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
DECr
&no-end
DEC2 LDAk ,&loop JCN
&end
( depth ) POPr
JMP2r
@program ( Hello World! )
"++++++++[>++++[>++>+++>+++>+<<<<
"-]>+>+>->>+[<]<-]>>.>---.+++++++
"..+++.>>.<-.<.+++.------.-------
"-.>>+.>++. $1
@memory