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


C# Block.RemoveAt方法代码示例

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


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

示例1: RemoveUnreachableCode

 public static int RemoveUnreachableCode(Block block, Statement statement, int index)
 {
     if (statement is Statement.If)
     {
         Statement.If _if = (Statement.If)statement;
         int v;
         if (_if.Condition.TryGetIntValue(out v))
         {
             if (v == 0)
             {
                 block.RemoveAt(index);
                 block.InsertRange(index, _if.ElseDoThis);
                 return index;
             }
             else
             {
                 block.RemoveAt(index);
                 block.InsertRange(index, _if.ThenDoThis);
                 return index;
             }
         }
     }
     else if (statement is Statement.While)
     {
         Statement.While loop = (Statement.While)statement;
         int v;
         if (loop.WhileThisIsTrue.TryGetIntValue(out v) && v == 0)
         {
             block.RemoveAt(index);
             return index;
         }
     }
     else if (statement is Statement.Return)
     {
         block.RemoveRange(index + 1, block.Count - (index + 1));
     }
     return index + 1;
 }
开发者ID:duncanc,项目名称:Red-Herring-Farm,代码行数:38,代码来源:CodeFilter.cs

示例2: UnnestBlocks

        // unnest any child blocks
        private void UnnestBlocks(Block node)
        {
            // before we unnest the blocks or do anything else, let's identify any directive prologues.
            // if the parent is null (we are the program) or a function object...
            if (node.Parent == null || node.Parent is FunctionObject)
            {
                // directive prologues are the first n statements of a program or function body that
                // are expression statements consisting entirely of a string literal. So let's walk from
                // the beginning until we find the first statement that is not a ConstantWrapper of primitive
                // type string. Mark the ones we find as directive prologues
                for (var ndx = 0; ndx < node.Count; ++ndx)
                {
                    var constWrapper = node[ndx] as ConstantWrapper;
                    if (constWrapper == null || constWrapper.PrimitiveType != PrimitiveType.String)
                    {
                        // not a constant, not a string -- we are done searching!
                        break;
                    }

                    // otherwise it is a directive prologue. Mark it as such
                    constWrapper.IsDirectivePrologue = true;

                    // if it's a "use strict" prologue, let's mark the appropriate scope as strict
                    if (string.CompareOrdinal(constWrapper.ToString(), "use strict") == 0)
                    {
                        // the actual code cannot contain any escape sequences or line-contunations,
                        // so check the context code to make sure it ALSO is the right text. Be sure
                        // not to compare with the quote delimiters.
                        if (constWrapper.Context == null
                            || string.CompareOrdinal(constWrapper.Context.Code, 1, "use strict", 0, 10) == 0)
                        {
                            var funcObject = node.Parent as FunctionObject;
                            if (funcObject != null)
                            {
                                // function scope
                                funcObject.FunctionScope.UseStrict = true;
                            }
                            else
                            {
                                // global scope
                                node.Parser.GlobalScope.UseStrict = true;
                            }
                        }
                    }
                }
            }

            // walk the list of items backwards -- if we come
            // to any blocks, unnest the block recursively. We walk backwards because
            // we could be adding any number of statements and we don't want to have
            // to modify the counter
            for (int ndx = node.Count - 1; ndx >= 0; --ndx)
            {
                var nestedBlock = node[ndx] as Block;
                if (nestedBlock != null)
                {
                    // unnest recursively
                    UnnestBlocks(nestedBlock);

                    // remove the nested block
                    node.RemoveAt(ndx);

                    // then start adding the statements in the nested block to our own.
                    // go backwards so we can just keep using the same index
                    node.InsertRange(ndx, nestedBlock.Children);
                }
                else if (ndx > 0)
                {
                    // see if the previous node is a conditional-compilation comment, because
                    // we will also combine adjacent those
                    var previousComment = node[ndx - 1] as ConditionalCompilationComment;
                    if (previousComment != null)
                    {
                        ConditionalCompilationComment thisComment = node[ndx] as ConditionalCompilationComment;
                        if (thisComment != null)
                        {
                            // two adjacent conditional comments -- combine them into the first.
                            previousComment.Statements.Append(thisComment.Statements);

                            // and remove the second one (which is now a duplicate)
                            node.RemoveAt(ndx);
                        }
                    }
                }
            }
        }
开发者ID:nuxleus,项目名称:ajaxmin,代码行数:87,代码来源:ReorderScopeVisitor.cs

