Skip to content
Snippets Groups Projects
Verified Commit 31e77e27 authored by Emily Rowlands's avatar Emily Rowlands
Browse files

Added integer underflow

parent 4d8d17c6
No related branches found
No related tags found
No related merge requests found
clear x;
decr x;
\ No newline at end of file
...@@ -4,8 +4,10 @@ import java.io.BufferedReader; ...@@ -4,8 +4,10 @@ import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
...@@ -13,13 +15,14 @@ import java.util.regex.Matcher; ...@@ -13,13 +15,14 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class Interpreter { public class Interpreter {
private Map<String, Integer> m_vars = new HashMap<>(); private Map<String, Integer> m_vars = new HashMap<>();
private Map<Integer, String> m_code = new HashMap<>(); private List<String> m_code = new ArrayList<>();
private Map<Integer, Integer> whiles = new HashMap<>(); private Map<Integer, Integer> m_whiles = new HashMap<>();
private int m_lineCount = 0; private int m_lineCount = 0;
private int m_whileDepth = 0; private int m_whileDepth = 0;
private final String m_filename;
private static final Map<InstructionType, String> INSTRUCTION_NAMES; private static final Map<InstructionType, String> INSTRUCTION_NAMES;
static { static {
...@@ -36,7 +39,7 @@ public class Interpreter { ...@@ -36,7 +39,7 @@ public class Interpreter {
INCR, DECR, CLEAR, WHILE, END, INCR, DECR, CLEAR, WHILE, END,
} }
private InstructionType stringToInstructionType(String instruction) { private static InstructionType stringToInstructionType(String instruction) {
for(Entry<InstructionType, String> entry : INSTRUCTION_NAMES for(Entry<InstructionType, String> entry : INSTRUCTION_NAMES
.entrySet()) { .entrySet()) {
if(Objects.equals(instruction, entry.getValue())) { if(Objects.equals(instruction, entry.getValue())) {
...@@ -56,8 +59,14 @@ public class Interpreter { ...@@ -56,8 +59,14 @@ public class Interpreter {
return stripWhitespace(m.group(1)); return stripWhitespace(m.group(1));
} }
public void run(String fileName) { public Interpreter(String fileName) {
readCode(fileName); m_filename = fileName;
}
public void run() {
readCode();
validateCode();
exec();
} }
public String variablesToString() { public String variablesToString() {
...@@ -147,12 +156,12 @@ public class Interpreter { ...@@ -147,12 +156,12 @@ public class Interpreter {
} }
} }
private void readCode(String nameOfTheFile) { private void readCode() {
try(BufferedReader br = try(BufferedReader reader =
new BufferedReader(new FileReader(nameOfTheFile))) { new BufferedReader(new FileReader(m_filename))) {
String line; String line;
while((line = br.readLine()) != null) { while((line = reader.readLine()) != null) {
m_code.put(m_lineCount, line); m_code.add(line);
m_lineCount++; m_lineCount++;
} }
} catch(FileNotFoundException e) { } catch(FileNotFoundException e) {
...@@ -160,9 +169,6 @@ public class Interpreter { ...@@ -160,9 +169,6 @@ public class Interpreter {
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
validateCode();
exec();
} }
private void exec() { private void exec() {
...@@ -210,9 +216,10 @@ public class Interpreter { ...@@ -210,9 +216,10 @@ public class Interpreter {
System.err.println("Error: Variable " + name + " doesn't exist"); System.err.println("Error: Variable " + name + " doesn't exist");
System.exit(1); System.exit(1);
} else if(m_vars.get(name) == 0) { } else if(m_vars.get(name) == 0) {
System.err // System.err
.println("Error: Variable " + name + " cannot be negative"); // .println("Error: Variable " + name + " cannot be negative");
System.exit(1); // System.exit(1);
m_vars.put(name, Integer.MAX_VALUE);
} else { } else {
m_vars.put(name, m_vars.get(name) - 1); m_vars.put(name, m_vars.get(name) - 1);
} }
...@@ -226,7 +233,7 @@ public class Interpreter { ...@@ -226,7 +233,7 @@ public class Interpreter {
String line; String line;
if(splitLine[0].equals("while")) { if(splitLine[0].equals("while")) {
m_whileDepth++; m_whileDepth++;
whiles.put(m_whileDepth, idx); m_whiles.put(m_whileDepth, idx);
int condition = Integer.parseInt(splitLine[3]); int condition = Integer.parseInt(splitLine[3]);
if(isVariableNull(splitLine[1]) if(isVariableNull(splitLine[1])
...@@ -238,12 +245,12 @@ public class Interpreter { ...@@ -238,12 +245,12 @@ public class Interpreter {
} }
} }
} else { } else {
line = m_code.get(whiles.get(m_whileDepth)); line = m_code.get(m_whiles.get(m_whileDepth));
splitLine = splitLine(line); splitLine = splitLine(line);
int condition = Integer.parseInt(splitLine[3]); int condition = Integer.parseInt(splitLine[3]);
if(m_vars.get(splitLine[1]) != condition) if(m_vars.get(splitLine[1]) != condition)
idx = whiles.get(m_whileDepth); idx = m_whiles.get(m_whileDepth);
else { else {
m_whileDepth--; m_whileDepth--;
} }
......
...@@ -3,8 +3,8 @@ package jrr1g18.bb; ...@@ -3,8 +3,8 @@ package jrr1g18.bb;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Interpreter interpreter = new Interpreter(); Interpreter interpreter = new Interpreter("negative.bb");
interpreter.run("multiply.bb"); interpreter.run();
System.out.println(interpreter.variablesToString()); System.out.println(interpreter.variablesToString());
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment