コード例 #1
0
        static void Simplify(string input)
        {
            var newLookup = NodeDeserializer.DeserializeDense(File.ReadAllText(input, Encoding.UTF8)).Simplify(5).UnmarkUnsure(3);

            Console.WriteLine("Simplified dense representation on next line:");
            Console.WriteLine(ObjectToCode.PlainObjectToCode(NodeSerializer.SerializeDense(newLookup)));
            Console.WriteLine();
            Console.WriteLine("Simplified no-statistics dense representation on next line:");
            Console.WriteLine(ObjectToCode.PlainObjectToCode(NodeSerializer.SerializeDenseNoStats(newLookup)));
        }
コード例 #2
0
        static string ComplexObjectToPseudoCode(ExpressionToCodeConfiguration config, object?val, int indent, int valueSize)
        {
            var retval = ObjectToCode.PlainObjectToCode(val);

            if (val is string)
            {
                return(ElidePossiblyMultilineString(config, retval ?? throw new InvalidOperationException("retval cannot be null for strings"), indent, valueSize).Trim());
            }
            else if (retval != null)
            {
                return(ElideAfter(retval, valueSize));
            }
            else if (val == null)
            {
                throw new Exception("Impossible: if val is null, retval cannot be");
            }
            else if (val is Array arrayVal)
            {
                return("new[] " + FormatEnumerable(config, arrayVal, indent, valueSize - 6));
            }
            else if (val is IEnumerable enumerableVal)
            {
                return(FormatTypeWithListInitializerOrNull(config, val, indent, valueSize, enumerableVal) ?? FormatEnumerable(config, enumerableVal, indent, valueSize));
            }
            else if (val is Expression exprVal)
            {
                return(ElideAfter(config.GetExpressionToCode().ToCode(exprVal), valueSize));
            }
            else if (val is IStructuralComparable tuple && val is IComparable && CSharpFriendlyTypeName.IsValueTupleType(val.GetType().GetTypeInfo()))
            {
                var collector = new NastyHackyTupleCollector();
                tuple.CompareTo(tuple, collector);
                var sb = new StringBuilder();
                sb.Append("(");
                for (var index = 0; index < collector.CollectedObjects.Count; index++)
                {
                    var item     = collector.CollectedObjects[index];
                    var asString = ComplexObjectToPseudoCode(config, item, indent + 4, valueSize);
                    if (index > 0)
                    {
                        sb.Append(", ");
                    }

                    sb.Append(asString);
                }

                sb.Append(")");
                return(ElidePossiblyMultilineString(config, sb.ToString(), indent, valueSize).Trim());
            }
コード例 #3
0
        static string ComplexObjectToPseudoCode(ExpressionToCodeConfiguration config, object val, int indent, int valueSize)
        {
            var retval = ObjectToCode.PlainObjectToCode(val);

            if (val is string)
            {
                return(ElidePossiblyMultilineString(config, retval, indent, valueSize).Trim());
            }
            else if (retval != null)
            {
                return(ElideAfter(retval, valueSize));
            }
            else if (val is Array)
            {
                return("new[] " + FormatEnumerable(config, (IEnumerable)val, indent, valueSize - 6));
            }
            else if (val is IEnumerable)
            {
                return(FormatEnumerable(config, (IEnumerable)val, indent, valueSize));
            }
            else if (val is Expression)
            {
                return(ElideAfter(config.GetExpressionToCode().ToCode((Expression)val), valueSize));
            }
            else if (val.GetType().GuessTypeClass() == ReflectionHelpers.TypeClass.AnonymousType)
            {
                var type = val.GetType();
                return("new {" +
                       string.Join(
                           "",
                           type.GetTypeInfo()
                           .GetProperties()
                           .Select(
                               pi =>
                               "\n" + new string(' ', indent + 4) + pi.Name + " = "
                               + ComplexObjectToPseudoCode(config, pi.GetValue(val, null), indent + 4, valueSize - pi.Name.Length) + ",")
                           )
                       + "\n" + new string(' ', indent) + "}");
            }
            else
            {
                return(ElideAfter(val.ToString(), valueSize));
            }
        }
コード例 #4
0
        static string ComplexObjectToPseudoCode(ExpressionToCodeConfiguration config, object val, int indent, int valueSize)
        {
            var retval = ObjectToCode.PlainObjectToCode(val);

            if (val is string)
            {
                return(ElidePossiblyMultilineString(config, retval, indent, valueSize).Trim());
            }
            else if (retval != null)
            {
                return(ElideAfter(retval, valueSize));
            }
            else if (val is Array arrayVal)
            {
                return("new[] " + FormatEnumerable(config, arrayVal, indent, valueSize - 6));
            }
            else if (val is IEnumerable enumerableVal)
            {
                var type = val.GetType();
                if (type.GetConstructor(Type.EmptyTypes) is ConstructorInfo ci && ci.IsPublic)
                {
                    foreach (var pi in type.GetProperties())
                    {
                        if (
                            pi.Name == "Item" &&
                            pi.CanWrite &&
                            pi.GetIndexParameters() is ParameterInfo[] indexPars &&
                            indexPars.Length == 1 &&
                            typeof(IEnumerable <>).MakeGenericType(typeof(KeyValuePair <,>).MakeGenericType(indexPars[0].ParameterType, pi.PropertyType)) is Type keyEnumerableType &&
                            keyEnumerableType.IsAssignableFrom(type)
                            )
                        {
                            var typeName = type.ToCSharpFriendlyTypeName();
                            return("new " + typeName + " " + PrintInitializerContents(config, enumerableVal, indexPars[0].ParameterType, pi.PropertyType, indent, valueSize - typeName.Length));
                        }
                    }
                }

                return(FormatEnumerable(config, enumerableVal, indent, valueSize));
            }
            else if (val is Expression exprVal)
            {
                return(ElideAfter(config.GetExpressionToCode().ToCode(exprVal), valueSize));
            }
            else if (val is IStructuralComparable tuple && val is IComparable && CSharpFriendlyTypeName.IsValueTupleType(val.GetType().GetTypeInfo()))
            {
                var collector = new NastyHackyTupleCollector();
                tuple.CompareTo(tuple, collector);
                var sb = new StringBuilder();
                sb.Append("(");
                for (var index = 0; index < collector.CollectedObjects.Count; index++)
                {
                    var item     = collector.CollectedObjects[index];
                    var asString = ComplexObjectToPseudoCode(config, item, indent + 4, valueSize);
                    if (index > 0)
                    {
                        sb.Append(", ");
                    }

                    sb.Append(asString);
                }

                sb.Append(")");
                return(ElidePossiblyMultilineString(config, sb.ToString(), indent, valueSize).Trim());
            }