Skip to content

Commit

Permalink
adding math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EarliestFall988 committed Dec 4, 2023
1 parent 9c8879b commit 341650a
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 2 deletions.
41 changes: 41 additions & 0 deletions State Machine/Function Definitions/Math/AddNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Diagnostics;

namespace Avalon
{
public class AddNumber : FunctionDefinition
{

public AddNumber()
{
Setup();
DefineFunction();
}

void Setup()
{
ExpectedParameters = new Dictionary<string, ReferenceTuple>()
{
{ "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;
};
}

}
}
51 changes: 51 additions & 0 deletions State Machine/Function Definitions/Math/DivideNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Diagnostics;

namespace Avalon
{
/// <summary>
/// Divide Number Function Definition
/// </summary>
public class DivideNumber : FunctionDefinition
{


public DivideNumber()
{
Setup();
DefineFunction();
}

void Setup()
{
ExpectedParameters = new Dictionary<string, ReferenceTuple>()
{
{ "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;
};
}

}
}
43 changes: 43 additions & 0 deletions State Machine/Function Definitions/Math/MultiplyNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Diagnostics;

namespace Avalon
{
/// <summary>
/// Multiply Number Function Definition
/// </summary>
public class MultiplyNumber : FunctionDefinition
{

public MultiplyNumber()
{
Setup();
DefineFunction();
}

void Setup()
{
ExpectedParameters = new Dictionary<string, ReferenceTuple>()
{
{ "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;
};
}
}
}
45 changes: 45 additions & 0 deletions State Machine/Function Definitions/Math/Pow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Diagnostics;

namespace Avalon
{
/// <summary>
/// Power Number Function Definition
/// </summary>
public class PowNumber : FunctionDefinition
{

public PowNumber()
{
Setup();
DefineFunction();
}

void Setup()
{
ExpectedParameters = new Dictionary<string, ReferenceTuple>()
{
{ "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;
};
}
}
}
43 changes: 43 additions & 0 deletions State Machine/Function Definitions/Math/SubtractNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Diagnostics;

namespace Avalon
{
/// <summary>
/// Subtact Number Function Definition
/// </summary>
public class SubtractNumber : FunctionDefinition
{

public SubtractNumber()
{
Setup();
DefineFunction();
}

void Setup()
{
ExpectedParameters = new Dictionary<string, ReferenceTuple>()
{
{ "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;
};
}
}
}
24 changes: 23 additions & 1 deletion State Machine/FunctionLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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

}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions State Machine/KeyTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public void SetValue(string value)
{
this.Value = value;
}

public void SetValue(decimal value)
{
this.Value = value.ToString();
}
}

/// <summary>
Expand Down
12 changes: 11 additions & 1 deletion State Machine/ReferenceTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 341650a

Please sign in to comment.