(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:
> 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 )
( usage: cat program.bf | uxncli brainfuck.rom )
|0100 ( -> )
|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
;memory
;program
&while
LDAk LIT "> NEQ ,&movr JCN [ SWP2 INC2 SWP2 ] &movr
LDAk LIT "< NEQ ,&movl JCN [ SWP2 #0001 SUB2 SWP2 ] &movl
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
LDAk LIT ". NEQ ,&emit JCN [ OVR2 LDA #18 DEO ] &emit
LDAk LIT "[ NEQ ,&next JCN [ ,goto-next JSR ] &next
LDAk LIT "] NEQ ,&prev JCN [ ,goto-back JSR ] &prev
INC2 LDAk ,&while JCN
POP2
( halt )
#010f DEO
|0100
BRK
@on-reset ( -> )
;on-console .Console/vector DEO2
BRK
@goto-next ( -- )
@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
OVR2 LDA #00 EQU JMP JMP2r
( depth ) LITr 00
INC2
&loop
LDAk LIT "[ NEQ JMP INCr
LDAk LIT "] NEQ ,&no-end JCN
STHkr #00 EQU ,&end JCN
LITr 01 SUBr
&no-end
INC2 LDAk ,&loop JCN
&end
( depth ) POPr
@<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
JMP2r
@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
@goto-back ( -- )
@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
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
@program
JMP2r
@program ( Hello World! )
"++++++++[>++++[>++>+++>+++>+<<<<
"-]>+>+>->>+[<]<-]>>.>---.+++++++
"..+++.>>.<-.<.+++.------.-------
"-.>>+.>++. $1
@memory

View File

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