Guest User

Hq9eFuck interpreter source in C

a guest
Jan 21st, 2016
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. /*I know this code ain't good, but the main thing is idea for you to do it better.*/
  7. void run_99_bottles_of_beer()
  8. {
  9.     char i = 99;
  10.     while (i >= 3)
  11.     {
  12.         printf("%i bottles of beer on the wall, %i bottles of beer!\n", i, i);
  13.         printf("Take one down, pass it around, %i bottles of beer on the wall.\n\n", --i);
  14.     }
  15.     printf("2 bottles of beer on the wall, 2 bottles of beer.\n");
  16.     printf("Take one down, pass it around, 1 bottle of beer on the wall.\n\n");
  17.     printf("1 bottle of beer on the wall, 1 bottle of beer.\n");
  18.     printf("Take one down, pass it around, no more bottles of beer on the wall.\n\n");
  19.     printf("No more bottles of beer on the wall, no more bottles of beer.\n");
  20.     printf("Go to the store and buy some more, 99 bottles of beer on the wall.\n\n");
  21. }
  22.  
  23. int main(int argc, char **argv)
  24. {
  25.  
  26.     FILE* source_file;
  27.     long size;
  28.  
  29.     if (argc <= 1)
  30.     {
  31.         puts("No source.");
  32.         return 0;
  33.     }
  34.  
  35.     source_file = fopen(argv[1], "r");
  36.  
  37.     if (source_file == NULL)
  38.     {
  39.         puts("Can't open file.");
  40.         return 0;
  41.     }
  42.  
  43.     fseek(source_file, 0, SEEK_END);
  44.     size = ftell(source_file);
  45.     fseek(source_file, 0, SEEK_SET);
  46.  
  47.     char* tape = calloc(60000, 1);
  48.     char* source = malloc(size);
  49.  
  50.     long i = 0;
  51.     while (size-- > 0)
  52.     {
  53.         char ch = getc(source_file);
  54.         source[i] = ch;
  55.         ++i;
  56.     }
  57.  
  58.     int tape_pointer = 0;
  59.     int src_pointer = 0;
  60.     int src_length = strlen(source);
  61.     int loop = 0;
  62.  
  63.     while (src_pointer < src_length)
  64.     {
  65.         char src_op = source[src_pointer];
  66.  
  67.         /*Brainfuck extension*/
  68.         if (src_op == '<') tape_pointer--; else
  69.         if (src_op == '>') tape_pointer++; else
  70.         if (src_op == '+') tape[tape_pointer]++; else
  71.         if (src_op == '-') tape[tape_pointer]--; else
  72.         if (src_op == ',') tape[tape_pointer] = getchar(); else
  73.         if (src_op == '.') putchar(tape[tape_pointer]); else
  74.         if (src_op == ']')
  75.         {
  76.             if (tape[tape_pointer] != 0)
  77.             {
  78.                 loop = 1;
  79.                 while (loop != 0)
  80.                 {
  81.                     --src_pointer;
  82.                     if (source[src_pointer] == '[') loop--; else
  83.                     if (source[src_pointer] == ']') loop++;
  84.                 }
  85.             }
  86.         } else
  87.         if (src_op == '[')
  88.         {
  89.             if (tape[tape_pointer] == 0)
  90.             {
  91.                 loop = 1;
  92.                 while (loop != 0)
  93.                 {
  94.                     ++src_pointer;
  95.                     if (source[src_pointer] == ']') loop--; else
  96.                     if (source[src_pointer] == '[') loop++;
  97.                 }
  98.             }
  99.         }
  100.  
  101.         /*HQ9+ extension (command '+' taken by brainfuck)*/
  102.         if (src_op == 'H') puts("Hello world!"); else
  103.         if (src_op == 'Q') puts(source); else
  104.         if (src_op == '9') run_99_bottles_of_beer();
  105.  
  106.         /*Deep Thought extension, you write 'DT' in code, it prints '42'*/
  107.         if (src_op == 'D' && source[src_pointer + 1] == 'T')
  108.         {
  109.             ++src_pointer;
  110.             printf("%i\n", 42);
  111.         }
  112.         src_pointer++;
  113.     }
  114.  
  115.     return 0;
  116. }
Add Comment
Please, Sign In to add comment