Refactored EOX

dev-time-step
krzys-h 2015-12-31 17:59:48 +01:00
parent a70381e1c8
commit d63773d89d
17 changed files with 140 additions and 240 deletions

View File

@ -22,8 +22,6 @@
#define STACKMEM 1 /// \def preserve memory for the execution stack #define STACKMEM 1 /// \def preserve memory for the execution stack
#define MAXSTACK 990 /// \def stack size reserved #define MAXSTACK 990 /// \def stack size reserved
#define EOX (reinterpret_cast<CBotStack*>(-1)) /// \def tag special condition
#define MAXARRAYSIZE 9999 #define MAXARRAYSIZE 9999
//! Define the current CBot version //! Define the current CBot version

View File

@ -71,7 +71,8 @@ bool CBotExternalCallList::CheckCall(const std::string& name)
return m_list.count(name) > 0; return m_list.count(name) > 0;
} }
int CBotExternalCallList::DoCall(CBotToken* token, CBotVar* thisVar, CBotVar** ppVar, CBotStack* pStack, const CBotTypResult& rettype) int CBotExternalCallList::DoCall(CBotToken* token, CBotVar* thisVar, CBotVar** ppVar, CBotStack* pStack,
const CBotTypResult& rettype)
{ {
if (token == nullptr) if (token == nullptr)
return -1; return -1;
@ -81,18 +82,18 @@ int CBotExternalCallList::DoCall(CBotToken* token, CBotVar* thisVar, CBotVar** p
CBotExternalCall* pt = m_list[token->GetString()].get(); CBotExternalCall* pt = m_list[token->GetString()].get();
CBotStack* pile = pStack->AddStackEOX(pt); if (pStack->IsCallFinished()) return true;
if (pile == EOX) return true; CBotStack* pile = pStack->AddStackExternalCall(pt);
// lists the parameters depending on the contents of the stack (pStackVar) // lists the parameters depending on the contents of the stack (pStackVar)
CBotVar* pVar = MakeListVars(ppVar, true); CBotVar* pVar = MakeListVars(ppVar, true);
// creates a variable to the result // creates a variable to the result
CBotVar* pResult = rettype.Eq(CBotTypVoid) ? nullptr : CBotVar::Create("", rettype); CBotVar* pResult = rettype.Eq(CBotTypVoid) ? nullptr : CBotVar::Create("", rettype);
pile->SetVar(pVar); pile->SetVar(pVar);
CBotStack* pile2 = pile->AddStack(); CBotStack* pile2 = pile->AddStack();
pile2->SetVar(pResult); pile2->SetVar(pResult);
pile->SetError(CBotNoErr, token); // save token for the position in case of error pile->SetError(CBotNoErr, token); // save token for the position in case of error
@ -106,8 +107,8 @@ bool CBotExternalCallList::RestoreCall(CBotToken* token, CBotVar* thisVar, CBotV
CBotExternalCall* pt = m_list[token->GetString()].get(); CBotExternalCall* pt = m_list[token->GetString()].get();
CBotStack* pile = pStack->RestoreStackEOX(pt); CBotStack* pile = pStack->RestoreStackEOX(pt);
if ( pile == nullptr ) return true; if (pile == nullptr) return true;
pile->RestoreStack(); pile->RestoreStack();
return true; return true;
@ -142,8 +143,8 @@ CBotTypResult CBotExternalCallDefault::Compile(CBotVar* thisVar, CBotVar* args,
bool CBotExternalCallDefault::Run(CBotVar* thisVar, CBotStack* pStack) bool CBotExternalCallDefault::Run(CBotVar* thisVar, CBotStack* pStack)
{ {
CBotStack* pile = pStack->AddStackEOX(this); if (pStack->IsCallFinished()) return true;
if ( pile == EOX ) return true; CBotStack* pile = pStack->AddStackExternalCall(this);
CBotVar* args = pile->GetVar(); CBotVar* args = pile->GetVar();
CBotStack* pile2 = pile->AddStack(); CBotStack* pile2 = pile->AddStack();
@ -167,48 +168,4 @@ bool CBotExternalCallDefault::Run(CBotVar* thisVar, CBotStack* pStack)
return true; return true;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// }
CBotExternalCallDefaultClass::CBotExternalCallDefaultClass(RuntimeFunc rExec, CompileFunc rCompile)
{
m_rExec = rExec;
m_rComp = rCompile;
}
CBotExternalCallDefaultClass::~CBotExternalCallDefaultClass()
{
}
CBotTypResult CBotExternalCallDefaultClass::Compile(CBotVar* thisVar, CBotVar* args, void* user)
{
return m_rComp(nullptr, args);
}
// TODO: Figure out why classes do pStack->SetVar while normal calls do pStack->SetCopyVar
bool CBotExternalCallDefaultClass::Run(CBotVar* thisVar, CBotStack* pStack)
{
CBotStack* pile = pStack->AddStackEOX(this);
if ( pile == EOX ) return true;
CBotVar* args = pile->GetVar();
CBotStack* pile2 = pile->AddStack();
CBotVar* result = pile2->GetVar();
int exception = CBotNoErr; // TODO: Change to CBotError
bool res = m_rExec(thisVar, args, result, exception, pStack->GetUserPtr());
pStack->SetVar(result);
if (!res)
{
if (exception != CBotNoErr)
{
pStack->SetError(static_cast<CBotError>(exception));
}
return false;
}
return true;
}
} // namespace CBot

View File

@ -105,36 +105,6 @@ private:
CompileFunc m_rComp; CompileFunc m_rComp;
}; };
/**
* \brief Default implementation of CBot external class call, using compilation and runtime functions
*/
class CBotExternalCallDefaultClass : public CBotExternalCall
{
public:
typedef bool (*RuntimeFunc)(CBotVar* thisVar, CBotVar* args, CBotVar* result, int& exception, void* user);
typedef CBotTypResult (*CompileFunc)(CBotVar* thisVar, CBotVar*& args); // TODO: Add user pointer
/**
* \brief Constructor
* \param rExec Runtime function
* \param rCompile Compilation function
* \see CBotClass::AddFunction()
*/
CBotExternalCallDefaultClass(RuntimeFunc rExec, CompileFunc rCompile);
/**
* \brief Destructor
*/
virtual ~CBotExternalCallDefaultClass();
virtual CBotTypResult Compile(CBotVar* thisVar, CBotVar* args, void* user);
virtual bool Run(CBotVar* thisVar, CBotStack* pStack);
private:
RuntimeFunc m_rExec;
CompileFunc m_rComp;
};
/** /**
* \brief Class for mangaging CBot external calls * \brief Class for mangaging CBot external calls

View File

@ -73,12 +73,12 @@ std::size_t fRead(void *buffer,
FILE* filehandle); FILE* filehandle);
/*! /*!
* \brief SaveVar * \brief SaveVars
* \param pf * \param pf
* \param pVar * \param pVar
* \return * \return
*/ */
bool SaveVar(FILE* pf, CBotVar* pVar); bool SaveVars(FILE* pf, CBotVar* pVar);
/*! /*!
* \brief WriteWord * \brief WriteWord

View File

@ -72,7 +72,6 @@ bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prev
{ {
CBotStack* pj = pile; CBotStack* pj = pile;
pile = pile->AddStack(this); // changes in output stack pile = pile->AddStack(this); // changes in output stack
if (pile == EOX) return true;
if (pVar->GetType(CBotVar::GetTypeMode::CLASS_AS_POINTER) != CBotTypPointer) if (pVar->GetType(CBotVar::GetTypeMode::CLASS_AS_POINTER) != CBotTypPointer)
@ -107,7 +106,7 @@ bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prev
} }
// request the update of the element, if applicable // request the update of the element, if applicable
pVar->Maj(pile->GetUserPtr()); pVar->Update(pile->GetUserPtr());
if ( m_next3 != nullptr && if ( m_next3 != nullptr &&
!m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return false; !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return false;

View File

@ -91,7 +91,7 @@ bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prev
return pj->Return(pile); return pj->Return(pile);
} }
pVar->Maj(pile->GetUserPtr()); pVar->Update(pile->GetUserPtr());
if ( m_next3 != nullptr && if ( m_next3 != nullptr &&
!m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return false; !m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return false;

View File

@ -101,14 +101,14 @@ bool CBotTry::Execute(CBotStack* &pj)
} }
val = pile1->GetError(); val = pile1->GetError();
if ( val == 0 && CBotStack::m_initimer == 0 ) // mode step? if ( val == CBotNoErr && pile1->GetTimer() == 0 ) // mode step?
return false; // does not make the catch return false; // does not make the catch
pile1->IncState(); pile1->IncState();
pile2->SetState(val); // stores the error number pile2->SetState(val); // stores the error number
pile1->SetError(CBotNoErr); // for now there is are more errors! pile1->SetError(CBotNoErr); // for now there is are more errors!
if ( val == 0 && CBotStack::m_initimer < 0 ) // mode step? if ( val == CBotNoErr && pile1->GetTimer() < 0 ) // mode step?
return false; // does not make the catch return false; // does not make the catch
} }

View File

@ -36,9 +36,9 @@
namespace CBot namespace CBot
{ {
#define ITIMER 100 const int DEFAULT_TIMER = 100;
int CBotStack::m_initimer = ITIMER; int CBotStack::m_initimer = DEFAULT_TIMER;
int CBotStack::m_timer = 0; int CBotStack::m_timer = 0;
CBotVar* CBotStack::m_retvar = nullptr; CBotVar* CBotStack::m_retvar = nullptr;
CBotError CBotStack::m_error = CBotNoErr; CBotError CBotStack::m_error = CBotNoErr;
@ -61,7 +61,7 @@ CBotStack* CBotStack::AllocateStack()
// completely empty // completely empty
memset(p, 0, size); memset(p, 0, size);
p-> m_bBlock = BlockVisibilityType::BLOCK; p->m_block = BlockVisibilityType::BLOCK;
m_timer = m_initimer; // sets the timer at the beginning m_timer = m_initimer; // sets the timer at the beginning
CBotStack* pp = p; CBotStack* pp = p;
@ -80,7 +80,7 @@ CBotStack* CBotStack::AllocateStack()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotStack::Delete() void CBotStack::Delete()
{ {
if ( this == nullptr || this == EOX ) return; if ( this == nullptr ) return;
m_next->Delete(); m_next->Delete();
m_next2->Delete(); m_next2->Delete();
@ -125,32 +125,29 @@ CBotStack* CBotStack::AddStack(CBotInstr* instr, BlockVisibilityType bBlock)
while ( p->m_prev != nullptr ); while ( p->m_prev != nullptr );
m_next = p; // chain an element m_next = p; // chain an element
p->m_bBlock = bBlock; p->m_block = bBlock;
p->m_instr = instr; p->m_instr = instr;
p->m_prog = m_prog; p->m_prog = m_prog;
p->m_step = 0; p->m_step = 0;
p->m_prev = this; p->m_prev = this;
p->m_state = 0; p->m_state = 0;
p->m_call = nullptr; p->m_call = nullptr;
p->m_bFunc = IsFunction::NO; p->m_func = IsFunction::NO;
return p; p->m_callFinished = false;
return p;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::AddStackEOX(CBotExternalCall* instr, BlockVisibilityType bBlock) CBotStack* CBotStack::AddStackExternalCall(CBotExternalCall* instr, BlockVisibilityType bBlock)
{ {
assert(!m_callFinished);
if (m_next != nullptr) if (m_next != nullptr)
{ {
if ( m_next == EOX )
{
m_next = nullptr;
return EOX;
}
return m_next; // included in an existing stack return m_next; // included in an existing stack
} }
CBotStack* p = AddStack(nullptr, bBlock); CBotStack* p = AddStack(nullptr, bBlock);
p->m_call = instr; p->m_call = instr;
p->m_bFunc = IsFunction::EXTERNAL_CALL; // special p->m_func = IsFunction::EXTERNAL_CALL;
return p; return p;
} }
@ -172,7 +169,7 @@ CBotStack* CBotStack::AddStack2(BlockVisibilityType bBlock)
m_next2 = p; // chain an element m_next2 = p; // chain an element
p->m_prev = this; p->m_prev = this;
p->m_bBlock = bBlock; p->m_block = bBlock;
p->m_prog = m_prog; p->m_prog = m_prog;
p->m_step = 0; p->m_step = 0;
return p; return p;
@ -181,7 +178,7 @@ CBotStack* CBotStack::AddStack2(BlockVisibilityType bBlock)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotStack::BlockVisibilityType CBotStack::GetBlock() CBotStack::BlockVisibilityType CBotStack::GetBlock()
{ {
return m_bBlock; return m_block;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -282,7 +279,7 @@ bool CBotStack::IfContinue(int state, const std::string& name)
m_state = state; // where again? m_state = state; // where again?
m_error = CBotNoErr; m_error = CBotNoErr;
m_labelBreak.clear(); m_labelBreak.clear();
if ( m_next != EOX ) m_next->Delete(); // purge above stack m_next->Delete(); // purge above stack
return true; return true;
} }
@ -327,7 +324,7 @@ CBotVar* CBotStack::FindVar(CBotToken*& pToken, bool bUpdate)
if (pp->GetName() == name) if (pp->GetName() == name)
{ {
if ( bUpdate ) if ( bUpdate )
pp->Maj(m_pUser); pp->Update(m_pUser);
return pp; return pp;
} }
@ -370,7 +367,7 @@ CBotVar* CBotStack::FindVar(long ident, bool bUpdate)
if (pp->GetUniqNum() == ident) if (pp->GetUniqNum() == ident)
{ {
if ( bUpdate ) if ( bUpdate )
pp->Maj(m_pUser); pp->Update(m_pUser);
return pp; return pp;
} }
@ -451,6 +448,11 @@ void CBotStack::SetTimer(int n)
m_initimer = n; m_initimer = n;
} }
int CBotStack::GetTimer()
{
return m_initimer;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool CBotStack::Execute() bool CBotStack::Execute()
{ {
@ -476,7 +478,7 @@ bool CBotStack::Execute()
pile->m_next->Delete(); pile->m_next->Delete();
pile->m_next = EOX; // special for recovery pile->m_callFinished = true;
return true; return true;
} }
@ -517,12 +519,10 @@ void CBotStack::AddVar(CBotVar* pVar)
CBotStack* p = this; CBotStack* p = this;
// returns to the father element // returns to the father element
while (p != nullptr && p->m_bBlock == BlockVisibilityType::INSTRUCTION) p = p->m_prev; while (p != nullptr && p->m_block == BlockVisibilityType::INSTRUCTION) p = p->m_prev;
if ( p == nullptr ) return; if ( p == nullptr ) return;
// p->m_bDontDelete = bDontDelete;
CBotVar** pp = &p->m_listVar; CBotVar** pp = &p->m_listVar;
while ( *pp != nullptr ) pp = &(*pp)->m_next; while ( *pp != nullptr ) pp = &(*pp)->m_next;
@ -532,8 +532,8 @@ void CBotStack::AddVar(CBotVar* pVar)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotStack::SetProgram(CBotProgram* p) void CBotStack::SetProgram(CBotProgram* p)
{ {
m_prog = p; m_prog = p;
m_bFunc = IsFunction::YES; m_func = IsFunction::YES;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -593,23 +593,6 @@ void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar)
m_prog->GetFunctions()->RestoreCall(nIdent, token->GetString(), ppVar, this); m_prog->GetFunctions()->RestoreCall(nIdent, token->GetString(), ppVar, this);
} }
////////////////////////////////////////////////////////////////////////////////
bool SaveVar(FILE* pf, CBotVar* pVar)
{
while ( true )
{
if ( pVar == nullptr )
{
return WriteWord(pf, 0); // is a terminator
}
if ( !pVar->Save0State(pf)) return false; // common header
if ( !pVar->Save1State(pf) ) return false; // saves as the child class
pVar = pVar->GetNext();
}
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotStack::GetRunPos(std::string& functionName, int& start, int& end) void CBotStack::GetRunPos(std::string& functionName, int& start, int& end)
{ {
@ -623,7 +606,7 @@ void CBotStack::GetRunPos(std::string& functionName, int& start, int& end)
while (p->m_next != nullptr) while (p->m_next != nullptr)
{ {
if ( p->m_instr != nullptr ) instr = p->m_instr; if ( p->m_instr != nullptr ) instr = p->m_instr;
if ( p->m_bFunc == IsFunction::YES && p->m_instr != nullptr ) funct = p->m_instr; if (p->m_func == IsFunction::YES && p->m_instr != nullptr ) funct = p->m_instr;
if ( p->m_next->m_prog != prog ) break ; if ( p->m_next->m_prog != prog ) break ;
if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ; if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ;
@ -631,7 +614,7 @@ void CBotStack::GetRunPos(std::string& functionName, int& start, int& end)
} }
if ( p->m_instr != nullptr ) instr = p->m_instr; if ( p->m_instr != nullptr ) instr = p->m_instr;
if ( p->m_bFunc == IsFunction::YES && p->m_instr != nullptr ) funct = p->m_instr; if (p->m_func == IsFunction::YES && p->m_instr != nullptr ) funct = p->m_instr;
if ( funct == nullptr ) return; if ( funct == nullptr ) return;
@ -665,13 +648,13 @@ CBotVar* CBotStack::GetStackVars(std::string& functionName, int level)
// descends upon the elements of block // descends upon the elements of block
while ( p != nullptr && p->m_bBlock == BlockVisibilityType::INSTRUCTION) p = p->m_prev; while ( p != nullptr && p->m_block == BlockVisibilityType::INSTRUCTION) p = p->m_prev;
// Now p is on the beggining of the top block (with local variables) // Now p is on the beggining of the top block (with local variables)
while ( p != nullptr && level++ < 0 ) while ( p != nullptr && level++ < 0 )
{ {
p = p->m_prev; p = p->m_prev;
while ( p != nullptr && p->m_bBlock == BlockVisibilityType::INSTRUCTION) p = p->m_prev; while ( p != nullptr && p->m_block == BlockVisibilityType::INSTRUCTION) p = p->m_prev;
} }
// Now p is on the block "level" // Now p is on the block "level"
@ -681,7 +664,7 @@ CBotVar* CBotStack::GetStackVars(std::string& functionName, int level)
CBotStack* pp = p; CBotStack* pp = p;
while ( pp != nullptr ) while ( pp != nullptr )
{ {
if ( pp->m_bFunc == IsFunction::YES) break; if (pp->m_func == IsFunction::YES) break;
pp = pp->m_prev; pp = pp->m_prev;
} }
@ -696,59 +679,73 @@ CBotVar* CBotStack::GetStackVars(std::string& functionName, int level)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool CBotStack::SaveState(FILE* pf) bool CBotStack::SaveState(FILE* pf)
{ {
if ( this == nullptr ) // end of the tree? if (m_next2 != nullptr)
{ {
return WriteWord(pf, 0); // is a terminator if (!WriteWord(pf, 2)) return false; // a marker of type (m_next2)
} if (!m_next2->SaveState(pf)) return false; // saves the next element
if ( m_next2 != nullptr )
{
if (!WriteWord(pf, 2)) return false; // a mark of pursuit
if (!m_next2->SaveState(pf)) return false;
} }
else else
{ {
if (!WriteWord(pf, 1)) return false; // a mark of pursuit if (!WriteWord(pf, 1)) return false; // a marker of type (m_next)
} }
if (!WriteWord(pf, static_cast<unsigned short>(m_bBlock))) return false; // is a local block if (!WriteWord(pf, static_cast<unsigned short>(m_block))) return false;
if (!WriteWord(pf, m_state)) return false; // in what state? if (!WriteWord(pf, m_state)) return false;
if (!WriteWord(pf, 0)) return false; // by compatibility m_bDontDelete if (!WriteWord(pf, 0)) return false; // for backwards combatibility (m_bDontDelete)
if (!WriteWord(pf, m_step)) return false; // in what state? if (!WriteWord(pf, m_step)) return false;
if (!SaveVar(pf, m_var)) return false; // current result if (!SaveVars(pf, m_var)) return false; // current result
if (!SaveVar(pf, m_listVar)) return false; // local variables if (!SaveVars(pf, m_listVar)) return false; // local variables
return m_next->SaveState(pf); // saves the following if (m_next != nullptr)
{
if (!m_next->SaveState(pf)) return false; // saves the next element
}
else
{
if (!WriteWord(pf, 0)) return false; // terminator
}
return true;
}
bool SaveVars(FILE* pf, CBotVar* pVar)
{
while (pVar != nullptr)
{
if (!pVar->Save0State(pf)) return false; // common header
if (!pVar->Save1State(pf)) return false; // saves the data
pVar = pVar->GetNext();
}
return WriteWord(pf, 0); // terminator
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack)
{ {
unsigned short w; unsigned short w;
pStack = nullptr; pStack = nullptr;
if (!ReadWord(pf, w)) return false; if (!ReadWord(pf, w)) return false;
if ( w == 0 ) return true; if ( w == 0 ) return true; // 0 - terminator
if ( this == nullptr ) pStack = AllocateStack(); if ( this == nullptr ) pStack = AllocateStack();
else pStack = AddStack(); else pStack = AddStack();
if ( w == 2 ) if ( w == 2 ) // 2 - m_next2
{ {
if (!pStack->RestoreState(pf, pStack->m_next2)) return false; if (!pStack->RestoreState(pf, pStack->m_next2)) return false;
} }
if (!ReadWord(pf, w)) return false; // is a local block if (!ReadWord(pf, w)) return false;
pStack->m_bBlock = static_cast<BlockVisibilityType>(w); pStack->m_block = static_cast<BlockVisibilityType>(w);
if (!ReadWord(pf, w)) return false; // in what state ? if (!ReadWord(pf, w)) return false;
pStack->SetState(static_cast<short>(w)); // in a good state pStack->SetState(static_cast<short>(w));
if (!ReadWord(pf, w)) return false; // dont delete? if (!ReadWord(pf, w)) return false; // backwards compatibility (m_bDontDelete)
// uses more
if (!ReadWord(pf, w)) return false; // step by step if (!ReadWord(pf, w)) return false;
pStack->m_step = w; pStack->m_step = w;
if (!CBotVar::RestoreState(pf, pStack->m_var)) return false; // temp variable if (!CBotVar::RestoreState(pf, pStack->m_var)) return false; // temp variable
@ -767,32 +764,6 @@ bool CBotVar::Save0State(FILE* pf)
return WriteString(pf, m_token->GetString()); // and variable name return WriteString(pf, m_token->GetString()); // and variable name
} }
namespace
{
bool ParseInitType(int rawInitType, CBotVar::InitType* initType)
{
switch (rawInitType)
{
case static_cast<int>(CBotVar::InitType::UNDEF):
*initType = CBotVar::InitType::UNDEF;
break;
case static_cast<int>(CBotVar::InitType::DEF):
*initType = CBotVar::InitType::DEF;
break;
case static_cast<int>(CBotVar::InitType::IS_POINTER):
*initType = CBotVar::InitType::IS_POINTER;
break;
case static_cast<int>(CBotVar::InitType::IS_NAN):
*initType = CBotVar::InitType::IS_NAN;
break;
default:
*initType = CBotVar::InitType::UNDEF;
return false;
}
return true;
}
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar)
{ {
@ -828,8 +799,8 @@ bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar)
if ( w == CBotTypClass ) w = CBotTypIntrinsic; // necessarily intrinsic if ( w == CBotTypClass ) w = CBotTypIntrinsic; // necessarily intrinsic
CBotVar::InitType initType = CBotVar::InitType::UNDEF; if (!ReadWord(pf, wi)) return false; // init ?
if (!ReadWord(pf, wi) || !ParseInitType(wi, &initType)) return false; // init ? CBotVar::InitType initType = static_cast<CBotVar::InitType>(wi);
if (!ReadString(pf, name)) return false; // variable name if (!ReadString(pf, name)) return false; // variable name
CBotToken token(name, std::string()); CBotToken token(name, std::string());
@ -929,4 +900,9 @@ bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar)
return true; return true;
} }
bool CBotStack::IsCallFinished()
{
return m_callFinished;
}
} // namespace CBot } // namespace CBot

View File

@ -240,7 +240,8 @@ public:
* *
* \todo Document params & return * \todo Document params & return
*/ */
CBotStack* AddStackEOX(CBotExternalCall* instr = nullptr, BlockVisibilityType bBlock = BlockVisibilityType::INSTRUCTION); CBotStack* AddStackExternalCall(CBotExternalCall* instr = nullptr,
BlockVisibilityType bBlock = BlockVisibilityType::INSTRUCTION);
/** /**
* \brief Restore CBotInstr pointer after loading stack from file * \brief Restore CBotInstr pointer after loading stack from file
@ -446,6 +447,10 @@ public:
* \todo Full documentation of the timer * \todo Full documentation of the timer
*/ */
static void SetTimer(int n); static void SetTimer(int n);
/**
* \brief Get the current configured maximum number of "timer ticks" (parts of instructions) to execute
*/
static int GetTimer();
/** /**
* \brief Get current position in the program * \brief Get current position in the program
@ -462,41 +467,41 @@ public:
*/ */
CBotVar* GetStackVars(std::string& functionName, int level); CBotVar* GetStackVars(std::string& functionName, int level);
bool IsCallFinished();
private: private:
CBotStack* m_next; CBotStack* m_next;
CBotStack* m_next2; CBotStack* m_next2;
CBotStack* m_prev; CBotStack* m_prev;
friend class CBotDefArray;
int m_state; int m_state;
int m_step; int m_step;
static CBotError m_error; static CBotError m_error;
static int m_start; static int m_start;
static int m_end; static int m_end;
static static CBotVar* m_retvar; // result of a return
CBotVar* m_retvar; // result of a return
CBotVar* m_var; // result of the operations CBotVar* m_var; // result of the operations
CBotVar* m_listVar; // variables declared at this level CBotVar* m_listVar; // variables declared at this level
BlockVisibilityType m_bBlock; // is part of a block (variables are local to this block) BlockVisibilityType m_block; // is part of a block (variables are local to this block)
bool m_bOver; // stack limits? bool m_bOver; // stack limits?
// bool m_bDontDelete; // special, not to destroy the variable during delete //! CBotProgram instance the execution is in in this stack level
CBotProgram* m_prog; // user-defined functions CBotProgram* m_prog;
static static int m_initimer;
int m_initimer; static int m_timer;
static static std::string m_labelBreak;
int m_timer; static void* m_pUser;
static
std::string m_labelBreak;
static
void* m_pUser;
CBotInstr* m_instr; // the corresponding instruction //! The corresponding instruction
IsFunction m_bFunc; // an input of a function? CBotInstr* m_instr;
CBotExternalCall* m_call; // recovery point in a extern call //! If this stack level holds a function call
friend class CBotTry; IsFunction m_func;
//! Extern call on this level (only if m_func == IsFunction::EXTERNAL_CALL)
CBotExternalCall* m_call;
bool m_callFinished;
}; };
} // namespace CBot } // namespace CBot

View File

@ -128,10 +128,8 @@ bool CBotVar::Save1State(FILE* pf)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotVar::Maj(void* pUser) void CBotVar::Update(void* pUser)
{ {
/* if (!bContinu && m_pMyThis != nullptr)
m_pMyThis->Maj(pUser, true);*/
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -365,7 +365,7 @@ public:
* \param pUser User pointer to pass to the update function * \param pUser User pointer to pass to the update function
* \see CBotClass::SetUpdateFunc() * \see CBotClass::SetUpdateFunc()
*/ */
virtual void Maj(void* pUser); virtual void Update(void* pUser);
/** /**
* \brief Set unique identifier of this variable * \brief Set unique identifier of this variable

View File

@ -141,7 +141,7 @@ std::string CBotVarArray::GetValString()
bool CBotVarArray::Save1State(FILE* pf) bool CBotVarArray::Save1State(FILE* pf)
{ {
if ( !WriteType(pf, m_type) ) return false; if ( !WriteType(pf, m_type) ) return false;
return SaveVar(pf, m_pInstance); // saves the instance that manages the table return SaveVars(pf, m_pInstance); // saves the instance that manages the table
} }
} // namespace CBot } // namespace CBot

View File

@ -228,7 +228,7 @@ CBotClass* CBotVarClass::GetClass()
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotVarClass::Maj(void* pUser) void CBotVarClass::Update(void* pUser)
{ {
// retrieves the user pointer according to the class // retrieves the user pointer according to the class
// or according to the parameter passed to CBotProgram::Run() // or according to the parameter passed to CBotProgram::Run()
@ -467,7 +467,7 @@ bool CBotVarClass::Save1State(FILE* pf)
if ( !WriteType(pf, m_type) ) return false; if ( !WriteType(pf, m_type) ) return false;
if ( !WriteLong(pf, m_ItemIdent) ) return false; if ( !WriteLong(pf, m_ItemIdent) ) return false;
return SaveVar(pf, m_pVar); // content of the object return SaveVars(pf, m_pVar); // content of the object
} }
} // namespace CBot } // namespace CBot

View File

@ -56,7 +56,7 @@ public:
bool Save1State(FILE* pf) override; bool Save1State(FILE* pf) override;
void Maj(void* pUser) override; void Update(void* pUser) override;
//! \name Reference counter //! \name Reference counter
//@{ //@{

View File

@ -51,22 +51,19 @@ CBotVarPointer::CBotVarPointer(const CBotToken& name, CBotTypResult& type)
m_pClass = nullptr; m_pClass = nullptr;
m_pVarClass = nullptr; // will be defined by a SetPointer() m_pVarClass = nullptr; // will be defined by a SetPointer()
SetClass(type.GetClass() ); SetClass(type.GetClass());
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotVarPointer::~CBotVarPointer() CBotVarPointer::~CBotVarPointer()
{ {
if ( m_pVarClass != nullptr ) m_pVarClass->DecrementUse(); // decrement reference if (m_pVarClass != nullptr) m_pVarClass->DecrementUse(); // decrement reference
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotVarPointer::Maj(void* pUser) void CBotVarPointer::Update(void* pUser)
{ {
/* if ( !bContinu && m_pMyThis != nullptr ) if (m_pVarClass != nullptr) m_pVarClass->Update(pUser);
m_pMyThis->Maj(pUser, false);*/
if ( m_pVarClass != nullptr) m_pVarClass->Maj(pUser);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -189,7 +186,7 @@ bool CBotVarPointer::Save1State(FILE* pf)
if (!WriteLong(pf, GetIdent())) return false; // the unique reference if (!WriteLong(pf, GetIdent())) return false; // the unique reference
// also saves the proceedings copies // also saves the proceedings copies
return SaveVar(pf, GetPointer()); return SaveVars(pf, GetPointer());
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -63,7 +63,7 @@ public:
bool Save1State(FILE* pf) override; bool Save1State(FILE* pf) override;
void Maj(void* pUser) override; void Update(void* pUser) override;
bool Eq(CBotVar* left, CBotVar* right) override; bool Eq(CBotVar* left, CBotVar* right) override;
bool Ne(CBotVar* left, CBotVar* right) override; bool Ne(CBotVar* left, CBotVar* right) override;

View File

@ -508,7 +508,7 @@ void PutList(const char *baseName, bool bArray, CBot::CBotVar *var, Ui::CList *l
index = 0; index = 0;
while ( var != nullptr ) while ( var != nullptr )
{ {
var->Maj(nullptr); var->Update(nullptr);
pStatic = var->GetStaticVar(); // finds the static element pStatic = var->GetStaticVar(); // finds the static element
p = pStatic->GetName(); // variable name p = pStatic->GetName(); // variable name