Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
Comments (with %), direct use of johnny instructions (with @)
Browse files Browse the repository at this point in the history
  • Loading branch information
florianhartung committed Dec 1, 2020
1 parent 066717f commit 5dc705f
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 7 deletions.
17 changes: 13 additions & 4 deletions src/main/java/jcompiler/compiler/extended/ExtendedCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import jcompiler.compiler.DataAccessor;
import jcompiler.compiler.JohnnyInstruction;
import jcompiler.compiler.Statement;
import jcompiler.compiler.extended.johnnyinstructions.Read;
import jcompiler.compiler.extended.statements.Input;
import jcompiler.compiler.extended.statements.Output;
import jcompiler.compiler.extended.statements.Raw;
import jcompiler.compiler.standard.ConstructStates;
import jcompiler.compiler.standard.johnnyinstructions.Jump;
import jcompiler.compiler.standard.statements.*;
Expand All @@ -27,17 +27,24 @@ public void compile(BufferedReader input, BufferedWriter output) throws MemoryOv

StringBuilder currentStatement = new StringBuilder();
int buffer;
boolean comment = false;
while ((buffer = input.read()) != -1) {
switch ((char) buffer) {
case '\n':
case '\r':
comment = false;
if (currentStatement.length() > 0) {
compileStatement(currentStatement.toString());
compileStatement(currentStatement.toString().replaceAll(" *%", "%"));
currentStatement = new StringBuilder();
}
break;
case '%':
comment = true;
break;
default:
currentStatement.append((char) buffer);
if (!comment) {
currentStatement.append((char) buffer);
}
}
}
if (currentStatement.length() > 0) {
Expand Down Expand Up @@ -106,7 +113,9 @@ public void compile(BufferedReader input, BufferedWriter output) throws MemoryOv
}

