mtsuyugu #3955(2007/11/13 13:41 GMT) [ C ] Rating0/0=0.00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <stdio.h> #include <stdlib.h> char *code[] = { "sp++;", "sp--;", "(*sp)++;", "(*sp)--;", "putchar(*sp);", "*sp = getchar();", "while(*sp){", "}" }; int main( int argc, char *argv[] ){ int c,i,j,n=0; int indent = 1; int length; FILE *fp; if( argc < 2 ){ fprintf(stderr,"Usage: %s sourcefile\n", argv[0] ); return EXIT_FAILURE; } if( (fp = fopen( argv[1], "r" )) == NULL ){ fprintf(stderr,"Error: %s cannot opened\n", argv[1] ); return EXIT_FAILURE; } if( !argv[2] || (length = atoi( argv[2] )) <= 0 ){ length = 256; } puts("#include <stdio.h>"); puts("#include <stdlib.h>"); printf("#define DATA_LEN %d\n", length); puts("char code[DATA_LEN];"); puts("int main (void){"); puts(" char *sp = code;"); while( (c=fgetc( fp )) != EOF ){ switch(c){ case '>': i = 0; break; case '<': i = 1; break; case '+': i = 2; break; case '-': i = 3; break; case '.': i = 4; break; case ',': i = 5; break; case '[': i = 6; indent++; break; case ']': i = 7; indent--; break; default: /* skip other characters */ continue; } for( j = 0; j < (indent+(i==6?-1:0)); j++ ){ printf(" "); } printf("%s\n", code[i] ); } puts(" return EXIT_SUCCESS;"); puts("}"); fclose(fp); return EXIT_SUCCESS; }
Rating0/0=0.00-0+
1 reply [ reply ]
mtsuyugu
#3955()
[
C
]
Rating0/0=0.00
./bf2c hello.bf > hello.c
または
./bf2c hello.bf 128 > hello.c
のように使います。
Rating0/0=0.00-0+
1 reply [ reply ]