Documented files

This commit is contained in:
Lukasz Baldyga committed 2020-12-04 18:46:47 +00:00
1 parent d854b5c350
commit 12455d3267
12 files changed
+407 -49

No files matched your search

@@ -1,23 +1,40 @@
namespace Advanced_Software_Engineering.Verbs.Flow {
/// <summary>
/// Declares a method. See <see cref="DrawingVerbs.Actions.NoOp"/> because these classes behave similarly
/// </summary>
public class DeclareMethod : IVerb, IVerbChunk {
private readonly string methodName;
private readonly Drawer drawer;
/// <summary>
/// Creates a new DeclareMethod object
/// </summary>
/// <param name="drawer">The drawer is used for getting the method specifically</param>
/// <param name="methodName">The name of the method</param>
public DeclareMethod(Drawer drawer, string methodName) {
this.drawer = drawer;
this.methodName = methodName;
}
/// <summary>
/// Adds a verb to the verb list. More specifically, the verb list of the MethodChunk.
/// See <see cref="MethodChunk"/>
/// </summary>
/// <param name="verb">The verb to add</param>
public void AddVerb(IVerb verb) {
MethodChunk methodChunk = drawer.Methods[methodName];
methodChunk.AddVerb(verb);
}
public void ExecuteVerb() {
//Declaration of method happens at parse time
}
/// <summary>
/// This method quite literally does nothing upon execution because the declaration of method happens at parse time
/// </summary>
public void ExecuteVerb() { }
/// <summary>
/// Gets the description of what the chunk will do
/// </summary>
/// <returns>A description of the chunk's actions</returns>
public string GetDescription() {
return "Declares a method";
}
@@ -1,30 +1,54 @@
using System.Collections.Generic;
namespace Advanced_Software_Engineering.Verbs.Flow {
/// <summary>
/// A simple VerbChunk. Used for storing and executing a bunch of verbs
/// </summary>
public class VerbChunk : IVerbChunk, IVerb {
private readonly List<IVerb> verbs;
/// <summary>
/// Create a new VerbChunk with verbs in it.
/// </summary>
/// <param name="verbs">Verbs to be added to the verb chunk</param>
public VerbChunk(List<IVerb> verbs) {
this.verbs = verbs;
}
/// <summary>
/// Create an empty VerbChunk
/// </summary>
public VerbChunk() {
verbs = new List<IVerb> { };
}
/// <summary>
/// Adds a verb to the verb list
/// </summary>
/// <param name="verb"></param>
public void AddVerb(IVerb verb) {
verbs.Add(verb);
}
/// <summary>
/// Executes all of the verbs in order
/// </summary>
public void ExecuteVerb() {
foreach (IVerb verb in verbs) verb.ExecuteVerb();
}
/// <summary>
/// Returns all of the verbs stored
/// </summary>
/// <returns>the verbs stored</returns>
public IVerb[] GetVerbs() {
return verbs.ToArray();
}
/// <summary>
/// Gets a description of every verb
/// </summary>
/// <returns>The descriptions of every verb</returns>
public string GetDescription() {
string s = "VerbChunk Start";
foreach (IVerb verb in verbs) s += "\n" + verb.GetDescription();
@@ -2,23 +2,39 @@
using System.Collections.Generic;
namespace Advanced_Software_Engineering.Verbs.Flow {
/// <summary>
/// A Chunk that loops over its code
/// </summary>
public class WhileChunk : VerbChunk, IVerbChunk, IVerb {
private readonly IValue conditional;
private readonly ValueStorage valueStorage;
/// <summary>
/// A For Loop object
/// </summary>
/// <param name="valueStorage">The ValueStorage for increasing and decreasing the stack</param>
/// <param name="conditional">Preferably <see cref="Value.ValueTypes.BoolValue"/>, weather the loop should continue to execute.</param>
public WhileChunk(ValueStorage valueStorage, IValue conditional) : base() {
this.conditional = conditional;
this.valueStorage = valueStorage;
valueStorage.IncreaseStack();
}
/// <summary>
/// Create a new if object
/// </summary>
/// <param name="valueStorage">The ValueStorage for increasing and decreasing the stack</param>
/// <param name="conditional">Preferably <see cref="Value.ValueTypes.BoolValue"/>, weather the loop should continue to execute.</param>
/// <param name="oneLineVerb">A verb to execute in a single line. (Probably a bad idea)</param>
public WhileChunk(ValueStorage valueStorage, IValue conditional, IVerb oneLineVerb) : base(new List<IVerb> { oneLineVerb }) {
this.conditional = conditional;
this.valueStorage = valueStorage;
valueStorage.IncreaseStack();
}
/// <summary>
/// Executes every verb in the chunk
/// </summary>
public new void ExecuteVerb() {
valueStorage.IncreaseStack();
while (conditional.ToBool()) base.ExecuteVerb();
@@ -2,7 +2,9 @@
using System;
namespace Advanced_Software_Engineering.Verbs.Value {
/// <summary>
/// Declares a variable
/// </summary>
public class DeclareVariable : IVerb {
private readonly ValueStorage storage;
private readonly IValue value;
@@ -46,7 +48,11 @@ namespace Advanced_Software_Engineering.Verbs.Value {
return true;
}
/// <summary>
/// Creates a new Declare variable
/// </summary>
/// <param name="storage">The storage to use</param>
/// <param name="assignment">The assignment</param>
public DeclareVariable(ValueStorage storage, string assignment) {
this.storage = storage;
@@ -55,7 +61,7 @@ namespace Advanced_Software_Engineering.Verbs.Value {
assignment = assignment.Replace(op, " " + op + " ");
}
// get variable name
// Get variable name
// assignment looks something like this at this point:
// i = 20
// or
@@ -108,15 +114,26 @@ namespace Advanced_Software_Engineering.Verbs.Value {
}
}
/// <summary>
/// Sets the variable to null
/// </summary>
public void ExecuteVerb() {
if (value != null) storage.SetVariable(name, value.Clone());
else storage.SetVariable(name, value);
}
/// <summary>
/// Gets the description of the verb
/// </summary>
/// <returns></returns>
public string GetDescription() {
return "Declares the variable " + name;
}
/// <summary>
/// Gets the name of the variable to assign
/// </summary>
/// <returns></returns>
public string GetName() {
return name;
}
@@ -1,23 +1,56 @@
using System.Drawing;
namespace Advanced_Software_Engineering.Verbs.Value {
/// <summary>
/// A value
/// </summary>
public interface IValue {
/// <summary>
/// The description of the value
/// </summary>
/// <returns>The description of the IValue</returns>
string GetDescription();
/// <summary>
/// Converts the value to an integer
/// </summary>
/// <returns>the integer representation if the IValue</returns>
int ToInt();
/// <summary>
/// Get the double representation if the IValue
/// </summary>
/// <returns>the double representation if the IValue</returns>
double ToDouble();
/// <summary>
/// Get the boolean representation if the IValue
/// </summary>
/// <returns>the boolean representation if the IValue</returns>
bool ToBool();
/// <summary>
/// Get the color representation if the IValue
/// </summary>
/// <returns>the color representation if the IValue</returns>
Color ToColor();
/// <summary>
/// Gets the original type of the IValue
/// </summary>
/// <returns>original type of the IValue</returns>
string GetOriginalType();
/// <summary>
/// Checks if the IValue is initialised
/// </summary>
/// <returns>True if IValue is initialised</returns>
bool isInitialised();
/// <summary>
/// Clones the IValue
/// </summary>
/// <returns></returns>
IValue Clone();
}
}
@@ -1,33 +1,60 @@
using System;
namespace Advanced_Software_Engineering.Verbs.Value {
/// <summary>
/// Used for updating the variables
/// </summary>
public class UpdateVariable : IVerb {
protected ValueStorage storage;
protected string name;
protected IValue value;
private ValueStorage storage;
private string name;
private IValue value;
/// <summary>
/// Initilise the object
/// </summary>
/// <param name="storage">The storage object</param>
/// <param name="name">The name of the variable</param>
/// <param name="value">The value of the variable</param>
private void Init(ValueStorage storage, string name, IValue value) {
this.name = name;
this.value = value;
this.storage = storage;
}
/// <summary>
/// Create a new UpdateVariable object
/// </summary>
/// <param name="storage">The storage object</param>
/// <param name="name">The name of the variable</param>
/// <param name="value">The value of the variable</param>
public UpdateVariable(ValueStorage storage, string name, IValue value) {
Init(storage, name, value);
}
/// <summary>
/// Create a new UpdateVariable object
/// </summary>
/// <param name="storage">The storage object</param>
/// <param name="name">The name of the variable</param>
/// <param name="value">The expression of the value</param>
public UpdateVariable(ValueStorage storage, string name, string value) {
// incoming values look something like this "= 10"
// or "= 10 + i"
Init(storage, name, ValueHelper.ConvertToIValue(value, storage));
}
/// <summary>
/// Executes the verb and updates the variable
/// </summary>
public void ExecuteVerb() {
if (!storage.CheckVariableExists(name)) throw new Exception("Cannot update variable because it is not declared");
storage.SetVariable(name, value.Clone());
}
/// <summary>
/// Gets the description of the update
/// </summary>
/// <returns>A string representation of the description of the update</returns>
public string GetDescription() {
return "Updates " + name + " to " + value;
}
@@ -3,7 +3,9 @@ using Advanced_Software_Engineering.Verbs.Value.ValueTypes;
using System;
namespace Advanced_Software_Engineering.Verbs.Value {
/// <summary>
/// Value factory creates values
/// </summary>
public class ValueFactory {
/// <summary>
@@ -11,7 +13,7 @@ namespace Advanced_Software_Engineering.Verbs.Value {
/// </summary>
/// <param name="value">Text representation of the value</param>
/// <param name="type">What type the value should be</param>
/// <returns></returns>
/// <returns>a new IValue</returns>
public static IValue CreateValue(string value, string type) {
switch (type) {
case "int":
@@ -31,6 +33,16 @@ namespace Advanced_Software_Engineering.Verbs.Value {
}
}
/// <summary>
/// Creates a constant Value to be used later during execution. Only used for Color
/// </summary>
/// <param name="storage">The storage</param>
/// <param name="paramater1">First parameter</param>
/// <param name="paramater2">Second parameter</param>
/// <param name="parameter3">Third parameter</param>
/// <param name="parameter4">Fourth parameter</param>
/// <param name="type">the type</param>
/// <returns>a new IValue</returns>
public static IValue CreateValue(ValueStorage storage, string paramater1, string paramater2, string parameter3, string parameter4, string type) {
switch (type) {
case "color":
@@ -46,6 +58,15 @@ namespace Advanced_Software_Engineering.Verbs.Value {
}
}
/// <summary>
/// Creates a constant Value to be used later during execution. Only used for Color
/// </summary>
/// <param name="storage">The storage</param>
/// <param name="paramater1">First parameter</param>
/// <param name="paramater2">Second parameter</param>
/// <param name="parameter3">Third parameter</param>
/// <param name="type">the type</param>
/// <returns>a new IValue</returns>
public static IValue CreateValue(ValueStorage storage, string paramater1, string paramater2, string parameter3, string type) {
switch (type) {
case "color":
@@ -60,20 +81,41 @@ namespace Advanced_Software_Engineering.Verbs.Value {
}
}
/// <summary>
/// Creates a constant Value to be used later during execution
/// </summary>
/// <param name="value">Text representation of the value</param>
/// <param name="storage">The value storage to check</param>
/// <returns>a new IValue</returns>
public static IValue CreateValue(ValueStorage storage, string value) {
value = HelperFunctions.Strip(value);
if (storage.CheckVariableExists(value)) return new VariableValue(storage, value);
else return ValueHelper.ConvertToIValue(value, storage);
}
/// <summary>
/// Create a new boolean IValue
/// </summary>
/// <param name="b">boolean input</param>
/// <returns><see cref="BoolValue"/></returns>
public static IValue CreateValue(bool b) {
return new BoolValue(b);
}
/// <summary>
/// Create a new double IValue
/// </summary>
/// <param name="d">double input</param>
/// <returns><see cref="DoubleValue"/></returns>
public static IValue CreateValue(double d) {
return new DoubleValue(d);
}
/// <summary>
/// Create a new int IValue
/// </summary>
/// <param name="i">int input</param>
/// <returns><see cref="IntValue"/></returns>
public static IValue CreateValue(int i) {
return new IntValue(i);
}
@@ -104,6 +104,11 @@ namespace Advanced_Software_Engineering.Verbs.Value {
return Color.FromArgb(a, r, g, b);
}
/// <summary>
/// Convert to boolean
/// </summary>
/// <param name="text">A text boolean</param>
/// <returns>boolean representation of the text</returns>
public static bool ConvertToBool(string text) {
switch (text) {
case "true":
@@ -121,6 +126,12 @@ namespace Advanced_Software_Engineering.Verbs.Value {
}
}
/// <summary>
/// Converts the text to IValue
/// </summary>
/// <param name="text">text to convert</param>
/// <param name="storage">storage to use (can be null) (optional)</param>
/// <returns>An IValue representation of the text</returns>
public static IValue ConvertToIValue(string text, ValueStorage storage = null) {
text = HelperFunctions.Strip(text);
string value = text;
@@ -198,9 +209,5 @@ namespace Advanced_Software_Engineering.Verbs.Value {
throw new Exception("Could not convert " + text + " to any type");
}
public static Exception ConvertToExpression(string expression) {
throw new NotImplementedException();
}
}
}
@@ -2,33 +2,43 @@
namespace Advanced_Software_Engineering.Verbs.Value.ValueObjects {
/// <summary>
/// A point IValue. Simply the same as <see cref="Point"/> but with IValues intstead of ints.
/// </summary>
public class PointValue {
private readonly IValue x;
private readonly IValue y;
/// <summary>
/// Create a new PointValue
/// </summary>
/// <param name="x">X IValue</param>
/// <param name="y">Y IValue</param>
public PointValue(IValue x, IValue y) {
this.x = x;
this.y = y;
}
public PointValue(ValueStorage storage, string parameter1, string parameter2) {
x = ValueFactory.CreateValue(storage, parameter1);
y = ValueFactory.CreateValue(storage, parameter2);
}
public PointValue(string parameter1, string parameter2) {
x = ValueFactory.CreateValue(parameter1, "int");
y = ValueFactory.CreateValue(parameter2, "int");
}
/// <summary>
/// Get the X component
/// </summary>
/// <returns>the X component</returns>
public int GetX() {
return x.ToInt();
}
/// <summary>
/// Get the Y component
/// </summary>
/// <returns>the Y component</returns>
public int GetY() {
return y.ToInt();
}
/// <summary>
/// Get the <see cref="Point"/>.
/// </summary>
/// <returns><see cref="Point"/></returns>
public Point GetPoint() {
return new Point(GetX(), GetY());
}
@@ -2,21 +2,41 @@
using System.Collections.Generic;
namespace Advanced_Software_Engineering.Verbs.Value {
/// <summary>
/// The storage for all the values
/// </summary>
public class ValueStorage {
protected int currentStack;
protected List<Dictionary<string, IValue>> Variables = new List<Dictionary<string, IValue>>();
private int currentStack;
/// <summary>
/// The list is structured something like this:
/// <code>
/// {stack, stack, stack stack}
/// </code>
/// Where 'stack' is a dictionary of the names and the values of the variables
/// </summary>
private List<Dictionary<string, IValue>> Variables = new List<Dictionary<string, IValue>>();
/// <summary>
/// Create a new ValueStorage
/// </summary>
public ValueStorage() {
Reset();
}
/// <summary>
/// Reset the ValueStorage
/// </summary>
public void Reset() {
Variables = new List<Dictionary<string, IValue>>();
Variables.Add(new Dictionary<string, IValue>());
currentStack = 0;
}
/// <summary>
/// Set the ValueStorage stack
/// </summary>
/// <param name="stack">stack number</param>
private void SetStack(int stack) {
if (stack < 0) throw new Exception("Stack cannot be less than 0");
@@ -32,16 +52,28 @@ namespace Advanced_Software_Engineering.Verbs.Value {
currentStack = stack;
}
/// <summary>
/// Increase the stack
/// </summary>
public void IncreaseStack() {
SetStack(currentStack + 1);
}
/// <summary>
/// Decrease the stack
/// </summary>
public void DecreaseStack() {
if (currentStack > 0) {
SetStack(currentStack - 1);
}
}
/// <summary>
/// Set a variable
/// </summary>
/// <param name="name">Variable name</param>
/// <param name="stack">the start stack</param>
/// <param name="value">the IValue</param>
private void SetVariable(string name, int stack, IValue value) {
for (int internalStack = stack; internalStack >= 0; internalStack--) {
if (value == null || CheckVariableExists(name, internalStack)) {
@@ -52,14 +84,30 @@ namespace Advanced_Software_Engineering.Verbs.Value {
Variables[stack][name] = value;
}
/// <summary>
/// Set a variable
/// </summary>
/// <param name="name">Variable name</param>
/// <param name="value">the IValue</param>
public void SetVariable(string name, IValue value) {
SetVariable(name, currentStack, value);
}
/// <summary>
/// Gets the variable at the specified stack
/// </summary>
/// <param name="name">variable name</param>
/// <param name="stack">stack</param>
/// <returns>IValue</returns>
private IValue GetVariable(string name, int stack) {
return Variables[stack][name];
}
/// <summary>
/// Gets the variable at the first available stack
/// </summary>
/// <param name="name">the names of the variable</param>
/// <returns>IValue</returns>
public IValue GetVariable(string name) {
for (int stack = currentStack; stack >= 0; stack--) {
if (CheckVariableExists(name, stack) && GetVariable(name, stack) != null) return GetVariable(name, stack);
@@ -67,11 +115,22 @@ namespace Advanced_Software_Engineering.Verbs.Value {
throw new Exception("Cannot find variable " + name);
}
/// <summary>
/// Check if the variable exists at the stack specified
/// </summary>
/// <param name="name">variable name</param>
/// <param name="stack">stack</param>
/// <returns>IValue</returns>
private bool CheckVariableExists(string name, int stack) {
if (name == null) return false;
return Variables[stack].ContainsKey(name);
}
/// <summary>
/// Check if the variable exists in the first available stack
/// </summary>
/// <param name="name">variable name</param>
/// <returns>IValue</returns>
public bool CheckVariableExists(string name) {
if (name == null) return false;
for (int stack = currentStack; stack >= 0; stack--) {
@@ -2,20 +2,41 @@
using System.Drawing;
namespace Advanced_Software_Engineering.Verbs.Value {
/// <summary>
/// Expression values are used when there are two IValues that need to be added or subtracted or anything from each other.
/// </summary>
public class ExpressionValue : IValue {
/// <summary>
/// An operation denoting addition
/// </summary>
public const int ADD = 0;
/// <summary>
/// An operation denoting subtraction
/// </summary>
public const int SUBTRACT = 1;
/// <summary>
/// An operation denoting multiplication
/// </summary>
public const int MULTIPLY = 2;
/// <summary>
/// An operation denoting division
/// </summary>
public const int DIVIDE = 3;
private readonly IValue variable1;
private readonly IValue variable2;
private readonly int operation;
private readonly bool evaluated = false;
private IValue evaluatedValue;
/// <summary>
/// Create a new Expression Value based on the two IValues
/// </summary>
/// <param name="variable1">IValue to perform the operation on</param>
/// <param name="variable2">IValue to perform the operation on</param>
/// <param name="operation">The operation. Can be <see cref="ADD"/>, <see cref="SUBTRACT"/>, <see cref="MULTIPLY"/> or <see cref="DIVIDE"/>.</param>
public ExpressionValue(IValue variable1, IValue variable2, int operation) {
this.variable1 = variable1;
this.variable2 = variable2;
@@ -25,6 +46,12 @@ namespace Advanced_Software_Engineering.Verbs.Value {
if (operation > 3 && operation < 0) throw new Exception("Bad operation");
}
/// <summary>
/// Create a new Expression Value based on the two IValues
/// </summary>
/// <param name="variable1">IValue to perform the operation on</param>
/// <param name="variable2">IValue to perform the operation on</param>
/// <param name="operation">The operation. Can be "+"(<see cref="ADD"/>), "-"(<see cref="SUBTRACT"/>), "*"(<see cref="MULTIPLY"/>) or "/"(<see cref="DIVIDE"/>).</param>
public ExpressionValue(IValue variable1, IValue variable2, string operation) {
switch (operation) {
case "+":
@@ -54,10 +81,14 @@ namespace Advanced_Software_Engineering.Verbs.Value {
if (this.operation > 3 && this.operation < 0) throw new Exception("Bad operation");
}
/// <summary>
/// Evaluates the expression.
/// </summary>
/// <returns>an IValue with the result</returns>
private IValue Evaluate() {
IValue variable1 = this.variable1.Clone();
IValue variable2 = this.variable2.Clone();
IValue evaluatedValue = null;
string outputType = variable1.GetOriginalType();
switch (outputType) {
@@ -94,6 +125,10 @@ namespace Advanced_Software_Engineering.Verbs.Value {
return evaluatedValue;
}
/// <summary>
/// Describes the expression
/// </summary>
/// <returns>A string description of the expression</returns>
public string GetDescription() {
string operation;
switch (this.operation) {
@@ -119,30 +154,58 @@ namespace Advanced_Software_Engineering.Verbs.Value {
return "Expression of the variables " + variable1 + " and " + variable2 + " being " + operation;
}
/// <summary>
/// Get the type of the expression
/// </summary>
/// <returns>"expression"</returns>
public string GetOriginalType() {
return "expression";
}
/// <summary>
/// Expressions are always initialised
/// </summary>
/// <returns>true</returns>
public bool isInitialised() {
return true;
}
/// <summary>
/// Gets the bool of the result
/// </summary>
/// <returns>bool value of the result</returns>
public bool ToBool() {
return Evaluate().ToBool();
}
/// <summary>
/// Gets the color of the result
/// </summary>
/// <returns>color value of the result</returns>
public Color ToColor() {
return Evaluate().ToColor();
}
/// <summary>
/// Gets the double value of the result
/// </summary>
/// <returns>double value of the result</returns>
public double ToDouble() {
return Evaluate().ToDouble();
}
/// <summary>
/// Gets the int of the result
/// </summary>
/// <returns>int value of the result</returns>
public int ToInt() {
return Evaluate().ToInt();
}
/// <summary>
/// Clones the value of the result
/// </summary>
/// <returns>A new IValue</returns>
public IValue Clone() {
return Evaluate();
}
@@ -2,51 +2,94 @@
using System.Drawing;
namespace Advanced_Software_Engineering.Verbs.Value.ValueTypes {
/// <summary>
/// Variable value is responsible for setting up values for <see cref="UpdateVariable"/> to then update
/// </summary>
public class VariableValue : IValue {
private readonly ValueStorage storage;
private readonly string variableName;
/// <summary>
/// The new VariableValue
/// </summary>
/// <param name="storage">the storage to use</param>
/// <param name="variableName">variable name</param>
public VariableValue(ValueStorage storage, string variableName) {
this.variableName = variableName;
if (!storage.CheckVariableExists(variableName)) throw new Exception("Tried to access " + variableName + " before it was assigned. ");
this.storage = storage;
}
private IValue get() {
/// <summary>
/// Get the variable value
/// </summary>
/// <returns><see cref="IValue"/></returns>
private IValue Get() {
return storage.GetVariable(variableName);
}
/// <summary>
/// Gets the description of the IValue
/// </summary>
/// <returns>description of the IValue</returns>
public string GetDescription() {
return get().GetDescription();
return Get().GetDescription();
}
/// <summary>
/// Gets the original type of the IValue
/// </summary>
/// <returns>original type of the IValue</returns>
public string GetOriginalType() {
return get().GetOriginalType();
return Get().GetOriginalType();
}
/// <summary>
/// Get the initialised state of the IValue
/// </summary>
/// <returns>initialised state of the IValue</returns>
public bool isInitialised() {
return get().isInitialised();
return Get().isInitialised();
}
/// <summary>
/// Converts the IValue to a boolean
/// </summary>
/// <returns>Boolean value of the IValue</returns>
public bool ToBool() {
return get().ToBool();
return Get().ToBool();
}
/// <summary>
/// Converts the IValue to a color
/// </summary>
/// <returns>Color value of the IValue</returns>
public Color ToColor() {
return get().ToColor();
return Get().ToColor();
}
/// <summary>
/// Converts the IValue to a double
/// </summary>
/// <returns>Double value of the IValue</returns>
public double ToDouble() {
return get().ToDouble();
return Get().ToDouble();
}
/// <summary>
/// Converts the IValue to an integer
/// </summary>
/// <returns>Int value of the IValue</returns>
public int ToInt() {
return get().ToInt();
return Get().ToInt();
}
/// <summary>
/// Clone the value and return it
/// </summary>
/// <returns>Cloned IValue</returns>
public IValue Clone() {
return get().Clone();
return Get().Clone();
}
}
}