Commit fd344426 authored by jm15g21's avatar jm15g21
Browse files

Update Interpreter.java

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