Esempio n. 1
0
        public IPostfixMathCommand Remove(string key)
        {
            IPostfixMathCommand command = (IPostfixMathCommand)this.table[key];

            this.table.Remove(key);
            return(command);
        }
Esempio n. 2
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());
                }
            }
        }
Esempio n. 3
0
        public INode Match(Lookahead2Enumerator <Token> it, IGrammarParser parser)
        {
            Token token = it.PeekNext();

            if (token == null)
            {
                return(null);
            }
            if (!token.IsFunction())
            {
                return(null);
            }
            string source            = token.GetSource();
            IPostfixMathCommand pfmc = ((FunctionToken)token).GetPfmc();

            if (!this.open.Equals(it.PeekNextnext()))
            {
                return(null);
            }
            it.Consume();
            it.Consume();
            if (this.close.Equals(it.PeekNext()))
            {
                if (!pfmc.CheckNumberOfParameters(0))
                {
                    throw new Colosoft.Text.Jep.ParseException("Function " + pfmc + " invalid number of arguments 0");
                }
                it.Consume();
                return(this.nf.BuildFunctionNode(source, pfmc, new INode[0]));
            }
            List <INode> list = new List <INode>();

            while (true)
            {
                INode item = parser.ParseSubExpression();
                list.Add(item);
                if (this.close.Equals(it.PeekNext()))
                {
                    it.Consume();
                    if (!pfmc.CheckNumberOfParameters(list.Count))
                    {
                        throw new Colosoft.Text.Jep.ParseException(string.Concat(new object[] {
                            "Function ",
                            pfmc,
                            " invalid number of arguments ",
                            list.Count
                        }));
                    }
                    return(this.nf.BuildFunctionNode(source, pfmc, list.ToArray()));
                }
                if (!this.comma.Equals(it.PeekNext()))
                {
                    break;
                }
                it.Consume();
            }
            throw new Colosoft.Text.Jep.ParseException("Closing bracket not found. Next token is " + it.PeekNext());
        }
Esempio n. 4
0
 public Operator(string name, string symbol, IPostfixMathCommand pfmc, int flags)
 {
     this.precedence  = -1;
     this.distribOver = new Operator[0];
     this.name        = name;
     this.pfmc        = pfmc;
     this.symbol      = symbol;
     this.flags       = flags;
 }
        public override Token BuildToken(string s)
        {
            IPostfixMathCommand function = this.ft.GetFunction(s);

            if (function == null)
            {
                return(new IdentifierToken(s));
            }
            return(new FunctionToken(s, function));
        }
Esempio n. 6
0
        public ASTConstant BuildConstantNode(IPostfixMathCommand pfmc, INode[] children)
        {
            object obj2;
            INode  node = this.BuildFunctionNode("tmpfun", pfmc, children);

            try
            {
                obj2 = this.ev.Eval(node);
            }
            catch (EvaluationException exception)
            {
                throw new Colosoft.Text.Jep.ParseException("", exception);
            }
            return(this.BuildConstantNode(obj2));
        }
Esempio n. 7
0
        public ASTFunNode BuildFunctionNode(string name, IPostfixMathCommand pfmc, INode[] arguments)
        {
            if (!pfmc.CheckNumberOfParameters(arguments.Length))
            {
                throw new Colosoft.Text.Jep.ParseException(string.Concat(new object[] {
                    "Incorrect number of parameters ",
                    arguments.Length,
                    " for ",
                    name,
                    " "
                }));
            }
            ASTFunNode node = new ASTFunNode(JJTFUNNODE);

            node.SetFunction(name, pfmc);
            this.CopyChildren(node, arguments);
            return(node);
        }
Esempio n. 8
0
 public bool ContainsValue(IPostfixMathCommand value)
 {
     return(this.table.ContainsValue(value));
 }
Esempio n. 9
0
 public IPostfixMathCommand AddFunction(string name, IPostfixMathCommand pfmc)
 {
     this.table.Add(name, pfmc);
     return(pfmc);
 }
Esempio n. 10
0
 public Operator(string name, IPostfixMathCommand pfmc, int flags, int precedence) : this(name, pfmc, flags)
 {
     this.precedence = precedence;
 }
Esempio n. 11
0
 public void SetPFMC(IPostfixMathCommand pfmc)
 {
     this.pfmc = pfmc;
 }
Esempio n. 12
0
 public RhsTernaryOperator(string name, string symbol, IPostfixMathCommand pfmc, int flags, int precedence) : base(name, symbol, pfmc, flags, precedence)
 {
 }
Esempio n. 13
0
 public TernaryOperator(string name, string lhsSymbol, string rhsSymbol, IPostfixMathCommand pfmc, int flags, int precedence) : base(name, lhsSymbol, pfmc, flags, precedence)
 {
     this.symbol2 = rhsSymbol;
     this.rhs     = new RhsTernaryOperator(name, rhsSymbol, pfmc, flags, precedence);
     this.rhs.SetLhsOp(this);
 }
Esempio n. 14
0
 public IPostfixMathCommand AddFunction(string name, IPostfixMathCommand pfmc)
 {
     return(this._funTab.AddFunction(name, pfmc));
 }
Esempio n. 15
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()));
        }
Esempio n. 16
0
 public FunctionToken(string s, IPostfixMathCommand pfmc) : base(s)
 {
     this.pfmc = pfmc;
 }
Esempio n. 17
0
 public void SetFunction(string name_in, IPostfixMathCommand pfmc_in)
 {
     this.name = name_in;
     this.pfmc = pfmc_in;
 }