当前位置: 首页>>代码示例>>C#>>正文


C# Block.ReplaceChild方法代码示例

本文整理汇总了C#中Block.ReplaceChild方法的典型用法代码示例。如果您正苦于以下问题:C# Block.ReplaceChild方法的具体用法?C# Block.ReplaceChild怎么用?C# Block.ReplaceChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Block的用法示例。


在下文中一共展示了Block.ReplaceChild方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CombineExpressions

        private void CombineExpressions(Block node)
        {
            // walk backwards because we'll be removing items as we go along.
            // and don't bother looking at the first element, because we'll be attempting to combine
            // the current element with the previous element -- and the first element (0) has no
            // previous element.
            // we will check for:
            //      1) previous=expr1; this=expr2           ==> expr1,expr2
            //      2) previous=expr1; this=for(;...)       ==> for(expr1;...)
            //      3) previous=expr1; this=for(expr2;...)  ==> for(expr1,expr2;...)
            //      4) previous=expr1; this=return expr2    ==> return expr1,expr2
            for (var ndx = node.Count - 1; ndx > 0; --ndx)
            {
                ForNode forNode;

                // see if the previous statement is an expression
                if (node[ndx - 1].IsExpression)
                {
                    ReturnNode returnNode;
                    if (node[ndx].IsExpression)
                    {
                        // transform: expr1;expr2 to expr1,expr2
                        var binOp = new BinaryOperator(node[ndx - 1].Context.Clone().CombineWith(node[ndx].Context),
                            m_parser,
                            node[ndx - 1],
                            node[ndx],
                            JSToken.Comma);

                        // replace the current node and delete the previous
                        if (node.ReplaceChild(node[ndx], binOp))
                        {
                            node.ReplaceChild(node[ndx - 1], null);
                        }
                    }
                    else if ((returnNode = node[ndx] as ReturnNode) != null)
                    {
                        // see if the return node has an expression operand
                        if (returnNode.Operand != null && returnNode.Operand.IsExpression)
                        {
                            // check for expr1[ASSIGN]expr2;return expr1 and replace with return expr1[ASSIGN]expr2
                            var beforeExpr = node[ndx - 1] as BinaryOperator;
                            if (beforeExpr != null && beforeExpr.IsAssign
                                && beforeExpr.Operand1.IsEquivalentTo(returnNode.Operand))
                            {
                                // tranaform: expr1[ASSIGN]expr2;return expr1 and replace with return expr1[ASSIGN]expr2
                                // replace the operand on the return node with the previous expression and
                                // delete the previous node
                                if (returnNode.ReplaceChild(returnNode.Operand, beforeExpr))
                                {
                                    node.ReplaceChild(node[ndx - 1], null);
                                }
                            }
                            else
                            {
                                // transform: expr1;return expr2 to return expr1,expr2
                                var binOp = new BinaryOperator(null,
                                    m_parser,
                                    node[ndx - 1],
                                    returnNode.Operand,
                                    JSToken.Comma);

                                // replace the operand on the return node with the new expression and
                                // delete the previous node
                                if (returnNode.ReplaceChild(returnNode.Operand, binOp))
                                {
                                    node.ReplaceChild(node[ndx - 1], null);
                                }
                            }
                        }
                    }
                    else if ((forNode = node[ndx] as ForNode) != null)
                    {
                        if (forNode.Initializer == null)
                        {
                            // transform: expr1;for(;...) to for(expr1;...)
                            // simply move the previous expression to the for-statement's initializer
                            forNode.SetInitializer(node[ndx - 1]);
                            node.ReplaceChild(node[ndx - 1], null);
                        }
                        else if (forNode.Initializer.IsExpression)
                        {
                            // transform: expr1;for(expr2;...) to for(expr1,expr2;...)
                            var binOp = new BinaryOperator(null,
                                m_parser,
                                node[ndx - 1],
                                forNode.Initializer,
                                JSToken.Comma);

                            // replace the initializer with the new binary operator and remove the previous node
                            if (forNode.ReplaceChild(forNode.Initializer, binOp))
                            {
                                node.ReplaceChild(node[ndx - 1], null);
                            }
                        }
                    }
                }
            }
        }
