PageRenderTime 36ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/versions/c/BFI.c

http://github.com/rickardlindberg/brainfuck
C | 116 lines | 47 code | 21 blank | 48 comment | 45 complexity | b20319ee4aa9a34b51b5e5843a6d877e MD5 | raw file
  1. /*
  2. * BFI
  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: BFI
  21. * Version: 1.1
  22. * Date: 2003-04-29
  23. * Description: Interpreter for the Brainfuck Programming Language
  24. * License: GPL
  25. * Web page: http://www.brainfuck.ca
  26. * Download: http://www.brainfuck.ca/BFI.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: BFIwDebug
  39. * Category: Software Development > Programming languages
  40. */
  41. #include<stdio.h>
  42. int main(int argc, char **argv) {
  43. int pc, args, xc, prog_len, l = 0;
  44. int x[32768];
  45. int p[32768];
  46. FILE *stream, *fopen();
  47. for (args = 1; args < argc; args++) {
  48. stream = fopen(argv[args], "r");
  49. prog_len = 0;
  50. for (pc = 0; pc < 32768 && (p[pc] = getc(stream)) != EOF; pc++)
  51. prog_len++;
  52. pc = 0;
  53. fclose(stream);
  54. for(xc = 0; xc < 32768; xc++)
  55. x[xc] = 0;
  56. xc = 0;
  57. for(pc = 0; pc < prog_len; pc++) {
  58. // '+'
  59. if (p[pc] == 43) x[xc]++;
  60. // '-'
  61. else if (p[pc] == 45) x[xc]--;
  62. // '.'
  63. else if (p[pc] == 46) putchar(x[xc]);
  64. // ','
  65. else if (p[pc] == 44) x[xc] = getchar();
  66. // '>'
  67. else if (p[pc] == 62) xc++;
  68. // '<'
  69. else if (p[pc] == 60) xc--;
  70. // '['
  71. else if (p[pc] == 91) {
  72. if (x[xc] == 0) {
  73. pc++;
  74. while (l > 0 || p[pc] != 93) {
  75. if (p[pc] == 91) l++;
  76. if (p[pc] == 93) l--;
  77. pc++;
  78. }
  79. }
  80. }
  81. // ']'
  82. else if (p[pc] == 93) {
  83. pc--;
  84. while (l > 0 || p[pc] != 91) {
  85. if (p[pc] == 93) l++;
  86. if (p[pc] == 91) l--;
  87. pc--;
  88. }
  89. pc--;
  90. }
  91. }
  92. }
  93. putchar(10);
  94. return 0;
  95. }