Skip to content

Commit

Permalink
Inline comments added
Browse files Browse the repository at this point in the history
  • Loading branch information
NiPfi committed Sep 29, 2016
1 parent c556515 commit 31531df
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/JohnnyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class JohnnyScript {

private static final String OUTPUT_EXTENSION = ".ram";
private static final String LINE_COMMENT_DELIMITER = "//";

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

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

for (String line: lines) {
String compiledLine = compile(line);
if (compiledLine != null) {
if (!compiledLine.equals("")) {
compiled.add(compiledLine);
}
}
Expand All @@ -34,10 +35,13 @@ private static List<String> compileCode(List<String> lines) {
}

private static String compile(String line) {
if(line.startsWith("//")){
return null;
if (line.contains(LINE_COMMENT_DELIMITER)) {
int commentStart = line.indexOf(LINE_COMMENT_DELIMITER);
String nonComment = line.substring(0, commentStart);
return nonComment.trim();
}
else return line;
else
return line.trim();
}

/**
Expand Down
26 changes: 26 additions & 0 deletions test/JohnnyScriptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,30 @@ public void commentLine() throws Exception {

}

@Test
public void commentInline() throws Exception {
List<String> testCode = new ArrayList<>();
testCode.add("add 0");
testCode.add("add 1 //Test comment");
testCode.add("add 0");
Files.write(inputPath, testCode);

JohnnyScript.main(new String[]{validFile});

assertTrue("Output file not generated or incorrectly named", Files.exists(outputPath));

List outLines = Files.readAllLines(outputPath);

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

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

}

}

0 comments on commit 31531df

Please sign in to comment.