Guest User

Untitled

a guest
Nov 2nd, 2018
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.57 KB | None | 0 0
  1. import core.stdc.stdio: getchar;
  2. import std.stdio : writeln;
  3.  
  4.  
  5. auto get(A, I, D)(A arr, I idx, D def)
  6. {
  7.     if (arr.length - 1 >= idx)
  8.         return arr[idx];
  9.     else
  10.         return def;
  11. }
  12.  
  13. void main(string[] args)
  14. {
  15.     const string helloworld = q{
  16.         ++++++++++[>+++++++>++++++++
  17.         ++>+++>+<<<<-]>++.>+.+++++++
  18.         ..+++.>++.<<+++++++++++++++.
  19.         >.+++.------.--------.>+.>.
  20.     };
  21.  
  22.     string code   = args.get(1, helloworld);
  23.     string input  = args.get(2, "");
  24.     string buffer = "";
  25.  
  26.     //if (code.startWith("-f=")) // then load the file content
  27.     //{
  28.     //}
  29.  
  30.     const bool interactive = input.length == 0;
  31.  
  32.     long[long] tape; // memory tape
  33.     long head = 0;   // reading head
  34.     long curs = 0;   // code cursor
  35.  
  36.     while (curs >= 0 && curs < code.length)
  37.     {
  38.         long* slot = (head in tape);  // current memory slot
  39.  
  40.         switch (code[curs])
  41.         {
  42.             default:  // unknow character, thus ignored
  43.                 break;
  44.  
  45.             case '<':
  46.                 head--;
  47.                 break;
  48.  
  49.             case '>':
  50.                 head++;
  51.                 break;
  52.  
  53.             case '+':
  54.                 if (slot is null) tape[head] = 1;
  55.                 else (*slot)++;
  56.                 break;
  57.  
  58.             case '-':
  59.                 if (slot is null) tape[head] = -1;
  60.                 else (*slot)--;
  61.                 break;
  62.  
  63.             case '.':
  64.                 if (slot is null) buffer ~= "\0";
  65.                 else buffer ~= cast(char)(*slot);
  66.                 break;
  67.  
  68.             case ',':
  69.             {
  70.                 char chr;
  71.  
  72.                 if (interactive)  // interactive input mode, strip newline
  73.                     do chr = cast(char)getchar(); while(chr == '\n');
  74.                 else if (input.length == 0)
  75.                     chr = '\0';
  76.                 else
  77.                 {
  78.                     chr = input[0];
  79.                     input = input[1..$];
  80.                 }
  81.  
  82.                 if (slot is null) tape[head] = chr;
  83.                 else (*slot) = chr;
  84.                 break;
  85.             }
  86.  
  87.             case '[':
  88.                 if (slot is null || *slot == 0)  // skipping till the closing bracket
  89.                 {
  90.                     long match = 1;        // matching bracket counter
  91.                     long skip = curs + 1;  // like curs
  92.  
  93.                     while (skip >= 0 && skip < code.length)
  94.                     {
  95.                         if (code[skip] == '[') match++;
  96.                         else if (code[skip] == ']') match--;
  97.                         if (code[skip] == ']' && match == 0) break;
  98.                         skip++;
  99.                     }
  100.  
  101.                     curs = skip;
  102.                 }
  103.                 break;
  104.  
  105.             case ']':
  106.                 if (slot !is null && *slot != 0)  // skipping till the opening bracket
  107.                 {
  108.                     long match = 1;        // matching bracket counter
  109.                     long skip = curs - 1;  // like curs
  110.  
  111.                     while (skip >= 0 && skip < code.length)
  112.                     {
  113.                         if (code[skip] == ']') match++;
  114.                         else if (code[skip] == '[') match--;
  115.                         if (code[skip] == '[' && match == 0) break;
  116.                         skip--;
  117.                     }
  118.  
  119.                     curs = skip;
  120.                 }
  121.                 break;
  122.         }
  123.  
  124.         curs++;  // increment the code cursor
  125.     }
  126.  
  127.     writeln(buffer); // printing the result
  128. }
Advertisement
Add Comment
Please, Sign In to add comment