Example #1
0
        public static ConditionalCodeBlock GetConditionalBlockFrom(string[] lines, int index)
        {
            BlockType blockType = GetBlockTypeStartingAt(lines, index);

            if (blockType != BlockType.None)
            {
                ConditionalCodeBlock ccb = new ConditionalCodeBlock();
                ccb.BlockType = blockType;

                ccb.mLines = lines;

                ccb.LineWithConditionIndex = index;
                ccb.FirstLineOfBlockIndex  = index + 1;
                ccb.BlockLength            = 0;

                if (ccb.FirstLineOfBlockIndex < lines.Length)
                {
                    bool isBlockWithBrackets = lines[ccb.FirstLineOfBlockIndex].Trim().StartsWith("{");

                    if (!isBlockWithBrackets)
                    {
                        ccb.BlockLength = 1;
                    }
                    else
                    {
                        int numberOfBrackets = 0;

                        for (int i = ccb.FirstLineOfBlockIndex; i < lines.Length; i++)
                        {
                            numberOfBrackets += lines[i].CountOf('{');
                            numberOfBrackets -= lines[i].CountOf('}');

                            if (numberOfBrackets == 0)
                            {
                                ccb.BlockLength = 1 + i - ccb.FirstLineOfBlockIndex;
                                break;
                            }
                        }
                    }
                }

                return(ccb);
            }
            return(null);
        }
Example #2
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);
        }
Example #3
0
        private void ApplyLine(string line, CodeContext codeContext, string[] allLines, string fileName, ref int index)
        {
            bool succeeded = true;

            mParserLog.AppendLine("--- " + line + " ---");


            if (string.IsNullOrEmpty(line))
            {
                // do nothing
                // This may be empty
                // because the split function
                // may have returned a line with
                // only a newline character.  If so
                // that becomes an empty line when trim
                // is called on it.  This could be a line
                // that was trimmed which is now empty.
            }
            else if (line.Trim().StartsWith("//"))
            {
                // comment, carry on
            }
            else if (line.Trim().StartsWith("#region") || line.Trim().StartsWith("#endregion"))
            {
                // we can skip region blocks
            }
            else if (ConditionalCodeBlock.GetBlockTypeStartingAt(allLines, index) != BlockType.None)
            {
                ElementRuntime elementRuntime = codeContext.ContainerInstance as ElementRuntime;

                IElement element = null;

                if (elementRuntime != null)
                {
                    element = elementRuntime.AssociatedIElement;
                }

                succeeded = ApplyConditionalBlocks(allLines, element, codeContext, fileName, ref index);

                if (!succeeded)
                {
                    throw new Exception();
                }
            }
            else
            {
                // here we want to identify how many lines make up the statement since
                // it could be something like
                // this.X = 3
                //       + 4;
                int numberOfLinesInStatement = GetNumberOfLinesInRegularStatement(allLines, index);

                string combined = CombineLines(allLines, index, numberOfLinesInStatement);

                string trimmedCombined = combined.Trim();
                if (trimmedCombined.StartsWith("{") && trimmedCombined.EndsWith("}"))
                {
                    combined = trimmedCombined.Substring(1, trimmedCombined.Length - 2);
                }
                CSharpParser parser     = new CSharpParser();
                var          statements = parser.ParseStatements(combined);



                // I can be incremented by this function by the number of lines.
                // If this value is incremented, then the calling function is responsible
                // for recognizing that in its loop and acting properly.  The calling function
                // assumes an increment of 1 so we won't do anything if there's only 1 line in this
                // statement.
                if (numberOfLinesInStatement != 1)
                {
                    index += numberOfLinesInStatement;
                }

                foreach (var statement in statements)
                {
                    if (statement is ExpressionStatement)
                    {
                        Expression expression = ((ExpressionStatement)statement).Expression;
                        if (expression is InvocationExpression || expression is UnaryOperatorExpression)
                        {
                            mExpressionParser.EvaluateExpression(expression, codeContext);
                        }
                        else if (expression is AssignmentExpression)
                        {
                            succeeded = ApplyAssignmentLine(expression as AssignmentExpression, codeContext);
                            if (!succeeded)
                            {
                                throw new Exception();
                            }
                        }
                        else if (expression is IdentifierExpression || expression is MemberReferenceExpression)
                        {
                            // This is probably an incomplete line, so let's tolerate it and move on...
                        }
                        else
                        {
                            throw new Exception();
                        }
                    }
                    else if (statement is VariableDeclarationStatement)
                    {
                        VariableDeclarationStatement vds = statement as VariableDeclarationStatement;

                        foreach (var child in vds.Children)
                        {
                            if (child is VariableInitializer)
                            {
                                VariableInitializer variableInitializer = child as VariableInitializer;
                                ApplyAssignment(variableInitializer, vds, codeContext);
                                //variableInitializer.
                            }
                        }
                        //ApplyAssignment(codeContext, vds.Variables.First().GetText(), vds.
                        //vds.E
                        //ApplyAssignmentLine(statement.ToString(), codeContext);
                    }
                    //if ( is AssignmentExpression || result is MemberReferenceExpression)
                    //{
                    //    ApplyAssignmentLine(combined, codeContext);
                    //}
                    //else if (result is InvocationExpression)
                    //{
                    //    ApplyMethodCall(combined, codeContext);
                    //}
                    else
                    {
                        AddErrorText("Unknown line(s):  " + combined);
                    }
                }
            }
        }
        public static ConditionalCodeBlock GetConditionalBlockFrom(string[] lines, int index)
        {
            BlockType blockType = GetBlockTypeStartingAt(lines, index);

            if (blockType != BlockType.None)
            {
                ConditionalCodeBlock ccb = new ConditionalCodeBlock();
                ccb.BlockType = blockType;

                ccb.mLines = lines;

                ccb.LineWithConditionIndex = index;
                ccb.FirstLineOfBlockIndex = index + 1;
                ccb.BlockLength = 0;

                if (ccb.FirstLineOfBlockIndex < lines.Length)
                {
                    bool isBlockWithBrackets = lines[ccb.FirstLineOfBlockIndex].Trim().StartsWith("{");

                    if (!isBlockWithBrackets)
                    {
                        ccb.BlockLength = 1;
                    }
                    else
                    {
                        int numberOfBrackets = 0;

                        for (int i = ccb.FirstLineOfBlockIndex; i < lines.Length; i++)
                        {
                            numberOfBrackets += lines[i].CountOf('{');
                            numberOfBrackets -= lines[i].CountOf('}');

                            if (numberOfBrackets == 0)
                            {
                                ccb.BlockLength = 1 + i - ccb.FirstLineOfBlockIndex;
                                break;
                            }
                        }
                    }
                }

                return ccb;
            }
            return null;

        }