Used to parse cast expressions
Esempio n. 1
0
        ///
        /// <summary>
        ///		Applies numeric negation (i.e. unary minus) to numeric values
        /// </summary>
        ///
        /// <param name="obj">The CseObject with the value to negate</param>
        ///
        /// <returns>The CseObject after negation</returns>
        ///
        /// <exception cref="CseLogicExceptionType.CANT_NEGATE_NON_NUM" />
        /// <exception cref="CseLogicExceptionType.ERROR_NEGATING_VALUE_OF_TYPE" />
        ///
        internal static CseObject Negate(CseObject obj)
        {
            CseObject result = (CseObject)obj.Clone();

            dynamic value = obj.Value;
            double  numValue;

            if (value is string)
            {
                throw new CseLogicException(CseLogicExceptionType.CANT_NEGATE_NON_NUM, value.ToString());
            }
            else if (!double.TryParse(value.ToString(), out numValue))
            {
                MethodInfo mi = value.GetType().GetMethod(OpOverloadNames.UMINUS);
                if (null != mi)
                {
                    result.Value = value.GetType().InvokeMember(OpOverloadNames.UMINUS, OpOverloadNames.Flags, null, CsEval.evalEnvironment, new object[] { value });
                    return(result);
                }
                else
                {
                    throw new CseLogicException(CseLogicExceptionType.CANT_NEGATE_NON_NUM, value.ToString());
                }
            }

            numValue    *= -1;
            result.Value = numValue;

            try {
                result = CastExp.Parse(CsEval.evalEnvironment, value.GetType().Name, result);
            }
            catch {
                if (value.ToString().ToLower().Contains("e"))
                {
                    result = LiteralExp.ParseEType(numValue.ToString());
                }
                else if (value.ToString().Contains("."))
                {
                    result = LiteralExp.ParseFloatType(numValue.ToString(), null);
                }
                else
                {
                    throw new CseLogicException(CseLogicExceptionType.ERROR_NEGATING_VALUE_OF_TYPE, value.ToString(), value.GetType().Name);
                }
            }

            return(result);
        }
Esempio n. 2
0
        ///
        /// <summary>
        ///		Parses method calls
        /// </summary>
        ///
        /// <param name="environment">The environment containing the method</param>
        /// <param name="methName">Name of the method</param>
        /// <param name="args">CseObject array containing arguments to be sent to the method. Each CseObject is one argument</param>
        ///
        /// <returns>CseObject containing the return result of the method call or CseObject containing null if method is void</returns>
        ///
        /// <exception cref="CseLogicExceptionType.METHOD_CALL_AMBIGUOUS" />
        /// <exception cref="CseLogicExceptionType.METHOD_CANT_IMPLICITLY_COERCE_ARGS" />
        /// <exception cref="CseLogicExceptionType.METHOD_DOESNT_EXIST" />
        /// <exception cref="CseLogicExceptionType.METHOD_EXISTS_BUT_CANT_BE_INVOKED" />
        ///
        public static CseObject Parse(CseObject environment, string methName, List <CseObject> args)
        {
            MethResSettings mrSettings = new MethResSettings()
            {
                Args = (args == null ? new CseObject[] { } : args.ToArray()),
                Env  = environment.Value,
                Name = methName
            };
            List <MethResObject> appMeths = MethRes.GetApplicableMembers(mrSettings);

            if (appMeths.Count == 0)
            {
                mrSettings.IsExtInvocation = true;
                appMeths = MethRes.GetApplicableMembers(mrSettings);
            }

            MethodInfo mi      = null;
            Type       envType = TypeExp.GetTypeObj(environment.Value);

            try {
                switch (appMeths.Count)
                {
                // No methods exist with that name
                case 0:
                    // TODO: doesn't exist OR no applicable methods can be called
                    throw new CseLogicException(CseLogicExceptionType.METHOD_DOESNT_EXIST, methName);

                // Only 1 method exists with that name, so try to do what's necessary to coerce args (if they exist)
                // to match its signature.
                case 1:
                    MethResObject mrObj = appMeths[0];

                    if (args != null)
                    {
                        for (int i = 0; i < args.Count; i++)
                        {
                            try {
                                args[i] = CastExp.Parse(environment, mrObj.MethInfo.GetParameters()[i].ParameterType.Name, args[i]);
                            }
                            catch (CseLogicException) {
                                // f**k it, continue
                            }
                        }
                    }
                    //NarrowAllIntTypes(ref args);

                    mi = mrObj.MethInfo;
                    break;

                // Method is overloaded.
                // Idea is to simulate C#'s best match algorithm for finding the most appropriate method to call
                // and if still unsure, throw exception so user can use casting for further clarification.
                default:
                    Type[] types = GetTypeArray(args);

                    mi = envType.GetMethod(methName, findFlags | BindingFlags.ExactBinding, null, types, null);

                    if (mi != null)
                    {
                        break;
                    }

                    if (CanCoerce(args))
                    {
                        NarrowAllIntTypes(ref args);
                        types = GetTypeArray(args);
                    }

                    MethodInfo tryMeth = envType.GetMethod(methName, findFlags, null, types, null);
                    foreach (MethResObject m in appMeths)
                    {
                        if (m.MethInfo == tryMeth)
                        {
                            mi = tryMeth;
                            break;
                        }
                    }

                    if (mi != null)
                    {
                        break;
                    }

                    // TODO: Attempt to coerce args to formal param types of overloaded methods



                    if (mi == null)
                    {
                        throw new CseLogicException(CseLogicExceptionType.METHOD_CALL_AMBIGUOUS, methName);
                    }

                    break;
                }
            }
            catch (AmbiguousMatchException) {
                throw new CseLogicException(CseLogicExceptionType.METHOD_CALL_AMBIGUOUS, methName);
            }
            catch (CseLogicException) {
                throw;
            }
            catch {
                throw new CseLogicException(CseLogicExceptionType.METHOD_EXISTS_BUT_CANT_BE_INVOKED, methName);
            }

            if (mi == null)
            {
                throw new CseLogicException(CseLogicExceptionType.METHOD_EXISTS_BUT_CANT_BE_INVOKED, methName);
            }
            else
            {
                dynamic result = mi.Invoke(environment.Value, GetObjArgs(args));

                CseObject xo = new CseObject(result);
                xo.CompileTimeType = mi.ReturnType;

                if (args != null)
                {
                    foreach (CseObject arg in args)
                    {
                        if (arg.CallMod != CallArgMod.VAL)
                        {
                            AssignExp.Parse(arg.EnvChain, arg.EnvNames, arg.EnvIndices, arg.Value);
                        }
                    }
                }

                return(xo);
            }
        }