Implemented While loops

This commit is contained in:
Lukasz Baldyga committed 2020-12-03 20:35:18 +00:00
1 parent 229ce3ca56
commit 40754aae69
4 files changed
+35 -3

No files matched your search

@@ -96,6 +96,7 @@
<Compile Include="Verbs\Flow\IfChunk.cs" />
<Compile Include="Verbs\Flow\IVerbChunk.cs" />
<Compile Include="Verbs\Flow\MethodChunk.cs" />
<Compile Include="Verbs\Flow\WhileChunk.cs" />
<Compile Include="Verbs\Value\ValueObjects\PointValue.cs" />
<Compile Include="Verbs\Value\ValueStorage.cs" />
<Compile Include="Verbs\Value\ValueTypes\ComparisonValue.cs" />
@@ -0,0 +1,23 @@
using Advanced_Software_Engineering.Verbs.Value;
using System.Collections.Generic;
namespace Advanced_Software_Engineering.Verbs.Flow {
public class WhileChunk : VerbChunk, IVerbChunk, IVerb {
IValue conditional;
ValueStorage valueStorage;
public WhileChunk(ValueStorage valueStorage, IValue conditional) : base() {
this.conditional = conditional;
this.valueStorage = valueStorage;
valueStorage.IncreaseStack();
}
public new void ExecuteVerb() {
valueStorage.IncreaseStack();
while (conditional.ToBool()) base.ExecuteVerb();
valueStorage.DecreaseStack();
}
}
}
@@ -55,8 +55,6 @@ namespace Advanced_Software_Engineering.Verbs.Value {
}
private IValue Evaluate() {
if (evaluated) return evaluatedValue;
IValue variable1 = this.variable1.Clone();
IValue variable2 = this.variable2.Clone();
@@ -92,7 +90,7 @@ namespace Advanced_Software_Engineering.Verbs.Value {
case "color":
throw new Exception("Colors are immutable");
}
evaluated = true;
return evaluatedValue;
}
@@ -97,6 +97,16 @@ namespace Advanced_Software_Engineering {
} else throw new Exception(command + " has an incorrect number of parameters");
break;
case "while":
if (commandParameters.Length == 1) {
verbChunks.Add(new WhileChunk(rootValueStorage, ValueFactory.CreateValue(rootValueStorage, commandParameters[0])));
chunkDepth = verbChunks.Count - 1;
currentChunk = verbChunks[chunkDepth];
parsed = true;
tmpVerb = new NoOp();
} else throw new Exception(command + " has an incorrect number of parameters");
break;
case "def":
if (drawer.verbChunkGeneratingMethod) throw new Exception("Cannot declare a method while declaring method");
else if (chunkDepth != 0) throw new Exception("Cannot declare a method above root chunk"); //TODO, explain better