private void compileStatement(String statement) {
if (statement.toLowerCase().startsWith("print")) {
if (statement.startsWith("@")) {
statements.add(new Raw(statement));
} else if (statement.toLowerCase().startsWith("print")) {
String valueToPrint = statement.substring(6);

statements.add(new Print(valueToPrint));
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/jcompiler/compiler/extended/statements/Raw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package jcompiler.compiler.extended.statements;

import jcompiler.Memory;
import jcompiler.compiler.JohnnyInstruction;
import jcompiler.compiler.Statement;

import java.util.ArrayList;
import java.util.List;

public class Raw implements Statement {

public static final String VAR_REGEX = "[a-zA-Z]+";
public static final String NUM_REGEX = "[0-9]+";

private final String code;

public Raw(String code) {
this.code = code;
}

String[] names = {"take", "add", "sub", "save", "jmp", "tst", "inc", "dec", "null", "hlt", "read", "out", "set"};

@Override
public JohnnyInstruction[] compile() {
String[] splitCode = code.substring(1).split(" ");

int hi = 0;
int lo = 0;

if (splitCode[0].matches(NUM_REGEX)) {
hi = Integer.parseInt(splitCode[0]);
} else {
for (int i = 0; i < names.length; i++) {
if (splitCode[0].toLowerCase().equals(names[i])) {
hi = i + 1;
break;
}
}
if (hi == 0) {
throw new IllegalArgumentException("Could not find instruction at statement " + code);
}
}


boolean isAddress;
List<JohnnyInstruction> instructionList = new ArrayList<>();
if (splitCode[1].matches(NUM_REGEX) || (splitCode[1].startsWith("$") && splitCode[1].substring(1).matches(NUM_REGEX))) {
isAddress = splitCode[1].startsWith("$");

lo = isAddress ? Integer.parseInt(splitCode[1].substring(1)) : Integer.parseInt(splitCode[1]);

} else {
String varName = splitCode[1];
if (!Memory.variableExists(varName)) {
throw new IllegalStateException("Variable " + varName + " does not exist!");
}
lo = Memory.resolveId(varName);
isAddress = true;
}

instructionList.add(new jcompiler.compiler.standard.johnnyinstructions.Raw(hi, lo, isAddress));

return instructionList.toArray(new JohnnyInstruction[0]);
}
}
15 changes: 12 additions & 3 deletions src/main/java/jcompiler/compiler/standard/StandardCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ public void compile(BufferedReader input, BufferedWriter output) throws MemoryOv

StringBuilder currentStatement = new StringBuilder();
int buffer;
boolean comment = false;
while ((buffer = input.read()) != -1) {
switch ((char) buffer) {
case '\n':
case '\r':
comment = false;
if (currentStatement.length() > 0) {
compileStatement(currentStatement.toString());
compileStatement(currentStatement.toString().replaceAll(" *%", "%"));
currentStatement = new StringBuilder();
}
break;
case '%':
comment = true;
break;
default:
currentStatement.append((char) buffer);
if(!comment) {
currentStatement.append((char) buffer);
}
}
}
if (currentStatement.length() > 0) {
Expand Down Expand Up @@ -100,7 +107,9 @@ public void compile(BufferedReader input, BufferedWriter output) throws MemoryOv
}

private void compileStatement(String statement) {
if (statement.toLowerCase().startsWith("print")) {
if(statement.startsWith("@")) {
statements.add(new Raw(statement));
} else if (statement.toLowerCase().startsWith("print")) {
String valueToPrint = statement.substring(6);

statements.add(new Print(valueToPrint));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package jcompiler.compiler.standard.johnnyinstructions;

import jcompiler.compiler.DataAccessor;
import jcompiler.compiler.JohnnyInstruction;

public class Raw extends JohnnyInstruction implements DataAccessor {

private final int hi;
private int lo;
private final boolean isAddress;

public Raw(int hi, int lo, boolean isAddress) {
this.hi = hi;
this.lo = lo;
this.isAddress = isAddress;
}

@Override
public void shiftAddress(int shiftAmount) {
lo = isAddress ? lo + shiftAmount : lo;
}

@Override
public int getH() {
return hi;
}

@Override
public int getL() {
return lo;
}
}
65 changes: 65 additions & 0 deletions src/main/java/jcompiler/compiler/standard/statements/Raw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package jcompiler.compiler.standard.statements;

import jcompiler.Memory;
import jcompiler.compiler.JohnnyInstruction;
import jcompiler.compiler.Statement;

import java.util.ArrayList;
import java.util.List;

public class Raw implements Statement {

public static final String VAR_REGEX = "[a-zA-Z]+";
public static final String NUM_REGEX = "[0-9]+";

private final String code;

public Raw(String code) {
this.code = code;
}

String[] names = {"take", "add", "sub", "save", "jmp", "tst", "inc", "dec", "null", "hlt"};

@Override
public JohnnyInstruction[] compile() {
String[] splitCode = code.substring(1).split(" ");

int hi = 0;
int lo = 0;

if (splitCode[0].matches(NUM_REGEX)) {
hi = Integer.parseInt(splitCode[0]);
} else {
for (int i = 0; i < names.length; i++) {
if (splitCode[0].toLowerCase().equals(names[i])) {
hi = i + 1;
break;
}
}
if (hi == 0) {
throw new IllegalArgumentException("Could not find instruction at statement " + code);
}
}


boolean isAddress;
List<JohnnyInstruction> instructionList = new ArrayList<>();
if (splitCode[1].matches(NUM_REGEX) || (splitCode[1].startsWith("$") && splitCode[1].substring(1).matches(NUM_REGEX))) {
isAddress = splitCode[1].startsWith("$");

lo = isAddress ? Integer.parseInt(splitCode[1].substring(1)) : Integer.parseInt(splitCode[1]);

} else {
String varName = splitCode[1];
if (!Memory.variableExists(varName)) {
throw new IllegalStateException("Variable " + varName + " does not exist!");
}
lo = Memory.resolveId(varName);
isAddress = true;
}

instructionList.add(new jcompiler.compiler.standard.johnnyinstructions.Raw(hi, lo, isAddress));

return instructionList.toArray(new JohnnyInstruction[0]);
}
}

0 comments on commit 5dc705f

Please sign in to comment.