diff --git a/wk2/Java/src/jrr1g18/bb/Interpreter.java b/wk2/Java/src/jrr1g18/bb/Interpreter.java index a20774007e75884a40a0ffeaa0df77021d62ea19..a0bf9378cfdf01a462e50eff4b19f52c54779749 100644 --- a/wk2/Java/src/jrr1g18/bb/Interpreter.java +++ b/wk2/Java/src/jrr1g18/bb/Interpreter.java @@ -36,11 +36,6 @@ public class Interpreter { INCR, DECR, CLEAR, WHILE, END, } - @SuppressWarnings("unused") - private String instructionTypeToString(InstructionType it) { - return INSTRUCTION_NAMES.get(it); - } - private InstructionType stringToInstructionType(String instruction) { for(Entry<InstructionType, String> entry : INSTRUCTION_NAMES .entrySet()) { @@ -73,23 +68,23 @@ public class Interpreter { return m_code.get(line); } - private String[] splitLine(String line) { + private static String[] splitLine(String line) { return stripWhitespace(line).split("\\s+"); } - private String[] getSplitLine(int line) { - return splitLine(getLine(line)); - } - public static String stripWhitespace(String str) { return str.replaceAll("(^\\s+|\\s+$)", ""); } + private String[] getSplitLine(int line) { + return splitLine(getLine(line)); + } + private boolean isVariableNull(String name) { return m_vars.get(name) == null; } - private void codeCheck() { + private void validateCode() { String line; String[] splitLine; int whileDepth = 0; @@ -127,18 +122,14 @@ public class Interpreter { switch (instruction) { case WHILE: whileDepth++; - splitLine[4] = splitLine[1].replace( - splitLine[1].substring(splitLine[1].length() - 1), ""); - - if(!splitLine[2].equals("not") || splitLine[4].equals("do") - || splitLine.length > 5) { + if(!splitLine[2].equals("not") || splitLine.length > 5) { System.err.println("Syntax error on line " + (idx + 1)); System.exit(1); } break; case END: whileDepth--; - if(splitLine[0].equals("end;") && splitLine.length > 1) { + if(splitLine.length > 1) { System.err.println("Syntax error on line " + (idx + 1)); System.exit(1); } @@ -150,11 +141,9 @@ public class Interpreter { } if(whileDepth > 0) { - System.err.println( - "There are more while statements than end statements"); + System.err.println("Syntax error: unclosed while"); } else if(whileDepth < 0) { - System.err.println( - "There are more end statements than while statements"); + System.err.println("Syntax error: overclosed while"); } } @@ -172,7 +161,7 @@ public class Interpreter { e.printStackTrace(); } - codeCheck(); + validateCode(); exec(); }