vandench

BrainFuck Parser

Jun 11th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.18 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class BrainFuck
  4. {
  5.     static final Scanner IN = new Scanner(System.in);
  6.  
  7.     static
  8.     {
  9.         Runtime.getRuntime().addShutdownHook(new Thread(IN::close));
  10.     }
  11.  
  12.     public static void parse(Instruction[] instructions)
  13.     {
  14.         short[] memory = new short[256];
  15.         short ptr = 0;
  16.  
  17.         boolean continueToEndOfLoop = false;
  18.         int skips = 0;
  19.  
  20.         for(int index = 0; index < instructions.length; ++index)
  21.         {
  22.             Instruction i = instructions[index];
  23.             if(continueToEndOfLoop)
  24.             {
  25.                 if(i == Instruction.LOOP_BEGIN) { ++skips; }
  26.                 else if(i == Instruction.LOOP_END)
  27.                 {
  28.                     if(skips > 0)
  29.                     {
  30.                         --skips;
  31.                         continue;
  32.                     }
  33.                     continueToEndOfLoop = false;
  34.                 }
  35.                 continue;
  36.             }
  37.             switch(i)
  38.             {
  39.                 case INCREMENT_POINTER:
  40.                     ++ptr;
  41.                     break;
  42.                 case DECREMENT_POINTER:
  43.                     --ptr;
  44.                     break;
  45.                 case INCREMENT:
  46.                     ++memory[ptr];
  47.                     break;
  48.                 case DECREMENT:
  49.                     --memory[ptr];
  50.                     break;
  51.                 case PRINT_CHAR:
  52.                     System.out.print((char) memory[ptr]);
  53.                     break;
  54.                 case GET_CHAR:
  55.                     memory[ptr] = (short) IN.nextByte();
  56.                     break;
  57.                 case LOOP_BEGIN:
  58.                     if(memory[ptr] == 0) { continueToEndOfLoop = true; }
  59.                     break;
  60.                 case LOOP_END:
  61.                     while(true)
  62.                     {
  63.                         --index;
  64.                         if(instructions[index] == Instruction.LOOP_END) { ++skips; }
  65.                         else if(instructions[index] == Instruction.LOOP_BEGIN)
  66.                         {
  67.                             if(skips > 0) { --skips; }
  68.                             else
  69.                             {
  70.                                 --index;
  71.                                 break;
  72.                             }
  73.                         }
  74.                     }
  75.                     break;
  76.             }
  77.         }
  78.     }
  79.  
  80.     public static Instruction[] parse(String in)
  81.     {
  82.         in = in.replaceAll("[^><+\\-.,\\[\\]]", "");
  83.         Instruction[] out = new Instruction[in.length()];
  84.         for(int i = 0; i < in.length(); ++i) { out[i] = Instruction.get(in.charAt(i)); }
  85.         return out;
  86.     }
  87.  
  88.     public static void cString(Instruction[] instructions)
  89.     {
  90.         for(Instruction i : instructions) { System.out.println(i.cString); }
  91.     }
  92.  
  93.     public static void main(String[] args)
  94.     {
  95. //        parse(parse("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++.>"));
  96. //        parse(parse("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."));
  97. //        cString(instructions);
  98.         parse(parse(args[0]));
  99.     }
  100.  
  101.     public enum Instruction
  102.     {
  103.         INCREMENT_POINTER('>', "++ptr"),
  104.         DECREMENT_POINTER('<', "--ptr"),
  105.         INCREMENT('+', "++(*ptr)"),
  106.         DECREMENT('-', "--(*ptr)"),
  107.         PRINT_CHAR('.', "putchar(*ptr);"),
  108.         GET_CHAR(',', "*ptr = getchar();"),
  109.         LOOP_BEGIN('[', "while(*ptr != 0) {"),
  110.         LOOP_END(']', "}");
  111.  
  112.         public static final com.hyfloac.brainfuck.Instruction[] VALUES = values();
  113.  
  114.         public final char identifier;
  115.         public final String cString;
  116.  
  117.         Instruction(char identifier, String cString)
  118.         {
  119.             this.identifier = identifier;
  120.             this.cString = cString;
  121.         }
  122.  
  123.         public static com.hyfloac.brainfuck.Instruction get(char c)
  124.         {
  125.             for(com.hyfloac.brainfuck.Instruction i : VALUES)
  126.             {
  127.                 if(i.identifier == c)
  128.                 {
  129.                     return i;
  130.                 }
  131.             }
  132.             return null;
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment