mirror of
https://github.com/0xJ1M/MathsEngine.git
synced 2026-06-05 01:00:06 +00:00
130 lines
5.1 KiB
C#
130 lines
5.1 KiB
C#
namespace MathEngine.AST.Nodes
|
|
{
|
|
/// <summary>
|
|
/// Abstract class representing a Node in a Tree structure
|
|
/// </summary>
|
|
internal abstract class BaseNode
|
|
{
|
|
/// <summary>
|
|
/// Reference to any child Nodes
|
|
/// </summary>
|
|
protected List<BaseNode>? Children;
|
|
|
|
/// <summary>
|
|
/// Base method for adding two nodes, returning another BaseNode object
|
|
/// </summary>
|
|
/// <param name="otherNode">The node to be added to the current instance</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
protected virtual BaseNode Add(BaseNode otherNode)
|
|
{
|
|
throw new InvalidOperationException("Attempted to call BaseNode _add, which is invalid!");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base operator for adding two Base Nodes
|
|
/// </summary>
|
|
/// <param name="lhs">The left BaseNode</param>
|
|
/// <param name="rhs">The right BaseNode</param>
|
|
/// <returns>The addition of two BaseNodes, as defined by the calling type</returns>
|
|
public static BaseNode operator +(BaseNode lhs, BaseNode rhs)
|
|
{
|
|
return lhs.Add(rhs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base method for subtracting two nodes, returning another BaseNode object
|
|
/// </summary>
|
|
/// <param name="otherNode">The node to be subtract from the current instance</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
protected virtual BaseNode Subtract(BaseNode otherNode)
|
|
{
|
|
throw new InvalidOperationException("Attempted to call BaseNode Subtract, which is invalid!");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base operator for subtracting two Base Nodes
|
|
/// </summary>
|
|
/// <param name="lhs">The left BaseNode</param>
|
|
/// <param name="rhs">The right BaseNode</param>
|
|
/// <returns>The subtraction of two BaseNodes, as defined by the calling type</returns>
|
|
public static BaseNode operator -(BaseNode lhs, BaseNode rhs)
|
|
{
|
|
return lhs.Subtract(rhs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base method for multiplying two nodes, returning another BaseNode object
|
|
/// </summary>
|
|
/// <param name="otherNode">The node multiply the current instance by</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
protected virtual BaseNode Multiply(BaseNode otherNode)
|
|
{
|
|
throw new InvalidOperationException("Attempted to call BaseNode Multiply, which is invalid!");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base operator for multiplying two Base Nodes
|
|
/// </summary>
|
|
/// <param name="lhs">The left BaseNode</param>
|
|
/// <param name="rhs">The right BaseNode</param>
|
|
/// <returns>The product of two BaseNodes, as defined by the calling type</returns>
|
|
public static BaseNode operator *(BaseNode lhs, BaseNode rhs)
|
|
{
|
|
return lhs.Multiply(rhs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base method for dividing two nodes, returning another BaseNode object
|
|
/// </summary>
|
|
/// <param name="otherNode">The node which will divide the current instance</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
protected virtual BaseNode Divide(BaseNode otherNode)
|
|
{
|
|
throw new InvalidOperationException("Attempted to call BaseNode _add, which is invalid!");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base operator for dividing two Base Nodes
|
|
/// </summary>
|
|
/// <param name="lhs">The left BaseNode</param>
|
|
/// <param name="rhs">The right BaseNode</param>
|
|
/// <returns>The division of two BaseNodes, as defined by the calling type</returns>
|
|
public static BaseNode operator /(BaseNode lhs, BaseNode rhs)
|
|
{
|
|
return lhs.Divide(rhs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base method for exponentiating two nodes, returning another BaseNode object
|
|
/// </summary>
|
|
/// <param name="otherNode">The node which will be the power to raise the current instance to</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
protected virtual BaseNode Exponentiate(BaseNode otherNode)
|
|
{
|
|
throw new InvalidOperationException("Attempted to call BaseNode _exponentiate, which is invalid!");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base operator for exponentiating two Base Nodes
|
|
/// </summary>
|
|
/// <param name="lhs">The left BaseNode</param>
|
|
/// <param name="rhs">The right BaseNode</param>
|
|
/// <returns>The exponentiating of the two BaseNodes, as defined by the calling type</returns>
|
|
public static BaseNode operator ^(BaseNode lhs, BaseNode rhs)
|
|
{
|
|
return lhs.Exponentiate(rhs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Abstract Base method that evaluates the current Node
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public abstract BaseNode Evaluate();
|
|
}
|
|
}
|