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)); }
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)); }
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); }
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); }
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); }
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); }
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)); }
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); }
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); }
public static BinaryExpressionSyntax Sub(this ValueBridge a, ValueBridge b) => BinaryExpression(SyntaxKind.SubtractExpression, a, b);
public static BinaryExpressionSyntax Add(this ValueBridge a, ValueBridge b) => BinaryExpression(SyntaxKind.AddExpression, a, b);
public static AssignmentExpressionSyntax AddAssign(this VariableBridge a, ValueBridge b) => AssignmentExpression(SyntaxKind.AddAssignmentExpression, a, b);
public static BinaryExpressionSyntax As(this ValueBridge identifier, ValueBridge b) => BinaryExpression(SyntaxKind.AsExpression, identifier, b);
public static BinaryExpressionSyntax GThan(this ValueBridge a, ValueBridge b) => BinaryExpression(SyntaxKind.GreaterThanExpression, a, b);
public static BinaryExpressionSyntax Mod(this ValueBridge a, ValueBridge b) => BinaryExpression(SyntaxKind.ModuloExpression, a, b);
public static SeparatedSyntaxList <ExpressionSyntax> SeparatedPostIncrement(this ValueBridge identifier) => SyntaxFactoryHelper.CreateSeparatedExpressionList( PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, identifier));
public static PostfixUnaryExpressionSyntax PostIncrement(this ValueBridge identifier) => PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, identifier);
public static BinaryExpressionSyntax And(this ExpressionSyntax a, ValueBridge b) => BinaryExpression(SyntaxKind.LogicalAndExpression, ParenthesizedExpression(a), b);
public static PrefixUnaryExpressionSyntax Not(ValueBridge a) => PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, ParenthesizedExpression(a));
public static BinaryExpressionSyntax And(this ValueBridge a, ValueBridge b) => BinaryExpression(SyntaxKind.LogicalAndExpression, a, b);
public static MemberAccessExpressionSyntax Access(this ValueBridge identifier, VariableBridge accessed) => MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, identifier, accessed);
public static BinaryExpressionSyntax Mul(this ValueBridge a, ValueBridge b) => BinaryExpression(SyntaxKind.MultiplyExpression, a, b);
public static BinaryExpressionSyntax Div(this ValueBridge a, ValueBridge b) => BinaryExpression(SyntaxKind.DivideExpression, a, b);
public static CastExpressionSyntax Cast(this ValueBridge a, TypeBridge type) => CastExpression(type, a);
public static BinaryExpressionSyntax LeThan(this ValueBridge a, ValueBridge b) => BinaryExpression(SyntaxKind.LessThanOrEqualExpression, a, b);
public static AssignmentExpressionSyntax Assign(this ValueBridge a, ValueBridge b) => AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, a, b);
public static BinaryExpressionSyntax NotEqual(this ValueBridge a, ValueBridge b) => BinaryExpression(SyntaxKind.NotEqualsExpression, a, b);
public static AssignmentExpressionSyntax SubAssign(this VariableBridge a, ValueBridge b) => AssignmentExpression(SyntaxKind.SubtractAssignmentExpression, a, b);
public static PrefixUnaryExpressionSyntax PreDecrement(this ValueBridge identifier) => PrefixUnaryExpression(SyntaxKind.PreDecrementExpression, identifier);
public static ValueBridge ArrayAccess(this ValueBridge identifier, ValueBridge index) => ElementAccessExpression( identifier, BracketedArgumentList(SyntaxFactoryHelper.CreateSeparatedList(Argument(index))));