先介绍下BrainF**k:
Brainf**k 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.)
BrainF**k 语言,是一种按照“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 p, r, q;
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--; break;
case '>;': p++; break;
case '+': a[p]++; break;
case '-': a[p]--; break;
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",
*a,a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],3*p+2,"^");
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的是.下面这个程序则允许用户输入一个数字然后程序将输出小于这个数字的所有质数。
>++++++++[<++++++++>-]<++++++++++++++++.[-]>++++++++++[<+++
+++++++>-]<+++++++++++++.[-]>++++++++++[<++++++++++>-]<+++++
.[-]>++++++++++[<++++++++++>-]<+++++++++.[-]>++++++++++[<++++
++++++>-]<+.[-]>++++++++++[<++++++++++>-]<+++++++++++++++.[-]>
+++++[<+++++>-]<+++++++.[-]>++++++++++[<++++++++++>-]<+++++++
++++++++++.[-]>++++++++++[<++++++++++>-]<++++++++++++.[-]>+++
++[<+++++>-]<+++++++.[-]>++++++++++[<++++++++++>-]<+++++++++
++++++.[-]>++++++++++[<++++++++++>-]<+++++++++++.[-]>+++++++[
<+++++++>-]<+++++++++.[-]>+++++[<+++++>-]<+++++++.[-]+[->,----------
[<+>-------------------------------------->[>+>+<<-]>>[<<+>>-]<>>>+++++++++[<
<<[>+>+<<-]>>[<<+>>-]<[<<+>>-]>>-]<<<[-]<<[>+<-]]<]>>[<<+>>-]<<>+<-
[>+[>+>+<<-]>>[<<+>>-]<>+<-->>>>>>>>+<<<<<<<<[>+<-<[>>>+>+<<<
<-]>>>>[<<<<+>>>>-]<<<>[>>+>+<<<-]>>>[<<<+>>>-]<<<<>>>[>+>+<<-]
>>[<<+>>-]<<<[>>>>>+<<<[>+>+<<-]>>[<<+>>-]<[>>[-]<<-]>>[<<<<[>+>+
<<-]>>[<<+>>-]<>>>-]<<<-<<-]+>>[<<[-]>>-]<<>[-]<[>>>>>>[-]<<<<<<-]<
<>>[-]>[-]<<<]>>>>>>>>[-<<<<<<<[-]<<[>>+>+<<<-]>>>[<<<+>>>-]<<<>>
[>+<-]>[[>+>+<<-]>>[<<+>>-]<>++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<
<++++++++++>>-]<<-<-]+++++++++>[<->-]<[>+<-]<[>+<-]<[>+<-]>>>[<<<
+>>>-]<>+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>>+<-
]<<-<-]>>>>[<<<<+>>>>-]<<<<>[-]<<+>]<[[>+<-]+++++++[<+++++++>-]<-
><.[-]>>[<<+>>-]<<-]>++++[<++++++++>-]<.[-]>>>>>>>]<<<<<<<<>[-]<[-
]<<-]++++++++++.[-]
不知看完之后你吐了没,如果没有,建议你吃过饭后再看一遍。-____-b