Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class BrainFuck
- {
- static final Scanner IN = new Scanner(System.in);
- static
- {
- Runtime.getRuntime().addShutdownHook(new Thread(IN::close));
- }
- public static void parse(Instruction[] instructions)
- {
- short[] memory = new short[256];
- short ptr = 0;
- boolean continueToEndOfLoop = false;
- int skips = 0;
- for(int index = 0; index < instructions.length; ++index)
- {
- Instruction i = instructions[index];
- if(continueToEndOfLoop)
- {
- if(i == Instruction.LOOP_BEGIN) { ++skips; }
- else if(i == Instruction.LOOP_END)
- {
- if(skips > 0)
- {
- --skips;
- continue;
- }
- continueToEndOfLoop = false;
- }
- continue;
- }
- switch(i)
- {
- case INCREMENT_POINTER:
- ++ptr;
- break;
- case DECREMENT_POINTER:
- --ptr;
- break;
- case INCREMENT:
- ++memory[ptr];
- break;
- case DECREMENT:
- --memory[ptr];
- break;
- case PRINT_CHAR:
- System.out.print((char) memory[ptr]);
- break;
- case GET_CHAR:
- memory[ptr] = (short) IN.nextByte();
- break;
- case LOOP_BEGIN:
- if(memory[ptr] == 0) { continueToEndOfLoop = true; }
- break;
- case LOOP_END:
- while(true)
- {
- --index;
- if(instructions[index] == Instruction.LOOP_END) { ++skips; }
- else if(instructions[index] == Instruction.LOOP_BEGIN)
- {
- if(skips > 0) { --skips; }
- else
- {
- --index;
- break;
- }
- }
- }
- break;
- }
- }
- }
- public static Instruction[] parse(String in)
- {
- in = in.replaceAll("[^><+\\-.,\\[\\]]", "");
- Instruction[] out = new Instruction[in.length()];
- for(int i = 0; i < in.length(); ++i) { out[i] = Instruction.get(in.charAt(i)); }
- return out;
- }
- public static void cString(Instruction[] instructions)
- {
- for(Instruction i : instructions) { System.out.println(i.cString); }
- }
- public static void main(String[] args)
- {
- // parse(parse("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++.>"));
- // parse(parse("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."));
- // cString(instructions);
- parse(parse(args[0]));
- }
- public enum Instruction
- {
- INCREMENT_POINTER('>', "++ptr"),
- DECREMENT_POINTER('<', "--ptr"),
- INCREMENT('+', "++(*ptr)"),
- DECREMENT('-', "--(*ptr)"),
- PRINT_CHAR('.', "putchar(*ptr);"),
- GET_CHAR(',', "*ptr = getchar();"),
- LOOP_BEGIN('[', "while(*ptr != 0) {"),
- LOOP_END(']', "}");
- public static final com.hyfloac.brainfuck.Instruction[] VALUES = values();
- public final char identifier;
- public final String cString;
- Instruction(char identifier, String cString)
- {
- this.identifier = identifier;
- this.cString = cString;
- }
- public static com.hyfloac.brainfuck.Instruction get(char c)
- {
- for(com.hyfloac.brainfuck.Instruction i : VALUES)
- {
- if(i.identifier == c)
- {
- return i;
- }
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment