Пример #1
0
        /// <summary>
        /// Pretty format the specified object.
        /// </summary>
        /// <returns>The format.</returns>
        /// <param name="obj">Object.</param>
        public static string Format(IodineObject obj)
        {
            // Get the type of the object
            var type = obj.GetType();

            // Exceptions are special snowflakes
            if (obj.Base?.GetType().Name == "IodineException")
            {
                type = obj.Base.GetType();
            }

            // Try getting the formatting color
            ANSIColor color;

            ColorScheme.TryGetValue(type, out color);

            // Test if the object has a custom formatter
            if (Formatters.ContainsKey(type))
            {
                // Invoke the formatter
                return(ANSI.Sanitize(Formatters [type] (obj, color?.fg ?? string.Empty, $"{ANSI.ESC}[0m")));
            }

            // Oops, there's no custom formatter for that object
            Logger.Warn($"No formatter for type '{type.Name}'");
            return(ANSI.Sanitize($"{color ?? White}{obj}"));
        }
Пример #2
0
        private void WriteConstant(IodineObject obj)
        {
            var lookup = new Dictionary <Type, Action> ()
            {
                { typeof(IodineNull), () => WriteNull() },
                { typeof(IodineBool), () => WriteBool(obj as IodineBool) },
                { typeof(IodineName), () => WriteName(obj as IodineName) },
                { typeof(IodineInteger), () => WriteInt(obj as IodineInteger) },
                { typeof(IodineFloat), () => WriteFloat(obj as IodineFloat) },
                { typeof(IodineBigInt), () => WriteBigInt(obj as IodineBigInt) },
                { typeof(IodineString), () => WriteString(obj as IodineString) },
                { typeof(CodeBuilder), () => WriteCodeObject(obj as CodeBuilder) },
            };

            lookup [obj.GetType()] ();
        }