BF2C.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * BF2C
  3. * Copyright (C) 2003 Thomas Cort
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /*
  20. * Program Name: BF2C
  21. * Version: 1.0
  22. * Date: 2003-03-18
  23. * Description: Converts Brainfuck source to C source
  24. * License: GPL
  25. * Web page: http://www.brainfuck.ca
  26. * Download: http://www.brainfuck.ca/BF2C.c
  27. * Source Info: http://www.brainfuck.ca/downloads.html
  28. * Latest Ver: http://www.brainfuck.ca/downloads.html
  29. * Documentation: None
  30. * Help: tom@brainfuck.ca
  31. * Developement: tom@brainfuck.ca
  32. * Bugs: tom@brainfuck.ca
  33. * Maintainer: Thomas Cort <tom@brainfuck.ca>
  34. * Developer: Thomas Cort <tom@brainfuck.ca>
  35. * Interfaces: Command Line
  36. * Source Lang: C
  37. * Build Prereq: None
  38. * Related Progs: BF2Java
  39. * Category: Software Development > Programming language conversion
  40. */
  41. #include<stdio.h>
  42. int main(int argc, char **argv) {
  43. int args, /* index of current argument */
  44. pc, /* program counter */
  45. prog_len; /* length of program */
  46. int p[32768]; /* storage space for the prog */
  47. FILE *stream, *fopen();
  48. /* For every arguement do some interpreting */
  49. /* Each arguement should be a filename */
  50. for (args = 1; args < argc; args++) {
  51. /* Open da file */
  52. stream = fopen(argv[args], "r");
  53. prog_len = 0;
  54. /* read the file and store it in p[] */
  55. for (pc = 0; pc < 32768 && (p[pc] = getc(stream)) != EOF; pc++)
  56. prog_len++;
  57. /* reset the program counter */
  58. pc = 0;
  59. fclose(stream);
  60. /* print the beginning of the program */
  61. printf("#include<stdio.h>\n\nint main() {\nint x[32768];\nint xc;\nfor(xc = 0; xc < 32768; xc++) x[xc] = 0;\nxc=0;\n");
  62. /* visit every element that has part of the bf program in it */
  63. for(pc = 0; pc < prog_len; pc++) {
  64. /* '+' */
  65. if (p[pc] == 43) printf("x[xc]++;\n");
  66. /* '-' */
  67. else if (p[pc] == 45) printf("x[xc]--;\n");
  68. /* '.' */
  69. else if (p[pc] == 46) printf("putchar(x[xc]);\n");
  70. /* ',' */
  71. else if (p[pc] == 44) printf("x[xc] = getchar();\n");
  72. /* '>' */
  73. else if (p[pc] == 62) printf("xc++;\n");
  74. /* '<' */
  75. else if (p[pc] == 60) printf("xc--;\n");
  76. /* '[' */
  77. else if (p[pc] == 91) printf("while(x[xc] != 0) {\n");
  78. /* ']' */
  79. else if (p[pc] == 93) printf("}\n");
  80. }
  81. printf("printf(\"\\n\");\n}\n");
  82. }
  83. return 0;
  84. }