private BlockSyntax ReplaceNodes(BlockSyntax oldBody) { var markedNodesCount = markedNodes.Count; while (markedNodesCount > 0) { var firstExpression = replacementNodes[0]; var firstParent = markedNodes[0]; oldBody = oldBody.TrackNodes(new List <SyntaxNode>() { firstExpression.Item1, firstParent }); var currentParent = oldBody.GetCurrentNode(firstParent); oldBody = oldBody.InsertNodesBefore(currentParent, new List <SyntaxNode>() { firstExpression.Item3 }); var currentExpression = oldBody.GetCurrentNode(firstExpression.Item1); oldBody = oldBody.ReplaceNode(currentExpression, firstExpression.Item2); oldBody = this.Refactor(oldBody); markedNodesCount = markedNodes.Count; } return(oldBody); }
//STEP 2 private BlockSyntax ReplaceNodes(BlockSyntax oldBody) { oldBody = oldBody.TrackNodes(this.markedNodes); foreach (var tuple in this.replacementNodes) { var currentA = oldBody.GetCurrentNode(tuple.Item1); if (currentA != null) { var whileStatement = currentA.Parent; oldBody = oldBody.InsertNodesBefore(whileStatement, new List <SyntaxNode>() { tuple.Item3 }); var currentB = oldBody.GetCurrentNode(tuple.Item1); oldBody = oldBody.ReplaceNode(currentB, tuple.Item2); var currentWhile = oldBody.GetCurrentNode(tuple.Item4); //modify body var whileBody = currentWhile.Statement as BlockSyntax; //create new statement var localCondition = tuple.Item3 as LocalDeclarationStatementSyntax; var initializer = localCondition.Declaration.Variables.First(); var assignment = SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(initializer.Identifier), initializer.Initializer.Value)); var newStatements = whileBody.Statements.Add(assignment); whileBody = whileBody.WithStatements(newStatements); //updateWhile var newWhile = currentWhile.WithStatement(whileBody); oldBody = oldBody.ReplaceNode(currentWhile, newWhile); } } return(oldBody); }
private BlockSyntax ReplaceNodes(BlockSyntax oldBody) { List <SyntaxNode> nodesToTrack = this.markedNodes; var continueSts = continueStatements.SelectMany(x => x.Value); var breakSts = breakStatements.SelectMany(x => x.Value); nodesToTrack.AddRange(continueSts); nodesToTrack.AddRange(breakSts); oldBody = oldBody.TrackNodes(nodesToTrack); foreach (var tuple in this.whileReplacementNodes) { var currentA = oldBody.GetCurrentNode(tuple.Item1); if (currentA != null) { //create new statement var localCondition = tuple.Item3 as LocalDeclarationStatementSyntax; var initializer = localCondition.Declaration.Variables.First(); var updateCondition = SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(initializer.Identifier), initializer.Initializer.Value)); var whileStatement = currentA.Parent; oldBody = oldBody.InsertNodesBefore(whileStatement, new List <SyntaxNode>() { tuple.Item3 }); var currentB = oldBody.GetCurrentNode(tuple.Item1); oldBody = oldBody.ReplaceNode(currentB, tuple.Item2); //update continue statements foreach (var cont in continueStatements[tuple.Item4]) { var currentContinue = oldBody.GetCurrentNode(cont); oldBody = oldBody.ReplaceNode(currentContinue, new List <SyntaxNode>() { updateCondition, cont }); } //update break statements foreach (var brk in breakStatements[tuple.Item4]) { var currentBreak = oldBody.GetCurrentNode(brk); var invalidateCondition = SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(initializer.Identifier), SyntaxFactoryExtensions.BooleanLiteralExpression(false))); oldBody = oldBody.ReplaceNode(currentBreak, new List <SyntaxNode>() { invalidateCondition, SyntaxFactory.ContinueStatement() }); } var currentWhile = oldBody.GetCurrentNode(tuple.Item4); //modify body var whileBody = currentWhile.Statement as BlockSyntax; var newStatements = whileBody.Statements.Add(updateCondition); whileBody = whileBody.WithStatements(newStatements); //updateWhile var newWhile = currentWhile.WithStatement(whileBody); oldBody = oldBody.ReplaceNode(currentWhile, newWhile); } } return(oldBody); }
private BlockSyntax ReplaceNodes(BlockSyntax oldBody) { List <SyntaxNode> nodesToTrack = this.markedNodes; var continueSts = continueStatements.SelectMany(x => x.Value); var breakSts = breakStatements.SelectMany(x => x.Value); nodesToTrack.AddRange(continueSts); nodesToTrack.AddRange(breakSts); oldBody = oldBody.TrackNodes(nodesToTrack); foreach (var tuple in this.forReplacementNodes) { var currentFor = oldBody.GetCurrentNode(tuple.Item4); if (currentFor != null) { string oldName = tuple.Item1.Variables.First().Identifier.ValueText; string newName = ConditionIdentifier; var renameVisitor = new RenameVisitor(oldName, newName); var newIndexer = (VariableDeclarationSyntax)renameVisitor.Visit(tuple.Item1); oldBody = oldBody.InsertNodesBefore(currentFor, new List <SyntaxNode>() { SyntaxFactory.LocalDeclarationStatement(newIndexer) }); var newCurrentFor = oldBody.GetCurrentNode(tuple.Item4); //create while var whileBody = currentFor.Statement as BlockSyntax; //update continue statements foreach (var cont in continueStatements[tuple.Item4]) { var currentContinue = whileBody.GetCurrentNode(cont); whileBody = whileBody.ReplaceNode(currentContinue, new List <SyntaxNode>() { SyntaxFactory.ExpressionStatement(tuple.Item3), cont }); } //update break statements foreach (var brk in breakStatements[tuple.Item4]) { var currentBreak = whileBody.GetCurrentNode(brk); whileBody = whileBody.ReplaceNode(currentBreak, new List <SyntaxNode>() { SyntaxFactory.ExpressionStatement(tuple.Item3), brk }); } var newStatements = whileBody.Statements.Add(SyntaxFactory.ExpressionStatement(tuple.Item3)); whileBody = whileBody.WithStatements(newStatements); var whileStatement = SyntaxFactory.WhileStatement(tuple.Item2, whileBody); whileStatement = (WhileStatementSyntax)renameVisitor.Visit(whileStatement); //updateFor oldBody = oldBody.ReplaceNode(newCurrentFor, whileStatement); } } return(oldBody); }
private BlockSyntax ReplaceNodes(BlockSyntax oldBody) { oldBody = oldBody.TrackNodes(this.markedNodes); foreach (var tuple in this.replacementNodes) { var currentA = oldBody.GetCurrentNode(tuple.Item1); if (currentA != null) { var ifStatement = currentA.Parent; oldBody = oldBody.InsertNodesBefore(ifStatement, new List <SyntaxNode>() { tuple.Item3 }); var currentB = oldBody.GetCurrentNode(tuple.Item1); oldBody = oldBody.ReplaceNode(currentB, tuple.Item2); } } return(oldBody); }
private BlockSyntax ReplaceNodes(BlockSyntax oldBody) { oldBody = oldBody.TrackNodes(this.markedNodes); foreach (var tuple in this.replacementNodes) { var currentA = oldBody.GetCurrentNode(tuple.Item1); var replacedExpr = oldBody.ReplaceNode(currentA, tuple.Item2); oldBody = replacedExpr; } return(oldBody); }
private BlockSyntax ReplaceNodes(BlockSyntax oldBody) { var markedNodesCount = markedNodes.Count; while (markedNodesCount > 0) { var firstExpression = replacementNodes[0]; var firstParent = markedNodes[0]; oldBody = oldBody.TrackNodes(new List <SyntaxNode>() { firstExpression.Item1, firstParent }); //simple increment: index++; --index; if (firstExpression.Item1.Parent.Kind() == SyntaxKind.ExpressionStatement) { var currentOperation = oldBody.GetCurrentNode(firstExpression.Item1); var indexUpdate = ((ExpressionStatementSyntax)firstExpression.Item5).Expression; oldBody = oldBody.ReplaceNode(currentOperation, indexUpdate); oldBody = this.Refactor(oldBody); markedNodesCount = markedNodes.Count; continue; } if (firstExpression.Item3 == 0) //pre operation type { var currentParent = oldBody.GetCurrentNode(firstParent); oldBody = oldBody.InsertNodesBefore(currentParent, new List <SyntaxNode>() { firstExpression.Item5 }); currentParent = oldBody.GetCurrentNode(firstParent); oldBody = oldBody.InsertNodesBefore(currentParent, new List <SyntaxNode>() { firstExpression.Item4 }); } else //post opertion type { var currentParent = oldBody.GetCurrentNode(firstParent); oldBody = oldBody.InsertNodesBefore(currentParent, new List <SyntaxNode>() { firstExpression.Item4 }); currentParent = oldBody.GetCurrentNode(firstParent); oldBody = oldBody.InsertNodesBefore(currentParent, new List <SyntaxNode>() { firstExpression.Item5 }); } var updatedOperation = oldBody.GetCurrentNode(firstExpression.Item1); oldBody = oldBody.ReplaceNode(updatedOperation, firstExpression.Item2); oldBody = this.Refactor(oldBody); markedNodesCount = markedNodes.Count; } return(oldBody); }
private BlockSyntax ReplaceNodes(BlockSyntax oldBody) { //TODO: replace with auxiliary variable //TODO: insert auxiliary variables before //TODO: keep in mind the order oldBody = oldBody.TrackNodes(this.markedNodes); foreach (var tuple in this.replacementNodes) { var currentA = oldBody.GetCurrentNode(tuple.Item1); var replacedExpr = oldBody.ReplaceNode(currentA, tuple.Item2); oldBody = replacedExpr; } return(oldBody); }