Exemplo n.º 1
0
        private TreeNode ParseLogicalOperator()
        {
            Token token = GetCurrentToken();

            LogicalOperatorNode.Operators op = LogicalOperatorNode.Operators.Unknown;
            switch (token.Type)
            {
            case Token.Types.Equal: op = LogicalOperatorNode.Operators.Equal; break;

            case Token.Types.Greater: op = LogicalOperatorNode.Operators.Greater; break;

            case Token.Types.Less: op = LogicalOperatorNode.Operators.Less; break;

            case Token.Types.GreaterEqual: op = LogicalOperatorNode.Operators.GreaterEqual; break;

            case Token.Types.LessEqual: op = LogicalOperatorNode.Operators.LessEqual; break;
            }

            LogicalOperatorNode node = new LogicalOperatorNode(op);

            node.RightNode = PopNode();
            node.LeftNode  = PopNode();

            nodeStack.Push(node);
            return(node);
        }
Exemplo n.º 2
0
        private void ParseLogicalWord()
        {
            var word = SqlTokenHelper.Extract(_tokenType, _token);
            var node = new LogicalOperatorNode(word);

            AppendNode(node);
            _nodeStack.Push(node);
        }
Exemplo n.º 3
0
        public object VisitLogicalOperatorNode(LogicalOperatorNode node, Context parameter)
        {
            if (parameter.IsAvailable)
            {
                var wordNode = node.WordNode;
                wordNode.Accept(this, parameter);
            }

            foreach (var child in node.Children)
            {
                child.Accept(this, parameter);
            }
            return(null);
        }
Exemplo n.º 4
0
        public object Visit(LogicalOperatorNode node)
        {
            var lhs = Visit((dynamic)node.Left);
            var rhs = Visit((dynamic)node.Right);

            switch (node.Token.Type)
            {
            case TokenType.And:
                return(lhs && rhs);

            default:
                return(lhs || rhs);
            }
        }
Exemplo n.º 5
0
        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");
        }