Skip to content

Commit 31531df

Browse files
committed
Inline comments added
1 parent c556515 commit 31531df

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/JohnnyScript.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
public class JohnnyScript {
1212

1313
private static final String OUTPUT_EXTENSION = ".ram";
14+
private static final String LINE_COMMENT_DELIMITER = "//";
1415

1516
public static void main(String[] args) throws IOException {
1617

@@ -25,7 +26,7 @@ private static List<String> compileCode(List<String> lines) {
2526

2627
for (String line: lines) {
2728
String compiledLine = compile(line);
28-
if (compiledLine != null) {
29+
if (!compiledLine.equals("")) {
2930
compiled.add(compiledLine);
3031
}
3132
}
@@ -34,10 +35,13 @@ private static List<String> compileCode(List<String> lines) {
3435
}
3536

3637
private static String compile(String line) {
37-
if(line.startsWith("//")){
38-
return null;
38+
if (line.contains(LINE_COMMENT_DELIMITER)) {
39+
int commentStart = line.indexOf(LINE_COMMENT_DELIMITER);
40+
String nonComment = line.substring(0, commentStart);
41+
return nonComment.trim();
3942
}
40-
else return line;
43+
else
44+
return line.trim();
4145
}
4246

4347
/**

test/JohnnyScriptTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,30 @@ public void commentLine() throws Exception {
9191

9292
}
9393

94+
@Test
95+
public void commentInline() throws Exception {
96+
List<String> testCode = new ArrayList<>();
97+
testCode.add("add 0");
98+
testCode.add("add 1 //Test comment");
99+
testCode.add("add 0");
100+
Files.write(inputPath, testCode);
101+
102+
JohnnyScript.main(new String[]{validFile});
103+
104+
assertTrue("Output file not generated or incorrectly named", Files.exists(outputPath));
105+
106+
List outLines = Files.readAllLines(outputPath);
107+
108+
testCode = new ArrayList<>();
109+
testCode.add("add 0");
110+
testCode.add("add 1");
111+
testCode.add("add 0");
112+
113+
assertEquals(testCode.size(), outLines.size());
114+
for (int i = 0; i < testCode.size(); i++) {
115+
assertEquals("Line " + i, testCode.get(i),outLines.get(i));
116+
}
117+
118+
}
119+
94120
}

0 commit comments

Comments
 (0)