diff --git a/src/JohnnyScript.java b/src/JohnnyScript.java index f4ac742..fcea1a5 100644 --- a/src/JohnnyScript.java +++ b/src/JohnnyScript.java @@ -168,20 +168,38 @@ class RamCode { private ArrayList code; private Map variables; + private Map varLoc; RamCode() { code = new ArrayList<>(); variables = new LinkedHashMap<>(); + varLoc = new LinkedHashMap<>(); } - public void addCode(String input) { + void addCode(String input) { code.add(input); } - public void addVar(String name, int value) throws DuplicateVariableException { + void addVar(String name, int value) throws DuplicateVariableException { if (variables.containsKey(name)) { throw new DuplicateVariableException("Variable cannot be defined twice: " + name); - } else variables.put(name, value); + } else { + variables.put(name, value); + varLoc.put(name, variables.size()); + } + } + + void addVarRef(String input) throws VariableNotInitializedException { + String[] parts = input.split(" "); + assert parts.length == 2; + assert parts[1].contains("#"); + String var = parts[1].replace("#",""); + if (!variables.containsKey(var)) { + throw new VariableNotInitializedException("Variable has not been initialized: " + var); + } else { + int line = varLoc.get(var); + code.add(parts[0] + String.format("%03d",line)); + } } private List initializeZeros(List code) { @@ -240,3 +258,10 @@ class DuplicateVariableException extends Exception { DuplicateVariableException(String message) {super(message);} } + +class VariableNotInitializedException extends Exception { + + VariableNotInitializedException(String message) { + super(message); + } +} diff --git a/test/RamCodeTest.java b/test/RamCodeTest.java index ff07826..001c864 100644 --- a/test/RamCodeTest.java +++ b/test/RamCodeTest.java @@ -20,4 +20,63 @@ public void testInitialize() throws Exception { assertEquals("000",codeList.get(i)); } } + + @Test + public void testAddCode() throws Exception { + RamCode code = new RamCode(); + code.addCode(JohnnyScript.Codes.ADD.codeOrdinal + "001"); + List codeList = code.getCode(); + + assertEquals(JohnnyScript.Codes.JMP.codeOrdinal + "001",codeList.get(0)); + assertEquals(JohnnyScript.Codes.ADD.codeOrdinal + "001",codeList.get(1)); + + for (int i = 2; i < 1000; i++) { + assertEquals("000",codeList.get(i)); + } + } + + @Test + public void testAddVar() throws Exception { + RamCode code = new RamCode(); + code.addVar("tst",5); + + List codeList = code.getCode(); + + assertEquals(JohnnyScript.Codes.JMP.codeOrdinal + "002",codeList.get(0)); + assertEquals("005",codeList.get(1)); + + for (int i = 2; i < 1000; i++) { + assertEquals("000",codeList.get(i)); + } + } + + @Test(expected = VariableNotInitializedException.class) + public void testAddVarRefWithoutInit() throws Exception { + RamCode code = new RamCode(); + code.addVarRef(JohnnyScript.Codes.ADD.codeOrdinal+" #tst"); + } + + @Test(expected = DuplicateVariableException.class) + public void testAddDuplicateVar() throws Exception { + RamCode code = new RamCode(); + code.addVar("tst", 5); + code.addVar("tst", 8); + } + + @Test + public void testAddVarRef() throws Exception { + RamCode code = new RamCode(); + code.addVar("tst",5); + code.addVarRef(JohnnyScript.Codes.ADD.codeOrdinal+" #tst"); + + List codeList = code.getCode(); + + assertEquals(JohnnyScript.Codes.JMP.codeOrdinal + "002",codeList.get(0)); + assertEquals("005",codeList.get(1)); + assertEquals(JohnnyScript.Codes.ADD.codeOrdinal+"001",codeList.get(2)); + + for (int i = 3; i < 1000; i++) { + assertEquals("000",codeList.get(i)); + } + } } \ No newline at end of file