Implemented For loops

This commit is contained in:
Lukasz Baldyga committed 2020-12-04 01:39:32 +00:00
1 parent 39dc69d7b1
commit 7d9b53f80c
3 files changed
+46

No files matched your search

@@ -93,6 +93,7 @@
<Compile Include="Verbs\DrawingVerbs\Shape\Square.cs" /> <Compile Include="Verbs\DrawingVerbs\Shape\Square.cs" />
<Compile Include="Verbs\DrawingVerbs\Shape\Triangle.cs" /> <Compile Include="Verbs\DrawingVerbs\Shape\Triangle.cs" />
<Compile Include="Verbs\Flow\DeclareMethod.cs" /> <Compile Include="Verbs\Flow\DeclareMethod.cs" />
<Compile Include="Verbs\Flow\ForChunk.cs" />
<Compile Include="Verbs\Flow\IfChunk.cs" /> <Compile Include="Verbs\Flow\IfChunk.cs" />
<Compile Include="Verbs\Flow\IVerbChunk.cs" /> <Compile Include="Verbs\Flow\IVerbChunk.cs" />
<Compile Include="Verbs\Flow\MethodChunk.cs" /> <Compile Include="Verbs\Flow\MethodChunk.cs" />
@@ -0,0 +1,29 @@
using Advanced_Software_Engineering.Verbs.Value;
using System.Collections.Generic;
namespace Advanced_Software_Engineering.Verbs.Flow {
public class ForChunk : VerbChunk, IVerbChunk, IVerb {
private readonly IValue conditional;
private readonly ValueStorage valueStorage;
public ForChunk(ValueStorage valueStorage, IValue conditional) : base() {
this.conditional = conditional;
this.valueStorage = valueStorage;
valueStorage.IncreaseStack();
}
public ForChunk(ValueStorage valueStorage, IValue conditional, IVerb oneLineVerb) : base(new List<IVerb> { oneLineVerb }) {
this.conditional = conditional;
this.valueStorage = valueStorage;
valueStorage.IncreaseStack();
}
public new void ExecuteVerb() {
int iterations = conditional.ToInt();
valueStorage.IncreaseStack();
for (; iterations > 0; iterations--) base.ExecuteVerb();
valueStorage.DecreaseStack();
}
}
}
@@ -111,6 +111,22 @@ namespace Advanced_Software_Engineering {
} else throw new Exception(command + " has an incorrect number of parameters"); } else throw new Exception(command + " has an incorrect number of parameters");
break; break;
case "for":
if (commandParameters.Length == 1) {
verbChunks.Add(new ForChunk(rootValueStorage, ValueFactory.CreateValue(rootValueStorage, commandParameters[0])));
chunkDepth = verbChunks.Count - 1;
currentChunk = verbChunks[chunkDepth];
parsed = true;
tmpVerb = new NoOp();
} else if (commandParameters.Length >= 2) { //single line if
string commandAfterIf = fullCommand.Substring(fullCommand.IndexOf(",") + 1); // find the first ,
IValue conditional = ValueFactory.CreateValue(rootValueStorage, commandParameters[0]); //make the conditional
IVerb AfterIf = MakeVerb(drawer, commandAfterIf); //make the following command
tmpVerb = new ForChunk(rootValueStorage, conditional, AfterIf); //Create the chunk
parsed = true;
} else throw new Exception(command + " has an incorrect number of parameters");
break;
case "def": case "def":
if (drawer.verbChunkGeneratingMethod) throw new Exception("Cannot declare a method while declaring method"); 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 else if (chunkDepth != 0) throw new Exception("Cannot declare a method above root chunk"); //TODO, explain better