uxn/projects/examples/exercises/brainfuck.tal

70 lines
1.6 KiB
Tal
Raw Normal View History

2022-03-25 13:29:45 -04:00
( Brainfuck:
> Move the pointer to the right
< Move the pointer to the left
+ Increment the memory cell at the pointer
- Decrement the memory cell at the pointer
. Output the character signified by the cell at the pointer
, Input a character and store it in the cell at the pointer
[ Jump past the matching ] if the cell at the pointer is 0
] Jump back to the matching [ if the cell at the pointer is nonzero )
2021-11-12 21:51:39 -05:00
2022-03-25 13:29:45 -04:00
|0100 ( -> ) @reset
2021-11-12 21:51:39 -05:00
2022-02-15 13:24:11 -05:00
;memory
2021-11-12 21:51:39 -05:00
;program
&while
2022-02-15 13:24:11 -05:00
LDAk LIT '> NEQ ,&movr JCN [ SWP2 INC2 SWP2 ] &movr
2022-03-25 13:29:45 -04:00
LDAk LIT '< NEQ ,&movl JCN [ SWP2 #0001 SUB2 SWP2 ] &movl
2022-02-15 13:24:11 -05:00
LDAk LIT '+ NEQ ,&incr JCN [ OVR2 STH2k LDA INC STH2r STA ] &incr
2022-03-25 13:29:45 -04:00
LDAk LIT '- NEQ ,&decr JCN [ OVR2 STH2k LDA #01 SUB STH2r STA ] &decr
LDAk LIT '. NEQ ,&emit JCN [ OVR2 LDA #18 DEO ] &emit
2022-02-15 13:24:11 -05:00
LDAk LIT '[ NEQ ,&next JCN [ ,goto-next JSR ] &next
LDAk LIT '] NEQ ,&prev JCN [ ,goto-back JSR ] &prev
2021-11-12 21:51:39 -05:00
INC2 LDAk ,&while JCN
POP2
2022-03-25 13:29:45 -04:00
( halt ) #010f DEO
2021-11-12 21:51:39 -05:00
BRK
@goto-next ( -- )
2022-02-15 13:24:11 -05:00
OVR2 LDA #00 EQU JMP JMP2r
2021-11-12 21:51:39 -05:00
( depth ) LITr 00
INC2
&loop
2021-11-13 11:08:21 -05:00
LDAk LIT '[ NEQ JMP INCr
2021-11-12 21:51:39 -05:00
LDAk LIT '] NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
2022-03-25 13:29:45 -04:00
LITr 01 SUBr
2021-11-12 21:51:39 -05:00
&no-end
INC2 LDAk ,&loop JCN
&end
( depth ) POPr
2022-02-15 13:24:11 -05:00
JMP2r
2021-11-12 21:51:39 -05:00
@goto-back ( -- )
2022-02-15 13:24:11 -05:00
OVR2 LDA #00 NEQ JMP JMP2r
2021-11-12 21:51:39 -05:00
( depth ) LITr 00
2022-03-25 13:29:45 -04:00
#0001 SUB2
2021-11-12 21:51:39 -05:00
&loop
2021-11-13 11:08:21 -05:00
LDAk LIT '] NEQ JMP INCr
2021-11-12 21:51:39 -05:00
LDAk LIT '[ NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
2022-03-25 13:29:45 -04:00
LITr 01 SUBr
2021-11-12 21:51:39 -05:00
&no-end
2022-03-25 13:29:45 -04:00
#0001 SUB2 LDAk ,&loop JCN
2021-11-12 21:51:39 -05:00
&end
( depth ) POPr
2022-02-15 13:24:11 -05:00
JMP2r
2021-11-12 21:51:39 -05:00
@program ( Hello World! )
"++++++++[>++++[>++>+++>+++>+<<<<
"-]>+>+>->>+[<]<-]>>.>---.+++++++
"..+++.>>.<-.<.+++.------.-------
2022-02-15 13:24:11 -05:00
"-.>>+.>++. $1
@memory