(brainfuck.tal) Modernizing

This commit is contained in:
Devine Lu Linvega 2024-02-15 20:31:27 -08:00
parent 57396faa94
commit a41f1ce3e8
2 changed files with 56 additions and 61 deletions

View File

@ -1,71 +1,64 @@
( Brainfuck: ( usage: cat program.bf | uxncli brainfuck.rom )
> 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 )
|0100 ( -> ) |10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
;memory |0100
;program
&while @on-reset ( -> )
LDAk LIT "> NEQ ,&movr JCN [ SWP2 INC2 SWP2 ] &movr ;on-console .Console/vector DEO2
LDAk LIT "< NEQ ,&movl JCN [ SWP2 #0001 SUB2 SWP2 ] &movl BRK
LDAk LIT "+ NEQ ,&incr JCN [ OVR2 STH2k LDA INC STH2r STA ] &incr
LDAk LIT "- NEQ ,&decr JCN [ OVR2 STH2k LDA #01 SUB STH2r STA ] &decr @on-console ( -> )
LDAk LIT ". NEQ ,&emit JCN [ OVR2 LDA #18 DEO ] &emit .Console/read DEI DUP #20 GTH ?{
LDAk LIT "[ NEQ ,&next JCN [ ,goto-next JSR ] &next POP <eval>
LDAk LIT "] NEQ ,&prev JCN [ ,goto-back JSR ] &prev #0000 .Console/vector DEO2
INC2 LDAk ,&while JCN
POP2
( halt )
#010f DEO #010f DEO
BRK }
[ LIT2 &ptr =program ] INC2k ,&ptr STR2
STA
BRK
BRK @<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
@goto-next ( -- ) @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
OVR2 LDA #00 EQU JMP JMP2r @goto-back ( mem* prg* -- )
( depth ) LITr 00 OVR2 LDA ?{ JMP2r }
INC2 ( depth ) [ LITr 00 ] #0001 SUB2
&loop &loop ( -- )
LDAk LIT "[ NEQ JMP INCr LDAk [ LIT "] ] NEQ JMP
LDAk LIT "] NEQ ,&no-end JCN INCr LDAk [ LIT "[ ] NEQ ?&>no-end
STHkr #00 EQU ,&end JCN STHkr #00 EQU ?&end
LITr 01 SUBr LITr 01 SUBr &>no-end
&no-end #0001 SUB2 LDAk ?&loop
INC2 LDAk ,&loop JCN &end ( depth )
&end POPr JMP2r
( depth ) POPr
JMP2r @program
@goto-back ( -- )
OVR2 LDA #00 NEQ JMP JMP2r
( depth ) LITr 00
#0001 SUB2
&loop
LDAk LIT "] NEQ JMP INCr
LDAk LIT "[ NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
LITr 01 SUBr
&no-end
#0001 SUB2 LDAk ,&loop JCN
&end
( depth ) POPr
JMP2r
@program ( Hello World! )
"++++++++[>++++[>++>+++>+++>+<<<<
"-]>+>+>->>+[<]<-]>>.>---.+++++++
"..+++.>>.<-.<.+++.------.-------
"-.>>+.>++. $1
@memory

View File

@ -0,0 +1,2 @@
>++++++++[<+++++++++>-]<.>++++[<+++++++>-]<+.+++++++..+++.>>++++++[<+++++++>-]<++.------------.>++++++[<+++++++++>-]<+.<.+++.------.--------.>>>++++[<++++++++>-]<+.