private void CreateChildren(List <PsudoInstruction> instructions, FlowChartItemControl parent)
        {
            FlowChartItemControl tempParent = parent;
            FlowChartItemControl item       = null;

            foreach (PsudoInstruction inst in instructions)
            {
                item = new FlowChartItemControl(inst);
                this.drawingPanel.Controls.Add(item);

                if (inst is BlockInstruction)
                {
                    tempParent.AddChild(item);
                    CreateChildren(((BlockInstruction)inst).Instructions, item);
                }
                else if (inst is DecisionInstruction)
                {
                    tempParent.AddChild(item);
                    DecisionInstruction decision = (DecisionInstruction)inst;

                    if (decision.FalseInstruction is BlockInstruction || decision.FalseInstruction is NoOpInstruction)
                    {
                        FlowChartItemControl trueBlock  = new FlowChartItemControl("TRUE");
                        FlowChartItemControl falseBlock = new FlowChartItemControl("FALSE");
                        trueBlock.IsLabelNode  = true;
                        falseBlock.IsLabelNode = true;

                        item.AddChild(trueBlock);
                        item.AddChild(falseBlock);

                        this.drawingPanel.Controls.Add(trueBlock);
                        this.drawingPanel.Controls.Add(falseBlock);

                        CreateChildren(decision.CodeBlock.Instructions, trueBlock);
                        if (decision.FalseInstruction is BlockInstruction)
                        {
                            CreateChildren(((BlockInstruction)decision.FalseInstruction).Instructions, falseBlock);
                        }
                    }
                    else
                    {
                        CreateChildren(decision.CodeBlock.Instructions, item);
                        while (decision.FalseInstruction is DecisionInstruction)
                        {
                            decision = (DecisionInstruction)decision.FalseInstruction;

                            CreateChildren(decision.CodeBlock.Instructions, item);
                        }
                    }

                    FlowChartItemControl end = new FlowChartItemControl("End");
                    this.drawingPanel.Controls.Add(end);

                    foreach (FlowChartItemControl child in item.Children)
                    {
                        AddToEndofChildren(child, end);
                    }

                    item = end;
                }
                else if (inst is DoLoopInstruction)
                {
                    DoLoopInstruction    loop   = (DoLoopInstruction)inst;
                    FlowChartItemControl doNode = new FlowChartItemControl("DO");
                    this.drawingPanel.Controls.Add(doNode);
                    tempParent.AddChild(doNode);

                    CreateChildren(loop.Instructions, doNode);
                    AddToEndofChildren(tempParent, item);
                    item.LoopBack = doNode;
                }
                else if (inst is ForLoopInstruction)
                {
                    ForLoopInstruction   loop     = (ForLoopInstruction)inst;
                    FlowChartItemControl initNode = new FlowChartItemControl(loop.InitInstruction);
                    this.drawingPanel.Controls.Add(initNode);
                    tempParent.AddChild(initNode);

                    initNode.AddChild(item);
                    CreateChildren(loop.Instructions, item);

                    FlowChartItemControl updateNode = new FlowChartItemControl(loop.UpdateInstruction);
                    this.drawingPanel.Controls.Add(updateNode);
                    AddToEndofChildren(item, updateNode);
                    updateNode.LoopBack = item;
                }
                else if (inst is WhileLoopInstruction)
                {
                    tempParent.AddChild(item);

                    WhileLoopInstruction loop = (WhileLoopInstruction)inst;
                    CreateChildren(loop.Instructions, item);
                    FlowChartItemControl temp = item.Children[0];

                    while (temp.Children.Count > 0)
                    {
                        temp = temp.Children[0];
                    }
                    temp.LoopBack = item;
                }
                else
                {
                    tempParent.AddChild(item);
                }
                tempParent = item;
            }
        }
        protected PsudoInstruction CompileLoop(PsudoMethod method, int line, out int endLine)
        {
            if (Regex.IsMatch(codeLines[line], @"^[\s]*\b(while|dowhile)\b[\s]*", RegexOptions.IgnoreCase))
            {
                string decision = codeLines[line].Trim();
                if (decision.ToLower().StartsWith("dowhile"))
                {
                    decision = decision.Substring(7).Trim();
                }
                else
                {
                    decision = decision.Substring(5).Trim();
                }

                WhileLoopInstruction ins = new WhileLoopInstruction(line, method, decision);

                //int end = codeLines.ToList().FindIndex(line, item => Regex.IsMatch(item, @"^[\s]*\b(endwhile|enddo)\b[\s]*", RegexOptions.IgnoreCase));
                int end = FindNestedEnd(line, "(while|dowhile|do)", "(endwhile|enddo)");
                if (end < 0)
                {
                    throw new Exception("WHILE found with no ENDWHILE");
                }

                ins.Instructions = CompileBlock(method, line + 1, end - 1);

                endLine = end;
                return(ins);
            }
            if (Regex.IsMatch(codeLines[line], @"^[\s]*\b(repeat)\b[\s]*", RegexOptions.IgnoreCase))
            {
                //int end = codeLines.ToList().FindIndex(line, item => Regex.IsMatch(item, @"^[\s]*\b(until)\b[\s]*", RegexOptions.IgnoreCase));
                int end = FindNestedEnd(line, "(repeat)", "(until)");
                if (end < 0)
                {
                    throw new Exception("REPEAT found with no UNTIL");
                }

                string decision = codeLines[end].Trim();
                decision = decision.Substring(5).Trim();

                DoLoopInstruction ins = new DoLoopInstruction(line, method, decision);

                ins.Instructions = CompileBlock(method, line + 1, end - 1);

                endLine = end;
                return(ins);
            }
            if (Regex.IsMatch(codeLines[line], @"^[\s]*\b(do)\b[\s]*", RegexOptions.IgnoreCase))
            {
                string decision = codeLines[line].Trim();

                Match variable = Regex.Match(decision, @"(?<=do[\s]+)[a-zA-Z0-9_()-]+", RegexOptions.IgnoreCase);
                Match initVal  = Regex.Match(decision, @"(?<=[=][\s]*)[a-zA-Z0-9_()-]+(?=[\s]*(to))", RegexOptions.IgnoreCase);
                Match endVal   = Regex.Match(decision, @"(?<=\b(to)\b[\s]*)[a-zA-Z0-9_()-]+", RegexOptions.IgnoreCase);

                if (!variable.Success || !initVal.Success || !endVal.Success)
                {
                    throw new Exception("Error in Do Loop Formatting");
                }

                //int end = codeLines.ToList().FindIndex(line, item => Regex.IsMatch(item, @"^[\s]*\b(enddo)\b[\s]*", RegexOptions.IgnoreCase));
                int end = FindNestedEnd(line, "(do|while|dowhile|do)", "(enddo)");
                if (end < 0)
                {
                    throw new Exception("DO found with no ENDDO");
                }

                ForLoopInstruction ins = new ForLoopInstruction(line, method, variable.Value.Trim(), initVal.Value.Trim(), endVal.Value.Trim());

                ins.Instructions = CompileBlock(method, line + 1, end - 1);

                endLine = end;
                return(ins);
            }

            endLine = line;
            return(null);
        }