- Parse code — from +, -,[, ] and etc to commands:
ADD X — add x to current cell value
MOV X - move pointer on X cells
if x > 0, move from left to right
else move from right to left
PUT X - print cell char value X times
GET X - scan cell value x times. read the same value more than once is a little bit strange, but who knows?...
GOTO X - skip X commands
if X < 0, go back to X commands
ZERO X - make current cell 0
- How parsing works:
-
Go through all code symbols
-
If code[i] is one of BF commands (+,-,<,> etc)
- If previous command is same as current, just increase/decrease X
- else, add current command with X = 1/-1
-
If code[i] is [:
save this command number in special array, move this array iterator right and add GOTO command
-
if code[i] is ]:
go to last index in special array - thats number of corresponding [ command
then, make X of corresponding [ equals (] index) - ([ index) and ] X just as previous but *(-1)
-
If code[i] == [ and code[i+1] == (+ / -) and code[i+2] == ], then current command is ZERO
- Then just run parsed code