BrainFuck(图灵思想)
来源:互联网 发布:刻录光盘可启动数据 编辑:程序博客网 时间:2024/04/28 14:26
先介绍下BrainFuck:
Brainfuck is the ungodly creation of Urban Müller, whose goal was apparently to create a Turing-complete language for which he could write the smallest compiler ever, for the Amiga OS 2.0. His compiler was 240 bytes in size. (Though he improved upon this later -- he informed me at one point that he had managed to bring it under 200 bytes.)
BrainFuck 语言,是一种按照“Turing complete”思想设计的语言,它的主要设计思路是:用最小的概念实现一种“简单”的语言,BrainFuck 语言只有八种符号,所有的操作都由这八种符号的组合来完成。
>Increment the pointer.<Decrement the pointer.+Increment the byte at the pointer.-Decrement the byte at the pointer..Output the byte at the pointer.,Input a byte and store it in the byte at the pointer.[Jump forward past the matching ] if the byte at the pointer is zero.]Jump backward to the matching [ unless the byte at the pointer is zero.>
<
+
- 指针指向的字节的值减一
. 输出指针指向的单元内容(ASCII码)
, 输入内容到指针指向的单元(ASCII码)
[ 如果指针指向的单元值为零,向前跳转到对应的]指令的次一指令处
] 如果指针指向的单元值不为零,向后跳转到对应的[指令的次一指令处
因为 BrainFuck 只有八种指令,并且没有关键字,也不允许自定义标识符,
因此它的编译器实现起来非常简单,初学 C 语言不久的人都可以自己编出来,尽管在座的各位每人都可以自己编一个,不过为了引起大家的兴趣,我这里还是给出大家一个官方发布的版本:
CODE:
#include <stdio.h>;
int
char a[5000], f[5000], b, o, *s=f;
void interpret(char *c)
{
char *d;
r++;
while( *c ) {
//if(strchr("<>;+-,.[]\n",*c))printf("%c",*c);
switch(o=1,*c++) {
case '<': p--;
case '>;': p++;
case '+': a[p]++;
case '-': a[p]--;
case '.': putchar(a[p]); fflush(stdout); break;
case ',': a[p]=getchar();fflush(stdout); break;
case '[':
for( b=1,d=c; b && *c; c++ )
b+=*c=='[', b-=*c==']';
if(!b) {
c[-1]=0;
while( a[p] )
interpret(d);
c[-1]=']';
break;
}
case ']':
puts("UNBALANCED BRACKETS"), exit(0);
case '#':
if(q>;2)
printf("%2d %2d %2d %2d %2d %2d %2d %2d %2d %2d\n%*s\n",
break;
default: o=0;
}
if( p<0 || p>;100)
puts("RANGE ERROR"), exit(0);
}
r--;
// chkabort();
}
main(int argc,char *argv[])
{
FILE *z;
q=argc;
if(z=fopen(argv[1],"r")) {
while( (b=getc(z))>;0 )
*s++=b;
*s=0;
interpret(f);
}
}
再贴点BF程序:
“Hello World”程序:
>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]
<.>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]>++++++++[
<++++>-]<+.[-]++++++++++.
下面这个程序则允许用户输入一个数字,
然后程序将输出小于这个数字的所有质数。
>++++++++[<++++++++>-]<++++++++++++++++.[-]>++++++++++[<++++++++++>-]<
++++++++++++++.[-]>++++++++++[<++++++++++>-]<+++++.[-]>++++++++++[<+++
+++++++>-]<+++++++++.[-]>++++++++++[<++++++++++>-]<+.[-]>++++++++++[<+
+++++++++>-]<+++++++++++++++.[-]>+++++[<+++++>-]<+++++++.[-]>+++++++++
+[<++++++++++>-]<+++++++++++++++++.[-]>++++++++++[<++++++++++>-]<+++++
+++++++.[-]>+++++[<+++++>-]<+++++++.[-]>++++++++++[<++++++++++>-]<++++
++++++++++++.[-]>++++++++++[<++++++++++>-]<+++++++++++.[-]>+++++++[<++
+++++>-]<+++++++++.[-]>+++++[<+++++>-]<+++++++.[-]+[->,----------[<+>-
------------------------------------->[>+>+<<-]>>[<<+>>-]<>>>+++++++++
[<<<[>+>+<<-]>>[<<+>>-]<[<<+>>-]>>-]<<<[-]<<[>+<-]]<]>>[<<+>>-]<<>+<-[
>+[>+>+<<-]>>[<<+>>-]<>+<-->>>>>>>>+<<<<<<<<[>+<-<[>>>+>+<<<<-]>>>>[<<
<<+>>>>-]<<<>[>>+>+<<<-]>>>[<<<+>>>-]<<<<>>>[>+>+<<-]>>[<<+>>-]<<<[>>>
>>+<<<[>+>+<<-]>>[<<+>>-]<[>>[-]<<-]>>[<<<<[>+>+<<-]>>[<<+>>-]<>>>-]<<
<-<<-]+>>[<<[-]>>-]<<>[-]<[>>>>>>[-]<<<<<<-]<<>>[-]>[-]<<<]>>>>>>>>[-<
<<<<<<[-]<<[>>+>+<<<-]>>>[<<<+>>>-]<<<>>[>+<-]>[[>+>+<<-]>>[<<+>>-]<>+
++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>-]<<-<-]+++++++++>[<
->-]<[>+<-]<[>+<-]<[>+<-]>>>[<<<+>>>-]<>+++++++++<[>>>+<<[>+>[-]<<-]>[
<+>-]>[<<++++++++++>>>+<-]<<-<-]>>>>[<<<<+>>>>-]<<<<>[-]<<+>]<[[>+<-]+
++++++[<+++++++>-]<-><.[-]>>[<<+>>-]<<-]>++++[<++++++++>-]<.[-]>>>>>>>
]<<<<<<<<>[-]<[-]<<-]++++++++++.[-]
===================头昏脑胀的分割线 ^-^===========================
怎样,这个语言很牛X吧?但更牛X的是.还有人用BF语言为BF语言写了个解释器.............
>>>,[->+>+<<]>>[-<<+>>]>++++[<++++++++>-]<+<[->>+>>+<<<<]>>>>[-<<<<+>>>>]<<<[-
>>+>+<<<]>>>[-<<<+>>>]<<[>[->+<]<[-]]>[-]>[[-]<<<<->-<[->>+>>+<<<<]>>>>[-<<<<+
>>>>]<<<[->>+>+<<<]>>>[-<<<+>>>]<<[>[->+<]<[-]]>[-]>]<<<<[->>+<<]>[->+<]>[[-]<
<<[->+>+<<]>>[-<<+>>]>++++++[<+++++++>-]<+<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<[->
+>>+<<<]>>>[-<<<+>>>]<[<[->>+<<]>[-]]<[-]>>[[-]<<<<->-<[->>>+>+<<<<]>>>>[-<<<<
+>>>>]<<<[->+>>+<<<]>>>[-<<<+>>>]<[<[->>+<<]>[-]]<[-]>>]<<<<[->>>+<<<]>[->>+<<
]>+>[<->[-]]<[<<<<+>>>>[-]]<<<[->+>+<<]>>[-<<+>>]>+++++[<+++++++++>-]<<[->>>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<<[[-<<<<<+>>>>>]>[-<<<<<+>>>>>]<<<<<<-]<<<<<<<<[-]>>>>>>>>>[
-<<<<<<<<<+>>>>>>>>>]<<<<<<<<<<->+[->>>>>>>>>+<<<<<<+<<<]>>>[-<<<+>>>]>>>>>>[<
[->>>>>+<<<<<]>[->>>>>+<<<<<]>>>> ...
转自: http://kingittiger.diandian.com/post/2012-10-19/40039755054
- BrainFuck(图灵思想)
- Brainfuck
- [Brainfuck]基于Whitespace语言保密思想的BF语句构建-(无字天书)
- Brainfuck VM
- brainfuck language
- brainfuck 一览
- brainfuck语言
- BrainFuck语言
- Brainfuck--PyFuck
- BrainFuck解释器 (BrainFuck Interpreter) JavaScript实现
- Prolog Brainfuck 解析器
- UVALive 3497 brainfuck [模拟]
- Brainfuck Compiler on PHP
- BrainFuck解释器
- Y分钟学Brainfuck
- Brainfuck Fuck your brain
- pwnable 之brainfuck
- pwnable之brainfuck
- C语言内存管理(初级)----链表
- redis代码分析
- 人体运动捕获
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- 关于CLASS , SEL, IMP的说明
- BrainFuck(图灵思想)
- KMP source code
- 如何获取用户附近的商户或者其他好友(LBS)
- 代码行数统计工具
- Android学习笔记 notifiy更新 来电拦截 并删除
- Dependency Injection
- JNI编程小技巧
- IOS的网络开发架构说明
- 解决6410 WINCE6 应用层调用SetSystemPowerState api关机无效的问题