示例3: RelocateVar

        private static int RelocateVar(Block block, int insertAt, Var varStatement)
        {
            // if the var statement is at the next position to insert, then we don't need
            // to do anything.
            if (block[insertAt] != varStatement && !InsideConditionalComment(varStatement))
            {
                // check to see if the current position is a var and we are the NEXT statement.
                // if that's the case, we don't need to break out the initializer, just append all the
                // vardecls as-is to the current position.
                var existingVar = block[insertAt] as Var;
                if (existingVar != null && block[insertAt + 1] == varStatement)
                {
                    // just append our vardecls to the insertion point, then delete our statement
                    existingVar.Append(varStatement);
                    block.RemoveAt(insertAt + 1);
                }
                else
                {
                    // iterate through the decls and count how many have initializers
                    var initializerCount = 0;
                    for (var ndx = 0; ndx < varStatement.Count; ++ndx)
                    {
                        if (varStatement[ndx].Initializer != null)
                        {
                            ++initializerCount;
                        }
                    }

                    // if there are more than two decls with initializers, then we won't actually
                    // be gaining anything by moving the var to the top. We'll get rid of the four
                    // bytes for the "var ", but we'll be adding two bytes for the name and comma
                    // because name=init will still need to remain behind.
                    if (initializerCount <= 2)
                    {
                        // first iterate through all the declarations in the var statement,
                        // constructing an expression statement that is made up of assignment
                        // operators for each of the declarations that have initializers (if any)
                        // and removing all the initializers
                        var assignments = new List<AstNode>();
                        for (var ndx = 0; ndx < varStatement.Count; ++ndx)
                        {
                            var varDecl = varStatement[ndx];
                            if (varDecl.Initializer != null)
                            {
                                if (varDecl.IsCCSpecialCase)
                                {
                                    // create a vardecl with the same name and no initializer
                                    var copyDecl = new VariableDeclaration(
                                        varDecl.Context,
                                        varDecl.Parser,
                                        varDecl.Identifier,
                                        varDecl.Field.OriginalContext,
                                        null,
                                        0,
                                        true);

                                    // replace the special vardecl with the copy
                                    varStatement.ReplaceChild(varDecl, copyDecl);

                                    // add the original vardecl to the list of "assignments"
                                    assignments.Add(varDecl);
                                }
                                else
                                {
                                    // hold on to the object so we don't lose it to the GC
                                    var initializer = varDecl.Initializer;

                                    // remove it from the vardecl
                                    varDecl.ReplaceChild(initializer, null);

                                    // create an assignment operator for a lookup to the name
                                    // as the left, and the initializer as the right, and add it to the list
                                    assignments.Add(new BinaryOperator(
                                        varDecl.Context,
                                        varDecl.Parser,
                                        new Lookup(varDecl.Identifier, varDecl.Field.OriginalContext, varDecl.Parser),
                                        initializer,
                                        JSToken.Assign));
                                }
                            }
                        }

                        // now if there were any initializers...
                        if (assignments.Count > 0)
                        {
                            // we want to create one big expression from all the assignments and replace the
                            // var statement with the assignment(s) expression. Start at position n=1 and create
                            // a binary operator of n-1 as the left, n as the right, and using a comma operator.
                            var expression = assignments[0];
                            for (var ndx = 1; ndx < assignments.Count; ++ndx)
                            {
                                expression = new BinaryOperator(
                                    null,
                                    expression.Parser,
                                    expression,
                                    assignments[ndx],
                                    JSToken.Comma);
                            }

                            // replace the var with the expression.
//.........这里部分代码省略.........
开发者ID:nuxleus,项目名称:ajaxmin,代码行数:101,代码来源:ReorderScopeVisitor.cs

示例4: Visit

        public override void Visit(Block node)
        {
            if (node != null)
            {
                // javascript doesn't have block scope, so there really is no point
                // in nesting blocks. Unnest any now, before we start combining var statements
                UnnestBlocks(node);

                if (m_combineAdjacentVars)
                {
                    // look at the statements in the block.
                    // if there are multiple var statements adjacent to each other, combine them.
                    // walk BACKWARDS down the list because we'll be removing items when we encounter
                    // multiple vars, etc.
                    // we also don't need to check the first one, since there is nothing before it.
                    for (int ndx = node.Count - 1; ndx > 0; --ndx)
                    {
                        // if the previous node is not a Var, then we don't need to try and combine
                        // it with the current node
                        var previousVar = node[ndx - 1] as Var;
                        if (previousVar != null && node[ndx] is Var)
                        {
                            // add the items in this VAR to the end of the previous
                            previousVar.Append(node[ndx]);

                            // delete this item from the block
                            node.RemoveAt(ndx);
                        }
                    }
                }

                // recurse down the tree after we've combined the adjacent var statements
                base.Visit(node);
            }
        }
开发者ID:nuxleus,项目名称:ajaxmin,代码行数:35,代码来源:ReorderScopeVisitor.cs

示例5: StripDebugStatements

 private static void StripDebugStatements(Block node)
 {
     // walk the list backwards
     for (int ndx = node.Count - 1; ndx >= 0; --ndx)
     {
         // if this item pops positive...
         if (node[ndx].IsDebuggerStatement)
         {
             // just remove it
             node.RemoveAt(ndx);
         }
     }
 }
开发者ID:nuxleus,项目名称:ajaxmin,代码行数:13,代码来源:AnalyzeNodeVisitor.cs

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