uxn/projects/examples/exercises/brainfuck.tal

57 lines
1.2 KiB
Tal
Raw Permalink Normal View History

2024-02-15 23:31:27 -05:00
( usage: cat program.bf | uxncli brainfuck.rom )
2021-11-12 21:51:39 -05:00
2024-02-15 23:31:27 -05:00
|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
2021-11-12 21:51:39 -05:00
2024-02-15 23:31:27 -05:00
|0100
2022-09-04 16:08:38 -04:00
2024-02-15 23:31:27 -05:00
@on-reset ( -> )
;on-console .Console/vector DEO2
BRK
2021-11-12 21:51:39 -05:00
2024-02-15 23:31:27 -05:00
@on-console ( -> )
.Console/read DEI DUP #20 GTH ?{
POP <eval>
#0000 .Console/vector DEO2
#010f DEO
BRK }
[ LIT2 &ptr =program ] INC2k ,&ptr STR2
STA
BRK
2021-11-12 21:51:39 -05:00
2024-02-15 23:31:27 -05:00
@<eval> ( -- )
2024-02-16 00:00:00 -05:00
[ LIT2r =on-console/ptr ] LDA2r ;program
2024-02-15 23:31:27 -05:00
&while ( -- )
2024-02-16 00:00:00 -05:00
LDAk [ LIT "+ ] NEQ ?{ LDAkr STHr INC STH2kr STA }
LDAk [ LIT "- ] NEQ ?{ LDAkr STHr #01 SUB STH2kr STA }
LDAk [ LIT "> ] NEQ ?{ INC2r }
LDAk [ LIT "< ] NEQ ?{ [ LIT2r 0001 ] SUB2r }
LDAk [ LIT ". ] NEQ ?{ LDAkr [ LITr 18 ] DEOr }
2024-02-15 23:31:27 -05:00
LDAk [ LIT "[ ] NEQ ?{ goto-next }
LDAk [ LIT "] ] NEQ ?{ goto-back }
INC2 LDAk ?&while
2024-02-16 00:00:00 -05:00
POP2 POP2r JMP2r
2021-11-12 21:51:39 -05:00
2024-02-16 00:00:00 -05:00
@goto-next ( `mem* prg* -- )
OVR2r LDAr STHr ?{ JMP2r }
2024-02-15 23:47:53 -05:00
[ LITr 00 ] INC2
2024-02-15 23:31:27 -05:00
&loop ( -- )
2024-02-15 23:47:53 -05:00
LDAk [ LIT "[ ] NEQ ?{ INCr }
LDAk [ LIT "] ] NEQ ?{
STHkr ?{ POPr JMP2r }
[ LITr 01 ] SUBr }
INC2 !&loop
2021-11-12 21:51:39 -05:00
2024-02-16 00:00:00 -05:00
@goto-back ( `mem* prg* -- )
OVR2r LDAr STHr ?{ JMP2r }
2024-02-15 23:47:53 -05:00
[ LITr 00 ] #0001 SUB2
2024-02-15 23:31:27 -05:00
&loop ( -- )
2024-02-15 23:47:53 -05:00
LDAk [ LIT "] ] NEQ ?{ INCr }
LDAk [ LIT "[ ] NEQ ?{
STHkr ?{ POPr JMP2r }
[ LITr 01 ] SUBr }
#0001 SUB2 !&loop
2021-11-12 21:51:39 -05:00
2024-02-15 23:31:27 -05:00
@program
2021-11-12 21:51:39 -05:00