Beispiel #1
0
        public void VisitForStatement(ForStatementSyntax node)
        {
            DatabaseAccessingForLoopDeclaration <LINQToSQL> dbAccessingFor =
                (from n in node.DescendantNodes().OfType <IdentifierNameSyntax>()
                 from v in _databaseQueryVariables.Keys
                 where n.Identifier.Text == v.DescendantNodes().OfType <VariableDeclaratorSyntax>().First().Identifier.Text
                 select new DatabaseAccessingForLoopDeclaration <LINQToSQL>()).FirstOrDefault();

            if (dbAccessingFor != null)
            {
                DatabaseAccessingLoopDeclarations.Add(dbAccessingFor);
            }
            else
            {
                LoopDeclarations.Add(new ForLoopDeclaration());
            }
        }
Beispiel #2
0
        private static int AnalyzeForSyntax(ForStatementSyntax forSyntax, int nesting)
        {
            int score = 1;

            score += forSyntax.Declaration.DescendantNodesAndSelf().Where(t => t is InvocationExpressionSyntax).Count();

            score += forSyntax.Incrementors.Where(t => t is InvocationExpressionSyntax).Count();

            if (forSyntax.Condition is BinaryExpressionSyntax bes)
            {
                var leftNested  = bes.Left.DescendantNodesAndSelf().OfType <InvocationExpressionSyntax>();
                var rightNested = bes.Right.DescendantNodesAndSelf().OfType <InvocationExpressionSyntax>();
                score += leftNested.Count() + rightNested.Count();
            }

            var nested = forSyntax.DescendantNodes().OfType <StatementSyntax>();

            score += AnalyzeStatements(new SyntaxList <StatementSyntax>(nested), nesting + 1);

            return(score);
        }
Beispiel #3
0
        public static void Go(HaxeWriter writer, ForStatementSyntax forStatement)
        {
            if (forStatement.DescendantNodes().OfType <ContinueStatementSyntax>().Any())
            {
                throw new Exception("Cannot use \"continue\" in a \"for\" loop.  Consider changing to a while loop instead. " + Utility.Descriptor(forStatement));
            }

            writer.WriteLine("{ //for");
            writer.Indent++;

            if (forStatement.Declaration != null)
            {
                foreach (var variable in forStatement.Declaration.Variables)
                {
                    writer.WriteIndent();
                    writer.Write("var ");
                    writer.Write(variable.Identifier.ValueText);
                    writer.Write(TypeProcessor.ConvertTypeWithColon(forStatement.Declaration.Type));

                    if (variable.Initializer != null)
                    {
                        writer.Write(" = ");
                        Core.Write(writer, variable.Initializer.Value);
                    }

                    writer.Write(";\r\n");
                }
            }

            foreach (var init in forStatement.Initializers)
            {
                writer.WriteIndent();
                Core.Write(writer, init);
                writer.Write(";\r\n");
            }

            writer.WriteIndent();
            writer.Write("while (");

            if (forStatement.Condition == null)
            {
                writer.Write("true");
            }
            else
            {
                Core.Write(writer, forStatement.Condition);
            }

            writer.Write(")\r\n");
            writer.WriteOpenBrace();

            if (forStatement.Statement is BlockSyntax)
            {
                foreach (var statement in forStatement.Statement.As <BlockSyntax>().Statements)
                {
                    Core.Write(writer, statement);
                }
            }
            else
            {
                Core.Write(writer, forStatement.Statement);
            }

            foreach (var iterator in forStatement.Incrementors)
            {
                writer.WriteIndent();
                Core.Write(writer, iterator);
                writer.Write(";\r\n");
            }

            writer.WriteCloseBrace();
            writer.Indent--;
            writer.WriteLine("} //end for");
        }