uxn/projects/examples/exercises/brainfuck.tal

65 lines
1.3 KiB
Tal
Raw 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> ( -- )
;on-console/ptr LDA2 ;program
&while ( -- )
LDAk [ LIT "+ ] NEQ ?{
OVR2 STH2k LDA INC STH2r STA }
LDAk [ LIT "- ] NEQ ?{
OVR2 STH2k LDA #01 SUB STH2r STA }
LDAk [ LIT "> ] NEQ ?{ SWP2 INC2 SWP2 }
LDAk [ LIT "< ] NEQ ?{
SWP2 #0001 SUB2 SWP2 }
LDAk [ LIT ". ] NEQ ?{
OVR2 LDA #18 DEO }
LDAk [ LIT "[ ] NEQ ?{ goto-next }
LDAk [ LIT "] ] NEQ ?{ goto-back }
INC2 LDAk ?&while
POP2 JMP2r
2021-11-12 21:51:39 -05:00
2024-02-15 23:31:27 -05:00
@goto-next ( mem* prg* -- )
OVR2 LDA ?{ JMP2r }
( depth ) [ LITr 00 ] INC2
&loop ( -- )
LDAk [ LIT "[ ] NEQ JMP
INCr LDAk [ LIT "] ] NEQ ?&>no-end
STHkr #00 EQU ?&end
LITr 01 SUBr &>no-end
INC2 LDAk ?&loop
&end ( depth )
POPr JMP2r
2021-11-12 21:51:39 -05:00
2024-02-15 23:31:27 -05:00
@goto-back ( mem* prg* -- )
OVR2 LDA ?{ JMP2r }
( depth ) [ LITr 00 ] #0001 SUB2
&loop ( -- )
LDAk [ LIT "] ] NEQ JMP
INCr LDAk [ LIT "[ ] NEQ ?&>no-end
STHkr #00 EQU ?&end
LITr 01 SUBr &>no-end
#0001 SUB2 LDAk ?&loop
&end ( depth )
POPr JMP2r
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