Esempio n. 1
0
        /// <summary>
        /// Deal with a constant expression. Exactly how this is dealt with depends on the value. We process
        /// the most important ones directly, and the MEF-off the others. :-)
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        protected override Expression VisitConstant(ConstantExpression expression)
        {
            if (expression.Type == typeof(int) ||
                expression.Type == typeof(uint))
            {
                _result = new ValSimple(expression.Value.ToString(), expression.Type, null);
            }
            else if (expression.Type == typeof(float) ||
                     expression.Type == typeof(double))
            {
                // Make sure it is a legal expression.
                if ((expression.Type == typeof(double) && (double.IsNaN((double)expression.Value) || double.IsInfinity((double)expression.Value))) ||
                    (expression.Type == typeof(float) && (float.IsNaN((float)expression.Value) || float.IsInfinity((float)expression.Value))))
                {
                    throw new ArgumentException($"Can't translate {expression.Value.ToString()} to C++. Illegal number.");
                }

                var s = expression.Value.ToString();
                if (!s.Contains("."))
                {
                    s += ".0";
                }
                _result = new ValSimple(s, expression.Type, null);
            }
            else if (expression.Type == typeof(bool))
            {
                if ((bool)expression.Value)
                {
                    _result = new ValSimple("true", typeof(bool), null);
                }
                else
                {
                    _result = new ValSimple("false", typeof(bool), null);
                }
            }
            else if (expression.Type == typeof(string))
            {
                _result = new ConstantString(expression.Value as string);
            }
            else if (expression.Value == null)
            {
                _result = new ValSimple("0", expression.Type, null);
            }
            else
            {
                _result = TypeHandlers.ProcessConstantReference(expression, _codeEnv, MEFContainer);
            }

            return(expression);
        }