Bt reer program language

Source: Internet
Author: User

From: http://coolshell.cn/articles/1142.html

There will never be a lack of alternative things in the world of human nature, and the world of computer. In terms of programming language, friends who have read this site "6 abnormal C language Hello World Program" must be familiar with Bt and alternative programs, but they are all pediatrics, the true Bt and alternative are completed at the language level. Let's take a look at the alternative language brainfuck. If you see the name of this language, please do not think it is a funny language. This is a "serious thing". Please read this article with "the most devout attitude.

BF language Introduction

BrainfuckIs a minimal computer language, which was created by urban Müller in 1993. This language is sometimes calledBrainf ** KOrBrainf ***Or even referred toBF. This language is a language designed according to the idea of "Turing complete (complete Turing Machine)". Its main design idea is to implement a "simple" language with the smallest concept, the brainf ** K language has only eight symbols, and all operations are completed by the combination of these eight symbols.

BF is based on a simple machine model. Besides eight commands, this machine also includes: an array in bytes, initialized to zero, a pointer to the array (the first byte pointing to the array at the beginning), and two byte streams used for input and output.

The following is a description of these eight commands, each of which is identified by one character:

 

Character Description
> Pointer plus one
< Pointer minus one
+ The value of the byte pointed to by the pointer plus one
- The value of the byte pointed to by the pointer minus one.
. The unit content pointed to by the output pointer (ASCII code)
, Input content to the Unit pointed to by the pointer (ASCII code)
[ If the unit value pointed to by the pointer is zero, jump backward to the corresponding]Command
] If the unit value pointed to by the pointer is not zero, jump forward to the corresponding[Command

(According to the simple statement that saves more time,]It can also be said that "Jump backward to the corresponding[Status ". These two interpretations are the same .)

(Third, the same price statement,[It means "jump forward to the corresponding]",]It means "Jump backward to the corresponding[Command, if the pointer points to a non-zero byte. ")

The brainfuck program can be translated into C language using the following replacement method (assumingptrYeschar*Type ):

Brainfuck C
> ++ptr;
< --ptr;
+ ++*ptr;
- --*ptr;
. putchar(*ptr);
, *ptr =getchar();
[ while (*ptr) {
] }
BF Interpreter

Because brainfuck has only eight commands, and does not have keywords or custom identifiers, it is very simple to implement the compiler. Anyone who is new to C can compile it by themselves, although everyone here can compile one by themselves, I will give you an official version to attract your interest. This program has only 50 lines and is fully written by ansi c. Therefore, you can find a C-language compiler and compile it.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 #include <stdio.h>; intp, 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);    }}

Of course, if you think that using the C language to implement the brainfuck language interpreter is an insult to the brainfuck language, our brainfuck community will never tolerate this idea. Because we have a compiler written in 100% pure brainfuck.Awib: Http://code.google.com/p/awib/

Hello World
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

Why? Can't you understand it? The following is an explanation:

+++ +++ +++ +           initialize counter (cell #0) to 10[                       use loop to set the next four cells to 70/100/30/10    > +++ +++ +             add  7 to cell #1    > +++ +++ +++ +         add 10 to cell #2    > +++                   add  3 to cell #3    > +                     add  1 to cell #4    <<< < -                 decrement counter (cell #0)]>++ .                   print 'H'>+.                     print 'e'+++ +++ +.              print 'l'.                       print 'l'+++ .                   print 'o'>++ .                   print ' '<<+ +++ +++ +++ +++ ++. print 'W'>.                      print 'o'+++ .                   print 'r'--- --- .               print 'l'--- --- --.             print 'd'>+.                     print '!'>.                      print '/n'

Related Links:

  • BF Official Website: http://www.muppetlabs.com /~ Breadbox/BF /.
  • BF Wikipedia: http://en.wikipedia.org/wiki/brainfuck.
Other alternative languages

If you want to think that BF is already very BT, you will be wrong. The following programming languages are more BT.

Whitespace Language

This is a programming language that uses only blank characters (spaces, tabs, and carriage returns), while all other visible characters are annotated. The following is an example:

 

What? You didn't see anything. That's right, because it is unique in the language. Visit the following link to view the Hello, world example. Remember to press Ctrl + A to view the program.

Official Website: http://compsoc.dur.ac.uk/whitespace/index.php.

Lolcode Language

Lolcode is a programming language built on a highly abbreviated network English language. Generally, if a person can understand this network English, he can understand the lolcode program source code without training. The following is the Hello, world routine:

HAICAN HAS STDIO?VISIBLE "HAI WORLD!"KTHXBYE

Official Website: http://lolcode.com/

Chinese Programming Language

Do not think that only foreigners are so Bt. China also has its own BT programming language.

Chinese basic

Chinese commands   Applesoft Basic
10 bu = 0   10 y = 0
20 water, fire   20 input E, F
30 from day = water to fire   30 For A = e to F
40 bu = Bu + logarithm (day)   40 y = Y + Log ()
50 next day   50 next
60 Bu   60 print y

Official website can not access, can only look at Wikipedia: http://en.wikipedia.org/wiki/Chinese_BASIC

Chinese Python language)

Is the following program cool?

#! /Usr/local/bin/cpython answer = read ('Do you think Chinese programming languages are valuable? (Yes/No) ') if the answer = 'you': Write' OK, let's work together! 'Otherwise, the answer =' 00': write' well, Chinese is not as a programming language value. 'otherwise: write' please think carefully before replying .'

Official Website: http://www.chinesepython.org/

It's almost the end. I 'd like to explain again that this is a very serious article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.