private TreeNode ParseArithmeticOperator() { Token token = GetCurrentToken(); ArithmeticOperatorNode.Operators op = ArithmeticOperatorNode.Operators.Unknown; switch (token.Type) { case Token.Types.Addition: op = ArithmeticOperatorNode.Operators.Addition; break; case Token.Types.Subtraction: op = ArithmeticOperatorNode.Operators.Subtraction; break; case Token.Types.Multiplication: op = ArithmeticOperatorNode.Operators.Multiplication; break; case Token.Types.Division: op = ArithmeticOperatorNode.Operators.Division; break; case Token.Types.Remainder: op = ArithmeticOperatorNode.Operators.Remainder; break; case Token.Types.Power: op = ArithmeticOperatorNode.Operators.Power; break; } ArithmeticOperatorNode node = new ArithmeticOperatorNode(op); node.RightNode = PopNode(); node.LeftNode = PopNode(); nodeStack.Push(node); return(node); }
private static void Print(TreeNode Node, int Indent, StringBuilder Builder) { ++Indent; if (Node is NumberNode) { PrintLine(((NumberNode)Node).Number.ToString(), Indent, Builder); } else if (Node is ParameterNode) { PrintLine(((ParameterNode)Node).Name, Indent, Builder); } else if (Node is FunctionNode) { FunctionNode node = (FunctionNode)Node; PrintLine(node.Name + " (", Indent, Builder); TreeNodeCollection nodes = node.Parameters; for (int i = 0; i < nodes.Count; ++i) { Print(nodes[i], Indent + 1, Builder); } PrintLine(" )", Indent, Builder); } else if (Node is LogicalOperatorNode) { LogicalOperatorNode node = (LogicalOperatorNode)Node; Print(node.LeftNode, Indent, Builder); PrintLine(node.Operator.ToString(), Indent, Builder); Print(node.RightNode, Indent, Builder); } else if (Node is ArithmeticOperatorNode) { ArithmeticOperatorNode node = (ArithmeticOperatorNode)Node; Print(node.LeftNode, Indent, Builder); PrintLine(node.Operator.ToString(), Indent, Builder); Print(node.RightNode, Indent, Builder); } else { throw new System.Exception("Unknown TreeNode type"); } }
public static double Calculate(TreeNode Node, ArgumentsMap Arguments) { if (Node is NumberNode) { return(((NumberNode)Node).Number); } else if (Node is ParameterNode) { string parameterName = ((ParameterNode)Node).Name; if (!Arguments.ContainsKey(parameterName)) { throw new System.Exception("Parameter [" + parameterName + "] doesn't supplied"); } return(Arguments[parameterName]); } else if (Node is FunctionNode) { FunctionNode node = (FunctionNode)Node; return(PredefinedFunctions.Calculate(node.Name, node.Parameters, Arguments)); } else if (Node is LogicalOperatorNode) { LogicalOperatorNode node = (LogicalOperatorNode)Node; double leftValue = Calculate(node.LeftNode, Arguments); double rightValue = Calculate(node.RightNode, Arguments); switch (node.Operator) { case LogicalOperatorNode.Operators.Equal: return(leftValue == rightValue ? 1 : 0); case LogicalOperatorNode.Operators.Greater: return(leftValue > rightValue ? 1 : 0); case LogicalOperatorNode.Operators.Less: return(leftValue < rightValue ? 1 : 0); case LogicalOperatorNode.Operators.GreaterEqual: return(leftValue >= rightValue ? 1 : 0); case LogicalOperatorNode.Operators.LessEqual: return(leftValue <= rightValue ? 1 : 0); default: throw new System.Exception("Unknown Logical Operator type"); } } else if (Node is ArithmeticOperatorNode) { ArithmeticOperatorNode node = (ArithmeticOperatorNode)Node; double leftValue = Calculate(node.LeftNode, Arguments); double rightValue = Calculate(node.RightNode, Arguments); switch (node.Operator) { case ArithmeticOperatorNode.Operators.Addition: return(leftValue + rightValue); case ArithmeticOperatorNode.Operators.Subtraction: return(leftValue - rightValue); case ArithmeticOperatorNode.Operators.Multiplication: return(leftValue * rightValue); case ArithmeticOperatorNode.Operators.Division: return(leftValue / rightValue); case ArithmeticOperatorNode.Operators.Remainder: return(leftValue % rightValue); case ArithmeticOperatorNode.Operators.Power: return(System.Math.Pow(leftValue, rightValue)); default: throw new System.Exception("Unknown Arithmetic Operator type"); } } throw new System.Exception("Unknown TreeNode type"); }