This commit is contained in:
Sem van der Hoeven
2019-12-02 22:42:34 +01:00
parent 4be93f3404
commit 19c07d7409
9 changed files with 179 additions and 13 deletions

44
.classpath Normal file
View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

23
.project Normal file
View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>BrainfuckInterpreter</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false

View File

@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.5

View File

@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

11
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,11 @@
{
"configurations": [
{
"type": "java",
"name": "CodeLens (Launch) - Main",
"request": "launch",
"mainClass": "brainfuck.interpreter.Main",
"projectName": "BrainfuckInterpreter"
}
]
}

View File

@@ -1,13 +0,0 @@
package brainfuck.interpreter;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

@@ -0,0 +1,65 @@
package brainfuck.interpreter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
/**
* Brainfuck interpreter.
*
* @author Sem van der Hoeven
*/
public class BfInterpreter {
private char[] code;
private byte[] memory;
private Scanner sc;
public final int MAX_SIZE = 65535;
public BfInterpreter(String code) throws Exception {
if (code == null) {
throw new Exception("The code cannot be null!");
}
this.sc = new Scanner(System.in);
this.code = code.toCharArray();
this.memory = new byte[MAX_SIZE];
}
public void interpret() {
int pointer = 0;
for (int i = 0; i < this.code.length; i++) {
switch (this.code[i]) {
case '+':
memory[pointer]++;
break;
case '-':
memory[pointer]--;
break;
case '>':
pointer++;
break;
case '<':
pointer--;
break;
case '.':
System.out.print(String.valueOf((char) memory[pointer]));
break;
case ',':
memory[pointer] = sc.next().trim().getBytes()[0];
break;
case '[': // als de memory[pointer] 0 is, spring naar de plek na de volgende ] ipv een vooruit
break;
case ']': // als de memory[pointer] niet 0 is, spring terug naar de plek voor de vorige [ ipv een vooruit
break;
default:
break;
}
}
}
}

View File

@@ -0,0 +1,21 @@
package brainfuck.interpreter;
import java.io.UnsupportedEncodingException;
/**
* Main
*/
public class Main {
public static void main(String[] args) {
String excl = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++.";
try {
BfInterpreter interpreter = new BfInterpreter(excl);
interpreter.interpret();
} catch (Exception e) {
e.printStackTrace();
}
}
}