Skip to content

Commit

Permalink
Implemented variable reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Pfister committed Oct 6, 2016
1 parent c1280c1 commit 31c04c1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/JohnnyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,38 @@ class RamCode {

private ArrayList<String> code;
private Map<String, Integer> variables;
private Map<String, Integer> 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<String> initializeZeros(List<String> code) {
Expand Down Expand Up @@ -240,3 +258,10 @@ class DuplicateVariableException extends Exception {

DuplicateVariableException(String message) {super(message);}
}

class VariableNotInitializedException extends Exception {

VariableNotInitializedException(String message) {
super(message);
}
}
59 changes: 59 additions & 0 deletions test/RamCodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}

0 comments on commit 31c04c1

Please sign in to comment.