Skip to content

Instantly share code, notes, and snippets.

@Krewn
Last active August 29, 2015 14:07
Show Gist options
  • Save Krewn/afd569d12beec4a10a02 to your computer and use it in GitHub Desktop.
Save Krewn/afd569d12beec4a10a02 to your computer and use it in GitHub Desktop.
(1)01000010 66 B
(2)01110010 114 r
(3)01100001 97 a
(4)01101001 105 i
(5)01101110 110 n
(6)01000110 70 F
(7)01110101 117 u
(8)01100011 99 c
(9)01101011 107 k
>++++++++++[
>+++++++[
>+>+>+>+>+>+>+>+>+
<<<<<<<<<-
]+++[>>+>+>+>+>>+>+>+
<<<<<<<<<-
]>>+>>>+>>++>>+
<<<<<<<<<<-
]
>+++[
>->+>- >+>> >->>-
<<<<<<<<<-]
>-.>+.>.>++.>.>.>.>-.>.
http://enDOTwikipediaDOTorg/wiki/Brainfuck
Prints("BrainFuck")
#.----. .----. .--. .-..-. .-..----..-. .-. .---. .-. .-.
#| {} }| {} } / {} \ | || `| || {_ | { } |/ ___}| |/ /
#| {} }| .-. \/ /\ \| || |\ || | | {_} |\ }| |\ \
#`----' `-' `-'`-' `-'`-'`-' `-'`-' `-----' `---' `-' `-'
# .--. .---. .-. .-..----. .-..-. .-. .---. .---. .----. .--. .----.
# / {} \ {_ _}| { } || {} }| || `| |/ __} {_ _}| {} } / {} \ | {} }
#/ /\ \ | | | {_} || .-. \| || |\ |\ {_ } | | | .-. \/ /\ \| .--'
#`-' `-' `-' `-----'`-' `-'`-'`-' `-' `---' `-' `-' `-'`-' `-'`-'
#Author : Kevin_Nelson
import sys
output=''
mydata={}
place = 0
iplace=0
proc = -1
Max=0
Min=0
File=''
mydata[0]=0
debugMode=False
try:
programName = sys.argv[1]
except(IndexError):
print('Please enter a program to run')
sys.exit()
try:
Input = sys.argv[2]
except(IndexError):
inputs=''
def ip():#Increment pointer # >
global place , mydata , Max
place = place + 1
if place > Max :
mydata[place] = 0
Max=place
def dp():#Decrement Pointer # <
global place , mydata , Min
place = place - 1
if place < Min :
mydata[place] = 0
Min=place
def po():#plus one # +
global place , mydata
mydata[place]=mydata[place]+1
def mo():#Minus one # -
global place , mydata
mydata[place]=mydata[place]-1
def read():#Print Ascci()e # .
global mydata , place , output
try:
output+=(str(unichr(mydata[place])))
except(UnicodeEncodeError):
print('Value (' +str(mydata[place])+ ') out of printable range')
def getInput():#Print Ascci()e # ,
global place,mydata,iplace,Input
mydata[place]=ord(Input[iplace])
iplace=iplace+1
def debug():#Print data (Binary.) # #
global place , mydata , output
for k in range(Min,Max):
output+=(str(k)+' :'+bin(mydata[k])+'\n')
def loop(Proc):#Repeat untill d[place]=<0 # []
global mydata
global File
global place
global proc
Proc2 = Proc
t=1
while(Proc2<len(File)):
Proc2=Proc2+1
if(Proc2>=len(File)):
print('looping error @ line : '+str(Proc))
sys.exit()
if(File[Proc2]==']'):t=t-1
if(File[Proc2]=='['):t=t+1
if(t==0):break
if(File[Proc2]==']'):
Proc3=Proc+1
while(mydata[place]!=0):
#print('loopBounds('+str(Proc3)+','+str(Proc2)+')')
k=Proc3
while(k<Proc2):
ks=k
#print('tempProc:'+str(k))
k=brainFuck(k)
k=k+1
return(Proc2)
else:
print('looping error @ line : '+str(Proc))
sys.exit()
def brainFuck(n):
global mydata,File,output
if(n>=len(File)):
print(output)
sys.exit()
else:
if(File[n]=='>'):
ip()
elif(File[n]=='<'):
dp()
elif(File[n]=='+'):
po()
elif(File[n]=='-'):
mo()
elif(File[n]=='['):
if(debugMode):print('pre'+str(n))
n=loop(n)
if(debugMode):print('post'+str(n))
elif(File[n]==','):
getInput()
elif(File[n]=='.'):
read()
elif(File[n]=='#'):
if(debugMode):debug()
return(n)
def run():
global programName,File,Input,place,Min,Max,mydata,proc
try:
with open (programName, "r") as BfFile:
File=BfFile.read().replace('\n', '')
except(IOError):
print (programName + ' is not a file.' )
sys.exit()
while(1==1):
if(debugMode):print('proc:'+str(proc))
proc=proc+1
proc=brainFuck(proc)
run()
@Krewn
Copy link
Author

Krewn commented Oct 2, 2014

python Brnfck.py File.bf input

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