Administrator approval is now required for registering new accounts. If you are registering a new account, and are external to the University, please ask the repository owner to contact ServiceLine to request your account be approved. Repository owners must include the newly registered email address, and specific repository in the request for approval.

Verified Commit 31e77e27 authored by Emily Rowlands's avatar Emily Rowlands
Browse files

Added integer underflow

parent 4d8d17c6
clear x;
decr x;
\ No newline at end of file
......@@ -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--;
}
......
......@@ -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());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment