Beispiel #1
0
        /// <summary>
        /// Converts from c# datatypes to fluentscript datatypes inside
        /// </summary>
        /// <param name="val"></param>
        public static LObject ConvertToLangLiteral(Token token)
        {
            if (token.Type == TokenTypes.Null)
            {
                return(LObjects.Null);
            }

            var type = token.Type;
            var kind = token.Kind;

            if (type == TokenTypes.LiteralNumber)
            {
                return(new LNumber(Convert.ToDouble(token.Value)));
            }

            if (type == TokenTypes.LiteralString)
            {
                return(new LString(Convert.ToString(token.Value)));
            }

            if (type == TokenTypes.LiteralDate)
            {
                return(new LDate(Convert.ToDateTime(token.Value)));
            }

            if (type == TokenTypes.LiteralTime)
            {
                return(new LTime((TimeSpan)token.Value));
            }

            if (type == TokenTypes.LiteralDay)
            {
                return(new LDayOfWeek((DayOfWeek)token.Value));
            }

            if (kind == TokenKind.LiteralBool)
            {
                return(new LBool(Convert.ToBoolean(token.Value)));
            }

            return(LangTypeHelper.ConvertToLangClass(token.Value));
        }
Beispiel #2
0
        /// <summary>
        /// Converts from c# datatypes to fluentscript datatypes inside
        /// </summary>
        /// <param name="val"></param>
        public static LObject ConvertToLangValue(object val)
        {
            if (val == null)
            {
                return(LObjects.Null);
            }

            var type = val.GetType();

            if (type == typeof(int))
            {
                return(new LNumber(Convert.ToDouble(val)));
            }

            if (type == typeof(double))
            {
                return(new LNumber((double)val));
            }

            if (type == typeof(string))
            {
                return(new LString((string)val));
            }

            if (type == typeof(DateTime))
            {
                return(new LDate((DateTime)val));
            }

            if (type == typeof(TimeSpan))
            {
                return(new LTime((TimeSpan)val));
            }

            if (type == typeof(DayOfWeek))
            {
                return(new LDayOfWeek((DayOfWeek)val));
            }

            if (type == typeof(bool))
            {
                return(new LBool((bool)val));
            }

            var isGenType = type.IsGenericType;

            if (isGenType)
            {
                var gentype = type.GetGenericTypeDefinition();
                if (type == typeof(List <object>) || gentype == typeof(List <>) || gentype == typeof(IList <>))
                {
                    return(new LArray((IList)val));
                }

                if (type == typeof(Dictionary <string, object>))
                {
                    return(new LMap((Dictionary <string, object>)val));
                }
            }
            // object
            return(LangTypeHelper.ConvertToLangClass(val));
        }