예제 #1
0
        public static ValueBridge Substitute(ValueBridge value, VariableBridge substituted, ValueBridge changedFor)
        {
            var str = value.ToString();

            str = str.Replace(substituted.Name, changedFor.ToString());
            return(SyntaxFactory.ParseExpression(str));
        }
예제 #2
0
        public static ExpressionSyntax TryRewrite(ValueBridge collection, TypeSyntax returnType, List <LinqStep> chain, InvocationExpressionSyntax node)
        {
            using var design = RewriteParametersFactory.BorrowParameters();
            design.SetData(collection, returnType, chain, node, false);
            string[] names = chain.Select(x => x.MethodName).ToArray();

            var(simplePreCheck, simpleResult) = TryRewriteSimple(design, names);
            if (simplePreCheck && simpleResult != null)
            {
                return(simpleResult);
            }
            if (simplePreCheck)
            {
                design.SetData(collection, returnType, chain, node, true);
            }

            design.HasResultMethod = MethodsWithResult.Contains(names.Last());
            RewriteComposite(design, names);

            var body = design.Error ? new [] { design.InitialStatements[0] } : GetMethodBody(design);

            if (design.NotRewrite)
            {
                throw new NotSupportedException("Not good for rewrite");
            }

            if (design.Data.CurrentMethodIsConditional && design.ReturnType.Type.ToString() != "void")
            {
                return(ConditionalExpression(design.CurrentCollection.IsEqual(null),
                                             Default(design.ReturnType), design.Rewrite.GetMethodInvocationExpression(design, body)));
            }
            return(design.Rewrite.GetMethodInvocationExpression(design, body));
        }
예제 #3
0
        public static bool AssertLesser(RewriteDesign design, ValueBridge smaller, ValueBridge bigger, bool initialCheck = true, bool preCheck = false)
        {
            if (design.Unchecked)
            {
                return(true);
            }
            bool biggerPass  = ExpressionSimplifier.TryGetDouble(bigger, out double biggerD);
            bool smallerPass = ExpressionSimplifier.TryGetDouble(smaller, out double smallerD);

            if (biggerPass && smallerPass)
            {
                if (smallerD < biggerD)
                {
                    return(true);
                }
                InitialError(design, "System.InvalidOperationException", "Index out of range");
                return(false);
            }
            if (preCheck)
            {
                return(true);
            }
            if (initialCheck)
            {
                design.PreUseAdd(If(smaller >= bigger, Throw("System.InvalidOperationException", "Index out of range")));
            }
            else
            {
                design.FinalAdd(If(smaller >= bigger, Throw("System.InvalidOperationException", "Index out of range")));
            }
            return(true);
        }
예제 #4
0
        private static VariableBridge KnownSourceSize(RewriteDesign design, ValueBridge currentLength, VariableBridge resultVariable, int enlarging)
        {
            var logVariable = CreateGlobalVariable(design, Int,
                                                   "LinqRewrite".Access("Core", "IntExtensions", "Log2")
                                                   .Invoke(Parenthesize(design.SourceSize).Cast(SyntaxKind.UIntKeyword)) - Constants.MinArraySizeLog);

            if (enlarging != 1)
            {
                design.PreUseAdd(logVariable.Assign(
                                     SyntaxFactory.ConditionalExpression(logVariable.GThan(enlarging),
                                                                         logVariable - logVariable % enlarging,
                                                                         IntValue(enlarging))));
            }
            else
            {
                design.PreUseAdd(logVariable.Assign(
                                     SyntaxFactory.ConditionalExpression(logVariable.GThan(1),
                                                                         logVariable,
                                                                         IntValue(1))));
            }

            var currentLengthVariable = CreateGlobalVariable(design, Int, currentLength);

            design.ForAdd(If(design.Indexer >= currentLengthVariable,
                             "LinqRewrite".Access("Core", "EnlargeExtensions", "LogEnlargeArray")
                             .Invoke(enlarging,
                                     design.SourceSize,
                                     RefArg(resultVariable),
                                     RefArg(logVariable),
                                     OutArg(currentLengthVariable))));

            design.ForAdd(resultVariable[design.Indexer].Assign(design.LastValue));
            return(resultVariable);
        }
예제 #5
0
        private static VariableBridge UnknownSourceSize(RewriteDesign design, ValueBridge currentLength, VariableBridge resultVariable, int enlarging)
        {
            var currentLengthVariable = CreateGlobalVariable(design, Int, currentLength);

            design.ForAdd(If(design.Indexer >= currentLengthVariable,
                             "LinqRewrite".Access("Core", "EnlargeExtensions", "LogEnlargeArray")
                             .Invoke(enlarging, RefArg(resultVariable), RefArg(currentLengthVariable))));

            design.ForAdd(resultVariable[design.Indexer].Assign(design.LastValue));
            return(resultVariable);
        }
예제 #6
0
        public static bool TryGetInt(ValueBridge expression, out int result)
        {
            if (expression == null)
            {
                result = 0;
                return(false);
            }
            var  str     = expression.ToString();
            bool success = int.TryParse(str, out result);

            return(success);
        }
