From d04410f551512ecfd4953428a6219f7650cb7164 Mon Sep 17 00:00:00 2001 From: Nicola Pfister Date: Thu, 29 Sep 2016 15:27:31 +0200 Subject: [PATCH] Base compiler finished Compiler now compiles all 10 base commands and can take a single number for a Hi of 00 and a low containing said number --- .idea/misc.xml | 2 +- JohnnyScript.iml | 2 +- src/JohnnyScript.java | 71 +++++++++++++++++++++++++++++++++----- test/JohnnyScriptTest.java | 16 +++++---- 4 files changed, 74 insertions(+), 17 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 18d93cc..e1a5887 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -51,7 +51,7 @@ - + \ No newline at end of file diff --git a/JohnnyScript.iml b/JohnnyScript.iml index 57d8cbd..5eb0105 100644 --- a/JohnnyScript.iml +++ b/JohnnyScript.iml @@ -7,7 +7,7 @@ - + diff --git a/src/JohnnyScript.java b/src/JohnnyScript.java index fa13f31..9bdaee2 100644 --- a/src/JohnnyScript.java +++ b/src/JohnnyScript.java @@ -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; @@ -24,24 +26,49 @@ public static void main(String[] args) throws IOException { private static List compileCode(List lines) { List 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; + } /** @@ -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); + } } diff --git a/test/JohnnyScriptTest.java b/test/JohnnyScriptTest.java index 3ef0b2d..e7b590a 100644 --- a/test/JohnnyScriptTest.java +++ b/test/JohnnyScriptTest.java @@ -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)); } @@ -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 + } \ No newline at end of file