Пример #1
0
        protected override void OptimizePre()
        {
            // Removes excessive levels of nesting.

            for (int i = 0; i < this.Statements.Count; i++)
            {
                StatementSilvernode thisStatement = this.Statements[i];

                // Blocks cannot happen with blocks. Remove nested blocks (deeply)!
                if (thisStatement is BlockSilvernode)
                {
                    BlockSilvernode block = (BlockSilvernode)thisStatement;
                    this.Statements.RemoveAt(i);
                    this.Statements.InsertRange(i, block.Statements);
                    i--;
                }
                else if (thisStatement is StatementsSequenceSilvernode)
                {
                    StatementsSequenceSilvernode sequence = (StatementsSequenceSilvernode)thisStatement;
                    sequence.OptimizeRecursively();
                    List <StatementSilvernode> points = sequence.List;
                    this.Statements.RemoveAt(i);
                    this.Statements.InsertRange(i, points);
                    i--;
                }
            }
        }
Пример #2
0
 public IfSilvernode(SyntaxNode originalNode,
                     Silvernode condition,
                     StatementSilvernode then,
                     StatementSilvernode elseBranch)
     : base(originalNode)
 {
     this.condition  = condition;
     this.then       = then;
     this.elseBranch = elseBranch;
     if (!(this.then is BlockSilvernode))
     {
         this.then = new BlockSilvernode(null, new List <StatementSilvernode> {
             then
         });
     }
     if (this.elseBranch != null && !(this.elseBranch is BlockSilvernode))
     {
         this.elseBranch = new BlockSilvernode(null, new List <StatementSilvernode> {
             elseBranch
         });
     }
 }
Пример #3
0
        protected override void OptimizePost()
        {
            // Removes the "goto end ; label end" sequence at the end if it's possible.
            BlockSilvernode block = this.Children.FirstOrDefault(s => s is BlockSilvernode) as BlockSilvernode;

            if (block != null)
            {
                int howManyGotos = block.Descendants.Count(sn => sn is GotoSilvernode && ((GotoSilvernode)sn).Label == Constants.SilverMethodEndLabel);
                StatementSilvernode lastStatement    = block.Statements.Count >= 1 ? block.Statements[block.Statements.Count - 1] : null;
                StatementSilvernode preLastStatement = block.Statements.Count >= 2 ? block.Statements[block.Statements.Count - 2] : null;
                if (lastStatement != null &&
                    (lastStatement.GetType() == typeof(LabelSilvernode) && ((LabelSilvernode)lastStatement).Label == Constants.SilverMethodEndLabel) &&
                    preLastStatement != null &&
                    (preLastStatement.GetType() == typeof(GotoSilvernode) && ((GotoSilvernode)preLastStatement).Label == Constants.SilverMethodEndLabel) &&
                    howManyGotos == 1)
                {
                    block.Statements.RemoveRange(block.Statements.Count - 2, 2);
                }
                else if (howManyGotos == 0 && (lastStatement.GetType() == typeof(LabelSilvernode) && ((LabelSilvernode)lastStatement).Label == Constants.SilverMethodEndLabel))
                {
                    block.Statements.RemoveAt(block.Statements.Count - 1);
                }
            }
        }
Пример #4
0
 public void Add(StatementSilvernode statement)
 {
     this.Statements.Add(statement);
 }
Пример #5
0
 public void Prepend(StatementSilvernode statement)
 {
     this.Statements.Insert(0, statement);
 }