Browse Source

Initial Commit

master
Denis Thiessen 2 years ago
commit
01a402a881
  1. 3
      .gitignore
  2. BIN
      bin/main/Compiler.class
  3. BIN
      bin/main/ICommand.class
  4. BIN
      bin/main/Main.class
  5. BIN
      bin/main/Navigator.class
  6. BIN
      bin/main/NewCompiler.class
  7. 265
      src/main/Compiler.java
  8. 7
      src/main/ICommand.java
  9. 12
      src/main/Main.java
  10. 5
      src/main/Navigator.java
  11. 10
      src/main/NewCompiler.java

3
.gitignore

@ -0,0 +1,3 @@
/.settings
.classpath
.project

BIN
bin/main/Compiler.class

BIN
bin/main/ICommand.class

BIN
bin/main/Main.class

BIN
bin/main/Navigator.class

BIN
bin/main/NewCompiler.class

265
src/main/Compiler.java

@ -0,0 +1,265 @@
package main;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
public class Compiler {
private String logo = "***************************\n" +
"* *\n" +
"* Brainfuck Compiler *\n" +
"* by *\n" +
"* D-Lord *\n" +
"* *\n" +
"***************************" + "\n\n";
private static Scanner scanner;
private String answer;
private String filename;
private String input;
private boolean isEntered;
private final String[] ansDir = {"directly", "d", "dir"};
private final String[] ansFile = {"File", "file", "f"};
public Compiler() {
scanner = new Scanner(System.in);
input = "";
isEntered = false;
}
public void init() {
System.out.println(logo);
while(!isEntered) {
System.out.println("Compile code directly or from a file?");
answer = scanner.nextLine();
// Directly
if(contains(ansDir, answer)) {
isEntered = true;
System.out.println("Please enter your brainfuck code:");
input = scanner.nextLine();
compile(input, scanner);
}else if(contains(ansFile, answer)){
// File
isEntered = true;
System.out.println("Enter the file name (with the file extention!). The file has to be in the same directory as this.");
filename = scanner.nextLine();
input = readFileAsString(Paths.get("").toAbsolutePath().toString() + "\\" + filename);
compile(input, scanner);
}else {
System.out.println("Wrong input. Enter either 'directly' or 'file'.");
}
}
scanner.close();
}
// This method actually compiles the brainfuck code which is stored as a string. The scanner is for the "," instruction, which gets the user-input.
public void compile(String input, Scanner scanner) {
// Some variables that track the position of the memory and the currently tracked character.
/*
* ----------------------------------------------
* | | | | | |
* | 104 | 101 | 108 | 108 | 111 |.................
* | | | | | |
* ----------------------------------------------
*
*
* memPos
*
*
*
* ++++++++[>++++[ .........
*
*
* pos
* */
ArrayList<Integer> mem = new ArrayList<Integer>();
int memPos = 0;
int pos = 0;
int b_counter = 0;
char[] inputArr = input.toCharArray();
int p_counter = 1;
int m_counter = 1;
int arr1_counter = 1;
int arr2_counter = 1;
while(pos < input.length()) {
// The instructions.
//System.out.println(mem.toString());
while(mem.size() <= memPos) {
mem.add(0);
}
switch(inputArr[pos]) {
case '>':
while(inputArr[pos + 1] == '>') {
arr1_counter++;
pos++;
}
memPos+=arr1_counter;
arr1_counter = 1;
break;
case '<':
if(memPos > 0) {
while(inputArr[pos + 1] == '<') {
arr2_counter++;
pos++;
}
memPos-=arr2_counter;
arr2_counter = 1;
}
else {
System.err.print("Outside of the band");
}
break;
case '+':
while(inputArr[pos + 1] == '+') {
p_counter++;
pos++;
}
mem.set(memPos, (mem.get(memPos) + p_counter));
p_counter = 1;
break;
case '-':
while(inputArr[pos + 1] == '-') {
m_counter++;
pos++;
}
mem.set(memPos, (mem.get(memPos) - m_counter));
m_counter = 1;
break;
case '[':
if(inputArr[pos + 1] == '-' && inputArr[pos + 2] == ']') {
mem.set(memPos, 0);
pos+=2;
break;
}
if(mem.get(memPos) == 0) {
pos++;
while (b_counter > 0 || inputArr[pos] != ']')
{
if (inputArr[pos] == '[') {
b_counter++; }
else if (inputArr[pos] == ']') {
b_counter--; }
pos++;
}
}
break;
case ']':
if(mem.get(memPos) != 0) {
pos--;
while (b_counter > 0 || inputArr[pos] != '[')
{
if (inputArr[pos] == ']') {
b_counter++; }
else if (inputArr[pos] == '[') {
b_counter--; }
pos--;
}
pos--;
}
break;
case '.':
System.out.print((char) ((int) mem.get(memPos)));
break;
case ',':
System.out.print("Please enter one character: ");
String s_inp = scanner.nextLine();
while(s_inp.length() != 1) {
System.out.println("Finally enter JUST ONE CHARACTER!!! ");
s_inp = scanner.nextLine();
}
mem.set(memPos, (int) (s_inp.charAt(0)));
break;
}
pos++;
}
}
private static String readFileAsString(String fileName)
{
String data = "";
try {
data = new String(Files.readAllBytes(Paths.get(fileName)));
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
private static boolean contains(String[] arr, String testString) {
for(int i = 0; i < arr.length; i++) {
if(arr[i].equals(testString)) {
return true;
}
}
return false;
}
}

7
src/main/ICommand.java

@ -0,0 +1,7 @@
package main;
public interface ICommand {
public void execute();
}

12
src/main/Main.java

@ -0,0 +1,12 @@
package main;
public class Main {
public static void main(String[] args) {
Compiler compiler = new Compiler();
compiler.init();
}
}

5
src/main/Navigator.java

@ -0,0 +1,5 @@
package main;
public class Navigator {
}

10
src/main/NewCompiler.java

@ -0,0 +1,10 @@
package main;
public class NewCompiler {
public NewCompiler()
{
}
}
Loading…
Cancel
Save