From 964d3574fd368dbed946ae3f4c7f18f54d258d59 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sat, 19 Dec 2015 21:20:41 +0100 Subject: [PATCH] Fixed treating 'bool' as 'int' in CBot engine Appears to fix #199 - further testing required --- src/CBot/CBotInstr/CBotFor.cpp | 2 +- src/CBot/CBotInstr/CBotFunction.cpp | 14 +++++------ src/CBot/CBotInstr/CBotInstrMethode.cpp | 4 ++-- src/CBot/CBotInstr/CBotListInstr.cpp | 2 +- src/CBot/CBotStack.cpp | 32 ++++++++++++------------- src/CBot/CBotStack.h | 17 ++++++------- 6 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/CBot/CBotInstr/CBotFor.cpp b/src/CBot/CBotInstr/CBotFor.cpp index e225b99b..80488328 100644 --- a/src/CBot/CBotInstr/CBotFor.cpp +++ b/src/CBot/CBotInstr/CBotFor.cpp @@ -115,7 +115,7 @@ CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack) //////////////////////////////////////////////////////////////////////////////// bool CBotFor :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this, true); // adds an item to the stack (variables locales) + CBotStack* pile = pj->AddStack(this, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); // adds an item to the stack (variables locales) // or find in case of recovery // if ( pile == EOX ) return true; diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index 7277486e..f61328b3 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -345,7 +345,7 @@ static int xx = 0; //////////////////////////////////////////////////////////////////////////////// bool CBotFunction::Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) { - CBotStack* pile = pj->AddStack(this, 2); // one end of stack local to this function + CBotStack* pile = pj->AddStack(this, CBotStack::UnknownEnumBlock::UNKNOWN_2); // one end of stack local to this function // if ( pile == EOX ) return true; pile->SetBotCall(m_pProg); // bases for routines @@ -401,7 +401,7 @@ void CBotFunction::RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInst pile->SetBotCall(m_pProg); // bases for routines - if ( pile->GetBlock() < 2 ) + if ( pile->GetBlock() != CBotStack::UnknownEnumBlock::UNKNOWN_2 ) { CBotStack* pile2 = pile->RestoreStack(nullptr); // one end of stack local to this function if ( pile2 == nullptr ) return; @@ -604,14 +604,14 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotS if ( pt != nullptr ) { - CBotStack* pStk1 = pStack->AddStack(pt, 2); // to put "this" + CBotStack* pStk1 = pStack->AddStack(pt, CBotStack::UnknownEnumBlock::UNKNOWN_2); // to put "this" // if ( pStk1 == EOX ) return true; pStk1->SetBotCall(pt->m_pProg); // it may have changed module if ( pStk1->IfStep() ) return false; - CBotStack* pStk3 = pStk1->AddStack(nullptr, true); // parameters + CBotStack* pStk3 = pStk1->AddStack(nullptr, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); // parameters // preparing parameters on the stack @@ -683,7 +683,7 @@ void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, pStk1->SetBotCall(pt->m_pProg); // it may have changed module - if ( pStk1->GetBlock() < 2 ) + if ( pStk1->GetBlock() != CBotStack::UnknownEnumBlock::UNKNOWN_2 ) { CBotStack* pStk2 = pStk1->RestoreStack(nullptr); // used more if ( pStk2 == nullptr ) return; @@ -733,11 +733,11 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar { // DEBUG( "CBotFunction::DoCall" + pt->GetName(), 0, pStack); - CBotStack* pStk = pStack->AddStack(pt, 2); + CBotStack* pStk = pStack->AddStack(pt, CBotStack::UnknownEnumBlock::UNKNOWN_2); // if ( pStk == EOX ) return true; pStk->SetBotCall(pt->m_pProg); // it may have changed module - CBotStack* pStk3 = pStk->AddStack(nullptr, true); // to set parameters passed + CBotStack* pStk3 = pStk->AddStack(nullptr, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); // to set parameters passed // preparing parameters on the stack diff --git a/src/CBot/CBotInstr/CBotInstrMethode.cpp b/src/CBot/CBotInstr/CBotInstrMethode.cpp index 3778a9c2..1e5c34a5 100644 --- a/src/CBot/CBotInstr/CBotInstrMethode.cpp +++ b/src/CBot/CBotInstr/CBotInstrMethode.cpp @@ -106,7 +106,7 @@ CBotInstr* CBotInstrMethode::Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* bool CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend) { CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, true); // a place for the copy of This + CBotStack* pile1 = pj->AddStack(this, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); // a place for the copy of This if (pVar->GetPointer() == nullptr) { @@ -221,7 +221,7 @@ void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, bool bMain) bool CBotInstrMethode::Execute(CBotStack* &pj) { CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, true); // place for the copy of This + CBotStack* pile1 = pj->AddStack(this, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); // place for the copy of This if (pile1->IfStep()) return false; diff --git a/src/CBot/CBotInstr/CBotListInstr.cpp b/src/CBot/CBotInstr/CBotListInstr.cpp index 13b4ded7..65bdb40e 100644 --- a/src/CBot/CBotInstr/CBotListInstr.cpp +++ b/src/CBot/CBotInstr/CBotListInstr.cpp @@ -80,7 +80,7 @@ CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal bool CBotListInstr::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this, true); //needed for SetState() + CBotStack* pile = pj->AddStack(this, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); //needed for SetState() if (pile->StackOver() ) return pj->Return( pile); diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 8577c451..c2c04d23 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -63,7 +63,7 @@ CBotStack* CBotStack::FirstStack() // completely empty memset(p, 0, size); - p-> m_bBlock = true; + p-> m_bBlock = UnknownEnumBlock::UNKNOWN_TRUE ; m_timer = m_initimer; // sets the timer at the beginning CBotStack* pp = p; @@ -140,7 +140,7 @@ void CBotStack::Delete() // routine improved //////////////////////////////////////////////////////////////////////////////// -CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) +CBotStack* CBotStack::AddStack(CBotInstr* instr, UnknownEnumBlock bBlock) { if (m_next != nullptr) { @@ -168,12 +168,12 @@ CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) p->m_prev = this; p->m_state = 0; p->m_call = nullptr; - p->m_bFunc = false; + p->m_bFunc = IsFunctionParam::FALSE; return p; } //////////////////////////////////////////////////////////////////////////////// -CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) +CBotStack* CBotStack::AddStackEOX(CBotCall* instr, UnknownEnumBlock bBlock) { if (m_next != nullptr) { @@ -186,12 +186,12 @@ CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) } CBotStack* p = AddStack(nullptr, bBlock); p->m_call = instr; - p->m_bFunc = 2; // special + p->m_bFunc = IsFunctionParam::UNKNOWN_EOX_SPECIAL; // special return p; } //////////////////////////////////////////////////////////////////////////////// -CBotStack* CBotStack::AddStack2(bool bBlock) +CBotStack* CBotStack::AddStack2(UnknownEnumBlock bBlock) { if (m_next2 != nullptr) { @@ -215,7 +215,7 @@ CBotStack* CBotStack::AddStack2(bool bBlock) } //////////////////////////////////////////////////////////////////////////////// -bool CBotStack::GetBlock() +CBotStack::UnknownEnumBlock CBotStack::GetBlock() { return m_bBlock; } @@ -723,7 +723,7 @@ void CBotStack::AddVar(CBotVar* pVar) CBotStack* p = this; // returns to the father element - while (p != nullptr && p->m_bBlock == 0) p = p->m_prev; + while (p != nullptr && p->m_bBlock == UnknownEnumBlock::UNKNOWN_FALSE) p = p->m_prev; if ( p == nullptr ) return; @@ -743,7 +743,7 @@ void CBotStack::AddVar(CBotVar* pVar) void CBotStack::SetBotCall(CBotProgram* p) { m_prog = p; - m_bFunc = true; + m_bFunc = IsFunctionParam::TRUE; } //////////////////////////////////////////////////////////////////////////////// @@ -826,7 +826,7 @@ void CBotStack::GetRunPos(const char* &FunctionName, int &start, int &end) while (p->m_next != nullptr) { if ( p->m_instr != nullptr ) instr = p->m_instr; - if ( p->m_bFunc == 1 && p->m_instr != nullptr ) funct = p->m_instr; + if ( p->m_bFunc == IsFunctionParam::TRUE && p->m_instr != nullptr ) funct = p->m_instr; if ( p->m_next->m_prog != prog ) break ; if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ; @@ -834,7 +834,7 @@ void CBotStack::GetRunPos(const char* &FunctionName, int &start, int &end) } if ( p->m_instr != nullptr ) instr = p->m_instr; - if ( p->m_bFunc == 1 && p->m_instr != nullptr ) funct = p->m_instr; + if ( p->m_bFunc == IsFunctionParam::TRUE && p->m_instr != nullptr ) funct = p->m_instr; if ( funct == nullptr ) return; @@ -868,13 +868,13 @@ CBotVar* CBotStack::GetStackVars(const char* &FunctionName, int level) // descends upon the elements of block - while ( p != nullptr && !p->m_bBlock ) p = p->m_prev; + while ( p != nullptr && p->m_bBlock != UnknownEnumBlock::UNKNOWN_FALSE ) p = p->m_prev; // Now p is on the beggining of the top block (with local variables) while ( p != nullptr && level++ < 0 ) { p = p->m_prev; - while ( p != nullptr && !p->m_bBlock ) p = p->m_prev; + while ( p != nullptr && p->m_bBlock != UnknownEnumBlock::UNKNOWN_FALSE ) p = p->m_prev; } // Now p is on the block "level" @@ -884,7 +884,7 @@ CBotVar* CBotStack::GetStackVars(const char* &FunctionName, int level) CBotStack* pp = p; while ( pp != nullptr ) { - if ( pp->m_bFunc == 1 ) break; + if ( pp->m_bFunc == IsFunctionParam::TRUE) break; pp = pp->m_prev; } @@ -913,7 +913,7 @@ bool CBotStack::SaveState(FILE* pf) { if (!WriteWord(pf, 1)) return false; // a mark of pursuit } - if (!WriteWord(pf, m_bBlock)) return false; // is a local block + if (!WriteWord(pf, static_cast(m_bBlock))) return false; // is a local block if (!WriteWord(pf, m_state)) return false; // in what state? if (!WriteWord(pf, 0)) return false; // by compatibility m_bDontDelete if (!WriteWord(pf, m_step)) return false; // in what state? @@ -947,7 +947,7 @@ bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) } if (!ReadWord(pf, w)) return false; // is a local block - pStack->m_bBlock = w; + pStack->m_bBlock = static_cast(w); if (!ReadWord(pf, w)) return false; // in what state ? pStack->SetState(static_cast(w)); // in a good state diff --git a/src/CBot/CBotStack.h b/src/CBot/CBotStack.h index 3b58116f..738cfd6c 100644 --- a/src/CBot/CBotStack.h +++ b/src/CBot/CBotStack.h @@ -45,6 +45,9 @@ class CBotToken; class CBotStack { public: + enum class UnknownEnumBlock : unsigned short { UNKNOWN_FALSE = 0, UNKNOWN_TRUE = 1, UNKNOWN_2 = 2 }; // TODO: figure out what these mean ~krzys_h + enum class IsFunctionParam : unsigned short { FALSE = 0, TRUE = 1, UNKNOWN_EOX_SPECIAL = 2 }; // TODO: just guessing the meaning of values, should be verified ~krzys_h + #if STACKMEM /** * \brief FirstStack Allocate first stack @@ -169,12 +172,12 @@ public: CBotVar* CopyVar(CBotToken& Token, bool bUpdate = false); - CBotStack* AddStack(CBotInstr* instr = nullptr, bool bBlock = false); // extends the stack - CBotStack* AddStackEOX(CBotCall* instr = nullptr, bool bBlock = false); // extends the stack + CBotStack* AddStack(CBotInstr* instr = nullptr, UnknownEnumBlock bBlock = UnknownEnumBlock::UNKNOWN_FALSE); // extends the stack + CBotStack* AddStackEOX(CBotCall* instr = nullptr, UnknownEnumBlock bBlock = UnknownEnumBlock::UNKNOWN_FALSE); // extends the stack CBotStack* RestoreStack(CBotInstr* instr = nullptr); CBotStack* RestoreStackEOX(CBotCall* instr = nullptr); - CBotStack* AddStack2(bool bBlock = false); // extends the stack + CBotStack* AddStack2(UnknownEnumBlock bBlock = UnknownEnumBlock::UNKNOWN_FALSE); // extends the stack bool Return(CBotStack* pFils); // transmits the result over bool ReturnKeep(CBotStack* pFils); // transmits the result without reducing the stack bool BreakReturn(CBotStack* pfils, const char* name = nullptr); @@ -206,7 +209,7 @@ public: void SetBotCall(CBotProgram* p); CBotProgram* GetBotCall(bool bFirst = false); void* GetPUser(); - bool GetBlock(); + UnknownEnumBlock GetBlock(); bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); @@ -221,8 +224,6 @@ public: void GetRunPos(const char* &FunctionName, int &start, int &end); CBotVar* GetStackVars(const char* &FunctionName, int level); - int m_temp; - private: CBotStack* m_next; CBotStack* m_next2; @@ -243,7 +244,7 @@ private: CBotVar* m_var; // result of the operations CBotVar* m_listVar; // variables declared at this level - bool m_bBlock; // is part of a block (variables are local to this block) + UnknownEnumBlock m_bBlock; // is part of a block (variables are local to this block) bool m_bOver; // stack limits? // bool m_bDontDelete; // special, not to destroy the variable during delete CBotProgram* m_prog; // user-defined functions @@ -258,7 +259,7 @@ private: void* m_pUser; CBotInstr* m_instr; // the corresponding instruction - bool m_bFunc; // an input of a function? + IsFunctionParam m_bFunc; // an input of a function? CBotCall* m_call; // recovery point in a extern call friend class CBotTry; };