public override StatementSyntax Visit(ConversionContext context, IfStmt ifStmt)
        {
            var condition       = ifStmt.getCondition();
            var conditionSyntax = ExpressionVisitor.VisitExpression(context, condition);

            var thenStmt   = ifStmt.getThenStmt();
            var thenSyntax = StatementVisitor.VisitStatement(context, thenStmt);

            if (thenSyntax == null)
            {
                return(null);
            }

            var elseStmt = ifStmt.getElseStmt();

            if (elseStmt == null)
            {
                return(SyntaxFactory.IfStatement(conditionSyntax, thenSyntax));
            }

            var elseStatementSyntax = StatementVisitor.VisitStatement(context, elseStmt);
            var elseSyntax          = SyntaxFactory.ElseClause(elseStatementSyntax);

            if (elseSyntax == null)
            {
                return(SyntaxFactory.IfStatement(conditionSyntax, thenSyntax));
            }

            return(SyntaxFactory.IfStatement(conditionSyntax, thenSyntax, elseSyntax));
        }
        public override StatementSyntax Visit(ConversionContext context, ForeachStmt foreachStmt)
        {
            var iterableExpr   = foreachStmt.getIterable();
            var iterableSyntax = ExpressionVisitor.VisitExpression(context, iterableExpr);

            var varExpr = foreachStmt.getVariable();
            var type    = TypeHelper.ConvertType(varExpr.getType().toString());

            var vars = varExpr.getVars()
                       .ToList <VariableDeclarator>()
                       .Select(i => SyntaxFactory.VariableDeclarator(i.toString()))
                       .ToArray();

            var body       = foreachStmt.getBody();
            var bodySyntax = StatementVisitor.VisitStatement(context, body);

            if (bodySyntax == null)
            {
                return(null);
            }

            var statementSyntax = SyntaxFactory.ForEachStatement(SyntaxFactory.ParseTypeName(type), vars[0].Identifier.ValueText, iterableSyntax, bodySyntax);

            return(statementSyntax);
        }
        public override StatementSyntax Visit(ConversionContext context, DoStmt statement)
        {
            var condition       = statement.getCondition();
            var conditionSyntax = ExpressionVisitor.VisitExpression(context, condition);

            var body       = statement.getBody();
            var bodySyntax = StatementVisitor.VisitStatement(context, body);

            return(SyntaxFactory.DoStatement(bodySyntax, conditionSyntax).AddComment(context, statement));
        }
        public override StatementSyntax Visit(ConversionContext context, WhileStmt whileStmt)
        {
            var expr   = whileStmt.getCondition();
            var syntax = ExpressionVisitor.VisitExpression(context, expr);

            var body       = whileStmt.getBody();
            var bodySyntax = StatementVisitor.VisitStatement(context, body);

            return(SyntaxFactory.WhileStatement(syntax, bodySyntax ?? SyntaxFactory.EmptyStatement()));
        }
Exemple #5
0
        public override StatementSyntax Visit(ConversionContext context, LabeledStmt labeledStmt)
        {
            var statement = labeledStmt.getStmt();
            var syntax    = StatementVisitor.VisitStatement(context, statement);

            if (syntax == null)
            {
                return(null);
            }

            return(SyntaxFactory.LabeledStatement(labeledStmt.getLabel(), syntax));
        }
        public override StatementSyntax Visit(ConversionContext context, WhileStmt whileStmt)
        {
            var expr   = whileStmt.getCondition();
            var syntax = ExpressionVisitor.VisitExpression(context, expr);

            var body       = whileStmt.getBody();
            var bodySyntax = StatementVisitor.VisitStatement(context, body);

            if (bodySyntax == null)
            {
                return(null);
            }

            return(SyntaxFactory.WhileStatement(syntax, bodySyntax).AddComment(context, whileStmt));
        }
Exemple #7
0
        public override StatementSyntax Visit(ConversionContext context, ForStmt forStmt)
        {
            var inits = forStmt.getInit().ToList <Expression>();

            var initSyntaxes = new List <ExpressionSyntax>();
            VariableDeclarationSyntax varSyntax = null;

            if (inits != null)
            {
                foreach (var init in inits)
                {
                    if (init is VariableDeclarationExpr)
                    {
                        var varExpr = init as VariableDeclarationExpr;

                        var type = TypeHelper.ConvertType(varExpr.getType().toString());

                        var vars = varExpr.getVars()
                                   .ToList <VariableDeclarator>()
                                   .Select(i => SyntaxFactory.VariableDeclarator(i.toString()))
                                   .ToArray();

                        varSyntax = SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName(type), SyntaxFactory.SeparatedList(vars, Enumerable.Repeat(SyntaxFactory.Token(SyntaxKind.CommaToken), vars.Length - 1)));
                    }
                    else
                    {
                        var initSyntax = ExpressionVisitor.VisitExpression(context, init);
                        initSyntaxes.Add(initSyntax);
                    }
                }
            }

            var condition       = forStmt.getCompare();
            var conditionSyntax = ExpressionVisitor.VisitExpression(context, condition);

            var increments        = forStmt.getUpdate().ToList <Expression>();
            var incrementSyntaxes = new List <ExpressionSyntax>();

            if (increments != null)
            {
                foreach (var increment in increments)
                {
                    var incrementSyntax = ExpressionVisitor.VisitExpression(context, increment);
                    incrementSyntaxes.Add(incrementSyntax);
                }
            }

            var body       = forStmt.getBody();
            var bodySyntax = StatementVisitor.VisitStatement(context, body);

            if (bodySyntax == null)
            {
                return(null);
            }

            var statementSyntax = SyntaxFactory.ForStatement(bodySyntax)
                                  .WithDeclaration(varSyntax)
                                  .AddInitializers(initSyntaxes.ToArray())
                                  .WithCondition(conditionSyntax)
                                  .AddIncrementors(incrementSyntaxes.ToArray());

            return(statementSyntax);
        }