コード例 #1
0
ファイル: RewriteToArray.cs プロジェクト: bernd5/LinqRewrite
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            var enlarging = args.FirstOrDefault()?.ToString() switch
            {
                "EnlargingCoefficient.By2" => 1,
                "EnlargingCoefficient.By4" => 2,
                "EnlargingCoefficient.By8" => 3,
                _ when design.SourceSize != null => 2,
                _ => 1
            };

            var(currentLength, currentResult) = GetResultVariable(design, design.LastValue.Type);
            SimplifyPart(design, currentResult);
            RewriteOther(design, currentLength, currentResult, enlarging);

            if (design.ResultSize == null)
            {
                design.ResultAdd(Return("LinqRewrite".Access("Core", "SimpleArrayExtensions", "EnsureFullArray")
                                        .Invoke(currentResult, design.Indexer)));
            }
            else
            {
                design.ResultAdd(Return(currentResult));
            }
        }
コード例 #2
0
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            var enlarging = args.FirstOrDefault()?.ToString() switch
            {
                "EnlargingCoefficient.By2" => 1,
                "EnlargingCoefficient.By4" => 2,
                "EnlargingCoefficient.By8" => 3,
                _ when design.SourceSize != null => 2,
                _ => 1
            };

            var(currentLength, currentResult) = RewriteToArray.GetResultVariable(design, design.LastValue.Type);
            VariableBridge result = RewriteToArray.RewriteOther(design, currentLength, currentResult, enlarging);

            RewriteToArray.SimplifyPart(design, result);

            var           listResultType = ParseTypeName($"LinqRewrite.Core.SimpleList.SimpleList<{design.LastValue.Type}>");
            LocalVariable finalResult    = CreateGlobalVariable(design, listResultType);

            design.ResultAdd(finalResult.Assign(New(listResultType)));
            design.ResultAdd(finalResult.Access("Items").Assign(result));
            design.ResultAdd(finalResult.Access("Count").Assign(design.ResultSize ?? design.Indexer));
            design.ResultAdd(Return(finalResult));
        }
    }
コード例 #3
0
ファイル: RewriteToList.cs プロジェクト: bernd5/LinqRewrite
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            var listVariable = CreateGlobalVariable(design, design.ReturnType, New(design.ReturnType));

            design.ForAdd(listVariable.Access("Add").Invoke(design.LastValue));
            design.ResultAdd(Return(listVariable));
        }
コード例 #4
0
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            var elementType = design.ReturnType.Type is NullableTypeSyntax nullable
                ? (TypeBridge)nullable.ElementType : design.ReturnType;
            var sumVariable = CreateGlobalVariable(design, elementType, 0);

            var value = args.Length switch
            {
                0 => design.LastValue,
                1 => args[0].Inline(design, design.LastValue)
            };

            if (design.ReturnType.Type is NullableTypeSyntax)
            {
                value = value.Reusable(design);
                design.ForAdd(If(value.NotEqual(null),
                                 sumVariable.AddAssign(value.Cast(elementType))));
            }
            else
            {
                design.ForAdd(sumVariable.AddAssign(value));
            }
            design.ResultAdd(Return(sumVariable));
        }
    }
コード例 #5
0
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            design.ForAdd(If(Not(args[0].Inline(design, design.LastValue)),
                             Return(false)));

            design.ResultAdd(Return(true));
        }
コード例 #6
0
ファイル: RewriteAverage.cs プロジェクト: bernd5/LinqRewrite
 private static void CalculateSimpleAverage(RewriteDesign design, TypedValueBridge selectionValue, LocalVariable sumVariable)
 {
     if (!AssertResultSizeGreaterEqual(design, 1))
     {
         return;
     }
     design.ForAdd(sumVariable.AddAssign(selectionValue));
     design.ResultAdd(Return(sumVariable / design.GetResultSize()));
 }
