Started docs of CBotStack; random refactorings

dev-time-step
krzys-h 2015-12-25 19:48:31 +01:00
parent 40b7d986aa
commit c72cfa234b
6 changed files with 45 additions and 79 deletions

View File

@ -56,9 +56,9 @@ public:
/** /**
* \brief Compile the function * \brief Compile the function
* *
* \param "this" variable for class calls, nullptr for normal calls * \param thisVar "this" variable for class calls, nullptr for normal calls
* \param Arguments (only types!) passed to the function * \param args Arguments (only types!) passed to the function
* \param User pointer provided to CBotProgram::Compile() * \param user User pointer provided to CBotProgram::Compile()
*/ */
virtual CBotTypResult Compile(CBotVar* thisVar, CBotVar* args, void* user) = 0; virtual CBotTypResult Compile(CBotVar* thisVar, CBotVar* args, void* user) = 0;
@ -155,10 +155,10 @@ public:
* This function sets an error in compilation stack in case of failure * This function sets an error in compilation stack in case of failure
* *
* \param p Token representing the function name * \param p Token representing the function name
* \param ppVars List of arguments (only types!)
* \param thisVar "this" variable for class calls, nullptr for normal calls * \param thisVar "this" variable for class calls, nullptr for normal calls
* \param ppVars List of arguments (only types!)
* \param pStack Compilation stack * \param pStack Compilation stack
* \return CBotTypResult representing the return type of the function (::CBotTypVar), or an error (::CBotError) * \return CBotTypResult representing the return type of the function (::CBotType), or an error (::CBotError)
*/ */
CBotTypResult CompileCall(CBotToken*& p, CBotVar* thisVar, CBotVar** ppVars, CBotCStack* pStack); CBotTypResult CompileCall(CBotToken*& p, CBotVar* thisVar, CBotVar** ppVars, CBotCStack* pStack);
@ -187,7 +187,7 @@ public:
* \brief Restore execution status after loading saved state * \brief Restore execution status after loading saved state
* *
* \param token Token representing the function name * \param token Token representing the function name
* \param "this" variable for class calls, nullptr for normal calls * \param thisVar "this" variable for class calls, nullptr for normal calls
* \param ppVar List of arguments * \param ppVar List of arguments
* \param pStack Runtime stack * \param pStack Runtime stack
* \return false on failure (e.g. function doesn't exist) * \return false on failure (e.g. function doesn't exist)

View File

