Advertisement
Guest User

Untitled

a guest
Apr 27th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. // initialize the tape with 30,000 zeroes
  9. unsigned long long int tape[30000] = {0};
  10. unsigned long long int registrar[3] = {0}; // R1, R2, and R3
  11. // set the pointer to point at the left-most cell of the tape
  12. unsigned long long int* ptr = tape;
  13.  
  14.  
  15.  
  16. void interpret(char* input) {
  17.     char current_char;
  18.     size_t i;
  19.     size_t i2;
  20.     size_t i3;
  21.     size_t loop;
  22.     char i4;
  23.  
  24.     for (i = 0; input[i] != 0; i++) {
  25.         current_char = input[i];
  26.         if (current_char == 'p') {
  27.             ++ptr;
  28.         } else if (current_char == 'P') {
  29.             --ptr;
  30.         } else if (current_char == '+') {
  31.             ++*ptr;
  32.         } else if (current_char == '-') {
  33.             --*ptr;
  34.         } else if (current_char == 'o' ) {
  35.                 putchar(*ptr);
  36.         } else if (current_char == 'i') {
  37.  
  38.             *ptr = getchar();
  39.  
  40.         } else if (current_char == '[') {
  41.             continue;
  42.         } else if (current_char == ']' && *ptr) {
  43.             loop = 1;
  44.             while (loop > 0) {
  45.                 current_char = input[--i];
  46.                 if (current_char == '[') {
  47.                     loop--;
  48.                 } else if (current_char == ']') {
  49.                     loop++;
  50.                 }
  51.             }
  52.         } else if (current_char == '/') { //push the value under the pointer to the register specified by the next value under the pointer
  53.             ++ptr;
  54.             if (*ptr > 2) {
  55.                 --ptr;
  56.             } else {
  57.                 char temp1 = *ptr;
  58.                 --ptr;
  59.                 registrar[temp1] = *ptr;
  60.             }
  61.         } else if (current_char == '\\') { //Same as /, except popped, also, i used an excapecode here to get past \
  62.             ++ptr;
  63.             if (*ptr > 2) {
  64.                 --ptr;
  65.             } else {
  66.                 char temp1 = *ptr;
  67.                 --ptr;
  68.                 *ptr = registrar[temp1];
  69.             }
  70.         } else if (current_char == 'a') { //Add R1 to R2 and put the sum in R3
  71.             registrar[2] = registrar[0] + registrar[1];
  72.         } else if (current_char == 's') { //Subtract R1 from R2 and put the diffrence in R3
  73.             registrar[2] = registrar[0] - registrar[1];
  74.         } else if (current_char == 'd') { // Divide R1 by R2 and put the quotient in R3
  75.             if (registrar[1] == 0) {
  76.                 printf("Division by zero occured, Ending!");
  77.                 break;
  78.             }
  79.             registrar[2] = registrar[0] / registrar[1];
  80.         } else if (current_char == 'm') { // Multiply R1 by R2 and put the result in R3
  81.             registrar[2] = registrar[0] * registrar[1];
  82.         } else if (current_char == '*') { // Terminate
  83.             break;
  84.         } else if (current_char == 'j') { // Jump forward n spaces
  85.  
  86.             for (i2 = *ptr; i2 != 0; i2--) {
  87.                 i++;
  88.                 current_char = input[i];
  89.             }
  90.         } else if (current_char == 'J') { // Jump backword n spaces
  91.             for (i2 = *ptr; i2 != 0; i2--) {
  92.                 i--;
  93.                 current_char = input[i];
  94.             }
  95.         } else if (current_char == 'r') { //prints out the data in the source until it hits a ;
  96.             i++;
  97.  
  98.             i3 = i;
  99.             for (input[i3];input[i3] != ';';i3++){
  100.                 putchar(input[i3]);
  101.                 i++;
  102.  
  103.             }
  104.  
  105.         } else if (current_char == 'R') {
  106.             i++;
  107.  
  108.             putchar(input[i]);
  109.         }
  110.         // More commands to come, the CIOL specs are not done
  111.     }
  112. }
  113.  
  114.  
  115. int main( int argc, char *argv[] ) {
  116.  
  117.     interpret(argv[1]);  // outputs input
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement