/// <summary> /// Take each token string from the list of postfixedtokens and build a parse tree /// with each node evaluated when possible. /// </summary> internal void AppendFunction(IToken token) { int numberOfChildLeafs = 0; FunctionToken functToken = null; switch (token.Type) { case TokenType.function: functToken = (FunctionToken)token; numberOfChildLeafs = functToken.numberOfChildren; break; case TokenType.infixOperator: numberOfChildLeafs = 2; break; case TokenType.suffixOperator: numberOfChildLeafs = 1; break; } //TODO: In the parser you must keep count of the amount of children and pass that number here //also have restrictions in certain cases string tokenString = token.TokenString; TreeNode child = new TreeNode(); child.type = nodeType.operation; child.name = tokenString; TreeNode[] childLeafNodes = new TreeNode[numberOfChildLeafs]; for (int i = 0; i < numberOfChildLeafs; i++) { if (children.Count() == 0) { throw new Exception("Post fixed token notation error."); } childLeafNodes[i] = children[0]; children.RemoveAt(0); child.children.Insert(0, childLeafNodes[i]); } if (childLeafNodes.All(i => i.numericalEvaluation)) { child.numericalEvaluation = true; List <Complex> paramaters = childLeafNodes.Select(i => i.val).ToList(); if (token.Type == TokenType.suffixOperator || token.Type == TokenType.infixOperator) { child.val = postFixedOperatorEvaluator(paramaters, tokenString); } else if (token.Type == TokenType.function) { //TODO: You should be evaluating parse trees not Complex values child.val = functToken.Function.Compute(paramaters); } else { throw new Exception("Not operator or function can't evaluate"); } } flattenTieredAddOrMult(child); children.Insert(0, child); }
/// <summary> /// Take each token string from the list of postfixedtokens and build a parse tree /// with each node evaluated when possible. /// </summary> internal void AppendFunction(IToken token) { int numberOfChildLeafs =0; FunctionToken functToken= null; switch (token.Type) { case TokenType.function: functToken = (FunctionToken)token; numberOfChildLeafs = functToken.numberOfChildren; break; case TokenType.infixOperator: numberOfChildLeafs = 2; break; case TokenType.suffixOperator: numberOfChildLeafs = 1; break; } //TODO: In the parser you must keep count of the amount of children and pass that number here //also have restrictions in certain cases string tokenString = token.TokenString; TreeNode child = new TreeNode(); child.type = nodeType.operation; child.name = tokenString; TreeNode[] childLeafNodes = new TreeNode[numberOfChildLeafs]; for (int i = 0; i < numberOfChildLeafs; i++) { if (children.Count() == 0) throw new Exception("Post fixed token notation error."); childLeafNodes[i] = children[0]; children.RemoveAt(0); child.children.Insert(0, childLeafNodes[i]); } if (childLeafNodes.All(i => i.numericalEvaluation)) { child.numericalEvaluation = true; List<Complex> paramaters = childLeafNodes.Select(i => i.val).ToList(); if (token.Type == TokenType.suffixOperator || token.Type == TokenType.infixOperator) child.val = postFixedOperatorEvaluator(paramaters, tokenString); else if (token.Type == TokenType.function) { //TODO: You should be evaluating parse trees not Complex values child.val = functToken.Function.Compute(paramaters); } else throw new Exception("Not operator or function can't evaluate"); } flattenTieredAddOrMult(child); children.Insert(0, child); }