diff --git a/wk2/Java/negative.bb b/wk2/Java/negative.bb new file mode 100644 index 0000000000000000000000000000000000000000..1522cde1839f15fa58410cccdbc477a11936ac42 --- /dev/null +++ b/wk2/Java/negative.bb @@ -0,0 +1,2 @@ +clear x; +decr x; \ No newline at end of file diff --git a/wk2/Java/src/jrr1g18/bb/Interpreter.java b/wk2/Java/src/jrr1g18/bb/Interpreter.java index a0bf9378cfdf01a462e50eff4b19f52c54779749..871072be8c9d091bc5fd48539055593218c6f55d 100644 --- a/wk2/Java/src/jrr1g18/bb/Interpreter.java +++ b/wk2/Java/src/jrr1g18/bb/Interpreter.java @@ -4,8 +4,10 @@ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; @@ -13,13 +15,14 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public class Interpreter { - private Map<String, Integer> m_vars = new HashMap<>(); - private Map<Integer, String> m_code = new HashMap<>(); - private Map<Integer, Integer> whiles = new HashMap<>(); + private List<String> m_code = new ArrayList<>(); + private Map<Integer, Integer> m_whiles = new HashMap<>(); private int m_lineCount = 0; private int m_whileDepth = 0; + private final String m_filename; + private static final Map<InstructionType, String> INSTRUCTION_NAMES; static { @@ -36,7 +39,7 @@ public class Interpreter { INCR, DECR, CLEAR, WHILE, END, } - private InstructionType stringToInstructionType(String instruction) { + private static InstructionType stringToInstructionType(String instruction) { for(Entry<InstructionType, String> entry : INSTRUCTION_NAMES .entrySet()) { if(Objects.equals(instruction, entry.getValue())) { @@ -56,8 +59,14 @@ public class Interpreter { return stripWhitespace(m.group(1)); } - public void run(String fileName) { - readCode(fileName); + public Interpreter(String fileName) { + m_filename = fileName; + } + + public void run() { + readCode(); + validateCode(); + exec(); } public String variablesToString() { @@ -147,12 +156,12 @@ public class Interpreter { } } - private void readCode(String nameOfTheFile) { - try(BufferedReader br = - new BufferedReader(new FileReader(nameOfTheFile))) { + private void readCode() { + try(BufferedReader reader = + new BufferedReader(new FileReader(m_filename))) { String line; - while((line = br.readLine()) != null) { - m_code.put(m_lineCount, line); + while((line = reader.readLine()) != null) { + m_code.add(line); m_lineCount++; } } catch(FileNotFoundException e) { @@ -160,9 +169,6 @@ public class Interpreter { } catch(IOException e) { e.printStackTrace(); } - - validateCode(); - exec(); } private void exec() { @@ -210,9 +216,10 @@ public class Interpreter { System.err.println("Error: Variable " + name + " doesn't exist"); System.exit(1); } else if(m_vars.get(name) == 0) { - System.err - .println("Error: Variable " + name + " cannot be negative"); - System.exit(1); +// System.err +// .println("Error: Variable " + name + " cannot be negative"); +// System.exit(1); + m_vars.put(name, Integer.MAX_VALUE); } else { m_vars.put(name, m_vars.get(name) - 1); } @@ -226,7 +233,7 @@ public class Interpreter { String line; if(splitLine[0].equals("while")) { m_whileDepth++; - whiles.put(m_whileDepth, idx); + m_whiles.put(m_whileDepth, idx); int condition = Integer.parseInt(splitLine[3]); if(isVariableNull(splitLine[1]) @@ -238,12 +245,12 @@ public class Interpreter { } } } else { - line = m_code.get(whiles.get(m_whileDepth)); + line = m_code.get(m_whiles.get(m_whileDepth)); splitLine = splitLine(line); int condition = Integer.parseInt(splitLine[3]); if(m_vars.get(splitLine[1]) != condition) - idx = whiles.get(m_whileDepth); + idx = m_whiles.get(m_whileDepth); else { m_whileDepth--; } diff --git a/wk2/Java/src/jrr1g18/bb/Main.java b/wk2/Java/src/jrr1g18/bb/Main.java index c19932fc21aa1c4831543f05807494c55e6453dc..7d3d9c8af199892ea9b02e2a466d670cc9405644 100644 --- a/wk2/Java/src/jrr1g18/bb/Main.java +++ b/wk2/Java/src/jrr1g18/bb/Main.java @@ -3,8 +3,8 @@ package jrr1g18.bb; public class Main { public static void main(String[] args) { - Interpreter interpreter = new Interpreter(); - interpreter.run("multiply.bb"); + Interpreter interpreter = new Interpreter("negative.bb"); + interpreter.run(); System.out.println(interpreter.variablesToString()); }