Changed dictionaries to lists

This commit is contained in:
Lukasz Baldyga committed 2020-12-03 15:09:46 +00:00
1 parent 3413d860fa
commit 2cf9e1a402
1 file changed
+16 -9
@@ -5,33 +5,40 @@ namespace Advanced_Software_Engineering.Verbs.Value {
public class ValueStorage { public class ValueStorage {
protected int currentStack; protected int currentStack;
protected Dictionary<int, Dictionary<string, IValue>> Variables = new Dictionary<int, Dictionary<string, IValue>>(); protected List<Dictionary<string, IValue>> Variables = new List<Dictionary<string, IValue>>();
public ValueStorage() { public ValueStorage() {
Reset(); Reset();
} }
public void Reset() { public void Reset() {
Variables = new Dictionary<int, Dictionary<string, IValue>>(); Variables = new List<Dictionary<string, IValue>>();
SetStack(0); Variables.Add(new Dictionary<string, IValue>());
currentStack = 0;
} }
private void SetStack(int stack) { private void SetStack(int stack) {
if (stack < 0) throw new Exception("Stack cannot be less than 0"); if (stack < 0) throw new Exception("Stack cannot be less than 0");
if (!Variables.ContainsKey(stack)) Variables.Add(currentStack, new Dictionary<string, IValue>()); //Decreased Stack
if(currentStack > stack) {
Variables.RemoveAt(currentStack);
}
//Increased Stack
else if (currentStack < stack) {
Variables.Add(new Dictionary<string, IValue>());
}
currentStack = stack;
} }
public void IncreaseStack() { public void IncreaseStack() {
currentStack++; SetStack(currentStack + 1);
SetStack(currentStack);
} }
public void DecreaseStack() { public void DecreaseStack() {
if (currentStack > 0) { if (currentStack > 0) {
Variables.Remove(currentStack); SetStack(currentStack - 1);
currentStack--;
SetStack(currentStack);
} }
} }