File tree Expand file tree Collapse file tree 2 files changed +34
-4
lines changed
Expand file tree Collapse file tree 2 files changed +34
-4
lines changed Original file line number Diff line number Diff line change 1111public 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 /**
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments