示例#1
0
        /// <summary>
        /// Add a statement to the current "cursor" point.
        /// </summary>
        /// <param name="s"></param>
        public void Add(IStatement s)
        {
            if (s == null)
            {
                throw new ArgumentNullException("Cannot add a statement that is null");
            }

            CurrentScopePointer.Add(s);

            ///
            /// Change the statement scope if there something that goes down a level.
            ///

            if (s is IBookingStatementBlock)
            {
                _scopeState.Push(CurrentScope);
            }

            if (s is IStatementCompound)
            {
                CurrentScopePointer = s as IStatementCompound;
            }
            if (s is IBookingStatementBlock)
            {
                PreviousDeclarationScopePointer = CurrentDeclarationScopePointer;
                CurrentDeclarationScopePointer  = s as IBookingStatementBlock;
                Depth++;
            }
        }
示例#2
0
 public BlockRenamer(IBookingStatementBlock holderOldStatements, IBookingStatementBlock holderNewStatements)
 {
     if (holderOldStatements == null)
         throw new ArgumentNullException("holder");
     this._holderBlockOld = holderOldStatements;
     if (holderNewStatements == null)
         throw new ArgumentNullException("holder");
     this._holderBlockNew = holderNewStatements;
 }
示例#3
0
 public BlockRenamer(IBookingStatementBlock holderOldStatements, IBookingStatementBlock holderNewStatements)
 {
     if (holderOldStatements == null)
     {
         throw new ArgumentNullException("holder");
     }
     this._holderBlockOld = holderOldStatements;
     if (holderNewStatements == null)
     {
         throw new ArgumentNullException("holder");
     }
     this._holderBlockNew = holderNewStatements;
 }
示例#4
0
        public static IBookingStatementBlock GetDeepestBookingLevel(GeneratedCode target)
        {
            IBookingStatementBlock result = target.CodeBody;

            IBookingStatementBlock last = result;

            while (last != null)
            {
                result = last;
                last   = result.Statements.LastOrDefault() as IBookingStatementBlock;
            }

            return(result);
        }
示例#5
0
 public static void DumpCodeToConsole(this IBookingStatementBlock code)
 {
     code.DumpCode().DumpToConsole();
 }
示例#6
0
 public IStatement CombineAndMark(IStatement statement, IBookingStatementBlock parent, bool appendIfNoCombine = true)
 {
     throw new NotImplementedException();
 }
示例#7
0
 public bool Combine(IEnumerable <IStatement> statements, IBookingStatementBlock parent, bool appendIfNoCombine = true, bool moveIfIdentical = false)
 {
     throw new NotImplementedException();
 }
示例#8
0
 /// <summary>
 /// The current booking scope is set to be the result scope.
 /// </summary>
 public void SetCurrentScopeAsResultScope()
 {
     CurrentResultScope = CurrentDeclarationScopePointer;
 }
        /// <summary>
        /// Try to combine a single statement. Return the statement that it was combined with. If appendIfNoCombine
        /// is true, then add it on, and still return null.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="appendIfNoCombine">The statement will always be added on the end.</param>
        /// <returns>The statement it was merged with, if merging took place.</returns>
        public IStatement CombineAndMark(IStatement statement, IBookingStatementBlock parent, bool appendIfNoCombine = true)
        {
            var s = CombineInternal(new IStatement[] { statement }, parent, appendIfNoCombine);

            return(s == null ? null : s.FirstOrDefault());
        }
 /// <summary>
 /// Combine a list of statements with a common parent into the this block.
 /// </summary>
 /// <param name="statements">List of statements to combine</param>
 /// <param name="parent">The common parent of the list of statements</param>
 /// <param name="appendIfCantCombine">If true always add the statements onto the end of the block</param>
 /// <returns>True if the statements were merged or appended onto the end of the block</returns>
 public bool Combine(IEnumerable <IStatement> statements, IBookingStatementBlock parent, bool appendIfCantCombine = true, bool moveIfIdentical = false)
 {
     return(CombineInternal(statements, parent, appendIfCantCombine, moveIfIdentical) != null || appendIfCantCombine);
 }
        /// <summary>
        /// Given a list of statements, attempt to combine them with the ones we already have
        /// internally. If we can't, then just append them. This is like our "Add" above, but we
        /// first check to see if any of the statements can be added in. This always
        /// succeeds (no need for a bool return) because we just add things onto the end.
        /// </summary>
        /// <param name="statements">List of statements that we need to combine</param>
        /// <remarks>Assume that the ordering given in the statements must be obeyed.
        ///     - Never insert a later statement before an earlier one!
        /// </remarks>
        private List <IStatement> CombineInternal(IEnumerable <IStatement> statements, IBookingStatementBlock parent, bool appendIfCantCombine = true, bool moveIfIdentical = false)
        {
            bool didAllCombine = true;
            ICodeOptimizationService myopt;
            var mergedIntoList = new List <IStatement>();

            if (parent != null)
            {
                myopt = new BlockRenamer(parent, this);
            }
            else
            {
                myopt = new FailingCodeOptimizer();
            }

            // when we move through this we have to be careful - the try combine has side effects!
            var currentStatements = Statements.ToArray();
            var firstStatement    = currentStatements.Length == 0 ? null : currentStatements[0];

            var statementsToRemove = new List <IStatement>();

            foreach (var s in statements)
            {
                var firstGood = currentStatements.SkipWhile(sinner => !sinner.TryCombineStatement(s, myopt)).ToArray();

                // If we couldn't find a way to combine this guy, then we will put it as the first statement in our list.
                if (firstGood.Length == 0)
                {
                    if (appendIfCantCombine)
                    {
                        s.Parent = null;
                        if (firstStatement != null)
                        {
                            AddBefore(s, firstStatement);
                        }
                        else
                        {
                            Add(s);
                        }
                    }
                    else
                    {
                        didAllCombine = false;
                    }
                }
                else
                {
                    // Add to our list of merged statements. Ignore this guy if it is the same one.
                    // Remove this guy from its old parent.
                    currentStatements = firstGood;
                    if (currentStatements[0] != s)
                    {
                        mergedIntoList.Add(currentStatements[0]);
                        if (moveIfIdentical)
                        {
                            statementsToRemove.Add(s);
                        }
                    }
                    currentStatements = currentStatements.Skip(1).ToArray();
                    firstStatement    = currentStatements.Length == 0 ? null : currentStatements[0];
                }
            }

            // Remove any statements that were combined
            foreach (var s in statementsToRemove)
            {
                (s.Parent as IBookingStatementBlock).Remove(s);
            }

            return(didAllCombine ? mergedIntoList : null);
        }
示例#12
0
 public IStatement CombineAndMark(IStatement statement, IBookingStatementBlock parent, bool appendIfNoCombine = true)
 {
     throw new NotImplementedException();
 }
示例#13
0
 public bool Combine(IEnumerable<IStatement> statements, IBookingStatementBlock parent, bool appendIfNoCombine = true, bool moveIfIdentical = false)
 {
     throw new NotImplementedException();
 }
示例#14
0
 /// <summary>
 /// The current booking scope is set to be the result scope.
 /// </summary>
 public void SetCurrentScopeAsResultScope()
 {
     CurrentResultScope = CurrentDeclarationScopePointer;
 }
示例#15
0
        /// <summary>
        /// Add a statement to the current "cursor" point.
        /// </summary>
        /// <param name="s"></param>
        public void Add(IStatement s)
        {
            if (s == null)
            {
                throw new ArgumentNullException("Cannot add a statement that is null");
            }

            CurrentScopePointer.Add(s);

            ///
            /// Change the statement scope if there something that goes down a level.
            /// 

            if (s is IBookingStatementBlock)
            {
                _scopeState.Push(CurrentScope);
            }

            if (s is IStatementCompound)
                CurrentScopePointer = s as IStatementCompound;
            if (s is IBookingStatementBlock)
            {
                PreviousDeclarationScopePointer = CurrentDeclarationScopePointer;
                CurrentDeclarationScopePointer = s as IBookingStatementBlock;
                Depth++;
            }
        }