Created
October 21, 2020 10:20
-
-
Save drkameleon/0a1c186eb640db614a3c2c1516dca4e1 to your computer and use it in GitHub Desktop.
BF interpreter in Arturo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print --- | |
========================= | |
Brainf*ck compiler | |
In Arturo | |
========================= | |
--- | |
tape: [0] | |
jumps: #[] | |
dataPointer: 0 | |
instructionPointer: 0 | |
precomputeJumps: $[][ | |
Stack: [] | |
instrPointer: 0 | |
while [instrPointer<codeLength] [ | |
command: get split code instrPointer | |
print ["checking command:" command] | |
case [command=] | |
when? ["["] [ 'Stack ++ instrPointer ] | |
when? ["]"] [ | |
target: last Stack | |
inspect Stack | |
Stack: slice Stack 0 (size Stack)-1 | |
set jumps to :string target instrPointer | |
set jumps to :string instrPointer target | |
] | |
else [] | |
inc 'instrPointer | |
] | |
] | |
stateIsValid: $[][ | |
(dataPointer >= 0) | |
and (instructionPointer >= 0) | |
and (dataPointer < size tape) | |
and (instructionPointer < codeLength) | |
] | |
interpret: $[][ | |
while [stateIsValid][ | |
command: get split code instructionPointer | |
case [command=] | |
when? ["+"] [ set tape dataPointer (get tape dataPointer)+1] | |
when? ["-"] [ set tape dataPointer (get tape dataPointer)-1] | |
when? [">"] [ inc 'dataPointer if dataPointer=(size tape) -> 'tape ++ 0] | |
when? ["<"] [ dec 'dataPointer ] | |
when? ["."] [ print to :char get tape dataPointer ] | |
when? [","] [ | |
inp: to :integer input "" | |
if inp=13 [ inp: 10 ] | |
if inp=3 [ print "something went wrong" exit ] | |
set tape dataPointer inp | |
] | |
when? ["["] [ | |
if (get tape dataPointer)=0 [ | |
instructionPointer: get jumps to :string instructionPointer ] | |
] | |
] | |
when? ["]"] [ | |
if (get tape dataPointer)<>0 [ | |
instructionPointer: get jumps to :string instructionPointer ] | |
] | |
] | |
else [] | |
inc 'instructionPointer | |
] | |
] | |
if (size arg)<1 [ print "Not enough arguments - Usage: bfc <script>" exit ] | |
code: read arg\0 | |
codeLength: size code | |
precomputeJumps | |
print jumps | |
interpret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment