Private
Public Access
Added correct use of chunks and stacks
This commit is contained in:
1 parent
92566266ee
commit
c7ab58d0bb
5 files changed
+47
-15
No files matched your search
@@ -70,7 +70,8 @@ namespace Advanced_Software_Engineering {
|
|||||||
pen = new Pen(defaultColor, defaultWidth);
|
pen = new Pen(defaultColor, defaultWidth);
|
||||||
brush = new SolidBrush(defaultColor);
|
brush = new SolidBrush(defaultColor);
|
||||||
fill = false;
|
fill = false;
|
||||||
valueStorage = new ValueStorage();
|
valueStorage.Reset();
|
||||||
|
verbChunks = new List<VerbChunk>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -5,17 +5,24 @@ namespace Advanced_Software_Engineering.Verbs.Flow {
|
|||||||
public class IfChunk : VerbChunk, IVerb {
|
public class IfChunk : VerbChunk, IVerb {
|
||||||
|
|
||||||
IValue conditional;
|
IValue conditional;
|
||||||
|
ValueStorage valueStorage;
|
||||||
|
|
||||||
public IfChunk(IValue conditional) : base() {
|
public IfChunk(ValueStorage valueStorage, IValue conditional) : base() {
|
||||||
this.conditional = conditional;
|
this.conditional = conditional;
|
||||||
|
this.valueStorage = valueStorage;
|
||||||
|
valueStorage.IncreaseStack();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IfChunk(IValue conditional, IVerb oneLineVerb) : base(new List<IVerb> { oneLineVerb }) {
|
public IfChunk(ValueStorage valueStorage, IValue conditional, IVerb oneLineVerb) : base(new List<IVerb> { oneLineVerb }) {
|
||||||
this.conditional = conditional;
|
this.conditional = conditional;
|
||||||
|
this.valueStorage = valueStorage;
|
||||||
|
valueStorage.IncreaseStack();
|
||||||
}
|
}
|
||||||
|
|
||||||
public new void ExecuteVerb() {
|
public new void ExecuteVerb() {
|
||||||
|
valueStorage.IncreaseStack();
|
||||||
if (conditional.ToBool()) base.ExecuteVerb();
|
if (conditional.ToBool()) base.ExecuteVerb();
|
||||||
|
valueStorage.DecreaseStack();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -129,9 +129,11 @@ namespace Advanced_Software_Engineering.Verbs.Value {
|
|||||||
value = value.Replace(op, " " + op + " ");
|
value = value.Replace(op, " " + op + " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool removedFirstChar = false;
|
||||||
value = HelperFunctions.Strip(value);
|
value = HelperFunctions.Strip(value);
|
||||||
if (value[0] == "="[0]) {
|
if (value[0] == "="[0]) {
|
||||||
value = value.Substring(1);
|
value = value.Substring(1);
|
||||||
|
removedFirstChar = true;
|
||||||
}
|
}
|
||||||
// now will definitly look like this:
|
// now will definitly look like this:
|
||||||
// 10 + i or i
|
// 10 + i or i
|
||||||
@@ -165,7 +167,7 @@ namespace Advanced_Software_Engineering.Verbs.Value {
|
|||||||
try {
|
try {
|
||||||
// assignmentStrings will look something like this:
|
// assignmentStrings will look something like this:
|
||||||
// ["i"] or ["10", "+", "i"]
|
// ["i"] or ["10", "+", "i"]
|
||||||
if (assignmentStrings.Length == 1) return ValueFactory.CreateValue(storage, assignmentStrings[0]);
|
if (assignmentStrings.Length == 1 && removedFirstChar) return ValueFactory.CreateValue(storage, assignmentStrings[0]);
|
||||||
else if (assignmentStrings.Length == 3) {
|
else if (assignmentStrings.Length == 3) {
|
||||||
// assignmentStrings looks like this => ["10", "+", "i"]
|
// assignmentStrings looks like this => ["10", "+", "i"]
|
||||||
IValue value1 = ValueFactory.CreateValue(storage, assignmentStrings[0]);
|
IValue value1 = ValueFactory.CreateValue(storage, assignmentStrings[0]);
|
||||||
|
|||||||
+27
-8
@@ -5,28 +5,47 @@ 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 Dictionary<int, Dictionary<string, IValue>> Variables = new Dictionary<int, Dictionary<string, IValue>>();
|
||||||
|
protected List<IValue> VariableList = new List<IValue>();
|
||||||
|
|
||||||
public ValueStorage() {
|
public ValueStorage() {
|
||||||
Reset();
|
Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset() {
|
public void Reset() {
|
||||||
Variables.Clear();
|
//for every ivalue
|
||||||
|
Variables = new Dictionary<int, Dictionary<string, IValue>>();
|
||||||
|
//for (int index = 0; index < VariableList.Count; index++) VariableList[index] = null;
|
||||||
SetStack(0);
|
SetStack(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset(int stack) {
|
|
||||||
Variables[stack].Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetStack(int stack) {
|
public void SetStack(int stack) {
|
||||||
currentStack = stack;
|
currentStack = stack < 0 ? 0 : stack;
|
||||||
if (!Variables.ContainsKey(stack)) Variables.Add(currentStack, new Dictionary<string, IValue>());
|
if (!Variables.ContainsKey(stack)) Variables.Add(currentStack, new Dictionary<string, IValue>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void IncreaseStack() {
|
||||||
|
currentStack++;
|
||||||
|
SetStack(currentStack);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DecreaseStack() {
|
||||||
|
currentStack--;
|
||||||
|
SetStack(currentStack);
|
||||||
|
}
|
||||||
|
|
||||||
public void SetVariable(string name, int stack, IValue value) {
|
public void SetVariable(string name, int stack, IValue value) {
|
||||||
Variables[stack][name] = value;
|
if (stack == 0) Variables[stack][name] = value;
|
||||||
|
else {
|
||||||
|
if (CheckVariableExists(name, stack)) Variables[stack][name] = value;
|
||||||
|
else if (CheckVariableExists(name, 0)) Variables[0][name] = value;
|
||||||
|
else// if(value == null) {
|
||||||
|
Variables[stack][name] = value;
|
||||||
|
//VariableList.Add(Variables[stack][name]);
|
||||||
|
//} else {
|
||||||
|
// throw new Exception("Tried setting a variable that was not declared");
|
||||||
|
//}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetVariable(string name, IValue value) {
|
public void SetVariable(string name, IValue value) {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ namespace Advanced_Software_Engineering {
|
|||||||
public static IVerb MakeVerb(Drawer drawer, string fullCommand) {
|
public static IVerb MakeVerb(Drawer drawer, string fullCommand) {
|
||||||
ValueStorage rootValueStorage = drawer.GetValueStorage();
|
ValueStorage rootValueStorage = drawer.GetValueStorage();
|
||||||
|
|
||||||
|
|
||||||
List<VerbChunk> verbChunks = drawer.verbChunks;
|
List<VerbChunk> verbChunks = drawer.verbChunks;
|
||||||
if (verbChunks.Count == 0) verbChunks.Add(null);
|
if (verbChunks.Count == 0) verbChunks.Add(null);
|
||||||
|
|
||||||
@@ -35,6 +36,7 @@ namespace Advanced_Software_Engineering {
|
|||||||
CommandAndParameterParserResult result = HelperFunctions.CommandAndParameterParser(fullCommand);
|
CommandAndParameterParserResult result = HelperFunctions.CommandAndParameterParser(fullCommand);
|
||||||
|
|
||||||
string command = result.getCommand();
|
string command = result.getCommand();
|
||||||
|
if (command == null) return new NoOp();
|
||||||
string[] commandParameters = result.getParameters();
|
string[] commandParameters = result.getParameters();
|
||||||
if (command == null) throw new Exception("There is no command to process");
|
if (command == null) throw new Exception("There is no command to process");
|
||||||
|
|
||||||
@@ -81,16 +83,16 @@ namespace Advanced_Software_Engineering {
|
|||||||
switch (command) {
|
switch (command) {
|
||||||
case "if":
|
case "if":
|
||||||
if (commandParameters.Length == 1) {
|
if (commandParameters.Length == 1) {
|
||||||
verbChunks.Add(new IfChunk(ValueFactory.CreateValue(rootValueStorage, commandParameters[0])));
|
verbChunks.Add(new IfChunk(rootValueStorage, ValueFactory.CreateValue(rootValueStorage, commandParameters[0])));
|
||||||
chunkDepth = verbChunks.Count - 1;
|
chunkDepth = verbChunks.Count - 1;
|
||||||
currentChunk = verbChunks[chunkDepth];
|
currentChunk = verbChunks[chunkDepth];
|
||||||
parsed = true;
|
parsed = true;
|
||||||
tmpVerb = new NoOp();
|
tmpVerb = new NoOp();
|
||||||
} else if (commandParameters.Length >= 2) {
|
} else if (commandParameters.Length >= 2) { //single line if
|
||||||
string commandAfterIf = fullCommand.Substring(fullCommand.IndexOf(",") + 1); // find the first ,
|
string commandAfterIf = fullCommand.Substring(fullCommand.IndexOf(",") + 1); // find the first ,
|
||||||
IValue conditional = ValueFactory.CreateValue(rootValueStorage, commandParameters[0]); //make the conditional
|
IValue conditional = ValueFactory.CreateValue(rootValueStorage, commandParameters[0]); //make the conditional
|
||||||
IVerb AfterIf = MakeVerb(drawer, commandAfterIf); //make the following command
|
IVerb AfterIf = MakeVerb(drawer, commandAfterIf); //make the following command
|
||||||
tmpVerb = new IfChunk(conditional, AfterIf); //Create the chunk
|
tmpVerb = new IfChunk(rootValueStorage, conditional, AfterIf); //Create the chunk
|
||||||
parsed = true;
|
parsed = true;
|
||||||
} else throw new Exception(command + " has an incorrect number of parameters");
|
} else throw new Exception(command + " has an incorrect number of parameters");
|
||||||
break;
|
break;
|
||||||
@@ -252,6 +254,7 @@ namespace Advanced_Software_Engineering {
|
|||||||
chunkDepth = verbChunks.Count - 1;
|
chunkDepth = verbChunks.Count - 1;
|
||||||
tmpVerb = currentChunk;
|
tmpVerb = currentChunk;
|
||||||
currentChunk = verbChunks[chunkDepth];
|
currentChunk = verbChunks[chunkDepth];
|
||||||
|
rootValueStorage.DecreaseStack();
|
||||||
|
|
||||||
} else throw new Exception(command + " doesn't take parameters");
|
} else throw new Exception(command + " doesn't take parameters");
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in new issue
Block a user