Example #1
0
        private double And(AstNode node)
        {
            var result = Interpret(node.GetChild(1)) == 1 && Interpret(node.GetChild(2)) == 1
            ? 1
            : 0;

            return(result);
        }
Example #2
0
            private static string getStringSubTree(AstNode node,
                                                   string indent, bool root)
            {
                if (node == null)
                {
                    return("");
                }
                string result = indent;

                if (!root)
                {
                    if (node.Index < node.Parent.ChildCount - 1)
                    {
                        result += MiddleNodeChar + " ";
                        indent += ConnectChar + " ";
                    }
                    else
                    {
                        result += LastNodeChar + " ";
                        indent += " ";
                    }
                }
                result += node + "\n";
                for (int i = 0; i < node.ChildCount; i++)
                {
                    result += getStringSubTree(node.GetChild(i), indent, false);
                }
                return(result);
            }
Example #3
0
        /// <summary>
        /// Performs the specified action on an expression tree.
        /// </summary>
        /// <returns>The top node of the results.</returns>
        /// <exception>ApplicationException</exception>
        /// <param name="node">The top node of the tree.</param>
        /// <param name="data">Data passed to the visitor (Typically not used).</param>
        /// <param name="pv">Visitor object that can be used to descend the tree.</param>
        /// <param name="stack">The stack to operate on.</param>
        /// <exception cref="ApplicationException">If operator must have 3 arguments.</exception>
        public virtual object Evaluate( AstNode node, object data, IAstNodeVisitor pv, Stack< double? > stack )
        {
            if ( node.NumChildren != 3 )
                throw new ApplicationException( "If operator must have 3 arguments." );

            AstNode condition = node.GetChild( 0 );
            condition.Accept( pv, data );

            CheckStack( stack );

            double? condVal = stack.Pop();
            if ( condVal.HasValue && ( condVal > 0.0 ) )
            {
                AstNode trueNode = node.GetChild( 1 );
                trueNode.Accept( pv, data );
                return data;
            }

            AstNode falseNode = node.GetChild( 2 );
            falseNode.Accept( pv, data );
            return data;
        }
Example #4
0
        private double GreaterThan(AstNode node)
        {
            var result = Interpret(node.GetChild(1)) > Interpret(node.GetChild(2)) ? 1 : 0;

            return(result);
        }
Example #5
0
        private double Not(AstNode node)
        {
            var result = Interpret(node.GetChild(1)) == 1 ? 0 : 1;

            return(result);
        }
Example #6
0
        private double LessThanOrEqual(AstNode node)
        {
            var result = Interpret(node.GetChild(1)) <= Interpret(node.GetChild(2)) ? 1 : 0;

            return(result);
        }
Example #7
0
        private double Equality(AstNode node)
        {
            var result = Interpret(node.GetChild(1)) == Interpret(node.GetChild(2)) ? 1 : 0;

            return(result);
        }
Example #8
0
        private double Plus(AstNode node)
        {
            var result = Interpret(node.GetChild(1)) + Interpret(node.GetChild(2));

            return(result);
        }
Example #9
0
        private double Multiply(AstNode node)
        {
            var result = Interpret(node.GetChild(1)) * Interpret(node.GetChild(2));

            return(result);
        }
Example #10
0
        private double Divide(AstNode node)
        {
            var result = Interpret(node.GetChild(1)) / Interpret(node.GetChild(2));

            return(result);
        }
Example #11
0
        private double Exponent(AstNode node)
        {
            var result = Math.Pow(Interpret(node.GetChild(1)), Interpret(node.GetChild(2)));

            return(result);
        }
Example #12
0
        private double Modulo(AstNode node)
        {
            var result = Interpret(node.GetChild(1)) % Interpret(node.GetChild(2));

            return(result);
        }