mirror of
https://github.com/0xJ1M/MathsEngine.git
synced 2026-06-04 23:20:08 +00:00
46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using MathEngine.AST.Nodes;
|
|
using MathEngine.Tokenizer;
|
|
using System.Linq.Expressions;
|
|
namespace MathEngine.AST
|
|
{
|
|
/// <summary>
|
|
/// Represents an Abstract Syntax tree for expresison evaluation
|
|
/// </summary>
|
|
internal class ExpressionTree
|
|
{
|
|
/// <summary>
|
|
/// The root node of the expression tree;
|
|
/// </summary>
|
|
private readonly BaseNode rootNode;
|
|
|
|
|
|
/// <summary>
|
|
/// Initialises a new instance of the MathEngine.Parser.Parser.Node class with a given Token
|
|
/// <param name="value">The token for the nodes value</param>
|
|
/// </summary>
|
|
/// <param name="expression">String representing the Mathematical expression</param>
|
|
public ExpressionTree(string expression)
|
|
{
|
|
rootNode = this.FromString(expression);
|
|
}
|
|
|
|
public ExpressionTree(BaseNode rootNode)
|
|
{
|
|
this.rootNode = rootNode;
|
|
}
|
|
|
|
|
|
private BaseNode FromString(string expression)
|
|
{
|
|
List<Token> tokens = ExpressionTokenizer.Tokenize(expression);
|
|
Queue<Token> rpnForm = Parser.Parser.Parse(tokens);
|
|
return TreeGenerator.TreeFromRPN(rpnForm);
|
|
}
|
|
|
|
public ExpressionTree Evaluate()
|
|
{
|
|
return new ExpressionTree(rootNode.Evaluate());
|
|
}
|
|
}
|
|
}
|