Skip to content

Commit d04410f

Browse files
author
Nicola Pfister
committed
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
1 parent 31531df commit d04410f

File tree

4 files changed

+74
-17
lines changed

4 files changed

+74
-17
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JohnnyScript.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
88
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
99
</content>
10-
<orderEntry type="inheritedJdk" />
10+
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
1111
<orderEntry type="sourceFolder" forTests="false" />
1212
<orderEntry type="module-library" scope="TEST">
1313
<library name="JUnit4">

src/JohnnyScript.java

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import com.sun.org.apache.bcel.internal.classfile.Code;
2+
13
import java.io.IOException;
24
import java.nio.file.FileSystems;
35
import java.nio.file.Path;
@@ -24,24 +26,49 @@ public static void main(String[] args) throws IOException {
2426
private static List<String> compileCode(List<String> lines) {
2527
List<String> compiled = new ArrayList<>();
2628

27-
for (String line: lines) {
28-
String compiledLine = compile(line);
29-
if (!compiledLine.equals("")) {
30-
compiled.add(compiledLine);
31-
}
29+
for (int i = 0; i <= 999; i++) {
30+
if (i < lines.size()) {
31+
try {
32+
String compiledLine = compile(lines.get(i));
33+
if (compiledLine != null) {
34+
compiled.add(compiledLine);
35+
} else {
36+
lines.remove(i); // Remove comment line from source
37+
i--; // Reduce index to continue at next line without skipping
38+
}
39+
} catch (InvalidScriptException e) {
40+
throw new CompilerHaltException("Invalid code at line " + i, e);
41+
}
42+
} else compiled.add("000");
3243
}
3344

3445
return compiled;
3546
}
3647

37-
private static String compile(String line) {
48+
private static String compile(String line) throws InvalidScriptException {
3849
if (line.contains(LINE_COMMENT_DELIMITER)) {
3950
int commentStart = line.indexOf(LINE_COMMENT_DELIMITER);
4051
String nonComment = line.substring(0, commentStart);
41-
return nonComment.trim();
52+
if (!nonComment.equals(""))
53+
return encode(nonComment);
54+
else return null;
4255
}
4356
else
44-
return line.trim();
57+
return encode(line);
58+
}
59+
60+
private static String encode(String line) throws InvalidScriptException {
61+
String[] parts = line.trim().split(" ");
62+
if (parts.length > 2) throw new InvalidScriptException("InvalidJohnnyScript (too many parts): " + line);
63+
if (parts.length == 1) {
64+
return String.format("%03d", Integer.parseInt(parts[0]));
65+
}
66+
// else there are two parts
67+
String code = parts[0].toUpperCase();
68+
String lo = String.format("%03d", Integer.parseInt(parts[1]));
69+
String hi = Codes.valueOf(code).getCode();
70+
return hi + lo;
71+
4572
}
4673

4774
/**
@@ -94,4 +121,32 @@ private static void checkInputFile(Path path) {
94121
}
95122
}
96123

124+
private enum Codes {
125+
TAKE(1), ADD(2), SUB(3), SAVE(4), JMP(5), TST(6), INC(7), DEC(8), NULL(9), HLT(10);
126+
127+
int codeOrdinal = 0;
128+
129+
Codes(int ord) {
130+
this.codeOrdinal = ord;
131+
}
132+
133+
public String getCode() {
134+
return String.valueOf(codeOrdinal);
135+
}
136+
}
137+
138+
}
139+
140+
class InvalidScriptException extends Exception {
141+
142+
InvalidScriptException(String message) {
143+
super(message);
144+
}
145+
}
146+
147+
class CompilerHaltException extends RuntimeException {
148+
149+
CompilerHaltException(String message, Throwable cause) {
150+
super(message, cause);
151+
}
97152
}

test/JohnnyScriptTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public void commentLine() throws Exception {
8181
List outLines = Files.readAllLines(outputPath);
8282

8383
testCode = new ArrayList<>();
84-
testCode.add("add 0");
85-
testCode.add("add 0");
84+
testCode.add("2000");
85+
testCode.add("2000");
8686

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

108108
testCode = new ArrayList<>();
109-
testCode.add("add 0");
110-
testCode.add("add 1");
111-
testCode.add("add 0");
109+
testCode.add("2000");
110+
testCode.add("2001");
111+
testCode.add("2000");
112112

113-
assertEquals(testCode.size(), outLines.size());
113+
assertEquals(1000, outLines.size());
114114
for (int i = 0; i < testCode.size(); i++) {
115115
assertEquals("Line " + i, testCode.get(i),outLines.get(i));
116116
}
117117

118118
}
119119

120+
//TODO Tests for every code
121+
120122
}

0 commit comments

Comments
 (0)