Moving CBotStack class in its own header and source files.

dev-time-step
Grunaka 2015-11-14 12:56:16 +01:00
parent 0373692ea1
commit e54d8f1ebc
48 changed files with 463 additions and 291 deletions

View File

@ -72,6 +72,8 @@
#include "CBotInstr/CBotInstArray.h"
#include "CBotInstr/CBotInt.h"
#include "CBotStack.h"
// Local include

View File

@ -53,254 +53,7 @@ class CBotDefParam; // paramerer list of a function
////////////////////////////////////////////////////////////////////////
// Management of the execution stack
////////////////////////////////////////////////////////////////////////
// actually, externally, the only thing it can do
// is to create an instance of a stack
// to use for routine CBotProgram :: Execute (CBotStack)
/**\class CBotStack
* \brief Management of the execution stack.
* \brief Actually the only thing it can do is to create an instance of a stack
* \brief to use for routine CBotProgram :: Execute(CBotStack)*/
class CBotStack
{
public:
#if STACKMEM
/**
* \brief FirstStack Allocate first stack
* \return pointer to created stack
*/
static CBotStack * FirstStack();
/** \brief Delete Remove current stack */
void Delete();
#endif
/**
* \brief CBotStack Constructor of the stack
* \param ppapa Not used.
*/
CBotStack(CBotStack* ppapa);
/** \brief ~CBotStack Destructor */
~CBotStack();
/**
* \brief StackOver Check if end of stack is reached
* \return true if end of stack
*/
bool StackOver();
/**
* \brief GetError Get error number of the stack
* \param [out] start beginning of the stack
* \param [out] end end of stack
* \return error number
*/
int GetError(int& start, int& end);
/**
* \brief GetError Get error number
* \return eror number
*/
int GetError();// rend le numéro d'erreur retourné
/**
* \brief Reset Reset error at and set user
* \param [in] pUser User of stack
*/
void Reset(void* pUser);
/**
* \brief SetType Determines the type.
* \param type Type of instruction on the stack.
*/
void SetType(CBotTypResult& type);
/**
* \brief GetType Get the type of value on the stack.
* \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic).
* \return Type number.
*/
int GetType(int mode = 0);
/**
* \brief Getes the type of complete value on the stack.
* \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic).
* \return Type of an element.
*/
CBotTypResult GetTypResult(int mode = 0);
/**
* \brief Adds a local variable.
* \param [in] p Variable to be added.
*/
void AddVar(CBotVar* p);
/**
* \brief Fetch a variable by its token.
* \brief This may be a composite variable
* \param [in] pToken Token upon which search is performed
* \param [in] bUpdate Not used. Probably need to be removed
* \param [in] bModif Not used. Probably need to be removed
* \return Found variable
*/
CBotVar* FindVar(CBotToken* &pToken, bool bUpdate = false,
bool bModif = false);
/**
* \brief Fetch a variable by its token.
* \brief This may be a composite variable
* \param [in] pToken Token upon which search is performed
* \param [in] bUpdate Not used. Probably need to be removed
* \param [in] bModif Not used. Probably need to be removed
* \return Found variable
*/
CBotVar* FindVar(CBotToken& pToken, bool bUpdate = false,
bool bModif = false);
/**
* \brief Fetch variable by its name
* \param [in] name Name of variable to find
* \return Found variable
*/
CBotVar* FindVar(const char* name);
/**
* \brief Fetch a variable on the stack according to its identification number
* \brief This is faster than comparing names
* \param [in] ident Identifier of a variable
* \param [in] bUpdate Not used. Probably need to be removed
* \param [in] bModif Not used. Probably need to be removed
* \return Found variable
*/
CBotVar* FindVar(long ident, bool bUpdate = false,
bool bModif = false);
/**
* \brief Find variable by its token and returns a copy of it.
* \param Token Token upon which search is performed
* \param bUpdate Not used.
* \return Found variable, nullptr if not found
*/
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* RestoreStack(CBotInstr* instr = nullptr);
CBotStack* RestoreStackEOX(CBotCall* instr = nullptr);
CBotStack* AddStack2(bool bBlock = 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);
// in case of eventual break
bool IfContinue(int state, const char* name);
// or "continue"
bool IsOk();
bool SetState(int n, int lim = -10); // select a state
int GetState(); // 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 );
void SetCopyVar( CBotVar* var );
CBotVar* GetVar();
CBotVar* GetCopyVar();
CBotVar* GetPtVar();
bool GetRetVar(bool bRet);
long GetVal();
void SetError(int n, CBotToken* token = nullptr);
void SetPosError(CBotToken* token);
void ResetError(int n, int start, int end);
void SetBreak(int val, const char* name);
void SetBotCall(CBotProgram* p);
CBotProgram* GetBotCall(bool bFirst = false);
void* GetPUser();
bool GetBlock();
bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype);
void RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar);
bool SaveState(FILE* pf);
bool RestoreState(FILE* pf, CBotStack* &pStack);
static
void SetTimer(int n);
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;
CBotStack* m_prev;
friend class CBotInstArray;
#ifdef _DEBUG
int m_index;
#endif
int m_state;
int m_step;
static int m_error;
static int m_start;
static int m_end;
static
CBotVar* m_retvar; // result of a return
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)
bool m_bOver; // stack limits?
// bool m_bDontDelete; // special, not to destroy the variable during delete
CBotProgram* m_prog; // user-defined functions
static
int m_initimer;
static
int m_timer;
static
CBotString m_labelBreak;
static
void* m_pUser;
CBotInstr* m_instr; // the corresponding instruction
bool m_bFunc; // an input of a function?
CBotCall* 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 int CBotStack::GetError()
{
return m_error;
}
////////////////////////////////////////////////////////////////////////
// Management of the stack of compilation

