diff --git a/Advanced Software Engineering/Advanced Software Engineering/Advanced Software Engineering.csproj b/Advanced Software Engineering/Advanced Software Engineering/Advanced Software Engineering.csproj index dd9c7cd..40bbaf0 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/Advanced Software Engineering.csproj +++ b/Advanced Software Engineering/Advanced Software Engineering/Advanced Software Engineering.csproj @@ -93,6 +93,7 @@ + diff --git a/Advanced Software Engineering/Advanced Software Engineering/Verbs/Flow/ForChunk.cs b/Advanced Software Engineering/Advanced Software Engineering/Verbs/Flow/ForChunk.cs new file mode 100644 index 0000000..7373b44 --- /dev/null +++ b/Advanced Software Engineering/Advanced Software Engineering/Verbs/Flow/ForChunk.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 { 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(); + } + } +} diff --git a/Advanced Software Engineering/Advanced Software Engineering/Verbs/VerbFactory.cs b/Advanced Software Engineering/Advanced Software Engineering/Verbs/VerbFactory.cs index d8d200f..4999d80 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/Verbs/VerbFactory.cs +++ b/Advanced Software Engineering/Advanced Software Engineering/Verbs/VerbFactory.cs @@ -111,6 +111,22 @@ namespace Advanced_Software_Engineering { } else throw new Exception(command + " has an incorrect number of parameters"); 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": 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