public override SyntaxList <StatementSyntax> VisitForStatement(CSS.ForStatementSyntax node) { StatementSyntax block; if (!ConvertForToSimpleForNext(node, out block)) { var stmts = ConvertBlock(node.Statement) .AddRange(node.Incrementors.Select(ConvertSingleExpression)); var condition = node.Condition == null?CommonConversions.Literal(true) : (ExpressionSyntax)node.Condition.Accept(_nodesVisitor); block = SyntaxFactory.WhileBlock( SyntaxFactory.WhileStatement(condition), stmts ); var declarations = new List <StatementSyntax>(); if (node.Declaration != null) { var syntaxTokenList = SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.DimKeyword)); declarations.Add(SyntaxFactory.LocalDeclarationStatement(syntaxTokenList, _commonConversions.RemodelVariableDeclaration(node.Declaration))); } return(SyntaxFactory.List(declarations.Concat(node.Initializers.Select(ConvertSingleExpression))).Add(block)); } return(SyntaxFactory.SingletonList(block)); }
bool ConvertForToSimpleForNext(CSS.ForStatementSyntax node, out StatementSyntax block) { // ForStatement -> ForNextStatement when for-loop is simple // only the following forms of the for-statement are allowed: // for (TypeReference name = start; name < oneAfterEnd; name += step) // for (name = start; name < oneAfterEnd; name += step) // for (TypeReference name = start; name <= end; name += step) // for (name = start; name <= end; name += step) // for (TypeReference name = start; name > oneAfterEnd; name -= step) // for (name = start; name > oneAfterEnd; name -= step) // for (TypeReference name = start; name >= end; name -= step) // for (name = start; name >= end; name -= step) block = null; // check if the form is valid and collect TypeReference, name, start, end and step bool hasVariable = node.Declaration != null && node.Declaration.Variables.Count == 1; if (!hasVariable && node.Initializers.Count != 1) { return(false); } if (node.Incrementors.Count != 1) { return(false); } var iterator = node.Incrementors.FirstOrDefault()?.Accept(_nodesVisitor) as AssignmentStatementSyntax; if (iterator == null || !iterator.IsKind(SyntaxKind.AddAssignmentStatement, SyntaxKind.SubtractAssignmentStatement)) { return(false); } var iteratorIdentifier = iterator.Left as IdentifierNameSyntax; if (iteratorIdentifier == null) { return(false); } var stepExpression = iterator.Right as LiteralExpressionSyntax; if (stepExpression == null || !(stepExpression.Token.Value is int)) { return(false); } int step = (int)stepExpression.Token.Value; if (SyntaxTokenExtensions.IsKind(iterator.OperatorToken, SyntaxKind.MinusEqualsToken)) { step = -step; } var condition = node.Condition as CSS.BinaryExpressionSyntax; if (condition == null || !(condition.Left is CSS.IdentifierNameSyntax)) { return(false); } if (((CSS.IdentifierNameSyntax)condition.Left).Identifier.IsEquivalentTo(iteratorIdentifier.Identifier)) { return(false); } ExpressionSyntax end; if (iterator.IsKind(SyntaxKind.SubtractAssignmentStatement)) { if (condition.IsKind(CS.SyntaxKind.GreaterThanOrEqualExpression)) { end = (ExpressionSyntax)condition.Right.Accept(_nodesVisitor); } else if (condition.IsKind(CS.SyntaxKind.GreaterThanExpression)) { end = SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression, (ExpressionSyntax)condition.Right.Accept(_nodesVisitor), SyntaxFactory.Token(SyntaxKind.PlusToken), CommonConversions.Literal(1)); } else { return(false); } } else { if (condition.IsKind(CS.SyntaxKind.LessThanOrEqualExpression)) { end = (ExpressionSyntax)condition.Right.Accept(_nodesVisitor); } else if (condition.IsKind(CS.SyntaxKind.LessThanExpression)) { end = SyntaxFactory.BinaryExpression(SyntaxKind.SubtractExpression, (ExpressionSyntax)condition.Right.Accept(_nodesVisitor), SyntaxFactory.Token(SyntaxKind.MinusToken), CommonConversions.Literal(1)); } else { return(false); } } VisualBasicSyntaxNode variable; ExpressionSyntax start; if (hasVariable) { var v = node.Declaration.Variables[0]; start = (ExpressionSyntax)v.Initializer?.Value.Accept(_nodesVisitor); if (start == null) { return(false); } variable = SyntaxFactory.VariableDeclarator( SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(_commonConversions.ConvertIdentifier(v.Identifier))), node.Declaration.Type.IsVar ? null : SyntaxFactory.SimpleAsClause((TypeSyntax)node.Declaration.Type.Accept(_nodesVisitor)), null ); } else { var initializer = node.Initializers.FirstOrDefault() as CSS.AssignmentExpressionSyntax; if (initializer == null || !initializer.IsKind(CS.SyntaxKind.SimpleAssignmentExpression)) { return(false); } if (!(initializer.Left is CSS.IdentifierNameSyntax)) { return(false); } if (((CSS.IdentifierNameSyntax)initializer.Left).Identifier.IsEquivalentTo(iteratorIdentifier.Identifier)) { return(false); } variable = initializer.Left.Accept(_nodesVisitor); start = (ExpressionSyntax)initializer.Right.Accept(_nodesVisitor); } block = SyntaxFactory.ForBlock( SyntaxFactory.ForStatement(variable, start, end, step == 1 ? null : SyntaxFactory.ForStepClause(CommonConversions.Literal(step))), ConvertBlock(node.Statement), SyntaxFactory.NextStatement() ); return(true); }