Guest User

Untitled

a guest
Mar 5th, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* CBF (Coed Brainfuck) Interpreter */
  2.  
  3. #load "io.h2"
  4. #load "lib.h2"
  5. #load "ntl.h2"
  6. #load "str.h2"
  7. #load "stat.h2"
  8.  
  9. #define ASCII   255
  10. #define BYTE    5000
  11. #define RDNLY   "-r"
  12.  
  13. struct Pointer
  14. {
  15.     array = [null];
  16.     loops = [];
  17.     ptr = 0;
  18. };
  19.  
  20. struct File
  21. {
  22.     descriptor = 0;
  23.     plength = 0;
  24.     string = "";
  25. };
  26.  
  27. function create_pointer();
  28. function starting_file(file_name);
  29.  
  30. function increase(*ptr);
  31. function decrease(*ptr);
  32.  
  33. function start_loop(*loop, counter);
  34. function end_loop(*pointer, *counter);
  35.  
  36. function main(argc, argv)
  37. {
  38.     pointer = create_pointer();
  39.  
  40.     if (!argv[1])
  41.     {
  42.         fprintf(stderr, "Usage: % <file name>\n", argv[0]);
  43.         exit(1);
  44.     }
  45.  
  46.     if (!strfind(argv[1], /.+\.bf/))
  47.     {
  48.         fprintf(stderr, "Cannot read file %\n", argv[1]);
  49.         exit(1);
  50.     }
  51.  
  52.     file = starting_file(argv[1]);
  53.  
  54.     for (i = 0; i < file.plength; i++)
  55.     {
  56.         switch (file.string[i])
  57.         {
  58.             case '<':
  59.                 decrease(&pointer.ptr, BYTE);
  60.                 break;
  61.  
  62.             case '>':
  63.                 increase(&pointer.ptr, BYTE);
  64.                 break;
  65.  
  66.             case '+':
  67.                 increase(&pointer.array[pointer.ptr], ARRAY);
  68.                 break;
  69.  
  70.             case '-':
  71.                 decrease(&pointer.array[pointer.ptr], ARRAY);
  72.                 break;
  73.  
  74.             case '.':
  75.                 putchar(pointer.array[pointer.ptr]);
  76.                 break;
  77.  
  78.             case ',':
  79.                 pointer.array[pointer.ptr] = getchar();
  80.                 break;
  81.  
  82.             case '[':
  83.                 start_loop(&pointer.loops, i);
  84.                 break;
  85.  
  86.             case ']':
  87.                 end_loop(&pointer, &i);
  88.                 break;
  89.         }
  90.     }
  91.  
  92.     close(file.descriptor);
  93.     return 0;
  94. }
  95.  
  96. function create_pointer()
  97. {
  98.     pointer = Pointer;
  99.  
  100.     for (i = 0; i < BYTE; i++)
  101.     {
  102.         pointer.array[i] = 0;
  103.     }
  104.  
  105.     return pointer;
  106. }
  107.  
  108. function starting_file(file_name)
  109. {
  110.     file = File;
  111.  
  112.     if ((file.descriptor = open(file_name, RDNLY)) < 0)
  113.     {
  114.         fprintf(stderr, "File % doesn't exist\n", file_name);
  115.         exit(1);
  116.     }
  117.  
  118.     file.string = read(file.descriptor);
  119.     file.plength = length(file.string);
  120.  
  121.     return file;
  122. }
  123.  
  124. function increase(*ptr, limit)
  125. {
  126.     if (*ptr == limit)
  127.     {
  128.         *ptr = 0;
  129.     }
  130.     else
  131.     {
  132.         ++(*ptr);
  133.     }
  134. }
  135.  
  136. function decrease(*ptr, limit)
  137. {
  138.     if (!*ptr)
  139.     {
  140.         *ptr = limit;
  141.     }
  142.     else
  143.     {
  144.         --(*ptr);
  145.     }
  146. }
  147.  
  148. function start_loop(*loop, counter)
  149. {
  150.     push(*loop, counter);
  151. }
  152.  
  153. function end_loop(*pointer, *counter)
  154. {
  155.     if (*pointer->array[*pointer->ptr])
  156.     {
  157.         pop(*pointer->loops);
  158.     }
  159.     else
  160.     {
  161.         *counter = *pointer->loops[length(*pointer->loops) - 1];
  162.     }
  163. }
Add Comment
Please, Sign In to add comment