Exemplo n.º 1
0
        public JNode VisitForeachStatement(ForeachStatement node)
        {
            var node2 = new JForInStatement
            {
                Initializer = J.Var(node.VariableName, node.VariableType.Resolve().Type.JAccess()),
                Member      = VisitExpression(node.InExpression),
                Statement   = VisitStatement(node.EmbeddedStatement)
            };

            return(node2);
        }
Exemplo n.º 2
0
 public void VisitForInStatement(JForInStatement node)
 {
     Write("for");
     Write("(");
     Visit(node.Initializer);
     Write(" : ");
     Visit(node.Member);
     Write(")");
     if (OpenBraceInNewLine)
     {
         WriteLine();
     }
     IndentVisitIfNeeded(node.Statement);
 }
Exemplo n.º 3
0
        public JNode VisitForeachStatement_old(ForeachStatement node)
        {
            if (node.InExpression != null)
            {
                var expRes       = node.InExpression.Resolve();
                var et           = expRes.Type.GetDefinitionOrArrayType(Compiler);
                var iteratorType = expRes.Type;
                //var et = node.expression.entity_typeref.GetEntityType();
                if (et != null)
                {
                    var jta = JMeta.GetJsTypeAttribute(et);
                    if (jta != null && jta.NativeEnumerator)
                    {
                        var node2 = new JForInStatement
                        {
                            Initializer = J.Var(node.VariableName, node.Resolve().Type.JAccess()),
                            Member      = VisitExpression(node.InExpression),
                            Statement   = VisitStatement(node.EmbeddedStatement)
                        };
                        return(node2);
                    }
                    else if (jta != null && jta.NativeArrayEnumerator)
                    {
                        VariableIteratorCounter++;
                        var iteratorName    = "$i" + VariableIteratorCounter;
                        var lengthCacheName = "$l" + VariableIteratorCounter;
                        var exp2            = VisitExpression(node.InExpression);
                        var target          = exp2;
                        var targetCacheName = "$t" + VariableIteratorCounter;
                        if (exp2 is JMemberExpression || ((JMemberExpression)exp2).PreviousMember != null)//is not simple name
                        {
                            target = J.Member(targetCacheName);
                        }
                        var itemAccess = target.IndexerAccess(J.Member(iteratorName));
                        var node2      = new JForStatement();

                        node2.Condition = J.Member(iteratorName).LessThan(J.Member(lengthCacheName));
                        node2.Iterators = new List <JStatement> {
                            J.Member(iteratorName).PlusPlus().Statement(), J.Member(node.VariableName).Assign(itemAccess).Statement()
                        };
                        if (target != exp2)//use target caching
                        {
                            node2.Initializers = new List <JStatement> {
                                J.Var(iteratorName, iteratorType.JAccess(), J.Value(0)).AndVar(targetCacheName, exp2).AndVar(lengthCacheName, target.Member("length")).AndVar(node.VariableName, itemAccess).Statement()
                            };
                        }
                        else
                        {
                            node2.Initializers = new List <JStatement> {
                                J.Var(iteratorName, iteratorType.JAccess(), J.Value(0)).AndVar(lengthCacheName, exp2.Member("length")).AndVar(node.VariableName, itemAccess).Statement()
                            };
                        }
                        node2.Statement = VisitStatement(node.EmbeddedStatement);
                        return(node2);
                    }
                }
            }

            var iteratorName2 = "$it" + VariableIteratorCounter;

            VariableIteratorCounter++;
            var    node3               = J.Var(iteratorName2, node.Resolve().Type.JAccess(), VisitExpression(node.InExpression).Member("GetEnumerator").Invoke()).Statement();
            var    whileNode           = J.While(J.Member(iteratorName2).Member("MoveNext").Invoke());
            var    getCurrentStatement = J.Var(node.VariableName, node.Resolve().Type.JAccess(), J.Member(iteratorName2).Member("get_Current").Invoke()).Statement();
            var    jsStatement         = VisitStatement(node.EmbeddedStatement);
            JBlock block;

            if (jsStatement is JBlock)
            {
                block = (JBlock)jsStatement;
            }
            else
            {
                block = J.Block().Add(jsStatement);
            }
            block.Statements.Insert(0, getCurrentStatement);
            whileNode.Statement = block;

            var block2 = J.Block().Add(node3).Add(whileNode);

            return(block2);
        }