Example #1
0
        private bool ApplyConditionalBlocks(string[] allLines, IElement element, CodeContext codeContext, string fileName, ref int index)
        {
            bool succeeded = true;

            BlockType lastBlockType = BlockType.None;

            BlockType nextBlockType = ConditionalCodeBlock.GetBlockTypeStartingAt(allLines, index);

            List <ConditionalCodeBlock> conditionalBlocks = new List <ConditionalCodeBlock>();

            while (nextBlockType.LinksFromPreviousType(lastBlockType))
            {
                ConditionalCodeBlock ccb = ConditionalCodeBlock.GetConditionalBlockFrom(allLines, index);
                conditionalBlocks.Add(ccb);
                lastBlockType = nextBlockType;

                index        += ccb.LineCountIncludingConditionLine;
                nextBlockType = ConditionalCodeBlock.GetBlockTypeStartingAt(allLines, index);
            }


            // Only one of these blocks can trigger
            foreach (ConditionalCodeBlock ccb in conditionalBlocks)
            {
                // This code context is for the contents of the condition.
                // For example, the i variable in a for-loop will have scope
                // limited to the application of the for-loop.
                codeContext.AddVariableStack();

                bool shouldExecute = BranchingParser.Self.DetermineIfShouldExecute(
                    this, codeContext, ccb, mExpressionParser, true);


                while (shouldExecute)
                {
                    codeContext.AddVariableStack();

                    int startBlock  = ccb.FirstLineOfBlockIndex;
                    int blockLength = ccb.BlockLength;
                    if (ccb.IsBlockWrappedInBrackets)
                    {
                        startBlock++;
                        blockLength -= 2;
                    }
                    succeeded = ApplyLinesInternal(allLines, startBlock, blockLength, element, codeContext, fileName);


                    shouldExecute = false;
                    if (succeeded)
                    {
                        if (ccb.BlockType == BlockType.For)
                        {
                            BranchingParser.Self.IncrementFor(codeContext, ccb, mExpressionParser);

                            shouldExecute = BranchingParser.Self.DetermineIfShouldExecute(
                                this, codeContext, ccb, mExpressionParser, false);
                        }
                        else if (ccb.BlockType == BlockType.While)
                        {
                            shouldExecute = BranchingParser.Self.DetermineIfShouldExecute(
                                this, codeContext, ccb, mExpressionParser, false);
                        }
                    }

                    codeContext.RemoveVariableStack();
                }
                codeContext.RemoveVariableStack();
            }

            return(succeeded);
        }