C) Brainfuck Interpreter

Zusammenfassung

package de.jbb.bf;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;

public class BrainFucker {

  private InputStream inp;
  private OutputStream out;
  private int[] fields;
  private LinkedList<Integer> openedLoops;

  public BrainFucker(InputStream input, OutputStream output) {
    this(input, output, 32768);
  }

  public BrainFucker(InputStream input, OutputStream output, int fieldCnt) {

    this.inp = input;
    this.out = output;
    this.fields = new int[fieldCnt];
    this.openedLoops = new LinkedList<Integer>();
  }

  public void interpret(char[] arr) {

    int length = arr.length;
    int pointer = 0;
    this.openedLoops.clear();
    try {
      for (int i = 0; i < length; i++) {
        switch (arr[i]) {
          case '+':
            this.fields[pointer] += 1;
            if (this.fields[pointer] > 255) {
              this.fields[pointer] = 0;
            }
            break;
          case '-':
            this.fields[pointer] -= 1;
            if (this.fields[pointer] < 0) {
              this.fields[pointer] = 255;
            }
            break;
          case '<':
            pointer--;
            if (pointer < 0) {
              pointer = this.fields.length - 1;
            }
            break;
          case '>':
            pointer++;
            if (pointer == this.fields.length) {
              pointer = 0;
            }
            break;
          case '[':
            if (this.fields[pointer] == 0) {
              i++;
              for (int cnt = 1;; i++) {
                if (arr[i] == ']') {
                  cnt--;
                }
                else if (arr[i] == '[') {
                  cnt++;
                }
                if (cnt == 0) {
                  break;
                }
              }
            }
            else {
              this.openedLoops.offerFirst(i);
            }
            break;
          case ']':
            i = this.openedLoops.pollFirst() - 1;
            break;
          case '.':
            this.out.write(this.fields[pointer]);
            break;
          case ',':
            this.fields[pointer] = this.inp.read();
            if (this.fields[pointer] > 255) {
              this.fields[pointer] = 255;
            }
            else if (this.fields[pointer] < 0) {
              this.fields[pointer] = 0;
            }
        }
      }
    }
    catch (IOException e) {
      throw new RuntimeException("Can't read or write", e);
    }
    catch (Exception e) {
      throw new RuntimeException("Syntaxerror", e);
    }
  }

  public static void main(String[] args) {
    new BrainFucker(System.in, System.out).interpret(
    // Clear needed Cells
    ("<<[-]>>[-]>[-]>[-]>[-]>[-]<<<<" +
    // ASCII Mover
    ">++++++++[<++++++++>-]<+." + 
    ">+++[<++++++>-]<." + 
    ">++++[<---->-]<." + 
    "++++++." + 
    "." + 
    ">++++++[<------->-]<+." + 
    ">+++++[<+++++++++>-]<." + 
    ">+++++[<+++++++>-]<-." + 
    "+++++++." + 
    ">++++[<---->-]<-." + 
    "+++++++++++++." +
    // Linebreak
    ">++++++++[<------------->-]<." +
    // Enter signs to move (1-9):
    ">+++++++[<++++++++>-]<+++." + 
    ">++++++[<+++++++>-]<-." + 
    "++++++." + 
    "---------------." + 
    "+++++++++++++." + 
    ">+++++++++[<--------->-]<-." + 
    ">+++++++++[<+++++++++>-]<++." + 
    "----------." + 
    "--." + 
    "+++++++." + 
    "+++++." + 
    ">+++++++++[<--------->-]<--." + 
    ">+++++++[<++++++++++++>-]<." + 
    "-----." + 
    ">++++++++[<---------->-]<+." + 
    ">+++++++[<+++++++++++>-]<." + 
    "++." + 
    "+++++++." + 
    ">++++[<---->-]<-." + 
    ">++++++++[<--------->-]<+++." + 
    "++++++++." + 
    "+++++++++." + 
    "----." + 
    "++++++++++++." + 
    ">++++[<---->-]<." + 
    ">++++[<++++>-]<+." +
    // Linebreak
    ">++++++[<-------->-]<.[-]" +
    // Read signs to move
    ">>>>," + 
    ">++++++" + 
    "[<-------->-]" +
    // Read linebreak signs (13, 10) and go to cell 1
    ">>,,<<<<<<" +
    // Enter phrase:
    ">++++++++[<+++++++++>-]<---." + 
    ">++++++[<+++++++>-]<-." + 
    "++++++." + 
    "---------------." + 
    "+++++++++++++." + 
    ">+++++++++[<--------->-]<-." + 
    ">++++++++[<++++++++++>-]<." + 
    "--------." + 
    "++++++++++." + 
    ">++++[<---->-]<-." + 
    ">+++[<++++++>-]<." + 
    "--------------." + 
    ">++++++[<------->-]<-." +
    // Linebreak
    ">++++++[<-------->-]<.[-]" +
    // move algo
    ">++++" + 
    "[<+++>-]" + 
    "<+" + 
    "[>," + 
    "  [-<->>+<]" + 
    "  <" + 
    "  [" + 
    "    >>>[-<+>>+<]<.>>[-<+<->>]<<" + 
    "    [-<<+>>]" + 
    "  ]" + 
    "  <<" + 
    "]").toCharArray());

    System.out.flush();
  }
}

Der integrierte BrainFuck Code stellt einen einfachen Verschlüsselungsalogrithmus dar. Der User gibt eine einstellige Zahl und anschließend einen beliebig langen Text ein. Beide Eingaben werden mit einem Zeilenumbruch bestätigt. Das BrainFuck-Programm gibt nun jeden Buchstaben des eingegebenen Textes um den vorher eingegebenen Faktor verschoben aus.

Previous Article

One Reply to “C) Brainfuck Interpreter”

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.