⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 brainfck.c

📁 brainf×ck的一种解释器。Brainf*ck
💻 C
字号:
#include <stdio.h> 
#ifndef cellsize 
#define cellsize 30000 
#endif 
const char *s_name = 
"    bfi" 
#if cellsize != 30000 
"%d" 
#endif 
" - brainf*ck language interpreter 1.0.1 build - "__DATE__"\n" 
#if cellsize != 30000 
"        **** Special version with %d cells in size ****\n" 
#endif 
"          
const char *s_license = 
" This program is free software; you can redistribute it and/or modify\n" 
" it under the terms of the GNU General Public License as published by\n" 
" the Free Software Foundation; either version 2 of the License, or\n" 
" (at your option) any later version.\n" 
"\n" 
" This program is distributed in the hope that it will be useful,\n" 
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n" 
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n" 
" GNU General Public License for more details.\n" 
"\n" 
" You should have received a copy of the GNU General Public License\n" 
" along with this program; if not, write to the\n" 
" Free Software Foundation, Inc.,\n" 
" 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n" 
"\n" 
" usage: $ %s [source code]\n" 
"\n"; 

const char *s_err_open = 
"[] error: unable to open file %s\n"; 
const char *s_err_ovfl = 
"[] error: unable to run, program too large.\n"; 
const char *s_err_unmt = 
"[] error: unmatch [ and ] in source file.\n"; 

char a[cellsize], o[cellsize]; 
int n=0,ip=0,dp=0; 

int main(int argc, char **argv) 
{ 
    FILE *fp = NULL; 
    if (1 == argc) { 
#if cellsize != 30000 
        fprintf(stderr, s_name, cellsize/1000, cellsize); 
#else 
        fprintf(stderr, s_name); 
#endif 
        fprintf(stderr, s_license, argv[0]); 
        return 0;
	} 
    if (NULL == (fp=fopen(argv[1],"r"))) { 
        fprintf(stderr, s_err_open, argv[1]); 
        return -1; 
    } 
    while(!feof(fp)) 
        switch(n=fgetc(fp)) { 
        case '[':ip+=2;case ']':--ip;case '>': 
        case '<':case '+':case '-':case '.':case ',': 
            o[dp++]=n; 
            if (dp==cellsize) { 
                fclose(fp); 
                fprintf(stderr, s_err_ovfl); 
                return -2; 
            } 
        } 
    fclose(fp); 
    if (ip) { 
        fprintf(stderr, s_err_unmt); 
        return -3; 
    } 
    n=ip=dp=0; 
    do { 
        n=1; 
        switch (o[ip]) { 
        case '>':++dp==cellsize?dp=0:0;break; 
        case '<':--dp==-1?dp+=cellsize:0;break; 
        case '+':a[dp]++;break; 
        case '-':a[dp]--;break; 
        case '.':putchar(a[dp]);fflush(stdout);break; 
        case ',':a[dp]=getchar();break; 
        case '[': 
            if(!a[dp]) 
                while(n) 
                    switch(o[++ip]){ 
                    case '[':n++;break; 
                    case ']':n--;break; 
                    } 
            break; 
        case ']': 
            if(a[dp]) 
                while(n) 
                    switch(o[--ip]){ 
                    case ']':n++;break; 
                    case '[':n--;break; 
                    } 
            break; 
        } 
    } while(o[++ip]); 
    return 0; 
}  
 
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -