要靠谱 反脑残

要靠谱 反脑残
两年前就认识了BrainFuck语言 当时一笑而过
今天看到CU上的C版斑竹的 帖子,他作了个BrainFuck的自举编译器 。不知到各位看了他的bf代码后感觉如何,我顿时即被他强烈的脑残主意所感染,模仿他的c代码 写个了Python版本
# !/usr/bin/env python
import  sys

class  VM():
    
def   __init__ (self):
        self.stack
= [0  for  i  in  range( 256 )]
        self.stackLen
= 0;
        self.pool
= [ 0  for  i  in  range( 4096 )]
        self.pointer
= 0
        self.currentInstructIndex
= 0
        self.codeLen
= 0
        self.code
= ''
    
def  run(self,reader):
        self.code
= reader.read()
        
print   " Run: " ,self.code
        self.codeLen
= len(self.code)
        
while  self.currentInstructIndex < self.codeLen:
            self.bfInterpreter(self.code[self.currentInstructIndex])
            self.currentInstructIndex
+= 1
    
def  dump(self,size = 10 ):
        pool
= self.pool[:size]
        stack
= self.stack[:size]
        
print   ' ============================================================================ '
        
print   ' pool ' ,pool
        
print   ' stack ' ,stack
        
print   ' CurrentInstructIndex:%d,%s ' % (self.currentInstructIndex,self.code[self.currentInstructIndex])
    
def  bfInterpreter(self,instruct):
        
if  instruct == ' + ' :
                self.pool[self.pointer]
+= 1
        
elif  instruct == ' - ' :
                self.pool[self.pointer]
-= 1
        
elif  instruct == ' > ' :
                self.pointer
+= 1
        
elif  instruct == ' < ' :
                self.pointer
-= 1
        
elif  instruct == ' . ' :
                
try :
                    sys.stdout.write(chr(self.pool[self.pointer]))
                
except :
                    
print  r " can't  print: " ,self.pool[self.pointer]
        
elif  instruct == ' , ' :
                self.pool[self.pointer]
= ord(sys.stdin.read())
        
elif  instruct == ' [ ' :
                
if  self.pool[self.pointer] != 0:
                    
try :         
                        self.stack[self.stackLen]
= self.currentInstructIndex
                        self.stackLen
+= 1
                    
except :
                        
print  self.stackLen
                        
raise  IndexError
                
else :
                    j
= 0;k = 0
                    
for  k  in  range(self.currentInstructIndex,self.codeLen):
                        
if  self.code[k] == ' [ ' :
                            j
+= 1
                        
if  self.code[k] == ' ] ' :
                            j
-= 1
                        
if  j == 0: break
                    
if  j == 0:
                        self.currentInstructIndex
= k
                    
else :
                        
print   ' Error '
                        
return  None
                        
        
elif  instruct == ' ] ' :
                self.stackLen
-= 1
                self.currentInstructIndex
= self.stack[self.stackLen] - 1

if   __name__ == ' __main__ ' :
    
if  len(sys.argv) <= 1 :
        
import  StringIO
        sample
= ' ++++++++++[>+++++++>++++++++++>+++>+\
<<<<- ] >++ . >+ . +++++++ .. +++ . >++ . <<++++++++++++++ \
+ . > . +++ . ------ . -------- . >+ . > . '
        
        reader
= StringIO.StringIO(sample)
    
else :
        
print   ' Run file ' ,sys.argv[ 1 ]
        reader
= open(sys.argv[ 1 ])
    vm
= VM()
    vm.run(reader)

输入如下代码 ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
可以得到Hello World!

当然也可以试一下CU上的那段
# Here is an example BF program
#
# +[>>+++++[<++++++>-]<[>++++[>++++++++<-]<[->+>.<<]>[-<+>]+++++
# [>+++++<-]>+++.-.<+++[>------<-]>.---------.<+++++[>++++++<-]>
# .--.[-]++++++++++.[-]<<-]>+++++[<++++++>-]>>[-]<<<[>++++[>++++
# ++++<-]>>[<.>>+<-]>[-<+>]<+<<+++++[->++++++<]>.--.<++++[>-----
# --<-]>.++++++++.<+++[>++++++<-]>.++++.[-]++++++++++.[-]<<-]<]

下一个目标 写Python版的编译器

以下抄自cu

BrainFuck语言一共只有8个符号
+-><.,[]
brainfuck的程序全部都由这8个符号构成
初始化的时候,有一个连续的两个方向都无限长的内存,内存的每个字节值是0。
有一个指针p,指向其中一个字节。
每个符号的意义如下:
+  指针指向的这个字节自加
-   指针指向的这个字节自减
>  指针向右移一个字节
<  指针向左移一个字节
.   输出指针指向的这个字节
,   输入这个字节,存在指针指向的这个字节
[   如果指针指向的这个字节,则进入循环节
]   回到匹配的[(比如[--.[++>.[+]---].-],^指着的两个是匹配的])
                               ^              ^

你可能感兴趣的:(要靠谱 反脑残)