Ejemplo n.º 1
0
            public bool HasConstantValue(FunctionSet set, FunctionParams pars, out double val)
            {
                // We have constant value if and only if subelement is constant.
                double[] v     = new double[arguments.Length];
                int      index = 0;

                foreach (IElement el in arguments)
                {
                    if (el.HasConstantValue(set, pars, out v[index]))
                    {
                        continue;
                    }
                    val = 0.0;
                    return(false);
                }

                // We find delegate
                Delegate s = null;

                switch (arguments.Length)
                {
                case 1:
                    s = set.Findd(Name);
                    break;

                case 2:
                    s = set.Find2d(Name);
                    break;

                case 3:
                    s = set.Find3d(Name);
                    break;

                case 4:
                    s = set.Find4d(Name);
                    break;

                default:
                    throw new InvalidMathExpression("Trying to call method with more then 4 arguments, unsuported.");
                }

                if (s == null)
                {
                    throw new InvalidMathExpression("We cannot compile expression because the function " +
                                                    Name + " does not exist in set.");
                }

                // We execute it.
                val = (double)s.DynamicInvoke(v[0]);
                return(true);
            }
Ejemplo n.º 2
0
            public void GenerateDouble(ILGenerator generator, FunctionSet set, FunctionParams pars)
            {
                // We check if we have const value expression.
                double constValue;

                if (HasConstantValue(set, pars, out constValue))
                {
                    generator.Emit(OpCodes.Ldc_R8, constValue);
                    return;
                }

                foreach (IElement el in arguments)
                {
                    el.GenerateDouble(generator, set, pars);
                }

                // We perform the call of this function. The function is prototyped
                // float F(float x).
                Delegate s = null;

                switch (arguments.Length)
                {
                case 1:
                    s = set.Findd(Name);
                    break;

                case 2:
                    s = set.Find2d(Name);
                    break;

                case 3:
                    s = set.Find3d(Name);
                    break;

                case 4:
                    s = set.Find4d(Name);
                    break;

                default:
                    throw new InvalidMathExpression("Trying to call method with more then 4 arguments, unsuported.");
                }
                if (s == null)
                {
                    throw new InvalidMathExpression("We cannot compile expression because the function " +
                                                    Name + " does not exist in set.");
                }

                // Call the method with argument.
                generator.EmitCall(OpCodes.Call, s.Method, null);
            }