bf interpreter


SUBMITTED BY: smiley

DATE: Nov. 26, 2015, 2:42 a.m.

FORMAT: C++

SIZE: 3.6 kB

HITS: 916

  1. /*****************************/
  2. /* bf Interpreter */
  3. /* Made by: smiley */
  4. /*****************************/
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #define DEBUG 0 //set to 1 to print "Hello World!" as a test
  10. typedef unsigned char byte;
  11. /*Operators*/
  12. #define INCREMENT_PTR '>'
  13. #define DECREMENT_PTR '<'
  14. #define INCREMENT_VAL '+'
  15. #define DECREMENT_VAL '-'
  16. #define OUTPUT_VAL '.'
  17. #define INPUT_VAL ','
  18. #define START_LOOP '['
  19. #define END_LOOP ']'
  20. /*Globals variables related to the interpreter*/
  21. #define MAX_NUMBER_CELLS 30000 //Each cells are a single byte
  22. #define MAX_CODE_SIZE 100000 //Max size of the source code, that should be enough
  23. byte Cells[MAX_NUMBER_CELLS]; //We will have to null them
  24. int main(int argc, char** argv){
  25. char src[MAX_CODE_SIZE];
  26. int curCell = 0; //Current Cell
  27. int flen = 0; //File Length
  28. /*ZeroMemory*/
  29. memset(src, 0, MAX_CODE_SIZE);
  30. memset(Cells, 0, MAX_NUMBER_CELLS);
  31. if (!DEBUG){ //This variable is set manually at the beginning of the code
  32. if (argc < 2){
  33. printf("Usage:Brainfuck.exe \"Input file\"\nExample: Brainfuck.exe \"C:\\test.txt\"\n\n");
  34. getchar();
  35. return -1;
  36. }
  37. /*Get Source code*/
  38. FILE* in = fopen(argv[1], "r");
  39. if (in != NULL){
  40. fseek(in, 0, SEEK_END);
  41. flen = ftell(in);
  42. if (flen > MAX_CODE_SIZE){
  43. fclose(in);
  44. printf("File is too big! Max length: %i\n", MAX_CODE_SIZE);
  45. getchar();
  46. return -1;
  47. }
  48. fseek(in, 0, SEEK_SET);
  49. fread(src, flen, 1, in);
  50. fclose(in);
  51. }
  52. else{
  53. printf("File cannot be read! Try to move it to your desktop?\n");
  54. getchar();
  55. return -1;
  56. }
  57. }
  58. else{
  59. /*This debug string will print "Hello World!" */
  60. strcpy(src, "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.");
  61. }
  62. int srcLen = strlen(src);
  63. /*Iterate through every char*/
  64. for (int i = 0; i < srcLen; i++){
  65. switch (src[i]){
  66. case INCREMENT_PTR:
  67. if (curCell == MAX_NUMBER_CELLS - 1) curCell = 0;
  68. else curCell++;
  69. break;
  70. case DECREMENT_PTR:
  71. if (curCell == 0) curCell = MAX_NUMBER_CELLS - 1;
  72. else curCell--;
  73. break;
  74. case INCREMENT_VAL:
  75. Cells[curCell]++;
  76. break;
  77. case DECREMENT_VAL:
  78. Cells[curCell]--;
  79. break;
  80. case OUTPUT_VAL:
  81. printf("%c", Cells[curCell]);
  82. break;
  83. case INPUT_VAL:
  84. printf("\nInput: ");
  85. Cells[curCell] = _getch();
  86. printf("\n");
  87. break;
  88. /*Gotta test*/
  89. case START_LOOP:
  90. /*incase we start a loop with 0 as a value*/
  91. if (Cells[curCell] == 0){
  92. int occurences = 0; //number of loops after it
  93. for (int a = i+1; a > srcLen; a++){
  94. if (src[a] == START_LOOP){
  95. occurences++; //we found another loop, we will have to skip his 'START_LOOP'
  96. }
  97. else if (src[a] == END_LOOP){
  98. if (occurences > 0){ occurences--; } //we have skipped a loop
  99. else{ break; } //gotta take a break
  100. }
  101. }
  102. }
  103. break;
  104. case END_LOOP:
  105. if (Cells[curCell] != 0){
  106. int occurences = 0; //number of loops before it
  107. for (int a = i-1; a > 0; a--){
  108. if (src[a] == START_LOOP){
  109. if (occurences > 0){ occurences--; }
  110. else{ i = a - 1; break; }
  111. }
  112. else if (src[a] == END_LOOP){
  113. occurences++;
  114. }
  115. if (a == 1){ return -1; }
  116. }
  117. }
  118. break;
  119. }
  120. }
  121. printf("\n\n\n\n****----DONE----****");
  122. getchar(); getchar();
  123. return 0;
  124. }

comments powered by Disqus