コード例 #7
0
ファイル: RewriteAverage.cs プロジェクト: bernd5/LinqRewrite
        private static void CalculateNullableAverage(RewriteDesign design, TypeBridge elementType, TypedValueBridge selectionValue, LocalVariable sumVariable)
        {
            var inlinedValue = selectionValue.Reusable(design);

            design.ForAdd(If(inlinedValue.IsEqual(null), Continue()));
            design.ForAdd(sumVariable.AddAssign(inlinedValue.Cast(elementType)));
            design.Indexer = null;

            if (design.Unchecked)
            {
                design.ResultAdd(sumVariable.Cast(design.ReturnType.Type) / design.Indexer);
            }
            else
            {
                design.ResultAdd(Return(SyntaxFactory.ConditionalExpression(design.Indexer.IsEqual(0),
                                                                            Null, sumVariable.Cast(design.ReturnType.Type) / design.Indexer)));
            }
        }
コード例 #8
0
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            var positionValue = args[0].ReusableConst(design);

            design.ForAdd(If(design.Indexer.IsEqual(positionValue),
                             Return(design.LastValue)));

            design.ResultAdd(Return(Default(design.ReturnType)));
        }
コード例 #9
0
 public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
 {
     if (args.Length != 0)
     {
         design.Indexer = null;
         design.ForAdd(If(Not(args[0].Inline(design, design.LastValue)),
                          Continue()));
     }
     design.ResultAdd(Return(design.GetIndexer(Long)));
 }
コード例 #10
0
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            var collectionValue = args[0];

            if (!AssertNotNull(design, collectionValue))
            {
                return;
            }
            if (ExpressionSimplifier.TryGetInt(design.ResultSize, out var resultSizeInt))
            {
                if (resultSizeInt == 0)
                {
                    InitialErrorAdd(design, Return((collectionValue.Access("Count").Invoke()).IsEqual(0)));
                    return;
                }
            }

            design.WrapWithTry = true;
            var itemType           = collectionValue.ItemType(design);
            var enumeratorVariable = CreateGlobalVariable(design, ParseTypeName($"System.Collections.Generic.IEnumerator<{itemType}>"));

            design.InitialAdd(enumeratorVariable.Assign(Parenthesize(collectionValue.Cast(ParseTypeName($"IEnumerable<{itemType}>")))
                                                        .Access("GetEnumerator").Invoke()));

            var equalityTestValue = args.Length switch
            {
                1 => enumeratorVariable.Access("Current", "Equals").Invoke(design.LastValue),
                2 => args[1].ReusableConst(design).Access("Equals").Invoke(design.LastValue, enumeratorVariable.Access("Current"))
            };

            design.ForAdd(If(Not(enumeratorVariable.Access("MoveNext").Invoke().And(equalityTestValue)),
                             Return(false)));

            design.ResultAdd(If(enumeratorVariable.Access("MoveNext").Invoke(),
                                Return(false)));

            design.FinalAdd(enumeratorVariable.Access("Dispose").Invoke());
            design.ResultAdd(Return(true));
        }
    }
コード例 #11
0
 public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
 {
     if (args.Length == 0)
     {
         design.ForAdd(Return(design.LastValue));
     }
     else
     {
         design.ForAdd(If(args[0].Inline(design, design.LastValue),
                          Return(design.LastValue)));
     }
     design.ResultAdd(Return(Default(design.ReturnType)));
 }
コード例 #12
0
ファイル: RewriteContains.cs プロジェクト: bernd5/LinqRewrite
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            var elementSyntax        = design.Node.ArgumentList.Arguments.First().Expression;
            var elementEqualityValue = args.Length switch
            {
                1 => design.LastValue.IsEqual(elementSyntax),
                2 => args[1].ReusableConst(design).Access("Equals").Invoke(design.LastValue.Value, elementSyntax)
            };

            design.ForAdd(If(elementEqualityValue, Return(true)));
            design.ResultAdd(Return(false));
        }
    }