View File

@ -29,6 +29,8 @@
#include "CBotInstr/CBotEmpty.h"
#include "CBotInstr/CBotListArray.h"
#include "CBotStack.h"
#include <cassert>

View File

@ -23,6 +23,8 @@
#include "CBotTwoOpExpr.h"
#include "CBotInstArray.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotBreak.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,8 @@
#include "CBotCase.h"
#include "CBotExprNum.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -22,6 +22,8 @@
#include "CBotBlock.h"
#include "CBotExpression.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -23,6 +23,8 @@
#include "CBotTwoOpExpr.h"
#include "CBotInstArray.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -22,6 +22,8 @@
#include "CBotBlock.h"
#include "CBotCondition.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotEmpty.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotExprAlpha.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotExprBool.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotExprNan.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotExprNull.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotExprNum.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,8 @@
#include "CBotExprUnaire.h"
#include "CBotParExpr.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -24,6 +24,8 @@
#include "CBotIndexExpr.h"
#include "CBotFieldExpr.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,8 @@
#include "CBotExpression.h"
#include "CBotTwoOpExpr.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotFieldExpr.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -23,6 +23,8 @@
#include "CBotTwoOpExpr.h"
#include "CBotInstArray.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -23,6 +23,8 @@
#include "CBotBlock.h"
#include "CBotBoolExpr.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -22,6 +22,8 @@
#include "CBotLeftExprVar.h"
#include "CBotTwoOpExpr.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -22,6 +22,8 @@
#include "CBotInstr/CBotBlock.h"
#include "CBotInstr/CBotCondition.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotIndexExpr.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -25,6 +25,8 @@
#include "CBotListArray.h"
#include "CBotEmpty.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,8 @@
#include "CBotInstrCall.h"
#include "CBotExpression.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotInstrMethode.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -24,6 +24,8 @@
#include "CBotInstArray.h"
#include "CBotTwoOpExpr.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -23,6 +23,8 @@
#include "CBotIndexExpr.h"
#include "CBotExpression.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotLeftExprVar.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -23,6 +23,8 @@
#include "CBotExprNull.h"
#include "CBotTwoOpExpr.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -25,6 +25,8 @@
#include "CBotBoolean.h"
#include "CBotInt.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,8 @@
#include "CBotListInstr.h"
#include "CBotBlock.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotLogicExpr.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -20,6 +20,8 @@
// Modules inlcude
#include "CBotNew.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -22,6 +22,8 @@
// Modules inlcude
#include "CBot.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,8 @@
#include "CBotPostIncExpr.h"
#include "CBotExprVar.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,8 @@
#include "CBotPreIncExpr.h"
#include "CBotExprVar.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,8 @@
#include "CBotReturn.h"
#include "CBotExpression.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -25,6 +25,8 @@
#include "CBotBlock.h"
#include "CBotExpression.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,8 @@
#include "CBotThrow.h"
#include "CBotExpression.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,8 @@
#include "CBotTry.h"
#include "CBotBlock.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -22,7 +22,8 @@
#include "CBotParExpr.h"
#include "CBotLogicExpr.h"
#include "CBotExpression.h"
#include "CBot.h"
#include "CBotStack.h"
// Local include

