Skip to content

Instantly share code, notes, and snippets.

@imakin
Last active December 7, 2016 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imakin/f303d572eef4078c083837802a313d30 to your computer and use it in GitHub Desktop.
Save imakin/f303d572eef4078c083837802a313d30 to your computer and use it in GitHub Desktop.
testing if possible to make brainfuck interpreter written in brainfuck itself
[
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
]
@imakin
Copy link
Author

imakin commented Dec 6, 2016

corrector

"""makin"""

class Main(object):
	"""output and input is read and write as ascii byte
	negative pointer means relative to the end of array
	"""
	ptr = 0
	mem = [0]
	def __init__(self):
		pass
	
	def interpret(self,code):
		self.mem = [0]
		self.ptr = 0
		i = 0
		while i < len(code):
			if code[i]==">":
				self.ptr += 1
				if len(self.mem)<(self.ptr+1):
					self.mem.append(0)
					
			elif code[i]=="<":
				self.ptr -= 1
				if self.ptr<0:
					print("ptr:",self.ptr)
					print("mem:",self.mem)
					print("interpreting:",code[i-100:i])
					print(code[i])
					print(code[i:i+100])
					raise Exception("kecu!")
					self.ptr = len(self.mem)+self.ptr
					
			elif code[i]=="+":
				self.mem[self.ptr] += 1
				
			elif code[i]=="-":
				
				self.mem[self.ptr] -= 1
				
			elif code[i]=="[" and self.mem[self.ptr]==0:
				skip = 0
				while True:
					#~ print(i)
					i += 1
					if code[i]=="]":
						if skip<=0:
							break
						else:
							skip -= 1
					elif code[i]=="[":
						skip += 1
				i += 1
				#i incremented in the end of this loop scope
				
			elif code[i]=="]" and self.mem[self.ptr]!=0:
				skip = 0
				while True:
					i -= 1
					if code[i]=="[":
						if skip<=0:
							break
						else:
							skip -= 1
					elif code[i]=="]":
						skip +=1
				#i incremented in the end of this loop scope
				
			elif code[i]==".":
				print(chr(self.mem[self.ptr]), end="")
				
			elif code[i]==",":
				v = input()[0]
				self.mem[self.ptr] = (ord(v)%256)
				
			else:
				#comment
				pass
			i += 1

code = ""
app.interpret(code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment