diff --git a/State Machine/Function Definitions/Math/AddNumber.cs b/State Machine/Function Definitions/Math/AddNumber.cs new file mode 100644 index 0000000..f3bd6e3 --- /dev/null +++ b/State Machine/Function Definitions/Math/AddNumber.cs @@ -0,0 +1,41 @@ +using System.Diagnostics; + +namespace Avalon +{ + public class AddNumber : FunctionDefinition + { + + public AddNumber() + { + Setup(); + DefineFunction(); + } + + void Setup() + { + ExpectedParameters = new Dictionary() + { + { "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, + { "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, + { "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) } + }; + } + + protected override void DefineFunction() + { + Name = nameof(AddNumber); + Function = () => + { + var a = Parameters["a"].GetDecimal(); + var b = Parameters["b"].GetDecimal(); + + Debug.WriteLine($"{a} + {b} = {a + b}"); + + Parameters["out"].SetValue(a + b); + + return 1; + }; + } + + } +} diff --git a/State Machine/Function Definitions/Math/DivideNumber.cs b/State Machine/Function Definitions/Math/DivideNumber.cs new file mode 100644 index 0000000..c7a9f9a --- /dev/null +++ b/State Machine/Function Definitions/Math/DivideNumber.cs @@ -0,0 +1,51 @@ +using System.Diagnostics; + +namespace Avalon +{ + /// + /// Divide Number Function Definition + /// + public class DivideNumber : FunctionDefinition + { + + + public DivideNumber() + { + Setup(); + DefineFunction(); + } + + void Setup() + { + ExpectedParameters = new Dictionary() + { + { "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, + { "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, + { "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) } + }; + } + + protected override void DefineFunction() + { + Name = nameof(DivideNumber); + Function = () => + { + var a = Parameters["a"].GetDecimal(); + var b = Parameters["b"].GetDecimal(); + + if (b == 0) + { + Debug.WriteLine("Cannot divide by zero"); + return -1; + } + + Debug.WriteLine($"{a} / {b} = {a / b}"); + + Parameters["out"].SetValue(a / b); + + return 1; + }; + } + + } +} diff --git a/State Machine/Function Definitions/Math/MultiplyNumber.cs b/State Machine/Function Definitions/Math/MultiplyNumber.cs new file mode 100644 index 0000000..350fd19 --- /dev/null +++ b/State Machine/Function Definitions/Math/MultiplyNumber.cs @@ -0,0 +1,43 @@ +using System.Diagnostics; + +namespace Avalon +{ + /// + /// Multiply Number Function Definition + /// + public class MultiplyNumber : FunctionDefinition + { + + public MultiplyNumber() + { + Setup(); + DefineFunction(); + } + + void Setup() + { + ExpectedParameters = new Dictionary() + { + { "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, + { "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, + { "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) } + }; + } + + protected override void DefineFunction() + { + Name = nameof(MultiplyNumber); + Function = () => + { + var a = Parameters["a"].GetDecimal(); + var b = Parameters["b"].GetDecimal(); + + Debug.WriteLine($"{a} * {b} = {a * b}"); + + Parameters["out"].SetValue(a * b); + + return 1; + }; + } + } +} diff --git a/State Machine/Function Definitions/Math/Pow.cs b/State Machine/Function Definitions/Math/Pow.cs new file mode 100644 index 0000000..34bd3b8 --- /dev/null +++ b/State Machine/Function Definitions/Math/Pow.cs @@ -0,0 +1,45 @@ +using System.Diagnostics; + +namespace Avalon +{ + /// + /// Power Number Function Definition + /// + public class PowNumber : FunctionDefinition + { + + public PowNumber() + { + Setup(); + DefineFunction(); + } + + void Setup() + { + ExpectedParameters = new Dictionary() + { + { "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, + { "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, + { "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) } + }; + } + + protected override void DefineFunction() + { + Name = nameof(PowNumber); + Function = () => + { + var a = Parameters["a"].GetDecimal(); + var b = Parameters["b"].GetDecimal(); + + var pow = Math.Pow((double)a, (double)b); + + Debug.WriteLine($"{a} to the power of {b} = {pow}"); + + Parameters["out"].SetValue((decimal)pow); + + return 1; + }; + } + } +} diff --git a/State Machine/Function Definitions/Math/SubtractNumber.cs b/State Machine/Function Definitions/Math/SubtractNumber.cs new file mode 100644 index 0000000..06857af --- /dev/null +++ b/State Machine/Function Definitions/Math/SubtractNumber.cs @@ -0,0 +1,43 @@ +using System.Diagnostics; + +namespace Avalon +{ + /// + /// Subtact Number Function Definition + /// + public class SubtractNumber : FunctionDefinition + { + + public SubtractNumber() + { + Setup(); + DefineFunction(); + } + + void Setup() + { + ExpectedParameters = new Dictionary() + { + { "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, + { "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, + { "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) } + }; + } + + protected override void DefineFunction() + { + Name = nameof(SubtractNumber); + Function = () => + { + var a = Parameters["a"].GetDecimal(); + var b = Parameters["b"].GetDecimal(); + + Debug.WriteLine($"{a} + {b} = {a - b}"); + + Parameters["out"].SetValue(a - b); + + return 1; + }; + } + } +} diff --git a/State Machine/FunctionLibrary.cs b/State Machine/FunctionLibrary.cs index 638f142..23dc3f9 100644 --- a/State Machine/FunctionLibrary.cs +++ b/State Machine/FunctionLibrary.cs @@ -28,7 +28,10 @@ public class FunctionLibrary public void BuildFunctionLibrary() { - //Debug.WriteLine("\t>Gathering Functions...\n"); + Debug.WriteLine("\t>Gathering Functions...\n"); + + #region simple guess game tutorial functions + ContinueFunction continueFunction = new ContinueFunction(); ExitQuestionFunction exitQuestion = new ExitQuestionFunction(); GreetUserFunction greetFunction = new GreetUserFunction(); @@ -41,6 +44,25 @@ public void BuildFunctionLibrary() ImportedFunctions.Add(greetFunction.Name, greetFunction); ImportedFunctions.Add(guessFunction.Name, guessFunction); ImportedFunctions.Add(randomFunction.Name, randomFunction); + + #endregion + + #region math functions + + var addNumber = new AddNumber(); + var subtractNumber = new SubtractNumber(); + var multiplyNumber = new MultiplyNumber(); + var divideNumber = new DivideNumber(); + var powerNumber = new PowNumber(); + + ImportedFunctions.Add(addNumber.Name, addNumber); + ImportedFunctions.Add(subtractNumber.Name, subtractNumber); + ImportedFunctions.Add(divideNumber.Name, divideNumber); + ImportedFunctions.Add(multiplyNumber.Name, multiplyNumber); + ImportedFunctions.Add(powerNumber.Name, powerNumber); + + #endregion + } /// diff --git a/State Machine/KeyTypeDefinition.cs b/State Machine/KeyTypeDefinition.cs index 8c88372..9e8aed0 100644 --- a/State Machine/KeyTypeDefinition.cs +++ b/State Machine/KeyTypeDefinition.cs @@ -91,6 +91,11 @@ public void SetValue(string value) { this.Value = value; } + + public void SetValue(decimal value) + { + this.Value = value.ToString(); + } } /// diff --git a/State Machine/ReferenceTuple.cs b/State Machine/ReferenceTuple.cs index 4424aa1..cee4ff0 100644 --- a/State Machine/ReferenceTuple.cs +++ b/State Machine/ReferenceTuple.cs @@ -14,10 +14,20 @@ public class ReferenceTuple public StateMachineVariableType type { get; set; } public bool applied { get; set; } - public ReferenceTuple(StateMachineVariableType type, bool applied, string description = "") + public VariableIO VariableIO { get; set; } + + public ReferenceTuple(StateMachineVariableType type, bool applied, VariableIO io = VariableIO.In, string description = "") { this.type = type; this.applied = applied; + this.VariableIO = io; } } + + + public enum VariableIO + { + In, + Out + } } \ No newline at end of file