View File

@ -22,6 +22,8 @@
#include "CBotBlock.h"
#include "CBotCondition.h"
#include "CBotStack.h"
// Local include
// Global include

View File

@ -21,6 +21,9 @@
// database management of CBoT program
#include "CBot.h"
#include "CBotStack.h"
#include <stdio.h>
CBotProgram::CBotProgram()

View File

@ -17,11 +17,12 @@
* along with this program. If not, see http://gnu.org/licenses
*/
//Management of the stack
// Modules inlcude
#include "CBotStack.h"
// Local include
#include "CBot.h"
// Global include
#include <cassert>
#include <cstdlib>
#include <cstring>
@ -29,10 +30,6 @@
#define ITIMER 100
////////////////////////////////////////////////////////////////////////////
// management of a execution of a stack
////////////////////////////////////////////////////////////////////////////
int CBotStack::m_initimer = ITIMER;
int CBotStack::m_timer = 0;
CBotVar* CBotStack::m_retvar = nullptr;
@ -44,6 +41,7 @@ void* CBotStack::m_pUser = nullptr;
#if STACKMEM
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::FirstStack()
{
CBotStack* p;
@ -82,17 +80,20 @@ CBotStack* CBotStack::FirstStack()
return p;
}
////////////////////////////////////////////////////////////////////////////////
CBotStack::CBotStack(CBotStack* ppapa)
{
// constructor must exist or the destructor is never called!
assert(0);
}
////////////////////////////////////////////////////////////////////////////////
CBotStack::~CBotStack()
{
assert(0); // use Delete () instead
}
////////////////////////////////////////////////////////////////////////////////
void CBotStack::Delete()
{
if ( this == nullptr || this == EOX ) return;
@ -129,8 +130,8 @@ void CBotStack::Delete()
free( this );
}
// routine improved
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock)
{
if (m_next != nullptr)
@ -163,6 +164,7 @@ CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock)
return p;
}
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock)
{
if (m_next != nullptr)
@ -180,6 +182,7 @@ CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock)
return p;
}
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::AddStack2(bool bBlock)
{
if (m_next2 != nullptr)
@ -203,11 +206,13 @@ CBotStack* CBotStack::AddStack2(bool bBlock)
return p;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::GetBlock()
{
return m_bBlock;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::Return(CBotStack* pfils)
{
if ( pfils == this ) return true; // special
@ -222,6 +227,7 @@ bool CBotStack::Return(CBotStack* pfils)
return (m_error == 0); // interrupted if error
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::ReturnKeep(CBotStack* pfils)
{
if ( pfils == this ) return true; // special
@ -233,6 +239,7 @@ bool CBotStack::ReturnKeep(CBotStack* pfils)
return (m_error == 0); // interrupted if error
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::StackOver()
{
if (!m_bOver) return false;
@ -242,6 +249,7 @@ bool CBotStack::StackOver()
#else
////////////////////////////////////////////////////////////////////////////////
CBotStack::CBotStack(CBotStack* ppapa)
{
m_next = nullptr;
@ -265,6 +273,7 @@ CBotStack::CBotStack(CBotStack* ppapa)
m_bFunc = false;
}
////////////////////////////////////////////////////////////////////////////////
// destructor
CBotStack::~CBotStack()
{
@ -277,6 +286,7 @@ CBotStack::~CBotStack()
if ( !m_bDontDelete ) delete m_listVar;
}
////////////////////////////////////////////////////////////////////////////////
// \TODO routine has/to optimize
CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock)
{
@ -293,6 +303,7 @@ CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock)
return p;
}
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock)
{
if (m_next != nullptr)
@ -314,6 +325,7 @@ CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock)
return p;
}
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::AddStack2(bool bBlock)
{
if (m_next2 != nullptr)
@ -331,6 +343,7 @@ CBotStack* CBotStack::AddStack2(bool bBlock)
return p;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::Return(CBotStack* pfils)
{
if ( pfils == this ) return true; // special
@ -345,6 +358,7 @@ bool CBotStack::Return(CBotStack* pfils)
return (m_error == 0); // interrupted if error
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::StackOver()
{
return false; // no overflow check in this version
@ -352,6 +366,7 @@ bool CBotStack::StackOver()
#endif
////////////////////////////////////////////////////////////////////////////////
void CBotStack::Reset(void* pUser)
{
m_timer = m_initimer; // resets the timer
@ -362,9 +377,7 @@ void CBotStack::Reset(void* pUser)
m_pUser = pUser;
}
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::RestoreStack(CBotInstr* instr)
{
if (m_next != nullptr)
@ -376,6 +389,7 @@ CBotStack* CBotStack::RestoreStack(CBotInstr* instr)
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
CBotStack* CBotStack::RestoreStackEOX(CBotCall* instr)
{
CBotStack* p = RestoreStack();
@ -383,8 +397,7 @@ CBotStack* CBotStack::RestoreStackEOX(CBotCall* instr)
return p;
}
////////////////////////////////////////////////////////////////////////////////
// routine for execution step by step
bool CBotStack::IfStep()
{
@ -392,7 +405,7 @@ bool CBotStack::IfStep()
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::BreakReturn(CBotStack* pfils, const char* name)
{
if ( m_error>=0 ) return false; // normal output
@ -406,6 +419,7 @@ bool CBotStack::BreakReturn(CBotStack* pfils, const char* name)
return Return(pfils);
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::IfContinue(int state, const char* name)
{
if ( m_error != -2 ) return false;
@ -420,6 +434,7 @@ bool CBotStack::IfContinue(int state, const char* name)
return true;
}
////////////////////////////////////////////////////////////////////////////////
void CBotStack::SetBreak(int val, const char* name)
{
m_error = -val; // reacts as an Exception
@ -432,7 +447,7 @@ void CBotStack::SetBreak(int val, const char* name)
}
// gives on the stack value calculated by the last CBotReturn
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::GetRetVar(bool bRet)
{
if (m_error == -3)
@ -446,6 +461,7 @@ bool CBotStack::GetRetVar(bool bRet)
return bRet; // interrupted by something other than return
}
////////////////////////////////////////////////////////////////////////////////
int CBotStack::GetError(int& start, int& end)
{
start = m_start;
@ -453,26 +469,28 @@ int CBotStack::GetError(int& start, int& end)
return m_error;
}
////////////////////////////////////////////////////////////////////////////////
int CBotStack::GetType(int mode)
{
if (m_var == nullptr) return -1;
return m_var->GetType(mode);
}
////////////////////////////////////////////////////////////////////////////////
CBotTypResult CBotStack::GetTypResult(int mode)
{
if (m_var == nullptr) return -1;
return m_var->GetTypResult(mode);
}
////////////////////////////////////////////////////////////////////////////////
void CBotStack::SetType(CBotTypResult& type)
{
if (m_var == nullptr) return;
m_var->SetType( type );
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::FindVar(CBotToken* &pToken, bool bUpdate, bool bModif)
{
CBotStack* p = this;
@ -497,6 +515,7 @@ CBotVar* CBotStack::FindVar(CBotToken* &pToken, bool bUpdate, bool bModif)
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::FindVar(const char* name)
{
CBotStack* p = this;
@ -516,6 +535,7 @@ CBotVar* CBotStack::FindVar(const char* name)
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::FindVar(long ident, bool bUpdate, bool bModif)
{
CBotStack* p = this;
@ -538,14 +558,14 @@ CBotVar* CBotStack::FindVar(long ident, bool bUpdate, bool bModif)
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::FindVar(CBotToken& pToken, bool bUpdate, bool bModif)
{
CBotToken* pt = &pToken;
return FindVar(pt, bUpdate, bModif);
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::CopyVar(CBotToken& Token, bool bUpdate)
{
CBotVar* pVar = FindVar( Token, bUpdate );
@ -557,7 +577,7 @@ CBotVar* CBotStack::CopyVar(CBotToken& Token, bool bUpdate)
return pCopy;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::SetState(int n, int limite)
{
m_state = n;
@ -566,6 +586,7 @@ bool CBotStack::SetState(int n, int limite)
return ( m_timer > limite ); // interrupted if timer pass
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::IncState(int limite)
{
m_state++;
@ -574,7 +595,7 @@ bool CBotStack::IncState(int limite)
return ( m_timer > limite ); // interrupted if timer pass
}
////////////////////////////////////////////////////////////////////////////////
void CBotStack::SetError(int n, CBotToken* token)
{
if ( n!= 0 && m_error != 0) return; // does not change existing error
@ -586,6 +607,7 @@ void CBotStack::SetError(int n, CBotToken* token)
}
}
////////////////////////////////////////////////////////////////////////////////
void CBotStack::ResetError(int n, int start, int end)
{
m_error = n;
@ -593,17 +615,20 @@ void CBotStack::ResetError(int n, int start, int end)
m_end = end;
}
////////////////////////////////////////////////////////////////////////////////
void CBotStack::SetPosError(CBotToken* token)
{
m_start = token->GetStart();
m_end = token->GetEnd();
}
////////////////////////////////////////////////////////////////////////////////
void CBotStack::SetTimer(int n)
{
m_initimer = n;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::Execute()
{
CBotCall* instr = nullptr; // the most highest instruction
@ -637,6 +662,7 @@ bool CBotStack::Execute()
}
// puts on the stack pointer to a variable
////////////////////////////////////////////////////////////////////////////////
void CBotStack::SetVar( CBotVar* var )
{
if (m_var) delete m_var; // replacement of a variable
@ -644,6 +670,7 @@ void CBotStack::SetVar( CBotVar* var )
}
// puts on the stack a copy of a variable
////////////////////////////////////////////////////////////////////////////////
void CBotStack::SetCopyVar( CBotVar* var )
{
if (m_var) delete m_var; // replacement of a variable
@ -652,11 +679,13 @@ void CBotStack::SetCopyVar( CBotVar* var )
m_var->Copy( var );
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::GetVar()
{
return m_var;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::GetPtVar()
{
CBotVar* p = m_var;
@ -664,6 +693,7 @@ CBotVar* CBotStack::GetPtVar()
return p;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::GetCopyVar()
{
if (m_var == nullptr) return nullptr;
@ -672,15 +702,14 @@ CBotVar* CBotStack::GetCopyVar()
return v;
}
////////////////////////////////////////////////////////////////////////////////
long CBotStack::GetVal()
{
if (m_var == nullptr) return 0;
return m_var->GetValInt();
}
////////////////////////////////////////////////////////////////////////////////
void CBotStack::AddVar(CBotVar* pVar)
{
CBotStack* p = this;
@ -702,19 +731,14 @@ void CBotStack::AddVar(CBotVar* pVar)
#endif
}
/*void CBotStack::RestoreVar(CBotVar* pVar)
{
if ( !m_bDontDelete ) __asm int 3;
delete m_listVar;
m_listVar = pVar; // direct replacement
}*/
////////////////////////////////////////////////////////////////////////////////
void CBotStack::SetBotCall(CBotProgram* p)
{
m_prog = p;
m_bFunc = true;
}
////////////////////////////////////////////////////////////////////////////////
CBotProgram* CBotStack::GetBotCall(bool bFirst)
{
if ( ! bFirst ) return m_prog;
@ -723,12 +747,13 @@ CBotProgram* CBotStack::GetBotCall(bool bFirst)
return p->m_prog;
}
////////////////////////////////////////////////////////////////////////////////
void* CBotStack::GetPUser()
{
return m_pUser;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype)
{
CBotTypResult res;
@ -754,6 +779,7 @@ bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBo
return true;
}
////////////////////////////////////////////////////////////////////////////////
void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar)
{
if ( m_next == nullptr ) return;
@ -762,7 +788,7 @@ void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar)
m_prog->GetFunctions()->RestoreCall(nIdent, token->GetString(), ppVar, this );
}
////////////////////////////////////////////////////////////////////////////////
bool SaveVar(FILE* pf, CBotVar* pVar)
{
while ( true )
@ -779,6 +805,7 @@ bool SaveVar(FILE* pf, CBotVar* pVar)
}
}
////////////////////////////////////////////////////////////////////////////////
void CBotStack::GetRunPos(const char* &FunctionName, int &start, int &end)
{
CBotProgram* prog = m_prog; // Current program
@ -813,6 +840,7 @@ void CBotStack::GetRunPos(const char* &FunctionName, int &start, int &end)
end = t->GetEnd();
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::GetStackVars(const char* &FunctionName, int level)
{
CBotProgram* prog = m_prog; // current program
@ -860,6 +888,7 @@ CBotVar* CBotStack::GetStackVars(const char* &FunctionName, int level)
return p->m_listVar;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::SaveState(FILE* pf)
{
if ( this == nullptr ) // end of the tree?
@ -888,7 +917,7 @@ bool CBotStack::SaveState(FILE* pf)
return m_next->SaveState(pf); // saves the following
}
////////////////////////////////////////////////////////////////////////////////
bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack)
{
unsigned short w;
@ -927,7 +956,7 @@ bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack)
return pStack->RestoreState(pf, pStack->m_next);
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVar::Save0State(FILE* pf)
{
if (!WriteWord(pf, 100+m_mPrivate))return false; // private variable?
@ -937,6 +966,7 @@ bool CBotVar::Save0State(FILE* pf)
return WriteString(pf, m_token->GetString()); // and variable name
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Save0State(FILE* pf)
{
if ( !m_defnum.IsEmpty() )
@ -948,28 +978,31 @@ bool CBotVarInt::Save0State(FILE* pf)
return CBotVar::Save0State(pf);
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Save1State(FILE* pf)
{
return WriteWord(pf, m_val); // the value of the variable
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarBoolean::Save1State(FILE* pf)
{
return WriteWord(pf, m_val); // the value of the variable
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarFloat::Save1State(FILE* pf)
{
return WriteFloat(pf, m_val); // the value of the variable
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarString::Save1State(FILE* pf)
{
return WriteString(pf, m_val); // the value of the variable
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarClass::Save1State(FILE* pf)
{
if ( !WriteType(pf, m_type) ) return false;
@ -1004,6 +1037,7 @@ bool ParseInitType(int rawInitType, CBotVar::InitType* initType)
}
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar)
{
unsigned short w, wi, prv, st;
@ -1152,7 +1186,7 @@ int CBotCStack::m_end = 0;
CBotTypResult CBotCStack::m_retTyp = CBotTypResult(0);
//CBotToken* CBotCStack::m_retClass= nullptr;
////////////////////////////////////////////////////////////////////////////////
CBotCStack::CBotCStack(CBotCStack* ppapa)
{
m_next = nullptr;
@ -1175,6 +1209,7 @@ CBotCStack::CBotCStack(CBotCStack* ppapa)
m_var = nullptr;
}
////////////////////////////////////////////////////////////////////////////////
// destructor
CBotCStack::~CBotCStack()
{
@ -1185,6 +1220,7 @@ CBotCStack::~CBotCStack()
delete m_listVar;
}
////////////////////////////////////////////////////////////////////////////////
// used only at compile
CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, bool bBlock)
{
@ -1199,7 +1235,7 @@ CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, bool bBlock)
return p;
}
////////////////////////////////////////////////////////////////////////////////
CBotInstr* CBotCStack::Return(CBotInstr* inst, CBotCStack* pfils)
{
if ( pfils == this ) return inst;
@ -1218,6 +1254,7 @@ CBotInstr* CBotCStack::Return(CBotInstr* inst, CBotCStack* pfils)
return inst;
}
////////////////////////////////////////////////////////////////////////////////
CBotFunction* CBotCStack::ReturnFunc(CBotFunction* inst, CBotCStack* pfils)
{
if (m_var != nullptr) delete m_var; // value replaced?
@ -1234,6 +1271,7 @@ CBotFunction* CBotCStack::ReturnFunc(CBotFunction* inst, CBotCStack* pfils)
return inst;
}
////////////////////////////////////////////////////////////////////////////////
int CBotCStack::GetError(int& start, int& end)
{
start = m_start;
@ -1241,11 +1279,13 @@ int CBotCStack::GetError(int& start, int& end)
return m_error;
}
////////////////////////////////////////////////////////////////////////////////
int CBotCStack::GetError()
{
return m_error;
}
////////////////////////////////////////////////////////////////////////////////
// type of instruction on the stack
CBotTypResult CBotCStack::GetTypResult(int mode)
{
@ -1254,6 +1294,7 @@ CBotTypResult CBotCStack::GetTypResult(int mode)
return m_var->GetTypResult(mode);
}
////////////////////////////////////////////////////////////////////////////////
// type of instruction on the stack
int CBotCStack::GetType(int mode)
{
@ -1262,6 +1303,7 @@ int CBotCStack::GetType(int mode)
return m_var->GetType(mode);
}
////////////////////////////////////////////////////////////////////////////////
// pointer on the stack is in what class?
CBotClass* CBotCStack::GetClass()
{
@ -1272,6 +1314,7 @@ CBotClass* CBotCStack::GetClass()
return m_var->GetClass();
}
////////////////////////////////////////////////////////////////////////////////
// type of instruction on the stack
void CBotCStack::SetType(CBotTypResult& type)
{
@ -1282,7 +1325,7 @@ void CBotCStack::SetType(CBotTypResult& type)
// seeks a variable on the stack
// the token may be a result of TokenTypVar (object of a class)
// or a pointer in the source
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotCStack::FindVar(CBotToken* &pToken)
{
CBotCStack* p = this;
@ -1304,12 +1347,14 @@ CBotVar* CBotCStack::FindVar(CBotToken* &pToken)
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotCStack::FindVar(CBotToken& Token)
{
CBotToken* pt = &Token;
return FindVar(pt);
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotCStack::CopyVar(CBotToken& Token)
{
CBotVar* pVar = FindVar( Token );
@ -1321,18 +1366,20 @@ CBotVar* CBotCStack::CopyVar(CBotToken& Token)
return pCopy;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotCStack::IsOk()
{
return (m_error == 0);
}
////////////////////////////////////////////////////////////////////////////////
void CBotCStack::SetStartError( int pos )
{
if ( m_error != 0) return; // does not change existing error
m_start = pos;
}
////////////////////////////////////////////////////////////////////////////////
void CBotCStack::SetError(int n, int pos)
{
if ( n!= 0 && m_error != 0) return; // does not change existing error
@ -1340,6 +1387,7 @@ void CBotCStack::SetError(int n, int pos)
m_end = pos;
}
////////////////////////////////////////////////////////////////////////////////
void CBotCStack::SetError(int n, CBotToken* p)
{
if (m_error) return; // does not change existing error
@ -1348,6 +1396,7 @@ void CBotCStack::SetError(int n, CBotToken* p)
m_end = p->GetEnd();
}
////////////////////////////////////////////////////////////////////////////////
void CBotCStack::ResetError(int n, int start, int end)
{
m_error = n;
@ -1355,6 +1404,7 @@ void CBotCStack::ResetError(int n, int start, int end)
m_end = end;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotCStack::NextToken(CBotToken* &p)
{
CBotToken* pp = p;
@ -1366,32 +1416,38 @@ bool CBotCStack::NextToken(CBotToken* &p)
return false;
}
////////////////////////////////////////////////////////////////////////////////
void CBotCStack::SetBotCall(CBotProgram* p)
{
m_prog = p;
}
////////////////////////////////////////////////////////////////////////////////
CBotProgram* CBotCStack::GetBotCall()
{
return m_prog;
}
////////////////////////////////////////////////////////////////////////////////
void CBotCStack::SetRetType(CBotTypResult& type)
{
m_retTyp = type;
}
////////////////////////////////////////////////////////////////////////////////
CBotTypResult CBotCStack::GetRetType()
{
return m_retTyp;
}
////////////////////////////////////////////////////////////////////////////////
void CBotCStack::SetVar( CBotVar* var )
{
if (m_var) delete m_var; // replacement of a variable
m_var = var;
}
////////////////////////////////////////////////////////////////////////////////
// puts on the stack a copy of a variable
void CBotCStack::SetCopyVar( CBotVar* var )
{
@ -1402,11 +1458,13 @@ void CBotCStack::SetCopyVar( CBotVar* var )
m_var->Copy( var );
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotCStack::GetVar()
{
return m_var;
}
////////////////////////////////////////////////////////////////////////////////
void CBotCStack::AddVar(CBotVar* pVar)
{
CBotCStack* p = this;
@ -1427,7 +1485,7 @@ void CBotCStack::AddVar(CBotVar* pVar)
}
// test whether a variable is already defined locally
////////////////////////////////////////////////////////////////////////////////
bool CBotCStack::CheckVarLocal(CBotToken* &pToken)
{
CBotCStack* p = this;
@ -1448,6 +1506,7 @@ bool CBotCStack::CheckVarLocal(CBotToken* &pToken)
return false;
}
////////////////////////////////////////////////////////////////////////////////
CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent)
{
nIdent = 0;
@ -1469,7 +1528,7 @@ CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nId
}
// test if a procedure name is already defined somewhere
////////////////////////////////////////////////////////////////////////////////
bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam)
{
CBotString name = pToken->GetString();

270
src/CBot/CBotStack.h Normal file
View File

@ -0,0 +1,270 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
#pragma once
// Modules inlcude
#include "CBot.h"
// Local include
// Global include
/*!
* \class CBotStack
* \brief The CBotStack class Management of the execution stack. Actually the
* only thing it can do is to create an instance of a stack. To use for routine
* CBotProgram :: Execute(CBotStack)
*/
class CBotStack
{
public:
#if STACKMEM
/**
* \brief FirstStack Allocate first stack
* \return pointer to created stack
*/
static CBotStack * FirstStack();
/** \brief Delete Remove current stack */
void Delete();
#endif
/**
* \brief CBotStack Constructor of the stack
* \param ppapa Not used.
*/
CBotStack(CBotStack* ppapa);
/** \brief ~CBotStack Destructor */
~CBotStack();
/**
* \brief StackOver Check if end of stack is reached
* \return true if end of stack
*/
bool StackOver();
/**
* \brief GetError Get error number of the stack
* \param [out] start beginning of the stack
* \param [out] end end of stack
* \return error number
*/
int GetError(int& start, int& end);
/**
* \brief GetError Get error number
* \return eror number
*/
int GetError();// rend le numéro d'erreur retourné
/**
* \brief Reset Reset error at and set user
* \param [in] pUser User of stack
*/
void Reset(void* pUser);
/**
* \brief SetType Determines the type.
* \param type Type of instruction on the stack.
*/
void SetType(CBotTypResult& type);
/**
* \brief GetType Get the type of value on the stack.
* \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic).
* \return Type number.
*/
int GetType(int mode = 0);
/**
* \brief Getes the type of complete value on the stack.
* \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic).
* \return Type of an element.
*/
CBotTypResult GetTypResult(int mode = 0);
/**
* \brief Adds a local variable.
* \param [in] p Variable to be added.
*/
void AddVar(CBotVar* p);
/**
* \brief Fetch a variable by its token.
* \brief This may be a composite variable
* \param [in] pToken Token upon which search is performed
* \param [in] bUpdate Not used. Probably need to be removed
* \param [in] bModif Not used. Probably need to be removed
* \return Found variable
*/
CBotVar* FindVar(CBotToken* &pToken, bool bUpdate = false,
bool bModif = false);
/**
* \brief Fetch a variable by its token.
* \brief This may be a composite variable
* \param [in] pToken Token upon which search is performed
* \param [in] bUpdate Not used. Probably need to be removed
* \param [in] bModif Not used. Probably need to be removed
* \return Found variable
*/
CBotVar* FindVar(CBotToken& pToken, bool bUpdate = false,
bool bModif = false);
/**
* \brief Fetch variable by its name
* \param [in] name Name of variable to find
* \return Found variable
*/
CBotVar* FindVar(const char* name);
/**
* \brief Fetch a variable on the stack according to its identification number
* \brief This is faster than comparing names
* \param [in] ident Identifier of a variable
* \param [in] bUpdate Not used. Probably need to be removed
* \param [in] bModif Not used. Probably need to be removed
* \return Found variable
*/
CBotVar* FindVar(long ident, bool bUpdate = false,
bool bModif = false);
/**
* \brief Find variable by its token and returns a copy of it.
* \param Token Token upon which search is performed
* \param bUpdate Not used.
* \return Found variable, nullptr if not found
*/
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* RestoreStack(CBotInstr* instr = nullptr);
CBotStack* RestoreStackEOX(CBotCall* instr = nullptr);
CBotStack* AddStack2(bool bBlock = 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);
// in case of eventual break
bool IfContinue(int state, const char* name);
// or "continue"
bool IsOk();
bool SetState(int n, int lim = -10); // select a state
int GetState(); // 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 );
void SetCopyVar( CBotVar* var );
CBotVar* GetVar();
CBotVar* GetCopyVar();
CBotVar* GetPtVar();
bool GetRetVar(bool bRet);
long GetVal();
void SetError(int n, CBotToken* token = nullptr);
void SetPosError(CBotToken* token);
void ResetError(int n, int start, int end);
void SetBreak(int val, const char* name);
void SetBotCall(CBotProgram* p);
CBotProgram* GetBotCall(bool bFirst = false);
void* GetPUser();
bool GetBlock();
bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype);
void RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar);
bool SaveState(FILE* pf);
bool RestoreState(FILE* pf, CBotStack* &pStack);
static
void SetTimer(int n);
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;
CBotStack* m_prev;
friend class CBotInstArray;
#ifdef _DEBUG
int m_index;
#endif
int m_state;
int m_step;
static int m_error;
static int m_start;
static int m_end;
static
CBotVar* m_retvar; // result of a return
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)
bool m_bOver; // stack limits?
// bool m_bDontDelete; // special, not to destroy the variable during delete
CBotProgram* m_prog; // user-defined functions
static
int m_initimer;
static
int m_timer;
static
CBotString m_labelBreak;
static
void* m_pUser;
CBotInstr* m_instr; // the corresponding instruction
bool m_bFunc; // an input of a function?
CBotCall* 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 int CBotStack::GetError()
{
return m_error;
}

View File

@ -25,6 +25,8 @@
#include "CBot.h"
#include "CBotStack.h"
#include <cassert>
#include <cmath>
#include <cstdio>