@ -103,7 +103,7 @@ bool CBotCatch :: TestCatch(CBotStack* &pile, int val)
{ {
if ( !m_Cond->Execute(pile) ) return false; if ( !m_Cond->Execute(pile) ) return false;
if ( val > 0 || pile->GetType() != CBotTypBoolean ) if ( val > 0 || pile->GetVar() == nullptr || pile->GetVar()->GetType() != CBotTypBoolean )
{ {
CBotVar* var = CBotVar::Create("", CBotTypBoolean); CBotVar* var = CBotVar::Create("", CBotTypBoolean);
var->SetValInt( pile->GetVal() == val ); var->SetValInt( pile->GetVal() == val );

View File

@ -36,8 +36,6 @@
// Global include // Global include
#include <cassert> #include <cassert>
#define MAX(a,b) ((a>b) ? a : b)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotTwoOpExpr::CBotTwoOpExpr() CBotTwoOpExpr::CBotTwoOpExpr()
{ {
@ -200,7 +198,7 @@ CBotInstr* CBotTwoOpExpr::Compile(CBotToken* &p, CBotCStack* pStack, int* pOpera
type2 = pStk->GetTypResult(); // what kind of results? type2 = pStk->GetTypResult(); // what kind of results?
// what kind of result? // what kind of result?
int TypeRes = MAX( type1.GetType(3), type2.GetType(3) ); int TypeRes = std::max( type1.GetType(3), type2.GetType(3) );
if ( TypeOp == ID_ADD && type1.Eq(CBotTypString) ) if ( TypeOp == ID_ADD && type1.Eq(CBotTypString) )
{ {
TypeRes = CBotTypString; TypeRes = CBotTypString;
@ -253,7 +251,7 @@ CBotInstr* CBotTwoOpExpr::Compile(CBotToken* &p, CBotCStack* pStack, int* pOpera
} }
if ( TypeRes != CBotTypString ) if ( TypeRes != CBotTypString )
TypeRes = MAX(type1.GetType(), type2.GetType()); TypeRes = std::max(type1.GetType(), type2.GetType());
inst = i; inst = i;
} }
@ -344,15 +342,16 @@ bool CBotTwoOpExpr::Execute(CBotStack* &pStack)
pStk2->IncState(); pStk2->IncState();
} }
CBotTypResult type1 = pStk1->GetTypResult(); // what kind of results? assert(pStk1->GetVar() != nullptr && pStk2->GetVar() != nullptr);
CBotTypResult type2 = pStk2->GetTypResult(); CBotTypResult type1 = pStk1->GetVar()->GetTypResult(); // what kind of results?
CBotTypResult type2 = pStk2->GetVar()->GetTypResult();
CBotStack* pStk3 = pStk2->AddStack(this); // adds an item to the stack CBotStack* pStk3 = pStk2->AddStack(this); // adds an item to the stack
if ( pStk3->IfStep() ) return false; // shows the operation if step by step if ( pStk3->IfStep() ) return false; // shows the operation if step by step
// creates a temporary variable to put the result // creates a temporary variable to put the result
// what kind of result? // what kind of result?
int TypeRes = MAX(type1.GetType(), type2.GetType()); int TypeRes = std::max(type1.GetType(), type2.GetType());
if ( GetTokenType() == ID_ADD && type1.Eq(CBotTypString) ) if ( GetTokenType() == ID_ADD && type1.Eq(CBotTypString) )
{ {
@ -374,14 +373,14 @@ bool CBotTwoOpExpr::Execute(CBotStack* &pStack)
TypeRes = CBotTypBoolean; TypeRes = CBotTypBoolean;
break; break;
case ID_DIV: case ID_DIV:
TypeRes = MAX(TypeRes, CBotTypFloat); TypeRes = std::max(TypeRes, static_cast<int>(CBotTypFloat));
} }
// creates a variable for the result // creates a variable for the result
CBotVar* result = CBotVar::Create("", TypeRes); CBotVar* result = CBotVar::Create("", TypeRes);
// creates a variable to perform the calculation in the appropriate type // creates a variable to perform the calculation in the appropriate type
TypeRes = MAX(type1.GetType(), type2.GetType()); TypeRes = std::max(type1.GetType(), type2.GetType());
if ( GetTokenType() == ID_ADD && type1.Eq(CBotTypString) ) if ( GetTokenType() == ID_ADD && type1.Eq(CBotTypString) )
{ {

View File

@ -318,20 +318,6 @@ CBotError CBotStack::GetError(int& start, int& end)
return m_error; return m_error;
} }
////////////////////////////////////////////////////////////////////////////////
int CBotStack::GetType(CBotVar::GetTypeMode mode)
{
if (m_var == nullptr) return -1;
return m_var->GetType(mode);
}
////////////////////////////////////////////////////////////////////////////////
CBotTypResult CBotStack::GetTypResult(CBotVar::GetTypeMode mode)
{
if (m_var == nullptr) return -1;
return m_var->GetTypResult(mode);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::FindVar(CBotToken*& pToken, bool bUpdate) CBotVar* CBotStack::FindVar(CBotToken*& pToken, bool bUpdate)
{ {
@ -408,9 +394,9 @@ CBotVar* CBotStack::FindVar(CBotToken& pToken, bool bUpdate)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotStack::CopyVar(CBotToken& Token, bool bUpdate) CBotVar* CBotStack::CopyVar(CBotToken& pToken, bool bUpdate)
{ {
CBotVar* pVar = FindVar(Token, bUpdate); CBotVar* pVar = FindVar(pToken, bUpdate);
if ( pVar == nullptr) return nullptr; if ( pVar == nullptr) return nullptr;
@ -557,7 +543,7 @@ void CBotStack::AddVar(CBotVar* pVar)
if ( p == nullptr ) return; if ( p == nullptr ) return;
/// p->m_bDontDelete = bDontDelete; // 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;

View File

@ -43,12 +43,12 @@ public:
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 IsFunctionParam : unsigned short { FALSE = 0, TRUE = 1, UNKNOWN_EOX_SPECIAL = 2 }; // TODO: just guessing the meaning of values, should be verified ~krzys_h
/** /**
* \brief AllocateStack Allocate the stack * \brief Allocate the stack
* \return pointer to created stack * \return pointer to created stack
*/ */
static CBotStack* AllocateStack(); static CBotStack* AllocateStack();
/** \brief Delete Remove current stack */ /** \brief Remove the current stack */
void Delete(); void Delete();
CBotStack() = delete; CBotStack() = delete;
@ -81,69 +81,50 @@ public:
void Reset(); void Reset();
/** /**
* \brief GetType Get the type of value on the stack. * \brief Adds a local variable
* \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). * \param var Variable to be added
* \return Type number.
*/ */
int GetType(CBotVar::GetTypeMode mode = CBotVar::GetTypeMode::NORMAL); void AddVar(CBotVar* var);
/** /**
* \brief Getes the type of complete value on the stack. * \brief Fetch a variable by its token
* \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). * \param pToken Token upon which search is performed
* \return Type of an element. * \param bUpdate true to automatically call update function for classes, see CBotClass::AddUpdateFunc()
*/ * \return Found variable, nullptr if not found
CBotTypResult GetTypResult(CBotVar::GetTypeMode mode = CBotVar::GetTypeMode::NORMAL);
/**
* \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); CBotVar* FindVar(CBotToken*& pToken, bool bUpdate);
/** /**
* \brief Fetch a variable by its token. * \copydoc FindVar(CBotToken*&, bool)
* \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); CBotVar* FindVar(CBotToken& pToken, bool bUpdate);
/** /**
* \brief Fetch variable by its name * \brief Fetch variable by its name
* \param [in] name Name of variable to find * \param name Name of variable to find
* \return Found variable * \return Found variable, nullptr if not found
*/ */
CBotVar* FindVar(const std::string& name); CBotVar* FindVar(const std::string& name);
/** /**
* \brief Fetch a variable on the stack according to its identification number * \brief Fetch a variable on the stack according to its unique identifier
* \brief This is faster than comparing names *
* \param [in] ident Identifier of a variable * This is faster than comparing names
* \param [in] bUpdate Not used. Probably need to be removed *
* \param [in] bModif Not used. Probably need to be removed * \param ident Unique identifier of a variable
* \return Found variable * \param bUpdate true to automatically call update function for classes, see CBotClass::AddUpdateFunc()
* \return Found variable, nullptr if not found
*/ */
CBotVar* FindVar(long ident, bool bUpdate); CBotVar* FindVar(long ident, bool bUpdate);
/** /**
* \brief Find variable by its token and returns a copy of it. * \brief Find variable by its token and returns a copy of it
* \param Token Token upon which search is performed *
* \param bUpdate Not used. * \param pToken Token upon which search is performed
* \param bUpdate true to automatically call update function for classes, see CBotClass::AddUpdateFunc()
* \return Found variable, nullptr if not found * \return Found variable, nullptr if not found
*/ */
CBotVar* CopyVar(CBotToken& Token, bool bUpdate = false); CBotVar* CopyVar(CBotToken& pToken, bool bUpdate = false);
CBotStack* AddStack(CBotInstr* instr = nullptr, UnknownEnumBlock bBlock = UnknownEnumBlock::UNKNOWN_FALSE); // extends the stack CBotStack* AddStack(CBotInstr* instr = nullptr, UnknownEnumBlock bBlock = UnknownEnumBlock::UNKNOWN_FALSE); // extends the stack

View File

@ -88,7 +88,7 @@ public:
/** /**
* \brief Constructor * \brief Constructor
* *
* \param token The string this token represents * \param text The string this token represents
* \param sep All separators that appeared after this token * \param sep All separators that appeared after this token
* \param start Beginning location in the source code of this token * \param start Beginning location in the source code of this token
* \param end Ending location in the source code of this token * \param end Ending location in the source code of this token
@ -121,7 +121,7 @@ public:
/** /**
* \brief Set the token string * \brief Set the token string
* \param The new string to set * \param name The new string to set
*/ */
void SetString(const std::string& name); void SetString(const std::string& name);
@ -144,7 +144,7 @@ public:
/** /**
* \brief Get the keyword id * \brief Get the keyword id
* \return The keyword id, see ::CBotTokenId * \return The keyword id, see ::TokenId
*/ */
long GetKeywordId(); long GetKeywordId();