Skip to content

Commit

Permalink
Base compiler finished
Browse files Browse the repository at this point in the history
Compiler now compiles all 10 base commands and can take a single number for a Hi of 00 and a low containing said number
  • Loading branch information
Nicola Pfister committed Sep 29, 2016
1 parent 31531df commit d04410f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion JohnnyScript.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
Expand Down
71 changes: 63 additions & 8 deletions src/JohnnyScript.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.sun.org.apache.bcel.internal.classfile.Code;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
Expand All @@ -24,24 +26,49 @@ public static void main(String[] args) throws IOException {
private static List<String> compileCode(List<String> lines) {
List<String> compiled = new ArrayList<>();

for (String line: lines) {
String compiledLine = compile(line);
if (!compiledLine.equals("")) {
compiled.add(compiledLine);
}
for (int i = 0; i <= 999; i++) {
if (i < lines.size()) {
try {
String compiledLine = compile(lines.get(i));
if (compiledLine != null) {
compiled.add(compiledLine);
} else {
lines.remove(i); // Remove comment line from source
i--; // Reduce index to continue at next line without skipping
}
} catch (InvalidScriptException e) {
throw new CompilerHaltException("Invalid code at line " + i, e);
}
} else compiled.add("000");
}

return compiled;
}

private static String compile(String line) {
private static String compile(String line) throws InvalidScriptException {
if (line.contains(LINE_COMMENT_DELIMITER)) {
int commentStart = line.indexOf(LINE_COMMENT_DELIMITER);
String nonComment = line.substring(0, commentStart);
return nonComment.trim();
if (!nonComment.equals(""))
return encode(nonComment);
else return null;
}
else
return line.trim();
return encode(line);
}

private static String encode(String line) throws InvalidScriptException {
String[] parts = line.trim().split(" ");
if (parts.length > 2) throw new InvalidScriptException("InvalidJohnnyScript (too many parts): " + line);
if (parts.length == 1) {
return String.format("%03d", Integer.parseInt(parts[0]));
}
// else there are two parts
String code = parts[0].toUpperCase();
String lo = String.format("%03d", Integer.parseInt(parts[1]));
String hi = Codes.valueOf(code).getCode();
return hi + lo;

}

/**
Expand Down Expand Up @@ -94,4 +121,32 @@ private static void checkInputFile(Path path) {
}
}

private enum Codes {
TAKE(1), ADD(2), SUB(3), SAVE(4), JMP(5), TST(6), INC(7), DEC(8), NULL(9), HLT(10);

int codeOrdinal = 0;

Codes(int ord) {
this.codeOrdinal = ord;
}

public String getCode() {
return String.valueOf(codeOrdinal);
}
}

}

class InvalidScriptException extends Exception {

InvalidScriptException(String message) {
super(message);
}
}

class CompilerHaltException extends RuntimeException {

CompilerHaltException(String message, Throwable cause) {
super(message, cause);
}
}
16 changes: 9 additions & 7 deletions test/JohnnyScriptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public void commentLine() throws Exception {
List outLines = Files.readAllLines(outputPath);

testCode = new ArrayList<>();
testCode.add("add 0");
testCode.add("add 0");
testCode.add("2000");
testCode.add("2000");

assertEquals(testCode.size(), outLines.size());
assertEquals(1000, outLines.size());
for (int i = 0; i < testCode.size(); i++) {
assertEquals("Line " + i, testCode.get(i),outLines.get(i));
}
Expand All @@ -106,15 +106,17 @@ public void commentInline() throws Exception {
List outLines = Files.readAllLines(outputPath);

testCode = new ArrayList<>();
testCode.add("add 0");
testCode.add("add 1");
testCode.add("add 0");
testCode.add("2000");
testCode.add("2001");
testCode.add("2000");

assertEquals(testCode.size(), outLines.size());
assertEquals(1000, outLines.size());
for (int i = 0; i < testCode.size(); i++) {
assertEquals("Line " + i, testCode.get(i),outLines.get(i));
}

}

//TODO Tests for every code

}

0 comments on commit d04410f

Please sign in to comment.