开发者ID:nuxleus,项目名称:ajaxmin,代码行数:98,代码来源:AnalyzeNodeVisitor.cs

示例2: Visit

        public override void Visit(Block node)
        {
            if (node != null)
            {
                // we might things differently if these statements are the body collection for a function
                // because we can assume the implicit return statement at the end of it
                bool isFunctionLevel = (node.Parent is FunctionObject);

                // if we want to remove debug statements...
                if (m_parser.Settings.StripDebugStatements && m_parser.Settings.IsModificationAllowed(TreeModifications.StripDebugStatements))
                {
                    // do it now before we try doing other things
                    StripDebugStatements(node);
                }

                // analyze all the statements in our block and recurse them
                if (node.BlockScope != null)
                {
                    ScopeStack.Push(node.BlockScope);
                }
                try
                {
                    // call the base class to recurse
                    base.Visit(node);
                }
                finally
                {
                    if (node.BlockScope != null)
                    {
                        ScopeStack.Pop();
                    }
                }

                if (m_parser.Settings.RemoveUnneededCode)
                {
                    // go forward, and check the count each iteration because we might be ADDING statements to the block.
                    // let's look at all our if-statements. If a true-clause ends in a return, then we don't
                    // need the else-clause; we can pull its statements out and stick them after the if-statement.
                    // also, if we encounter a return-, break- or continue-statement, we can axe everything after it
                    for (var ndx = 0; ndx < node.Count; ++ndx)
                    {
                        // see if it's an if-statement with both a true and a false block
                        var ifNode = node[ndx] as IfNode;
                        if (ifNode != null
                            && ifNode.TrueBlock != null
                            && ifNode.TrueBlock.Count > 0
                            && ifNode.FalseBlock != null)
                        {
                            // now check to see if the true block ends in a return statement
                            if (ifNode.TrueBlock[ifNode.TrueBlock.Count - 1] is ReturnNode)
                            {
                                // transform: if(cond){statements1;return}else{statements2} to if(cond){statements1;return}statements2
                                // it does. insert all the false-block statements after the if-statement
                                node.InsertRange(ndx + 1, ifNode.FalseBlock.Children);

                                // and then remove the false block altogether
                                ifNode.ReplaceChild(ifNode.FalseBlock, null);
                            }
                        }
                        else if (node[ndx] is ReturnNode
                            || node[ndx] is Break
                            || node[ndx] is ContinueNode)
                        {
                            // we have a return node -- no statments afterwards will be executed, so clear them out.
                            // transform: {...;return;...} to {...;return}
                            // transform: {...;break;...} to {...;break}
                            // transform: {...;continue;...} to {...;continue}
                            // we've found a return statement, and it's not the last statement in the function.
                            // walk the rest of the statements and delete anything that isn't a function declaration
                            // or a var statement.
                            for (var ndxRemove = node.Count - 1; ndxRemove > ndx; --ndxRemove)
                            {
                                var funcObject = node[ndxRemove] as FunctionObject;
                                if (funcObject == null || funcObject.FunctionType != FunctionType.Declaration)
                                {
                                    var varStatement = node[ndxRemove] as Var;
                                    if (varStatement != null)
                                    {
                                        // var statements can't be removed, but any initializers should
                                        // be deleted since they won't get executed.
                                        for (var ndxDecl = 0; ndxDecl < varStatement.Count; ++ndxDecl)
                                        {
                                            if (varStatement[ndxDecl].Initializer != null)
                                            {
                                                varStatement[ndxDecl].ReplaceChild(varStatement[ndxDecl].Initializer, null);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // not a function declaration, and not a var statement -- get rid of it
                                        node.RemoveAt(ndxRemove);
                                    }
                                }
                            }
                        }
                    }
                }

                // now check the last statement -- if it's an if-statement where the true-block is a single return
//.........这里部分代码省略.........
开发者ID:nuxleus,项目名称:ajaxmin,代码行数:101,代码来源:AnalyzeNodeVisitor.cs


注:本文中的Block.ReplaceChild方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。