internal void AppendNumber(double tokenVal) { TreeNode child = new TreeNode(); child.type = nodeType.number; child.val = new Value(tokenVal, Restrictions.none); child.name = tokenVal.ToString(); child.numericalEvaluation = true; children.Insert(0, child); //clear the static visualization output string: output = string.Empty; }
/// <summary> /// Take each token string from the list of postfixedtokens and build a parse tree /// with each node evaluated when possible. /// </summary> internal void AppendOperator(string tokenString) { //Will always take an operation and append two numbers as children //This is necessitated by the postfixed token construction const int numberOfChildLeafs = 2; //For primitive operations, this number is 2. For functions its variable TreeNode child = new TreeNode(); child.type = nodeType.operation; child.name = tokenString; TreeNode[] childLeafNodes = new TreeNode[numberOfChildLeafs]; for (int i = 0; i < numberOfChildLeafs; i++) { childLeafNodes[i] = children[0]; children.RemoveAt(0); child.children.Insert(0, childLeafNodes[i]); } if (childLeafNodes.All(i => i.numericalEvaluation)) { child.numericalEvaluation = true; child.val = new Value( postFixedOperatorEvaluator(childLeafNodes.Select(i => i.val.deciValue).ToList(), tokenString), new Factors(childLeafNodes.Select(i => i.val.factors).ToList()), Restrictions.none); } flattenTieredAddOrMult(child); children.Insert(0, child); }
internal void AppendVariable(string p) { if (p == "ans") { TreeNode child = new TreeNode(); child.type = nodeType.number; double tokenVal = OutputLog.returnValues.Last().deciValue; child.val = new Value(tokenVal, Restrictions.none); child.name = tokenVal.ToString(); child.numericalEvaluation = true; children.Insert(0, child); //clear the static visualization output string: output = string.Empty; } else throw new Exception("unknown variable"); }
/// <summary> /// For commutative operations its possible to have one operation node with many children /// instead of tiered iterations of the operation. This method is to change from the /// latter to the former.</summary> private void flattenTieredAddOrMult(TreeNode node) { TreeNode adjustedNode = new TreeNode(); switch (node.name) { case "+": for (int i = 0; i < node.children.Count(); i++) { if (node.children[i].name == "+") { adjustedNode = node.children[i]; node.children.RemoveAt(i); foreach (TreeNode t in adjustedNode.children) { node.children.Add(t); } } } break; case "*": List<int> commonFactors = new List<int>(); for(int i=0; i < node.children.Count(); i++){ if (node.children[i].name == "*") { adjustedNode = node.children[i]; node.children.RemoveAt(i); foreach (TreeNode t in adjustedNode.children) { node.children.Add(t); } } } if (this.numericalEvaluation) { //this means that all the children are numbers and we have evaluated //TODO: take out the common factors from val.factors //Make it work by using exponents } break; } }
public TreeNode BuildParseTree() { TreeNode parseTree = new TreeNode(); foreach (Token token in tokens) { switch (token.TokenType) { case TokenType.number: parseTree.AppendNumber(token.TokenNumValue); break; case TokenType.arithmeticOp: parseTree.AppendOperator(token.TokenString); break; case TokenType.variable: parseTree.AppendVariable(token.TokenString); break; default: throw new Exception("This token type cannot be appended to the parse tree"); } } //The root node is always redundant by construction return parseTree.children.First(); }