0
0
Fork 0
mirror of https://git.sr.ht/~rabbits/uxn synced 2024-11-05 22:05:04 +00:00
uxn/projects/examples/exercises/brainfuck.tal

75 lines
1.6 KiB
Tal
Raw Normal View History

2021-11-13 02:51:39 +00:00
( brainfuck interpreter )
%DEC { #01 SUB }
%DEC2 { #0001 SUB2 }
%DECr { LITr 01 SUBr }
%HALT { #0101 #0e DEO2 }
%EMIT { #18 DEO }
|0100 ( -> )
2022-02-15 18:24:11 +00:00
;memory
2021-11-13 02:51:39 +00:00
;program
&while
2022-02-15 18:24:11 +00:00
( 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
2021-11-13 02:51:39 +00:00
INC2 LDAk ,&while JCN
POP2
HALT
BRK
@goto-next ( -- )
2022-02-15 18:24:11 +00:00
OVR2 LDA #00 EQU JMP JMP2r
2021-11-13 02:51:39 +00:00
( depth ) LITr 00
INC2
&loop
2021-11-13 16:08:21 +00:00
LDAk LIT '[ NEQ JMP INCr
2021-11-13 02:51:39 +00:00
LDAk LIT '] NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
DECr
&no-end
INC2 LDAk ,&loop JCN
&end
( depth ) POPr
2022-02-15 18:24:11 +00:00
JMP2r
2021-11-13 02:51:39 +00:00
@goto-back ( -- )
2022-02-15 18:24:11 +00:00
OVR2 LDA #00 NEQ JMP JMP2r
2021-11-13 02:51:39 +00:00
( depth ) LITr 00
DEC2
&loop
2021-11-13 16:08:21 +00:00
LDAk LIT '] NEQ JMP INCr
2021-11-13 02:51:39 +00:00
LDAk LIT '[ NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
DECr
&no-end
DEC2 LDAk ,&loop JCN
&end
( depth ) POPr
2022-02-15 18:24:11 +00:00
JMP2r
2021-11-13 02:51:39 +00:00
@program ( Hello World! )
"++++++++[>++++[>++>+++>+++>+<<<<
"-]>+>+>->>+[<]<-]>>.>---.+++++++
"..+++.>>.<-.<.+++.------.-------
2022-02-15 18:24:11 +00:00
"-.>>+.>++. $1
@memory