Guest User

Untitled

a guest
Jun 18th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5.  
  6.  
  7. //#define DEBUG
  8.  
  9. #define MVR '>'
  10. #define MVL '<'
  11. #define INC '+'
  12. #define DEC '-'
  13. #define OUT '.'
  14. #define RIN ','
  15. #define OLP '['
  16. #define CLP ']'
  17.  
  18.  
  19. char* readall(FILE* ifp);
  20. void clean(char* buffer);
  21.  
  22.  
  23. /*
  24. * The 'stack' will store data from the BF program
  25. * 'sp' points to current "selected" "variable"
  26. * The 'jumpstack' keeps track of `[` to track loops
  27. * 'ip' points to instruction in the program
  28. */
  29. unsigned char stack[200] = { 0 };
  30. int sp = 0;
  31.  
  32. unsigned char jumpstack[50];
  33. int ip = 0;
  34.  
  35. char op;
  36.  
  37.  
  38.  
  39. int main(int argc, char *argv[]) {
  40.  
  41. if ( argc != 2 ) {
  42. printf("Usage: %s inputfile\n", argv[0]);
  43. return 1;
  44. }
  45.  
  46. char* fname = argv[1];
  47. FILE* ifp;
  48. char* buffer;
  49.  
  50. /*
  51. * Check if file exists
  52. * Exit if not
  53. */
  54. if ( access( fname, F_OK ) == -1 ) {
  55. printf("No such file: %s\n", fname);
  56. return 1;
  57. } else if ( access( fname, R_OK ) == -1 ) {
  58. printf("You do not have permission to read file: %s\n", fname);
  59. return 1;
  60. }
  61.  
  62. ifp = fopen(fname, "r");
  63.  
  64. if ( !( buffer = readall(ifp) ) ) {
  65. printf("Cannot open file");
  66. }
  67.  
  68. fclose(ifp);
  69.  
  70. clean(buffer);
  71.  
  72. #ifdef DEBUG
  73. printf("Contents of file:\n%s\n", buffer);
  74. #endif
  75.  
  76.  
  77.  
  78. /*
  79. * Main interpreter loop
  80. */
  81. for(;;){
  82.  
  83. /*
  84. * Define exit points for the interpreter
  85. */
  86.  
  87. op = buffer[ip];
  88. if ( !op ) return 0;
  89.  
  90. if ( sp < 0 ) {
  91. printf("Stackpointer was non-zero at character %d\n", ip);
  92. return 1;
  93. }
  94.  
  95.  
  96. switch ( op ) {
  97. case MVR:
  98. sp++;
  99. ip++;
  100. break;
  101. case MVL:
  102. sp--;
  103. ip++;
  104. break;
  105. case INC:
  106. stack[sp]++;
  107. ip++;
  108. break;
  109. case DEC:
  110. stack[sp]--;
  111. ip++;
  112. break;
  113. case OUT:
  114. putchar(stack[sp]);
  115. ip++;
  116. break;
  117. case RIN:
  118. break;
  119. case OLP:
  120. break;
  121. case CLP:
  122. break;
  123. default:
  124. printf("Unknown instruction: %c", op);
  125. return 1;
  126. }
  127.  
  128.  
  129.  
  130. }
  131.  
  132. free(buffer);
  133.  
  134. return 0;
  135. }
  136.  
  137.  
  138.  
  139. char* readall(FILE* ifp) {
  140. /*
  141. * Read stream 'ifp' to 'buffer'
  142. * Returns pointer to buffer, 1 on fail
  143. */
  144.  
  145. char* buffer;
  146.  
  147. fseek(ifp, 0L, SEEK_END);
  148. size_t fsize = ftell(ifp);
  149. rewind(ifp);
  150.  
  151. buffer = malloc(fsize);
  152. /* Check for memory error */
  153. if ( buffer == NULL )
  154. return NULL;
  155.  
  156. fread(buffer, 1, fsize, ifp);
  157.  
  158. return buffer;
  159. }
  160.  
  161.  
  162.  
  163. void clean(char* buffer) {
  164. /*
  165. * Returns stripped string
  166. * Used characters are: ><+-.,[]
  167. */
  168.  
  169. int i, bp;
  170. char c;
  171.  
  172. bp = 0;
  173. for ( i=0; buffer[i]; i++ ) {
  174. c = buffer[i];
  175.  
  176. if ( c == MVR ||
  177. c == MVL ||
  178. c == INC ||
  179. c == DEC ||
  180. c == OUT ||
  181. c == RIN ||
  182. c == OLP ||
  183. c == CLP ) {
  184. buffer[bp] = c;
  185. bp += 1;
  186. }
  187.  
  188. }
  189. buffer[bp] = 0x00;
  190.  
  191. buffer = realloc( buffer, strlen(buffer)+1 );
  192. }
Add Comment
Please, Sign In to add comment