More CBotStack docs

dev-time-step
krzys-h 2015-12-25 20:47:30 +01:00
parent c72cfa234b
commit 9b3b701785
8 changed files with 202 additions and 100 deletions

View File

@ -57,7 +57,6 @@ public:
* \param pThis
* \param ppVars
* \param pStack
* \param nIdent
* \return
*/
CBotTypResult CompileCall(const std::string& name, CBotVar* pThis, CBotVar** ppVars,
@ -65,7 +64,6 @@ public:
/*!
* \brief DoCall
* \param nIdent
* \param name
* \param pThis
* \param ppVars

View File

@ -350,6 +350,7 @@ public:
/*!
* \brief CheckCall Test if a procedure name is already defined somewhere.
* \param program
* \param pToken
* \param pParam
* \return

View File

@ -115,7 +115,7 @@ CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack)
////////////////////////////////////////////////////////////////////////////////
bool CBotFor :: Execute(CBotStack* &pj)
{
CBotStack* pile = pj->AddStack(this, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); // adds an item to the stack (variables locales)
CBotStack* pile = pj->AddStack(this, CBotStack::IsBlock::BLOCK); // adds an item to the stack (variables locales)
// or find in case of recovery
// if ( pile == EOX ) return true;

View File

@ -335,7 +335,7 @@ bad:
////////////////////////////////////////////////////////////////////////////////
bool CBotFunction::Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance)
{
CBotStack* pile = pj->AddStack(this, CBotStack::UnknownEnumBlock::UNKNOWN_2); // one end of stack local to this function
CBotStack* pile = pj->AddStack(this, CBotStack::IsBlock::FUNCTION); // one end of stack local to this function
// if ( pile == EOX ) return true;
pile->SetProgram(m_pProg); // bases for routines
@ -397,7 +397,7 @@ void CBotFunction::RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInst
pile->SetProgram(m_pProg); // bases for routines
if ( pile->GetBlock() != CBotStack::UnknownEnumBlock::UNKNOWN_2 )
if ( pile->GetBlock() != CBotStack::IsBlock::FUNCTION)
{
CBotStack* pile2 = pile->RestoreStack(nullptr); // one end of stack local to this function
if ( pile2 == nullptr ) return;
@ -600,14 +600,14 @@ int CBotFunction::DoCall(long& nIdent, const std::string& name, CBotVar** ppVars
if ( pt != nullptr )
{
CBotStack* pStk1 = pStack->AddStack(pt, CBotStack::UnknownEnumBlock::UNKNOWN_2); // to put "this"
CBotStack* pStk1 = pStack->AddStack(pt, CBotStack::IsBlock::FUNCTION); // to put "this"
// if ( pStk1 == EOX ) return true;
pStk1->SetProgram(pt->m_pProg); // it may have changed module
if ( pStk1->IfStep() ) return false;
CBotStack* pStk3 = pStk1->AddStack(nullptr, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); // parameters
CBotStack* pStk3 = pStk1->AddStack(nullptr, CBotStack::IsBlock::BLOCK); // parameters
// preparing parameters on the stack
@ -682,7 +682,7 @@ void CBotFunction::RestoreCall(long& nIdent, const std::string& name, CBotVar**
pStk1->SetProgram(pt->m_pProg); // it may have changed module
if ( pStk1->GetBlock() != CBotStack::UnknownEnumBlock::UNKNOWN_2 )
if ( pStk1->GetBlock() != CBotStack::IsBlock::FUNCTION)
{
CBotStack* pStk2 = pStk1->RestoreStack(nullptr); // used more
if ( pStk2 == nullptr ) return;
@ -725,7 +725,7 @@ int CBotFunction::DoCall(long& nIdent, const std::string& name, CBotVar* pThis,
CBotToken* pToken, CBotClass* pClass)
{
CBotTypResult type;
CBotProgram* pProgCurrent = pStack->GetBotCall();
CBotProgram* pProgCurrent = pStack->GetProgram();
CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type, false);
@ -733,11 +733,11 @@ int CBotFunction::DoCall(long& nIdent, const std::string& name, CBotVar* pThis,
{
// DEBUG( "CBotFunction::DoCall" + pt->GetName(), 0, pStack);
CBotStack* pStk = pStack->AddStack(pt, CBotStack::UnknownEnumBlock::UNKNOWN_2);
CBotStack* pStk = pStack->AddStack(pt, CBotStack::IsBlock::FUNCTION);
// if ( pStk == EOX ) return true;
pStk->SetProgram(pt->m_pProg); // it may have changed module
CBotStack* pStk3 = pStk->AddStack(nullptr, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); // to set parameters passed
CBotStack* pStk3 = pStk->AddStack(nullptr, CBotStack::IsBlock::BLOCK); // to set parameters passed
// preparing parameters on the stack
@ -767,7 +767,7 @@ int CBotFunction::DoCall(long& nIdent, const std::string& name, CBotVar* pThis,
{
if ( pt->m_bSynchro )
{
CBotProgram* pProgBase = pStk->GetBotCall(true);
CBotProgram* pProgBase = pStk->GetProgram(true);
if ( !pClass->Lock(pProgBase) ) return false; // expected to power \TODO attend de pouvoir
}
pStk->IncState();
@ -826,7 +826,7 @@ void CBotFunction::RestoreCall(long& nIdent, const std::string& name, CBotVar* p
if ( pStk->GetState() > 1 && // latching is effective?
pt->m_bSynchro )
{
CBotProgram* pProgBase = pStk->GetBotCall(true);
CBotProgram* pProgBase = pStk->GetProgram(true);
pClass->Lock(pProgBase); // locks the class
}

View File

@ -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, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); // a place for the copy of This
CBotStack* pile1 = pj->AddStack(this, CBotStack::IsBlock::BLOCK); // 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, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); // place for the copy of This
CBotStack* pile1 = pj->AddStack(this, CBotStack::IsBlock::BLOCK); // place for the copy of This
if (pile1->IfStep()) return false;

View File

@ -80,7 +80,7 @@ CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal
bool CBotListInstr::Execute(CBotStack* &pj)
{
CBotStack* pile = pj->AddStack(this, CBotStack::UnknownEnumBlock::UNKNOWN_TRUE); //needed for SetState()
CBotStack* pile = pj->AddStack(this, CBotStack::IsBlock::BLOCK); //needed for SetState()
if (pile->StackOver() ) return pj->Return( pile);

View File

@ -58,7 +58,7 @@ CBotStack* CBotStack::AllocateStack()
// completely empty
memset(p, 0, size);
p-> m_bBlock = UnknownEnumBlock::UNKNOWN_TRUE ;
p-> m_bBlock = IsBlock::BLOCK;
m_timer = m_initimer; // sets the timer at the beginning
CBotStack* pp = p;
@ -107,7 +107,7 @@ void CBotStack::Delete()
// routine improved
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::AddStack(CBotInstr* instr, UnknownEnumBlock bBlock)
CBotStack* CBotStack::AddStack(CBotInstr* instr, IsBlock bBlock)
{
if (m_next != nullptr)
{
@ -129,12 +129,12 @@ CBotStack* CBotStack::AddStack(CBotInstr* instr, UnknownEnumBlock bBlock)
p->m_prev = this;
p->m_state = 0;
p->m_call = nullptr;
p->m_bFunc = IsFunctionParam::FALSE;
p->m_bFunc = IsFunction::NO;
return p;
}
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::AddStackEOX(CBotExternalCall* instr, UnknownEnumBlock bBlock)
CBotStack* CBotStack::AddStackEOX(CBotExternalCall* instr, IsBlock bBlock)
{
if (m_next != nullptr)
{
@ -147,12 +147,12 @@ CBotStack* CBotStack::AddStackEOX(CBotExternalCall* instr, UnknownEnumBlock bBlo
}
CBotStack* p = AddStack(nullptr, bBlock);
p->m_call = instr;
p->m_bFunc = IsFunctionParam::UNKNOWN_EOX_SPECIAL; // special
p->m_bFunc = IsFunction::EXTERNAL_CALL; // special
return p;
}
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::AddStack2(UnknownEnumBlock bBlock)
CBotStack* CBotStack::AddStack2(IsBlock bBlock)
{
if (m_next2 != nullptr)
{
@ -176,7 +176,7 @@ CBotStack* CBotStack::AddStack2(UnknownEnumBlock bBlock)
}
////////////////////////////////////////////////////////////////////////////////
CBotStack::UnknownEnumBlock CBotStack::GetBlock()
CBotStack::IsBlock CBotStack::GetBlock()
{
return m_bBlock;
}
@ -193,7 +193,7 @@ bool CBotStack::Return(CBotStack* pfils)
m_next->Delete();m_next = nullptr; // releases the stack above
m_next2->Delete();m_next2 = nullptr; // also the second stack (catch)
return (m_error == 0); // interrupted if error
return IsOk(); // interrupted if error
}
////////////////////////////////////////////////////////////////////////////////
@ -205,7 +205,7 @@ bool CBotStack::ReturnKeep(CBotStack* pfils)
m_var = pfils->m_var; // result transmitted
pfils->m_var = nullptr; // not to destroy the variable
return (m_error == 0); // interrupted if error
return IsOk(); // interrupted if error
}
////////////////////////////////////////////////////////////////////////////////
@ -310,14 +310,6 @@ bool CBotStack::GetRetVar(bool bRet)
return bRet; // interrupted by something other than return
}
////////////////////////////////////////////////////////////////////////////////
CBotError CBotStack::GetError(int& start, int& end)
{
start = m_start;
end = m_end;
return m_error;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::FindVar(CBotToken*& pToken, bool bUpdate)
{
@ -509,23 +501,6 @@ CBotVar* CBotStack::GetVar()
return m_var;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::GetPtVar()
{
CBotVar* p = m_var;
m_var = nullptr; // therefore will not be destroyed
return p;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::GetCopyVar()
{
if (m_var == nullptr) return nullptr;
CBotVar* v = CBotVar::Create("", m_var->GetType());
v->Copy( m_var );
return v;
}
////////////////////////////////////////////////////////////////////////////////
long CBotStack::GetVal()
{
@ -539,7 +514,7 @@ void CBotStack::AddVar(CBotVar* pVar)
CBotStack* p = this;
// returns to the father element
while (p != nullptr && p->m_bBlock == UnknownEnumBlock::UNKNOWN_FALSE) p = p->m_prev;
while (p != nullptr && p->m_bBlock == IsBlock::INSTRUCTION) p = p->m_prev;
if ( p == nullptr ) return;
@ -555,11 +530,11 @@ void CBotStack::AddVar(CBotVar* pVar)
void CBotStack::SetProgram(CBotProgram* p)
{
m_prog = p;
m_bFunc = IsFunctionParam::TRUE;
m_bFunc = IsFunction::TRUE;
}
////////////////////////////////////////////////////////////////////////////////
CBotProgram* CBotStack::GetBotCall(bool bFirst)
CBotProgram* CBotStack::GetProgram(bool bFirst)
{
if ( ! bFirst ) return m_prog;
CBotStack* p = this;
@ -645,7 +620,7 @@ void CBotStack::GetRunPos(std::string& FunctionName, int& start, int& end)
while (p->m_next != nullptr)
{
if ( p->m_instr != nullptr ) instr = p->m_instr;
if ( p->m_bFunc == IsFunctionParam::TRUE && p->m_instr != nullptr ) funct = p->m_instr;
if ( p->m_bFunc == IsFunction::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 ;
@ -653,7 +628,7 @@ void CBotStack::GetRunPos(std::string& FunctionName, int& start, int& end)
}
if ( p->m_instr != nullptr ) instr = p->m_instr;
if ( p->m_bFunc == IsFunctionParam::TRUE && p->m_instr != nullptr ) funct = p->m_instr;
if ( p->m_bFunc == IsFunction::TRUE && p->m_instr != nullptr ) funct = p->m_instr;
if ( funct == nullptr ) return;
@ -687,13 +662,13 @@ CBotVar* CBotStack::GetStackVars(std::string& FunctionName, int level)
// descends upon the elements of block
while ( p != nullptr && p->m_bBlock == UnknownEnumBlock::UNKNOWN_FALSE ) p = p->m_prev;
while ( p != nullptr && p->m_bBlock == IsBlock::INSTRUCTION) 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 == UnknownEnumBlock::UNKNOWN_FALSE ) p = p->m_prev;
while ( p != nullptr && p->m_bBlock == IsBlock::INSTRUCTION) p = p->m_prev;
}
// Now p is on the block "level"
@ -703,7 +678,7 @@ CBotVar* CBotStack::GetStackVars(std::string& FunctionName, int level)
CBotStack* pp = p;
while ( pp != nullptr )
{
if ( pp->m_bFunc == IsFunctionParam::TRUE) break;
if ( pp->m_bFunc == IsFunction::TRUE) break;
pp = pp->m_prev;
}
@ -762,7 +737,7 @@ bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack)
}
if (!ReadWord(pf, w)) return false; // is a local block
pStack->m_bBlock = static_cast<UnknownEnumBlock>(w);
pStack->m_bBlock = static_cast<IsBlock>(w);
if (!ReadWord(pf, w)) return false; // in what state ?
pStack->SetState(static_cast<short>(w)); // in a good state

View File

@ -35,12 +35,18 @@ class CBotToken;
/**
* \brief The execution stack
*
* \nosubgrouping
*/
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
enum class IsBlock : unsigned short { INSTRUCTION = 0, BLOCK = 1, FUNCTION = 2 }; // TODO: figure out what these mean ~krzys_h
enum class IsFunction : unsigned short { NO = 0, TRUE = 1, EXTERNAL_CALL = 2 }; // TODO: just guessing the meaning of values, should be verified ~krzys_h
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//! \name Stack memory management
//@{
/**
* \brief Allocate the stack
@ -60,26 +66,54 @@ public:
*/
bool StackOver();
//@}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/** \name Error management
*
* Be careful - errors are stored in static variables!
* \todo Refactor that
*/
//@{
/**
* \brief Get last error
* \param[out] start Starting position in code of the error
* \param[out] end Ending position in code of the error
* \return Error number
*/
CBotError GetError(int& start, int& end);
CBotError GetError(int& start, int& end) { start = m_start; end = m_end; return m_error; }
/**
* \brief Get last error
* \return Error number
* \see GetError(int&, int&) for error position in code
*/
CBotError GetError();
CBotError GetError() { return m_error; }
/**
* \brief Check if there was an error
* \return false if an error occured
* \see GetError()
*/
bool IsOk()
{
return m_error == CBotNoErr;
}
//@}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* \brief Reset the stack - resets the error and timer
*/
void Reset();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//! \name Local variables
//@{
/**
* \brief Adds a local variable
* \param var Variable to be added
@ -126,46 +160,158 @@ public:
*/
CBotVar* CopyVar(CBotToken& pToken, bool bUpdate = false);
//@}
CBotStack* AddStack(CBotInstr* instr = nullptr, UnknownEnumBlock bBlock = UnknownEnumBlock::UNKNOWN_FALSE); // extends the stack
CBotStack* AddStackEOX(CBotExternalCall* instr = nullptr, UnknownEnumBlock bBlock = UnknownEnumBlock::UNKNOWN_FALSE); // extends the stack
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/** \name Child stacks
*
* When you enter a new code block or instruction, child stack is created
* for managing everything that happens inside that block / instruction.
*/
//@{
/**
* \brief Creates or gets the primary child stack
*
* If the stack already exists, it is returned.
* Otherwise, a new stack is created.
*
* \todo Document params
* \returns New stack element
*/
CBotStack* AddStack(CBotInstr* instr = nullptr, IsBlock bBlock = IsBlock::INSTRUCTION);
/**
* \brief Creates or gets the secondary child stack
* \todo What is it used for?
*
* If the stack already exists, it is returned.
* Otherwise, a new stack is created.
*
* \see AddStack()
* \return New stack element
*/
CBotStack* AddStack2(IsBlock bBlock = IsBlock::INSTRUCTION);
/**
* \brief Adds special EOX stack marker
*
* \todo What is this thing?
* Used by external calls
*
* \todo Document params & return
*/
CBotStack* AddStackEOX(CBotExternalCall* instr = nullptr, IsBlock bBlock = IsBlock::INSTRUCTION);
/**
* \brief Restore CBotInstr pointer after loading stack from file
* \todo Check what this does exactly
*/
CBotStack* RestoreStack(CBotInstr* instr = nullptr);
/**
* \brief Restores CBotExternalCall in the EOX marker after loading stack from file
* \todo Check what this does exactly
*/
CBotStack* RestoreStackEOX(CBotExternalCall* instr = nullptr);
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 std::string& name = nullptr);
// in case of eventual break
bool IfContinue(int state, const std::string& name);
// or "continue"
/**
* \brief Return to this point - copy the result from given stack, and destroy all child stacks from here
*
* \todo Better description
*
* \param pFils Stack to copy result from
* \return IsOk()
*/
bool Return(CBotStack* pFils);
/**
* \brief Like Return() but doesn't destroy the stacks
*
* \param pFils Stack to copy result from
* \return IsOk()
*/
bool ReturnKeep(CBotStack* pFils);
bool IsOk();
/**
* \todo Document
* in case of eventual break
*/
bool BreakReturn(CBotStack* pfils, const std::string& name = nullptr);
/**
* \todo Document
* or "continue"
*/
bool IfContinue(int state, const std::string& name);
//@}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/** \name Preserving execution status
*
* A "state" is a number that determines how much of CBotInstr::Execute() has been already executed.
* When CBotInstr::Execute() is called, it continues execution from the point it finished at.
* See various CBotInstr::Execute() implementations for details.
*
* Call CBotStack::Execute() to resume execution of the interrupted instruction
*
* \todo More detailed docs on functions
*/
//@{
bool SetState(int n, int lim = -10); // select a state
int GetState(); // in what state am I?
int GetState() { return m_state; } // in what state am I?
bool IncState(int lim = -10); // passes to the next state
bool IfStep(); // do step by step
bool Execute();
void SetVar( CBotVar* var );
//@}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// \name Result variable
//@{
/**
* \brief Set the result variable
* \todo CBotStack takes over the ownership - use std::unique_ptr here
* \param var Result variable to set
*/
void SetVar(CBotVar* var);
/**
* \brief Set the result variable to copy of given variable
* \param var Variable to copy as result
*/
void SetCopyVar( CBotVar* var );
/**
* \brief Return result variable
* \return Variable set with SetVar() or SetCopyVar()
*/
CBotVar* GetVar();
CBotVar* GetCopyVar();
CBotVar* GetPtVar();
/**
* \todo Document
*/
bool GetRetVar(bool bRet);
/**
* \brief Return the result variable as int
* \deprecated Please use GetVar()->GetValInt() instead
* \todo Remove
* \return GetVar()->GetValInt(), or 0 if GetVar() == nullptr
*/
long GetVal();
//@}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SetError(CBotError n, CBotToken* token = nullptr);
void SetPosError(CBotToken* token);
void ResetError(CBotError n, int start, int end);
void SetBreak(int val, const std::string& name);
void SetProgram(CBotProgram* p);
CBotProgram* GetBotCall(bool bFirst = false);
CBotProgram* GetProgram(bool bFirst = false);
void SetUserPtr(void* user);
void* GetUserPtr();
UnknownEnumBlock GetBlock();
IsBlock GetBlock();
bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype);
@ -174,8 +320,7 @@ public:
bool SaveState(FILE* pf);
bool RestoreState(FILE* pf, CBotStack* &pStack);
static
void SetTimer(int n);
static void SetTimer(int n);
void GetRunPos(std::string& FunctionName, int& start, int& end);
CBotVar* GetStackVars(std::string& FunctionName, int level);
@ -197,7 +342,7 @@ private:
CBotVar* m_var; // result of the operations
CBotVar* m_listVar; // variables declared at this level
UnknownEnumBlock m_bBlock; // is part of a block (variables are local to this block)
IsBlock 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
@ -212,24 +357,7 @@ private:
void* m_pUser;
CBotInstr* m_instr; // the corresponding instruction
IsFunctionParam m_bFunc; // an input of a function?
IsFunction m_bFunc; // an input of a function?
CBotExternalCall* m_call; // recovery point in a extern call
friend class CBotTry;
};
// inline routinees must be declared in file.h
inline bool CBotStack::IsOk()
{
return (m_error == 0);
}
inline int CBotStack::GetState()
{
return m_state;
}
inline CBotError CBotStack::GetError()
{
return m_error;
}