Files
MathsEngine/MathEngine/MathEngine/Parser/Nodes/BaseNode.cs
0xJ1M f837311da8 Feature/implement exponentiation (#6)
* Modified System to make each numeric node a non-generic for now.

Implemented decimal exponentation. This is a draft implementation and is not intended for final release

* Reverted change to NumericNode and fixed tests
2023-09-19 20:44:34 +01:00

137 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace MathEngine.Parser.Parser
{
/// <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();
}
}