コード例 #13
0
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            var foundVariable = CreateGlobalVariable(design, NullableType(design.ReturnType), null);

            if (args.Length == 0)
            {
                design.ForAdd(foundVariable.Assign(design.LastValue));
            }
            else
            {
                design.ForAdd(If(args[0].Inline(design, design.LastValue),
                                 foundVariable.Assign(design.LastValue)));
            }

            design.ResultAdd(If(foundVariable.IsEqual(null),
                                Return(Default(design.ReturnType)),
                                Return(foundVariable.Cast(design.ReturnType))));
        }
コード例 #14
0
ファイル: RewriteSingle.cs プロジェクト: bernd5/LinqRewrite
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            var foundVariable = CreateGlobalVariable(design, NullableType(design.ReturnType), null);

            if (args.Length == 0)
            {
                design.ForAdd(If(foundVariable.IsEqual(null),
                                 foundVariable.Assign(design.LastValue),
                                 Throw("System.InvalidOperationException", "The sequence contains more than single matching element.")));
            }
            else
            {
                design.ForAdd(If(args[0].Inline(design, design.LastValue),
                                 If(foundVariable.IsEqual(null),
                                    foundVariable.Assign(design.LastValue),
                                    Throw("System.InvalidOperationException", "The sequence contains more than single matching element."))));
            }
            design.ResultAdd(If(foundVariable.IsEqual(null),
                                Throw("System.InvalidOperationException", "The sequence did not contain any elements."),
                                Return(foundVariable.Cast(design.ReturnType))));
        }
コード例 #15
0
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            if (!AssertGreaterEqual(design, args[0], 0, true, true))
            {
                return;
            }
            if (!AssertLesser(design, args[0], design.ResultSize, true, true))
            {
                return;
            }

            var positionValue = args[0].ReusableConst(design);

            design.ForAdd(If(design.Indexer.IsEqual(positionValue),
                             Return(design.LastValue)));

            if (!design.Unchecked)
            {
                design.ResultAdd(Throw("System.InvalidOperationException", "The sequence did not contain enough elements."));
            }
        }
コード例 #16
0
ファイル: RewriteMin.cs プロジェクト: bernd5/LinqRewrite
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            if (!AssertResultSizeGreaterEqual(design, 1))
            {
                return;
            }
            var elementType = design.ReturnType.Type is NullableTypeSyntax nullable
                ? (TypeBridge)nullable.ElementType : design.ReturnType;

            var minVariable = elementType.ToString() switch
            {
                "int" => CreateGlobalVariable(design, Int, int.MaxValue),
                "long" => CreateGlobalVariable(design, Long, long.MaxValue),
                "float" => CreateGlobalVariable(design, Float, float.MaxValue),
                "double" => CreateGlobalVariable(design, Double, double.MaxValue),
                "decimal" => CreateGlobalVariable(design, Decimal, decimal.MaxValue),
                _ => null
            };

            var value = args.Length switch
            {
                0 => design.LastValue.Reusable(design),
                1 => args[0].Inline(design, design.LastValue).Reusable(design)
            };

            if (design.ReturnType.Type is NullableTypeSyntax)
            {
                design.ForAdd(If(value.IsEqual(null).Or(value >= minVariable), Continue()));
                design.ForAdd(minVariable.Assign(value.Cast(elementType)));
            }
            else
            {
                design.ForAdd(If(value >= minVariable, Continue()));
                design.ForAdd(minVariable.Assign(value));
            }

            design.ResultAdd(Return(minVariable));
        }
    }
}
コード例 #17
0
        public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args)
        {
            if (!AssertResultSizeGreaterEqual(design, 0, true))
            {
                return;
            }

            if (args.Length == 0)
            {
                design.ForAdd(Return(design.LastValue));
            }
            else
            {
                design.ForAdd(If(args[0].Inline(design, design.LastValue),
                                 Return(design.LastValue)));
            }

            if (!design.Unchecked)
            {
                design.ResultAdd(Throw("System.InvalidOperationException", "The sequence did not contain any elements."));
            }
        }