Last active
December 7, 2016 10:57
-
-
Save imakin/f303d572eef4078c083837802a313d30 to your computer and use it in GitHub Desktop.
testing if possible to make brainfuck interpreter written in brainfuck itself
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
[ | |
brainfuck interpreter written in brainfuck | |
flag of code instruction: | |
p if 62, ascii for POINTER_INC command ">" | |
p if 60, ascii for POINTER_DEC command "<" | |
p5 if 43, ascii for INC command "+" | |
p3 if 45, ascii for DEC command "-" | |
p2 if 46, ascii for OUTPUT command "." | |
p4 if 44, ascii for INPUT command "," | |
p if 91, ascii for loop begin command "[" ][ | |
p if 93, ascii for loop end command "]" | |
p of 26, end of code (commonly ctrl-z) | |
93 loop end | |
91 loop begin | |
62 > | |
60 < | |
46 . | |
45 - | |
44 , | |
43 + | |
33 ! for eof | |
26 eof | |
usage of memory from | |
p12 p13 p14 p15 p16 p17 p18 p19 p20 p21 p22 p23 p24 ... | |
3x code_ptr code_mem0 code_mem1 | |
code_saved code_saved | |
f0 f1 f2 f0 f1 f2 | |
code_ptr is the current position of pointer in the VM | |
code_memx is the memory in the VM | |
code_saved is saved instruction (in case begin loop and end loop instruction must be saved) | |
f0 f1 f2 is free to use | |
] | |
p0 for input flag | |
+ | |
[ | |
>, p1 is code read | |
FLAG if EOF26 | |
> + p2 1 | |
>[-] p3 0 | |
>[-] p4 0 | |
>[-] p5 0 | |
>[-] p6 0 | |
>[-] p7 0 | |
>[-] p8 0 | |
>[-] p9 0 | |
>[-] p10 0 | |
>[-] p11 0 | |
<<<<<<<<<< back to p1 | |
----- ----- | |
----- ----- | |
----- -- 26 | |
[ | |
FLAG if EOF 33 | |
>[-] p2 0 | |
> + p3 1 | |
>[-] p4 0 | |
>[-] p5 0 | |
>[-] p6 0 | |
>[-] p7 0 | |
>[-] p8 0 | |
>[-] p9 0 | |
>[-] p10 0 | |
>[-] p11 0 | |
<<<<<<<<<< back to p1 | |
--- | |
--- 33 | |
[ | |
FLAG if INC 43 | |
>[-] p2 0 | |
>[-] p3 0 | |
> + p4 1 | |
>[-] p5 0 | |
>[-] p6 0 | |
>[-] p7 0 | |
>[-] p8 0 | |
>[-] p9 0 | |
>[-] p10 0 | |
>[-] p11 0 | |
<<<<<<<<<< back to p1 | |
-- ----- | |
--- 43 | |
[ | |
; | |
FLAG if INPUT 44 | |
>[-] p2 0 | |
>[-] p3 0 | |
>[-] p4 0 | |
> + p5 1 | |
>[-] p6 0 | |
>[-] p7 0 | |
>[-] p8 0 | |
>[-] p9 0 | |
>[-] p10 0 | |
>[-] p11 0 | |
<<<<<<<<<< back to p1 | |
- 44 | |
[ | |
FLAG if DEC 45 | |
>[-] p2 0 | |
>[-] p3 0 | |
>[-] p4 0 | |
>[-] p5 0 | |
> + p6 1 | |
>[-] p7 0 | |
>[-] p8 0 | |
>[-] p9 0 | |
>[-] p10 0 | |
>[-] p11 0 | |
<<<<<<<<<< back to p1 | |
- 45 | |
[ | |
FLAG if OUTPUT 46 | |
>[-] p2 0 | |
>[-] p3 0 | |
>[-] p4 0 | |
>[-] p5 0 | |
>[-] p6 0 | |
> + p7 1 | |
>[-] p8 0 | |
>[-] p9 0 | |
>[-] p10 0 | |
>[-] p11 0 | |
<<<<<<<<<< back to p1 | |
- 46 | |
[ | |
FLAG if POINTER MOVE LEFT 60 | |
>[-] p2 0 | |
>[-] p3 0 | |
>[-] p4 0 | |
>[-] p5 0 | |
>[-] p6 0 | |
>[-] p7 0 | |
> + p8 1 | |
>[-] p9 0 | |
>[-] p10 0 | |
>[-] p11 0 | |
<<<<<<<<<< back to p1 | |
---- | |
----- ----- 60 | |
[ | |
FLAG if POINTER MOVE RIGHT 62 | |
>[-] p2 0 | |
>[-] p3 0 | |
>[-] p4 0 | |
>[-] p5 0 | |
>[-] p6 0 | |
>[-] p7 0 | |
>[-] p8 0 | |
> + p9 1 | |
>[-] p10 0 | |
>[-] p11 0 | |
<<<<<<<<<< back to p1 | |
-- 62 | |
[ | |
TODO | |
- (ignore for now) | |
>[-] p2 0 | |
>[-] p3 0 | |
>[-] p4 0 | |
>[-] p5 0 | |
>[-] p6 0 | |
>[-] p7 0 | |
>[-] p8 0 | |
>[-] p9 0 | |
>[-] p10 0 | |
>[-] p11 0 | |
] | |
>>>>> >>> goto p9 | |
[ | |
executed if POINTER MOVE RIGHT | |
code_pointer value saved in p12 p13 p14 | |
>>> | |
+>+>+ add value in p12 p13 p14 | |
<< | |
<<< back to p9 | |
TODO: code_pointer uses 2 bytes | |
] | |
<<<<< <<< p1 | |
] | |
>>>>> >> goto p8 | |
[ | |
executed if POINTER MOVE LEFT | |
>>>> | |
->->- reduce value in p12 p13 p14 where code_pointer saved | |
<< | |
<<<< back to p8 | |
- | |
] | |
<<<<< << p1 | |
] | |
>>>>> > goto p7 | |
[ | |
executed if OUTPUT | |
jump to code_memory(code_pointer) | |
>>>>> p12 | |
use f0 to store how much we should repeat the loop | |
f1 to store the copy of code_pointer | |
[[->>>>> +>+< <<<<< ] >>>>>- reduce next block f0 ] repeat until f0 is 0 | |
<< to code_mem | |
. execution; | |
>>> to f1 | |
[[-<<<<< + >>>>> ] <<<<<- ] repeat until f1 is 0 | |
now we're in p13 | |
revert p12 and p14 | |
> to p14 | |
<[-] empty p13 | |
<[-] empty p12 | |
>> to p14 | |
[-<<+>+>] p12 and p13 reverted to p14 | |
< to p13 | |
[->+<] revert p14 (p13 is not used) now in p13 | |
<<<<< < back to p7 | |
- | |
] | |
<<<<< < p1 | |
] | |
>>>>> goto p6 | |
[ | |
executed if DECREMENT | |
jump to code_memory(code_pointer) | |
>>>>> > p12 | |
use f0 to store how much we should repeat the loop | |
f1 to store the copy of code_pointer | |
[[->>>>> +>+< <<<<< ] >>>>>- reduce next block f0 ] repeat until f0 is 0 | |
<< to code_mem | |
- execution | |
>>> to f1 | |
[[-<<<<< + >>>>> ] <<<<<- ] repeat until f1 is 0 | |
now we're in p13 | |
revert p12 and p14 | |
> to p14 | |
<[-] empty p13 | |
<[-] empty p12 | |
>> to p14 | |
[-<<+>+>] p12 and p13 reverted to p14 | |
< to p13 | |
[->+<] revert p14 (p13 is not used) now in p13 | |
<<<<< << back to p6 | |
- | |
] | |
<<<<< p1 | |
] | |
>>>> goto p5 | |
[ | |
executed if INPUT | |
jump to code_memory(code_pointer) | |
>>>>> >> p12 | |
use f0 to store how much we should repeat the loop | |
f1 to store the copy of code_pointer | |
[[->>>>> +>+< <<<<< ] >>>>>- reduce next block f0 ] repeat until f0 is 0 | |
<< to code_mem | |
, execution | |
>>> to f1 | |
[[-<<<<< + >>>>> ] <<<<<- ] repeat until f1 is 0 | |
now we're in p13 | |
revert p12 and p14 | |
> to p14 | |
<[-] empty p13 | |
<[-] empty p12 | |
>> to p14 | |
[-<<+>+>] p12 and p13 reverted to p14 | |
< to p13 | |
[->+<] revert p14 (p13 is not used) now in p13 | |
<<<<< <<< back to p5 | |
- | |
] | |
<<<< p1 | |
] | |
>>> goto p4 | |
[ | |
executed if INCREMENT | |
jump to code_memory(code_pointer) | |
>>>>> >>> p12 | |
use f0 to store how much we should repeat the loop | |
f1 to store the copy of code_pointer | |
[[->>>>> +>+< <<<<< ] >>>>>- reduce next block f0 ] repeat until f0 is 0 | |
; | |
<< to code_mem | |
+ execution | |
; | |
>>> to f1 | |
[[-<<<<< + >>>>> ] <<<<<- ] repeat until f1 is 0 | |
now we're in p13 | |
revert p12 and p14 | |
> to p14 | |
<[-] empty p13 | |
<[-] empty p12 | |
>> to p14 | |
[-<<+>+>] p12 and p13 reverted to p14 | |
< to p13 | |
[->+<] revert p14 (p13 is not used) now in p13 | |
<<<<< <<<< back to p4 | |
- | |
] | |
<<< p1 | |
] | |
>> goto p3 | |
[ | |
executed if EOF | |
<<<[-] make p0=0 to end code reading | |
>>> | |
- | |
] | |
<< p1 | |
] | |
> goto p2 | |
[executed if EOF | |
<<[-] make p0=0 to end code reading | |
>> | |
- | |
] | |
< p1 | |
< recheck p0 | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
corrector