public void FindDoWhileLoops()
 {
     for (int i = 0; i < this.instructionCount; i++)
     {
         if (this.Instructions[i].OpCode == LuaOpCode.OpCodes.HKS_OPCODE_JMP && this.Instructions[i].visited == false)
         {
             if (this.Instructions[i].sBx < 0 && i + this.Instructions[i].sBx >= 0)
             {
                 if (LuaOpCode.isConditionOPCode(this, i - 1))
                 {
                     this.Instructions[i].visited = true;
                 }
             }
         }
     }
 }
        public void FindIfStatements()
        {
            int          lines  = -1;
            LuaCondition master = null;

            for (int i = 0; i < this.instructionCount; i++)
            {
                if (this.Instructions[i].OpCode == LuaOpCode.OpCodes.HKS_OPCODE_JMP && this.Instructions[i].visited == false)
                {
                    // Make sure the skip goes forward
                    if (this.Instructions[i].sBx >= 0)
                    {
                        if (LuaOpCode.isConditionOPCode(this, i - 1))
                        {
                            // Skip the while loops
                            if (this.Instructions[i + this.Instructions[i].sBx].OpCode == LuaOpCode.OpCodes.HKS_OPCODE_JMP && this.Instructions[i + this.Instructions[i].sBx].sBx < 0)
                            {
                                continue;
                            }
                            // OR statement
                            if (lines == 0)
                            {
                                this.conditions.Add(new LuaCondition(i - 1, LuaCondition.Type.If, LuaCondition.Prefix.or, master));
                                this.Instructions[i].visited = true;
                                lines--;
                                continue;
                            }
                            // and statement
                            if (lines == this.Instructions[i].sBx)
                            {
                                this.conditions.Add(new LuaCondition(i - 1, LuaCondition.Type.If, LuaCondition.Prefix.and, master));
                                this.Instructions[i].visited = true;
                                lines--;
                                continue;
                            }
                            master = new LuaCondition(i - 1, LuaCondition.Type.If, LuaCondition.Prefix.none);
                            this.conditions.Add(master);
                            lines = this.Instructions[i].sBx;
                            this.Instructions[i].visited = true;
                        }
                    }
                }
                lines--;
            }
            List <LuaCondition> newConditions = new List <LuaCondition>();

            foreach (LuaCondition condition in this.conditions)
            {
                if (condition.master == null)
                {
                    LuaCondition lastSon = condition;
                    foreach (LuaCondition conditionSon in this.conditions)
                    {
                        if (conditionSon.master == condition)
                        {
                            if (conditionSon.prefix == LuaCondition.Prefix.or)
                            {
                                if (this.Instructions[lastSon.line].A == 1)
                                {
                                    this.Instructions[lastSon.line].A = 0;
                                }
                                else
                                {
                                    this.Instructions[lastSon.line].A = 1;
                                }
                            }
                            lastSon = conditionSon;
                        }
                    }
                    int skipLines = this.Instructions[lastSon.line + 1].sBx;
                    if (this.Instructions[lastSon.line + 1 + skipLines].OpCode == LuaOpCode.OpCodes.HKS_OPCODE_JMP)
                    {
                        newConditions.Add(new LuaCondition(lastSon.line + 1 + skipLines, LuaCondition.Type.Else));
                        int skipElseLines = this.Instructions[lastSon.line + 1 + skipLines].sBx;
                        newConditions.Add(new LuaCondition(lastSon.line + 1 + skipLines + skipElseLines, LuaCondition.Type.End));
                    }
                    else
                    {
                        newConditions.Add(new LuaCondition(lastSon.line + 1 + skipLines, LuaCondition.Type.End));
                    }
                }
            }
            foreach (LuaCondition condition in newConditions)
            {
                this.conditions.Add(condition);
            }
        }