Example #1
0
        public override void VisitForStatement(JsForStatement node)
        {
            output.Append("for (");
            if (node.Declaration != null)
            {
                node.Declaration.Accept(this);
            }
            else
            {
                for (var i = 0; i < node.Initializers.Count; i++)
                {
                    var initializer = node.Initializers[i];
                    initializer.Accept(this);
                    if (i < node.Initializers.Count - 1)
                    {
                        output.Append(", ");
                    }
                }
            }
            output.Append("; ");
            node.Condition.Accept(this);
            output.Append("; ");
            for (var i = 0; i < node.Incrementors.Count; i++)
            {
                node.Incrementors[i].Accept(this);
                if (i < node.Incrementors.Count - 1)
                {
                    output.Append(", ");
                }
            }
            output.Append(")");

            WriteMaybeBlock(node.Body, true);
        }
        public override JsStatement VisitForStatement(JsForStatement statement, object data)
        {
            bool old = _unnamedIsMatch;

            _unnamedIsMatch = false;
            VisitStatement(statement.Body, null);
            _unnamedIsMatch = old;
            return(statement);
        }
Example #3
0
        public override JsNode Visit(JsForStatement node)
        {
            if (loop != null)
            {
                return(node);
            }
            loop = node;

            node.Body = TransformBody((JsStatement)node.Body.Accept(this));
            return(node);
        }
        public virtual JsStatement VisitForStatement(JsForStatement statement, TData data)
        {
            var initStatement = statement.InitStatement != null?VisitStatement(statement.InitStatement, data)        : null;

            var condition = statement.ConditionExpression != null?VisitExpression(statement.ConditionExpression, data) : null;

            var iterator = statement.IteratorExpression != null?VisitExpression(statement.IteratorExpression, data)  : null;

            var body = VisitStatement(statement.Body, data);

            return(ReferenceEquals(initStatement, statement.InitStatement) && ReferenceEquals(condition, statement.ConditionExpression) && ReferenceEquals(iterator, statement.IteratorExpression) && ReferenceEquals(body, statement.Body)
                             ? statement
                             : new JsForStatement(initStatement, condition, iterator, body));
        }
Example #5
0
 private bool NeedsBreakBeforeForLoop(IList <JsStatement> currentBlock, JsForStatement stmt, StackEntry location)
 {
     if (NeedsBreakBeforeLoop(currentBlock))
     {
         return(true);
     }
     else if (!(stmt.InitStatement is JsEmptyStatement) && !location.AfterForInitializer)
     {
         return(true);                   // If we have an initializer, the current location must state explicitly to have handled it.
     }
     else
     {
         return(false);
     }
 }
Example #6
0
 public virtual void Visit(JsForStatement node)
 {
     DefaultVisit(node);
     if (node.Declaration != null)
     {
         node.Declaration.Accept(this);
     }
     if (node.Condition != null)
     {
         node.Condition.Accept(this);
     }
     foreach (var incrementor in node.Incrementors)
     {
         incrementor.Accept(this);
     }
 }
Example #7
0
        public JsNode VisitForStatement(ForStatement node)
        {
            var node2 = new JsForStatement
            {
                Condition = VisitExpression(node.Condition),
                Statement = VisitStatement(node.EmbeddedStatement),
            };

            if (node.Iterators != null)
            {
                node2.Iterators = VisitStatements(node.Iterators);
            }
            if (node.Initializers != null)
            {
                node2.Initializers = VisitStatements(node.Initializers);
            }
            return(node2);
        }
        public override JsStatement VisitForStatement(JsForStatement statement, object data)
        {
            var initStatement = statement.InitStatement != null?VisitStatement(statement.InitStatement, data)        : null;

            var condition = statement.ConditionExpression != null?VisitExpression(statement.ConditionExpression, data) : null;

            var iterator = statement.IteratorExpression != null?VisitExpression(statement.IteratorExpression, data)  : null;

            var body = VisitStatement(statement.Body, data);

            if (initStatement is JsBlockStatement)                      // Will happen if the init statement is a variable declaration without initializers.
            {
                Debug.Assert(((JsBlockStatement)initStatement).Statements.Count == 0);
                initStatement = new JsEmptyStatement();
            }

            return(ReferenceEquals(initStatement, statement.InitStatement) && ReferenceEquals(condition, statement.ConditionExpression) && ReferenceEquals(iterator, statement.IteratorExpression) && ReferenceEquals(body, statement.Body)
                             ? statement
                             : new JsForStatement(initStatement, condition, iterator, body));
        }
Example #9
0
 public virtual JsNode Visit(JsForStatement node)
 {
     return(DefaultVisit(node, x =>
     {
         if (x.Declaration != null)
         {
             x.Declaration = (JsVariableDeclaration)x.Declaration.Accept(this);
         }
         if (x.Condition != null)
         {
             x.Condition = (JsExpression)x.Condition.Accept(this);
         }
         for (var i = 0; i < x.Incrementors.Count; i++)
         {
             x.Incrementors[i] = (JsExpression)x.Incrementors[i].Accept(this);
         }
         x.Body = (JsStatement)x.Body.Accept(this);
         return x;
     }));
 }
Example #10
0
        public object VisitForStatement(JsForStatement statement, bool addNewline)
        {
            _cb.Append("for").Append(_space + "(");
            VisitStatement(statement.InitStatement, false);

            if (statement.ConditionExpression != null)
            {
                _cb.Append(_space);
                VisitExpression(statement.ConditionExpression, false);
            }
            _cb.Append(";");

            if (statement.IteratorExpression != null)
            {
                _cb.Append(_space);
                VisitExpression(statement.IteratorExpression, false);
            }
            _cb.Append(")" + _space);
            VisitStatement(statement.Body, addNewline);
            return(null);
        }
        private bool HandleForStatement(JsForStatement stmt, StackEntry location, ImmutableStack <StackEntry> stack, ImmutableStack <Tuple <string, State> > breakStack, ImmutableStack <Tuple <string, State> > continueStack, State currentState, State returnState, IList <JsStatement> currentBlock, bool isFirstStatement)
        {
            if (!(isFirstStatement && (stmt.InitStatement is JsEmptyStatement || location.AfterForInitializer)))
            {
                // We have to create a new block for the statement.
                var topOfLoopState = CreateNewStateValue(currentState.FinallyStack);
                Enqueue(stack.Push(new StackEntry(location.Block, location.Index, true)), breakStack, continueStack, topOfLoopState, returnState);
                if (!(stmt.InitStatement is JsEmptyStatement))
                {
                    currentBlock.Add(stmt.InitStatement);
                }
                currentBlock.Add(new JsGotoStateStatement(topOfLoopState, currentState));
                return(false);
            }
            else
            {
                var iteratorState  = (stmt.IteratorExpression != null ? CreateNewStateValue(currentState.FinallyStack) : currentState);
                var afterLoopState = GetStateAfterStatement(location, stack, currentState.FinallyStack, returnState);

                if (stmt.ConditionExpression != null)
                {
                    currentBlock.Add(JsStatement.If(JsExpression.LogicalNot(stmt.ConditionExpression), new JsGotoStateStatement(afterLoopState.Item1, currentState), null));
                }
                string currentName = GetLabelForState(currentState);
                currentBlock.AddRange(Handle(ImmutableStack <StackEntry> .Empty.Push(new StackEntry(stmt.Body, 0)), breakStack.Push(Tuple.Create(currentName, afterLoopState.Item1)), continueStack.Push(Tuple.Create(currentName, iteratorState)), currentState, iteratorState, false, false));

                if (stmt.IteratorExpression != null)
                {
                    Enqueue(ImmutableStack <StackEntry> .Empty.Push(new StackEntry(JsStatement.Block(stmt.IteratorExpression), 0)), breakStack, continueStack, iteratorState, currentState);
                }

                if (!stack.IsEmpty || location.Index < location.Block.Statements.Count - 1)
                {
                    Enqueue(PushFollowing(stack, location), breakStack, continueStack, afterLoopState.Item1, returnState);
                }

                return(false);
            }
        }
        private bool HandleForStatement(JsForStatement stmt, StackEntry location, ImmutableStack<StackEntry> stack, ImmutableStack<Tuple<string, State>> breakStack, ImmutableStack<Tuple<string, State>> continueStack, State currentState, State returnState, IList<JsStatement> currentBlock)
        {
            if (currentBlock.Count > 0 || (!(stmt.InitStatement is JsEmptyStatement) && !location.AfterForInitializer)) {
                // We have to create a new block for the statement.
                var topOfLoopState = CreateNewStateValue(currentState.FinallyStack);
                Enqueue(stack.Push(new StackEntry(location.Block, location.Index, true)), breakStack, continueStack, topOfLoopState, returnState);
                if (!(stmt.InitStatement is JsEmptyStatement))
                    currentBlock.Add(stmt.InitStatement);
                currentBlock.Add(new JsGotoStateStatement(topOfLoopState, currentState));
                return false;
            }
            else {
                var iteratorState = (stmt.IteratorExpression != null ? CreateNewStateValue(currentState.FinallyStack) : currentState);
                var afterLoopState = GetStateAfterStatement(location, stack, currentState.FinallyStack, returnState);

                if (stmt.ConditionExpression != null)
                    currentBlock.Add(new JsIfStatement(JsExpression.LogicalNot(stmt.ConditionExpression), new JsGotoStateStatement(afterLoopState.Item1, currentState), null));
                string currentName = GetLabelForState(currentState);
                currentBlock.AddRange(Handle(ImmutableStack<StackEntry>.Empty.Push(new StackEntry(stmt.Body, 0)), breakStack.Push(Tuple.Create(currentName, afterLoopState.Item1)), continueStack.Push(Tuple.Create(currentName, iteratorState)), currentState, iteratorState));

                if (stmt.IteratorExpression != null) {
                    Enqueue(ImmutableStack<StackEntry>.Empty.Push(new StackEntry(JsBlockStatement.MakeBlock(new JsExpressionStatement(stmt.IteratorExpression)), 0)), breakStack, continueStack, iteratorState, currentState);
                }

                if (!stack.IsEmpty || location.Index < location.Block.Statements.Count - 1) {
                    Enqueue(PushFollowing(stack, location), breakStack, continueStack, afterLoopState.Item1, returnState);
                }

                return false;
            }
        }
Example #13
0
        public JsNode VisitForeachStatement(ForeachStatement node)
        {
            if (node.InExpression != null)
            {
                var expRes = node.InExpression.Resolve();
                var et     = expRes.Type.GetDefinitionOrArrayType();
                //var et = node.expression.entity_typeref.GetEntityType();
                if (et != null)
                {
                    var jta = Sk.GetJsTypeAttribute(et);
                    if (jta != null && jta.NativeEnumerator)
                    {
                        var node2 = new JsForInStatement
                        {
                            Initializer = Js.Var(node.VariableName),
                            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.NodeType != JsNodeType.MemberExpression || ((JsMemberExpression)exp2).PreviousMember != null)                        //is not simple name
                        {
                            target = Js.Member(targetCacheName);
                        }
                        var itemAccess = target.IndexerAccess(Js.Member(iteratorName));
                        var node2      = new JsForStatement();

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

            var iteratorName2 = "$it" + VariableIteratorCounter;

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

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

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

            return(block2);
        }
Example #14
0
 public void Visit(JsForStatement node)
 {
     BeforeVisit(node);
     DefaultVisit(node, VisitForStatement);
     AfterVisit(node);
 }
Example #15
0
 public virtual void VisitForStatement(JsForStatement node)
 {
 }
        public override JsStatement VisitForStatement(JsForStatement statement, object data)
        {
            var body = VisitLoopBody(statement.Body, data);

            return(ReferenceEquals(body, statement.Body) ? statement : new JsForStatement(statement.InitStatement, statement.ConditionExpression, statement.IteratorExpression, body));
        }
 private bool NeedsBreakBeforeForLoop(IList<JsStatement> currentBlock, JsForStatement stmt, StackEntry location)
 {
     if (NeedsBreakBeforeLoop(currentBlock))
         return true;
     else if (!(stmt.InitStatement is JsEmptyStatement) && !location.AfterForInitializer)
         return true;	// If we have an initializer, the current location must state explicitly to have handled it.
     else
         return false;
 }