mirror of
https://github.com/0xJ1M/MathsEngine.git
synced 2026-06-05 00:10:08 +00:00
86 lines
4.0 KiB
C#
86 lines
4.0 KiB
C#
using MathEngine.Parser.Parser.Nodes;
|
|
using MathEngine.Parser.Tokeniser;
|
|
namespace MathEngine.Parser.Parser
|
|
{
|
|
/// <summary>
|
|
/// Factory that creates a TreeNode
|
|
/// </summary>
|
|
internal class NodeFactory
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
/// Creates a binary TreeNode, that is a node with a root value and two children
|
|
/// </summary>
|
|
/// <param name="CurrentToken">The token to be the root node of the TreeNode</param>
|
|
/// <param name="LeftToken">TreeNode that is the left branch of the current node</param>
|
|
/// <param name="RightToken">TreeNode that is the right branch of the current node</param>
|
|
/// <returns>A TreeNode with CurrentToken as the root value and LeftBranch and RightBranch as Children</returns>
|
|
public static BaseNode CreateBinaryNode(Token CurrentToken, BaseNode LeftBranch, BaseNode RightBranch)
|
|
{
|
|
switch (CurrentToken.Token_Type)
|
|
{
|
|
case Token.Type.Addition:
|
|
return new BinaryNode(LeftBranch, RightBranch, (a, b) => a + b);
|
|
case Token.Type.Subtraction:
|
|
return new BinaryNode(LeftBranch, RightBranch, (a, b) => a - b);
|
|
case Token.Type.Multiplication:
|
|
return new BinaryNode(LeftBranch, RightBranch, (a, b) => a * b);
|
|
case Token.Type.Division:
|
|
return new BinaryNode(LeftBranch, RightBranch, (a, b) => a / b);
|
|
case Token.Type.Exponentiation:
|
|
return new BinaryNode(LeftBranch, RightBranch, (a, b) => a ^ b);
|
|
default:
|
|
throw new NotImplementedException("Attempted to create a BinaryNode with an invalid operation!");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a Function Node, that is a node that operates a number of arguments
|
|
/// </summary>
|
|
/// <param name="FunctionToken">The function to create the node for</param>
|
|
/// <param name="ArgumentStack">Reference to Stack of BaseNode objects to take the arguments from</param>
|
|
/// <returns></returns>
|
|
public static BaseNode CreateFunctionNode(Token FunctionToken, Stack<BaseNode> ArgumentStack)
|
|
{
|
|
throw new NotImplementedException("Not implemented at this time");
|
|
/* Check that the function exists, if it does we then check that the number of arguments
|
|
* for the function is are avaliable on the ArgumentStack. If so we can construct the Node.
|
|
* Otherwise, either the function does not exist or there is an argument mismatch */
|
|
/*try
|
|
{
|
|
functionAction = GetFunction(FunctionToken, out int functionArity);
|
|
if (functionArity >= ArgumentStack.Count)
|
|
{
|
|
throw new ArgumentException("Number of required arguments is greater than avaliable arugments");
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
throw;
|
|
}*/
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a Node that holds a numerical value
|
|
/// </summary>
|
|
/// <param name="CurrentToken">The token that holds the numeric value</param>
|
|
/// <returns></returns>
|
|
public static BaseNode CreateNumericNode(Token CurrentToken)
|
|
{
|
|
switch (CurrentToken.NumericalType)
|
|
{
|
|
case Token.NumericType.Integer:
|
|
throw new NotImplementedException("Integer Numbers are not implemented at this time");
|
|
case Token.NumericType.Decimal:
|
|
return new NumericNode<decimal>(Decimal.Parse(CurrentToken.TokenValue));
|
|
case Token.NumericType.Complex:
|
|
throw new NotImplementedException("Complex Numbers are not implemented at this time");
|
|
default:
|
|
throw new InvalidDataException("Attempted to create a NumericNode with non numeric data!");
|
|
}
|
|
}
|
|
}
|
|
}
|