Commit fd344426 authored by jm15g21's avatar jm15g21
Browse files

Update Interpreter.java

parent ed537859
...@@ -4,21 +4,21 @@ import Exceptions.InvalidIdentifierException; ...@@ -4,21 +4,21 @@ import Exceptions.InvalidIdentifierException;
import Exceptions.NegativeVariableException; import Exceptions.NegativeVariableException;
import Managers.IManager; import Managers.IManager;
import Managers.LoopManager; import Managers.LoopManager;
import Managers.ScopeManager;
import Operations.*; import Operations.*;
import jdk.nashorn.internal.runtime.Scope;
import java.io.BufferedReader; 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.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable; import java.util.Hashtable;
public class Interpreter { public class Interpreter {
private final boolean SIMPLIFIED_VIEW = false; private final boolean SIMPLIFIED_VIEW = true;
//A store of all the variables
private Hashtable<String, Integer> variableStore = new Hashtable<>();
public Interpreter() { public Interpreter() {
setupManagers(); setupManagers();
...@@ -101,6 +101,8 @@ public class Interpreter { ...@@ -101,6 +101,8 @@ public class Interpreter {
public void readProgram(String fileName) { public void readProgram(String fileName) {
//Clear out old instructions //Clear out old instructions
instructions.clear(); instructions.clear();
//Initiate the scope
getScopeManager().enterLocalScope();
//Create the stuff required to read a file //Create the stuff required to read a file
BufferedReader bufferedReader; BufferedReader bufferedReader;
//Create the file reader and buffered reader. //Create the file reader and buffered reader.
...@@ -135,7 +137,22 @@ public class Interpreter { ...@@ -135,7 +137,22 @@ public class Interpreter {
} }
public Hashtable<String, Integer> getVariableStore() { public Hashtable<String, Integer> getVariableStore() {
return variableStore; ScopeManager manager = getScopeManager();
return manager.getAllVariables();
}
public void modifyVariable(String varName, int newValue) {
ScopeManager manager = getScopeManager();
manager.modifyVariable(varName, newValue);
}
public void removeVariable(String varName) {
ScopeManager manager = getScopeManager();
manager.removeVariable(varName);
}
public ScopeManager getScopeManager() {
return (ScopeManager) getManager(ScopeManager.class);
} }
//======================= //=======================
...@@ -212,6 +229,7 @@ public class Interpreter { ...@@ -212,6 +229,7 @@ public class Interpreter {
private void setupManagers() { private void setupManagers() {
managerList = new Hashtable<>(); managerList = new Hashtable<>();
addManager(new LoopManager()); addManager(new LoopManager());
addManager(new ScopeManager());
} }
private void addManager(IManager manager) { private void addManager(IManager manager) {
...@@ -230,9 +248,14 @@ public class Interpreter { ...@@ -230,9 +248,14 @@ public class Interpreter {
if(SIMPLIFIED_VIEW) if(SIMPLIFIED_VIEW)
{ {
System.out.println("\n" + getCurrentInstruction().getIdentifier() + "(" + String.join(", ", getCurrentInstruction().getArguments()) + ")"); System.out.println("\n" + getCurrentInstruction().getIdentifier() + "(" + String.join(", ", getCurrentInstruction().getArguments()) + ")");
for (String variableName: variableStore.keySet()) { int i = 0;
int value = variableStore.get(variableName); for (Hashtable<String, Integer> localVariables : getScopeManager().getScopedVariables())
System.out.print(variableName + ": " + value + " | "); {
for (String variableName: localVariables.keySet()) {
int value = localVariables.get(variableName);
System.out.print("(" + i + ") " + variableName + ": " + value + " | ");
}
i ++;
} }
return; return;
} }
...@@ -250,8 +273,9 @@ public class Interpreter { ...@@ -250,8 +273,9 @@ public class Interpreter {
System.out.println("==========================="); System.out.println("===========================");
System.out.println(" VARIABLES"); System.out.println(" VARIABLES");
System.out.println("==========================="); System.out.println("===========================");
for (String variableName: variableStore.keySet()) { //Handle scope
int value = variableStore.get(variableName); for (String variableName: getVariableStore().keySet()) {
int value = getVariableStore().get(variableName);
System.out.println(variableName + ": " + value); System.out.println(variableName + ": " + value);
} }
System.out.println("==========================="); System.out.println("===========================");
......
Supports Markdown
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