免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: cjaizss

娱乐一下,介绍一种语言——brainfuck [复制链接]

论坛徽章:
0
发表于 2008-06-04 15:57 |显示全部楼层
该语言名副其实: 操 大脑

论坛徽章:
0
发表于 2008-06-04 16:58 |显示全部楼层
这东西就是用来玩的,呵呵
程序员自己的游戏

论坛徽章:
0
发表于 2008-06-05 00:50 |显示全部楼层
  友情支持一下。偶给你加个精表示表示

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2008-06-05 01:34 |显示全部楼层
原帖由 mik 于 2008-6-5 00:50 发表
  友情支持一下。偶给你加个精表示表示

呵呵,终于等到你来了,整一个月没上线了,.......

论坛徽章:
0
发表于 2008-06-05 01:42 |显示全部楼层
原帖由 cjaizss 于 2008-6-5 01:34 发表

呵呵,终于等到你来了,整一个月没上线了,.......


呵呵,brainfuck 你是自创的吗? 是不是应该给个原创才对

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2008-06-05 11:44 |显示全部楼层
brainfuck语言不是自创的,第一个用sed写的编译器不是我写的,后面都是我写的

论坛徽章:
0
发表于 2008-06-05 12:31 |显示全部楼层

回复 #21 zhongfangqing 的帖子

要用书面语:强奸大脑。文明多了,

论坛徽章:
0
发表于 2008-06-05 12:49 |显示全部楼层
我发个Python的版本 基本上是翻译了楼主的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)


[ 本帖最后由 zarra 于 2008-6-5 12:55 编辑 ]

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2008-06-05 13:00 |显示全部楼层
其实又想到了一个重要的优化,只是现在没时间,有时间把它写出来。

论坛徽章:
0
发表于 2008-06-05 15:23 |显示全部楼层
佩服。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP