Esempio n. 1
0
        public override void Visit(WhileNode node)
        {
            if (node != null)
            {
                if (m_parser.Settings.StripDebugStatements
                     && m_parser.Settings.IsModificationAllowed(TreeModifications.StripDebugStatements)
                     && node.Body != null
                     && node.Body.IsDebuggerStatement)
                {
                    node.ReplaceChild(node.Body, null);
                }

                // recurse
                base.Visit(node);

                // if the body is now empty, make it null
                if (node.Body != null && node.Body.Count == 0)
                {
                    node.ReplaceChild(node.Body, null);
                }
            }
        }
 public void Visit(WhileNode node)
 {
     // invalid! ignore
     IsValid = false;
 }
 public void Visit(WhileNode node)
 {
     Debug.Fail("shouldn't get here");
 }
 public void Visit(WhileNode node)
 {
     // starts with 'while', so we don't care
 }
Esempio n. 5
0
        public override void Visit(WhileNode node)
        {
            if (node != null)
            {
                // depth-first
                base.Visit(node);

                // see if the condition is a constant
                if (m_parser.Settings.IsModificationAllowed(TreeModifications.EvaluateNumericExpressions))
                {
                    ConstantWrapper constantCondition = node.Condition as ConstantWrapper;
                    if (constantCondition != null)
                    {
                        // TODO: (someday) we'd RATHER eliminate the statement altogether if the condition is always false,
                        // but we'd need to make sure var'd variables and declared functions are properly handled.
                        try
                        {
                            bool isTrue = constantCondition.ToBoolean();
                            if (isTrue)
                            {
                                // the condition is always true; we should change it to a for(;;) statement.
                                // less bytes than while(1)

                                // check to see if we want to combine a preceding var with a for-statement
                                AstNode initializer = null;
                                if (m_parser.Settings.IsModificationAllowed(TreeModifications.MoveVarIntoFor))
                                {
                                    // if the previous statement is a var, we can move it to the initializer
                                    // and save even more bytes. The parent should always be a block. If not,
                                    // then assume there is no previous.
                                    Block parentBlock = node.Parent as Block;
                                    if (parentBlock != null)
                                    {
                                        int whileIndex = parentBlock.StatementIndex(node);
                                        if (whileIndex > 0)
                                        {
                                            Var previousVar = parentBlock[whileIndex - 1] as Var;
                                            if (previousVar != null)
                                            {
                                                initializer = previousVar;
                                                parentBlock.ReplaceChild(previousVar, null);
                                            }
                                        }
                                    }
                                }

                                // create the for using our body and replace ourselves with it
                                ForNode forNode = new ForNode(node.Context, m_parser, initializer, null, null, node.Body);
                                node.Parent.ReplaceChild(node, forNode);
                            }
                            else if (constantCondition.IsNotOneOrPositiveZero)
                            {
                                // the condition is always false, so we can replace the condition
                                // with a zero -- only one byte
                                node.ReplaceChild(node.Condition, new ConstantWrapper(0, PrimitiveType.Number, null, m_parser));
                            }
                        }
                        catch (InvalidCastException)
                        {
                            // ignore any invalid cast exceptions
                        }
                    }
                }
            }
        }