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
*
* \param "this" variable for class calls, nullptr for normal calls
* \param Arguments (only types!) passed to the function
* \param User pointer provided to CBotProgram::Compile()
* \param thisVar "this" variable for class calls, nullptr for normal calls
* \param args Arguments (only types!) passed to the function
* \param user User pointer provided to CBotProgram::Compile()
*/
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
*
* \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 ppVars List of arguments (only types!)
* \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);
@ -187,7 +187,7 @@ public:
* \brief Restore execution status after loading saved state
*
* \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 pStack Runtime stack
* \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 ( val > 0 || pile->GetType() != CBotTypBoolean )
if ( val > 0 || pile->GetVar() == nullptr || pile->GetVar()->GetType() != CBotTypBoolean )
{
CBotVar* var = CBotVar::Create("", CBotTypBoolean);
var->SetValInt( pile->GetVal() == val );

View File

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

View File

@ -318,20 +318,6 @@ CBotError CBotStack::GetError(int& start, int& end)
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)
{
@ -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;
@ -557,7 +543,7 @@ void CBotStack::AddVar(CBotVar* pVar)
if ( p == nullptr ) return;
/// p->m_bDontDelete = bDontDelete;
// p->m_bDontDelete = bDontDelete;
CBotVar** pp = &p->m_listVar;
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
/**
* \brief AllocateStack Allocate the stack
* \brief Allocate the stack
* \return pointer to created stack
*/
static CBotStack* AllocateStack();
/** \brief Delete Remove current stack */
/** \brief Remove the current stack */
void Delete();
CBotStack() = delete;
@ -81,69 +81,50 @@ public:
void Reset();
/**
* \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.
* \brief Adds a local variable
* \param var Variable to be added
*/
int GetType(CBotVar::GetTypeMode mode = CBotVar::GetTypeMode::NORMAL);
void AddVar(CBotVar* var);
/**
* \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(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
* \brief Fetch a variable by its token
* \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
*/
CBotVar* FindVar(CBotToken*& pToken, bool bUpdate);
/**
* \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
* \copydoc FindVar(CBotToken*&, bool)
*/
CBotVar* FindVar(CBotToken& pToken, bool bUpdate);
/**
* \brief Fetch variable by its name
* \param [in] name Name of variable to find
* \return Found variable
* \param name Name of variable to find
* \return Found variable, nullptr if not found
*/
CBotVar* FindVar(const std::string& 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
* \brief Fetch a variable on the stack according to its unique identifier
*
* This is faster than comparing names
*
* \param ident Unique identifier of a 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);
/**
* \brief Find variable by its token and returns a copy of it.
* \param Token Token upon which search is performed
* \param bUpdate Not used.
* \brief Find variable by its token and returns a copy of it
*
* \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
*/
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

View File

@ -88,7 +88,7 @@ public:
/**
* \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 start Beginning 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
* \param The new string to set
* \param name The new string to set
*/
void SetString(const std::string& name);
@ -144,7 +144,7 @@ public:
/**
* \brief Get the keyword id
* \return The keyword id, see ::CBotTokenId
* \return The keyword id, see ::TokenId
*/
long GetKeywordId();