예제 #7
0
 public static VariableBridge RewriteOther(RewriteDesign design, ValueBridge currentLength, VariableBridge resultVariable, int enlarging)
 {
     if (design.ResultSize != null)
     {
         return(KnownSize(design, resultVariable));
     }
     if (design.SourceSize != null)
     {
         return(KnownSourceSize(design, currentLength, resultVariable, enlarging));
     }
     return(UnknownSourceSize(design, currentLength, resultVariable, enlarging));
 }
예제 #8
0
        public static MemberAccessExpressionSyntax Access(this ValueBridge identifier, params VariableBridge[] accessed)
        {
            var item = (ExpressionSyntax)identifier;

            for (var i = 0; i < accessed.Length; i++)
            {
                item = MemberAccessExpression(
                    SyntaxKind.SimpleMemberAccessExpression,
                    item,
                    accessed[i]);
            }
            return((MemberAccessExpressionSyntax)item);
        }
예제 #9
0
 public static bool AssertNotNull(RewriteDesign design, ValueBridge notNull, bool preCheck = false)
 {
     if (design.Unchecked)
     {
         return(true);
     }
     if (notNull.ToString() == "null")
     {
         InitialError(design, "System.InvalidOperationException", "Invalid null object");
         return(false);
     }
     if (preCheck)
     {
         return(true);
     }
     design.PreUseAdd(If(notNull.IsEqual(null), Throw("System.InvalidOperationException", "Invalid null object")));
     return(true);
 }
예제 #10
0
 public static BinaryExpressionSyntax Sub(this ValueBridge a, ValueBridge b)
 => BinaryExpression(SyntaxKind.SubtractExpression, a, b);
예제 #11
0
 public static BinaryExpressionSyntax Add(this ValueBridge a, ValueBridge b)
 => BinaryExpression(SyntaxKind.AddExpression, a, b);
예제 #12
0
 public static AssignmentExpressionSyntax AddAssign(this VariableBridge a, ValueBridge b)
 => AssignmentExpression(SyntaxKind.AddAssignmentExpression, a, b);
예제 #13
0
 public static BinaryExpressionSyntax As(this ValueBridge identifier, ValueBridge b)
 => BinaryExpression(SyntaxKind.AsExpression, identifier, b);
예제 #14
0
 public static BinaryExpressionSyntax GThan(this ValueBridge a, ValueBridge b)
 => BinaryExpression(SyntaxKind.GreaterThanExpression, a, b);
예제 #15
0
 public static BinaryExpressionSyntax Mod(this ValueBridge a, ValueBridge b)
 => BinaryExpression(SyntaxKind.ModuloExpression, a, b);
예제 #16
0
 public static SeparatedSyntaxList <ExpressionSyntax> SeparatedPostIncrement(this ValueBridge identifier)
 => SyntaxFactoryHelper.CreateSeparatedExpressionList(
     PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, identifier));
예제 #17
0
 public static PostfixUnaryExpressionSyntax PostIncrement(this ValueBridge identifier)
 => PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, identifier);
예제 #18
0
 public static BinaryExpressionSyntax And(this ExpressionSyntax a, ValueBridge b)
 => BinaryExpression(SyntaxKind.LogicalAndExpression, ParenthesizedExpression(a), b);
예제 #19
0
 public static PrefixUnaryExpressionSyntax Not(ValueBridge a)
 => PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, ParenthesizedExpression(a));
예제 #20
0
 public static BinaryExpressionSyntax And(this ValueBridge a, ValueBridge b)
 => BinaryExpression(SyntaxKind.LogicalAndExpression, a, b);
예제 #21
0
 public static MemberAccessExpressionSyntax Access(this ValueBridge identifier, VariableBridge accessed)
 => MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, identifier, accessed);
예제 #22
0
 public static BinaryExpressionSyntax Mul(this ValueBridge a, ValueBridge b)
 => BinaryExpression(SyntaxKind.MultiplyExpression, a, b);
예제 #23
0
 public static BinaryExpressionSyntax Div(this ValueBridge a, ValueBridge b)
 => BinaryExpression(SyntaxKind.DivideExpression, a, b);
예제 #24
0
 public static CastExpressionSyntax Cast(this ValueBridge a, TypeBridge type)
 => CastExpression(type, a);
예제 #25
0
 public static BinaryExpressionSyntax LeThan(this ValueBridge a, ValueBridge b)
 => BinaryExpression(SyntaxKind.LessThanOrEqualExpression, a, b);
예제 #26
0
 public static AssignmentExpressionSyntax Assign(this ValueBridge a, ValueBridge b)
 => AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, a, b);
예제 #27
0
 public static BinaryExpressionSyntax NotEqual(this ValueBridge a, ValueBridge b)
 => BinaryExpression(SyntaxKind.NotEqualsExpression, a, b);
예제 #28
0
 public static AssignmentExpressionSyntax SubAssign(this VariableBridge a, ValueBridge b)
 => AssignmentExpression(SyntaxKind.SubtractAssignmentExpression, a, b);
예제 #29
0
 public static PrefixUnaryExpressionSyntax PreDecrement(this ValueBridge identifier)
 => PrefixUnaryExpression(SyntaxKind.PreDecrementExpression, identifier);
예제 #30
0
 public static ValueBridge ArrayAccess(this ValueBridge identifier, ValueBridge index)
 => ElementAccessExpression(
     identifier,
     BracketedArgumentList(SyntaxFactoryHelper.CreateSeparatedList(Argument(index))));