From 4d8d17c6a6c2f50c6a7c252b6f22e49d67459ded Mon Sep 17 00:00:00 2001 From: Joseph Rowlands <jrr1g18@soton.ac.uk> Date: Wed, 17 Oct 2018 15:45:23 +0100 Subject: [PATCH] Cleaned up --- wk2/Java/src/jrr1g18/bb/Interpreter.java | 33 ++++++++---------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/wk2/Java/src/jrr1g18/bb/Interpreter.java b/wk2/Java/src/jrr1g18/bb/Interpreter.java index a207740..a0bf937 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(); } -- GitLab