Example #1
0
        protected void VisitFun(ASTFunNode node)
        {
            IPostfixMathCommand pFMC = node.GetPFMC();

            if (pFMC == null)
            {
                throw new EvaluationException("No function class associated with " + node.GetName());
            }
            if (pFMC is ICallbackEvaluation)
            {
                object item = ((ICallbackEvaluation)pFMC).Evaluate(node, this);
                if (this._trapNaN && (((item is double) && double.IsNaN((double)item)) || ((item is float) && float.IsNaN((float)item))))
                {
                    throw new EvaluationException("NaN value detected for result of function/operator " + node.GetName());
                }
                if (this._trapInfinity && (((item is double) && double.IsInfinity((double)item)) || ((item is float) && float.IsInfinity((float)item))))
                {
                    throw new EvaluationException("Infinite value " + item.ToString() + "detected for result of function/operator " + node.GetName());
                }
                this.stack.Push(item);
            }
            else
            {
                int n = node.JjtGetNumChildren();
                for (int i = 0; i < n; i++)
                {
                    INode node2 = node.JjtGetChild(i);
                    try
                    {
                        node2.JjtAccept(this, null);
                    }
                    catch (JepException exception)
                    {
                        throw new EvaluationException("", exception);
                    }
                }
                int numberOfParameters = pFMC.GetNumberOfParameters();
                if ((numberOfParameters != -1) && (numberOfParameters != n))
                {
                    throw new EvaluationException(string.Concat(new object[] {
                        "Incorrect number of children ",
                        n,
                        ". Should have been ",
                        numberOfParameters
                    }));
                }
                pFMC.SetCurNumberOfParameters(n);
                pFMC.Run(this.stack);
                object obj3 = this.stack.Peek();
                if (this._trapNaN && (((obj3 is double) && double.IsNaN((double)obj3)) || ((obj3 is float) && float.IsNaN((float)obj3))))
                {
                    throw new EvaluationException("NaN value detected for result of function/operator " + node.GetName());
                }
                if (this._trapInfinity && (((obj3 is double) && double.IsInfinity((double)obj3)) || ((obj3 is float) && float.IsInfinity((float)obj3))))
                {
                    throw new EvaluationException("Infinite value " + obj3.ToString() + "detected for result of function/operator " + node.GetName());
                }
            }
        }
Example #2
0
        protected double VisitFunction(ASTFunNode node)
        {
            IPostfixMathCommand pFMC = node.GetPFMC();

            if (pFMC == null)
            {
                throw new EvaluationException("PostfixMathCommand for " + node.GetName() + " not found");
            }
            if (pFMC is ICallbackEvaluation)
            {
                return(FromObject(((ICallbackEvaluation)pFMC).Evaluate(node, this)));
            }
            switch (node.JjtGetNumChildren())
            {
            case 0:
                if (pFMC is IRealNullaryFunction)
                {
                    return(((IRealNullaryFunction)pFMC).Evaluate());
                }
                if (pFMC is IRealNaryFunction)
                {
                    return(((IRealNaryFunction)pFMC).Evaluate(new double[0]));
                }
                pFMC.SetCurNumberOfParameters(0);
                pFMC.Run(this.stack);
                return(FromObject(this.stack.Pop()));

            case 1:
            {
                double val = this.Visit(node.JjtGetChild(0));
                if (pFMC is IRealUnaryFunction)
                {
                    return(((IRealUnaryFunction)pFMC).Evaluate(val));
                }
                if (pFMC is IRealNaryFunction)
                {
                    return(((IRealNaryFunction)pFMC).Evaluate(new double[] {
                            val
                        }));
                }
                this.stack.Push(new JepDouble(val));
                pFMC.SetCurNumberOfParameters(1);
                pFMC.Run(this.stack);
                return(FromObject(this.stack.Pop()));
            }

            case 2:
            {
                double l = this.Visit(node.JjtGetChild(0));
                double r = this.Visit(node.JjtGetChild(1));
                if (pFMC is IRealBinaryFunction)
                {
                    return(((IRealBinaryFunction)pFMC).Evaluate(l, r));
                }
                if (pFMC is IRealNaryFunction)
                {
                    return(((IRealNaryFunction)pFMC).Evaluate(new double[] {
                            l,
                            r
                        }));
                }
                this.stack.Push(new JepDouble(l));
                this.stack.Push(new JepDouble(r));
                pFMC.SetCurNumberOfParameters(2);
                pFMC.Run(this.stack);
                return(FromObject(this.stack.Pop()));
            }
            }
            double[] parameters = this.VisitChildren(node);
            if (pFMC is IRealNaryFunction)
            {
                return(((IRealNaryFunction)pFMC).Evaluate(parameters));
            }
            for (int i = 0; i < parameters.Length; i++)
            {
                this.stack.Push(new JepDouble(parameters[i]));
            }
            pFMC.SetCurNumberOfParameters(parameters.Length);
            pFMC.Run(this.stack);
            return(FromObject(this.stack.Pop()));
        }