Пример #1
0
        public static void Go(ScalaWriter writer, WhileStatementSyntax whileStatement)
        {
            var info = new LoopInfo(whileStatement);

            info.WritePreLoop(writer);
            writer.WriteIndent();
            writer.Write("while (");
            Core.Write(writer, whileStatement.Condition);
            writer.Write(")\r\n");

            writer.WriteOpenBrace();
            info.WriteLoopOpening(writer);
            Core.WriteStatementAsBlock(writer, whileStatement.Statement, false);
            info.WriteLoopClosing(writer);
            writer.WriteCloseBrace();
            info.WritePostLoop(writer);
        }
Пример #2
0
        public static void Go(ScalaWriter writer, ForStatementSyntax forStatement)
        {
            var info = new LoopInfo(forStatement);

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

            if (forStatement.Declaration != null)
            {
                foreach (var variable in forStatement.Declaration.Variables)
                {
                    writer.WriteIndent();
                    writer.Write("var ");
                    writer.Write(WriteIdentifierName.TransformIdentifier(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();

            info.WriteLoopOpening(writer);
            Core.WriteStatementAsBlock(writer, forStatement.Statement, false);
            info.WriteLoopClosing(writer);

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

            writer.WriteCloseBrace();
            info.WritePostLoop(writer);
            writer.Indent--;
            writer.WriteLine("} //end for");
        }
        public static void Go(ScalaWriter writer, ForEachStatementSyntax foreachStatement)
        {
            var info = new LoopInfo(foreachStatement);

            var types   = Program.GetModel(foreachStatement).GetTypeInfo(foreachStatement.Expression);
            var typeStr = TypeProcessor.GenericTypeName(types.Type);

            if (types.Type is IArrayTypeSymbol)
            {
                //It's faster to "while" through arrays than "for" through them
                writer.WriteOpenBrace();
                info.WritePreLoop(writer);

                writer.WriteLine("var __foreachindex:Int = 0;");

                writer.WriteIndent();
                writer.Write("val __foreacharray = ");
                Core.Write(writer, foreachStatement.Expression);
                writer.Write(";\r\n");


                writer.WriteLine("while (__foreachindex < __foreacharray.length)");
                writer.WriteOpenBrace();

                writer.WriteIndent();
                writer.Write("val ");
                writer.Write(WriteIdentifierName.TransformIdentifier(foreachStatement.Identifier.ValueText));
                writer.Write(" = __foreacharray(__foreachindex);\r\n");

                info.WriteLoopOpening(writer);
                Core.WriteStatementAsBlock(writer, foreachStatement.Statement, false);
                info.WriteLoopClosing(writer);

                writer.WriteLine("__foreachindex += 1;");
                writer.WriteCloseBrace();

                info.WritePostLoop(writer);
                writer.WriteCloseBrace();
            }
            else if (typeStr == "System.Collections.Generic.List<>"
                     //|| typeStr == "System.Collections.Generic.Dictionary<,>"
                     || typeStr == "System.Collections.Generic.Dictionary<,>.KeyCollection" ||
                     typeStr == "System.Collections.Generic.Dictionary<,>.ValueCollection")
            {
                //It's faster to "while" over a list's iterator than to "for" through it
                writer.WriteOpenBrace();
                info.WritePreLoop(writer);

                writer.WriteIndent();
                writer.Write("val __foreachiterator = ");
                Core.Write(writer, foreachStatement.Expression);
                writer.Write(".iterator();\r\n");


                writer.WriteLine("while (__foreachiterator.hasNext())");
                writer.WriteOpenBrace();

                writer.WriteIndent();
                writer.Write("val ");
                writer.Write(WriteIdentifierName.TransformIdentifier(foreachStatement.Identifier.ValueText));
                writer.Write(" = __foreachiterator.next();\r\n");

                info.WriteLoopOpening(writer);
                Core.WriteStatementAsBlock(writer, foreachStatement.Statement, false);
                info.WriteLoopClosing(writer);
                writer.WriteCloseBrace();

                info.WritePostLoop(writer);
                writer.WriteCloseBrace();
            }
            else
            {
                info.WritePreLoop(writer);
                writer.WriteIndent();
                writer.Write("for (");
                writer.Write(WriteIdentifierName.TransformIdentifier(foreachStatement.Identifier.ValueText));
                writer.Write(" <- ");
                Core.Write(writer, foreachStatement.Expression);
                writer.Write(")\r\n");
                writer.WriteOpenBrace();
                info.WriteLoopOpening(writer);
                Core.WriteStatementAsBlock(writer, foreachStatement.Statement, false);
                info.WriteLoopClosing(writer);
                writer.WriteCloseBrace();
                info.WritePostLoop(writer);
            }
        }