More random CBotToken refactoring, removed CBotToken::Delete

dev-time-step
krzys-h 2015-12-23 16:46:41 +01:00
parent fbdc071659
commit 6ef14617a0
10 changed files with 44 additions and 77 deletions

View File

@ -262,13 +262,13 @@ bool CBotCStack::NextToken(CBotToken* &p)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotCStack::SetBotCall(CBotProgram* p) void CBotCStack::SetProgram(CBotProgram* p)
{ {
m_prog = p; m_prog = p;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotProgram* CBotCStack::GetBotCall() CBotProgram* CBotCStack::GetProgram()
{ {
return m_prog; return m_prog;
} }

View File

@ -213,16 +213,16 @@ public:
CBotTypResult GetRetType(); CBotTypResult GetRetType();
/*! /*!
* \brief SetBotCall * \brief SetProgram
* \param p * \param p
*/ */
void SetBotCall(CBotProgram* p); void SetProgram(CBotProgram* p);
/*! /*!
* \brief GetBotCall * \brief GetProgram
* \return * \return
*/ */
CBotProgram* GetBotCall(); CBotProgram* GetProgram();
/*! /*!
* \brief CompileCall * \brief CompileCall

View File

@ -723,7 +723,7 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
if ( f != nullptr ) if ( f != nullptr )
{ {
f->m_pProg = pStack->GetBotCall(); f->m_pProg = pStack->GetProgram();
f->m_bSynchro = bSynchro; f->m_bSynchro = bSynchro;
// replaces the element in the chain // replaces the element in the chain
f->m_next = pf->m_next; f->m_next = pf->m_next;

View File

@ -70,7 +70,7 @@ CBotInstr* CBotExprVar::Compile(CBotToken*& p, CBotCStack* pStack, CBotVar::Prot
if (ident > 0 && ident < 9000) if (ident > 0 && ident < 9000)
{ {
if ( var->IsPrivate(privat) && if ( var->IsPrivate(privat) &&
!pStk->GetBotCall()->m_bCompileClass) !pStk->GetProgram()->m_bCompileClass)
{ {
pStk->SetError(CBotErrPrivate, p); pStk->SetError(CBotErrPrivate, p);
goto err; goto err;
@ -140,7 +140,7 @@ CBotInstr* CBotExprVar::Compile(CBotToken*& p, CBotCStack* pStack, CBotVar::Prot
{ {
i->SetUniqNum(var->GetUniqNum()); i->SetUniqNum(var->GetUniqNum());
if ( var->IsPrivate() && if ( var->IsPrivate() &&
!pStk->GetBotCall()->m_bCompileClass) !pStk->GetProgram()->m_bCompileClass)
{ {
pStk->SetError(CBotErrPrivate, pp); pStk->SetError(CBotErrPrivate, pp);
goto err; goto err;

View File

@ -68,7 +68,7 @@ CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack)
if (inst->m_nIdent > 0 && inst->m_nIdent < 9000) if (inst->m_nIdent > 0 && inst->m_nIdent < 9000)
{ {
if ( var->IsPrivate(CBotVar::ProtectionLevel::ReadOnly) && if ( var->IsPrivate(CBotVar::ProtectionLevel::ReadOnly) &&
!pStk->GetBotCall()->m_bCompileClass) !pStk->GetProgram()->m_bCompileClass)
{ {
pStk->SetError(CBotErrPrivate, p); pStk->SetError(CBotErrPrivate, p);
goto err; goto err;
@ -132,7 +132,7 @@ CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack)
if (var != nullptr) if (var != nullptr)
{ {
if ( var->IsPrivate(CBotVar::ProtectionLevel::ReadOnly) && if ( var->IsPrivate(CBotVar::ProtectionLevel::ReadOnly) &&
!pStk->GetBotCall()->m_bCompileClass) !pStk->GetProgram()->m_bCompileClass)
{ {
pStk->SetError(CBotErrPrivate, pp); pStk->SetError(CBotErrPrivate, pp);
goto err; goto err;

View File

@ -70,13 +70,13 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
m_error = CBotNoErr; m_error = CBotNoErr;
// Step 1. Process the code into tokens // Step 1. Process the code into tokens
CBotToken* pBaseToken = CBotToken::CompileTokens(program); auto tokens = CBotToken::CompileTokens(program);
if ( pBaseToken == nullptr ) return false; if (tokens == nullptr) return false;
CBotCStack* pStack = new CBotCStack(nullptr); auto pStack = std::unique_ptr<CBotCStack>(new CBotCStack(nullptr));
CBotToken* p = pBaseToken->GetNext(); // skips the first token (separator) CBotToken* p = tokens.get()->GetNext(); // skips the first token (separator)
pStack->SetBotCall(this); // defined used routines pStack->SetProgram(this); // defined used routines
CBotCall::SetPUser(pUser); CBotCall::SetPUser(pUser);
// Step 2. Find all function and class definitions // Step 2. Find all function and class definitions
@ -87,13 +87,13 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
if ( p->GetType() == ID_CLASS || if ( p->GetType() == ID_CLASS ||
( p->GetType() == ID_PUBLIC && p->GetNext()->GetType() == ID_CLASS )) ( p->GetType() == ID_PUBLIC && p->GetNext()->GetType() == ID_CLASS ))
{ {
CBotClass* nxt = CBotClass::Compile1(p, pStack); CBotClass* nxt = CBotClass::Compile1(p, pStack.get());
if (m_classes == nullptr ) m_classes = nxt; if (m_classes == nullptr ) m_classes = nxt;
else m_classes->AddNext(nxt); else m_classes->AddNext(nxt);
} }
else else
{ {
CBotFunction* next = CBotFunction::Compile1(p, pStack, nullptr); CBotFunction* next = CBotFunction::Compile1(p, pStack.get(), nullptr);
if (m_functions == nullptr ) m_functions = next; if (m_functions == nullptr ) m_functions = next;
else m_functions->AddNext(next); else m_functions->AddNext(next);
} }
@ -103,7 +103,6 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
m_error = pStack->GetError(m_errorStart, m_errorEnd); m_error = pStack->GetError(m_errorStart, m_errorEnd);
delete m_functions; delete m_functions;
m_functions = nullptr; m_functions = nullptr;
delete pBaseToken;
return false; return false;
} }
@ -111,7 +110,7 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
// CBotFunction* temp = nullptr; // CBotFunction* temp = nullptr;
CBotFunction* next = m_functions; // rewind the list CBotFunction* next = m_functions; // rewind the list
p = pBaseToken->GetNext(); // returns to the beginning p = tokens.get()->GetNext(); // returns to the beginning
while ( pStack->IsOk() && p != nullptr && p->GetType() != 0 ) while ( pStack->IsOk() && p != nullptr && p->GetType() != 0 )
{ {
@ -121,12 +120,12 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
( p->GetType() == ID_PUBLIC && p->GetNext()->GetType() == ID_CLASS )) ( p->GetType() == ID_PUBLIC && p->GetNext()->GetType() == ID_CLASS ))
{ {
m_bCompileClass = true; m_bCompileClass = true;
CBotClass::Compile(p, pStack); // completes the definition of the class CBotClass::Compile(p, pStack.get()); // completes the definition of the class
} }
else else
{ {
m_bCompileClass = false; m_bCompileClass = false;
CBotFunction::Compile(p, pStack, next); CBotFunction::Compile(p, pStack.get(), next);
if (next->IsExtern()) functions.push_back(next->GetName()/* + next->GetParams()*/); if (next->IsExtern()) functions.push_back(next->GetName()/* + next->GetParams()*/);
next->m_pProg = this; // keeps pointers to the module next->m_pProg = this; // keeps pointers to the module
next = next->Next(); next = next->Next();
@ -143,9 +142,6 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
m_functions = nullptr; m_functions = nullptr;
} }
delete pBaseToken;
delete pStack;
return (m_functions != nullptr); return (m_functions != nullptr);
} }

View File

@ -277,7 +277,7 @@ public:
*/ */
static bool DefineNum(const std::string& name, long val); static bool DefineNum(const std::string& name, long val);
/*! /**
* \brief Save the current execution status into a file * \brief Save the current execution status into a file
* \param pf * \param pf
* \parblock * \parblock
@ -291,7 +291,7 @@ public:
*/ */
bool SaveState(FILE* pf); bool SaveState(FILE* pf);
/*! /**
* \brief Restore the execution state from a file * \brief Restore the execution state from a file
* *
* The previous program code must already have been recompiled with Compile() before calling this function * The previous program code must already have been recompiled with Compile() before calling this function
@ -301,7 +301,7 @@ public:
*/ */
bool RestoreState(FILE* pf); bool RestoreState(FILE* pf);
/*! /**
* \brief GetPosition Gives the position of a routine in the original text * \brief GetPosition Gives the position of a routine in the original text
* the user can select the item to find from the beginning to the end * the user can select the item to find from the beginning to the end
* see the above modes in CBotGet. * see the above modes in CBotGet.
@ -321,7 +321,7 @@ public:
CBotGet modestart = GetPosExtern, CBotGet modestart = GetPosExtern,
CBotGet modestop = GetPosBloc); CBotGet modestop = GetPosBloc);
/*! /**
* \brief Returns the list of all user-defined functions in this program as instances of CBotFunction * \brief Returns the list of all user-defined functions in this program as instances of CBotFunction
* *
* This list includes all the functions (not only extern) * This list includes all the functions (not only extern)
@ -330,10 +330,10 @@ public:
*/ */
CBotFunction* GetFunctions(); CBotFunction* GetFunctions();
/*! /**
* \brief m_bCompileClass * \brief true while compiling class
* *
* TODO: document this * TODO: refactor this
*/ */
bool m_bCompileClass; bool m_bCompileClass;

View File

@ -20,7 +20,6 @@
#include "CBot/CBotToken.h" #include "CBot/CBotToken.h"
#include <cstdarg> #include <cstdarg>
#include <map>
//! \brief Keeps the string corresponding to keyword ID //! \brief Keeps the string corresponding to keyword ID
//! Map is filled with id-string pars that are needed for CBot language parsing //! Map is filled with id-string pars that are needed for CBot language parsing
@ -449,7 +448,7 @@ bis:
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotToken* CBotToken::CompileTokens(const std::string& program) std::unique_ptr<CBotToken> CBotToken::CompileTokens(const std::string& program)
{ {
CBotToken *nxt, *prv, *tokenbase; CBotToken *nxt, *prv, *tokenbase;
const char* p = program.c_str(); const char* p = program.c_str();
@ -477,13 +476,7 @@ CBotToken* CBotToken::CompileTokens(const std::string& program)
pp = p; pp = p;
} }
return tokenbase; return std::unique_ptr<CBotToken>(tokenbase);
}
////////////////////////////////////////////////////////////////////////////////
void CBotToken::Delete(CBotToken* pToken)
{
delete pToken;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -22,6 +22,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <map> #include <map>
#include <memory>
#include "CBot/CBotEnums.h" #include "CBot/CBotEnums.h"
@ -165,14 +166,7 @@ public:
* \return The first token of the linked liste. * \return The first token of the linked liste.
* \todo Replace the error code by an enum. * \todo Replace the error code by an enum.
*/ */
static CBotToken* CompileTokens(const std::string& p); static std::unique_ptr<CBotToken> CompileTokens(const std::string& p);
/**
* \brief Delete Releases the CBotToken linked list.
* \deprecated This function is deprecated because it only delete a pointer.
* \todo Remove this function.
*/
static void Delete(CBotToken* pToken);
/*! /*!
* \brief DefineNum This function define a language keyword with an associated * \brief DefineNum This function define a language keyword with an associated

View File

@ -156,13 +156,6 @@ bool CScript::IsEmpty()
bool CScript::CheckToken() bool CScript::CheckToken()
{ {
CBotToken* bt;
CBotToken* allBt;
std::string bs;
std::string token;
int cursor1, cursor2, i;
char used[100];
if ( !m_object->GetCheckToken() ) return true; if ( !m_object->GetCheckToken() ) return true;
m_error = CBotNoErr; m_error = CBotNoErr;
@ -171,25 +164,20 @@ bool CScript::CheckToken()
m_token[0] = 0; m_token[0] = 0;
m_bCompile = false; m_bCompile = false;
for ( i=0 ; i<m_main->GetObligatoryToken() ; i++ ) std::vector<bool> used(m_main->GetObligatoryToken(), false);
{
used[i] = 0; // token not used
}
allBt = CBotToken::CompileTokens(m_script.get()); auto tokens = CBotToken::CompileTokens(m_script.get());
bt = allBt; CBotToken* bt = tokens.get();
while ( bt != nullptr ) while ( bt != nullptr )
{ {
bs = bt->GetString(); std::string token = bt->GetString();
token = bs; int cursor1 = bt->GetStart();
int cursor2 = bt->GetEnd();
cursor1 = bt->GetStart(); int i = m_main->IsObligatoryToken(token.c_str());
cursor2 = bt->GetEnd();
i = m_main->IsObligatoryToken(token.c_str());
if ( i != -1 ) if ( i != -1 )
{ {
used[i] = 1; // token used used[i] = true; // token used
} }
if ( !m_main->IsProhibitedToken(token.c_str()) ) if ( !m_main->IsProhibitedToken(token.c_str()) )
@ -199,7 +187,6 @@ bool CScript::CheckToken()
m_cursor2 = cursor2; m_cursor2 = cursor2;
strcpy(m_title, "<prohibited>"); strcpy(m_title, "<prohibited>");
m_mainFunction[0] = 0; m_mainFunction[0] = 0;
CBotToken::Delete(allBt);
return false; return false;
} }
@ -207,20 +194,18 @@ bool CScript::CheckToken()
} }
// At least once every obligatory instruction? // At least once every obligatory instruction?
for ( i=0 ; i<m_main->GetObligatoryToken() ; i++ ) for (unsigned int i = 0; i < used.size(); i++)
{ {
if ( used[i] == 0 ) // token not used? if (!used[i]) // token not used?
{ {
strcpy(m_token, m_main->GetObligatoryToken(i)); strcpy(m_token, m_main->GetObligatoryToken(i));
m_error = static_cast<CBotError>(ERR_OBLIGATORYTOKEN); m_error = static_cast<CBotError>(ERR_OBLIGATORYTOKEN);
strcpy(m_title, "<obligatory>"); strcpy(m_title, "<obligatory>");
m_mainFunction[0] = 0; m_mainFunction[0] = 0;
CBotToken::Delete(allBt);
return false; return false;
} }
} }
CBotToken::Delete(allBt);
return true; return true;
} }
@ -635,7 +620,8 @@ void CScript::ColorizeScript(Ui::CEdit* edit, int rangeStart, int rangeEnd)
std::string text = std::string(edit->GetText(), edit->GetMaxChar()); std::string text = std::string(edit->GetText(), edit->GetMaxChar());
text = text.substr(rangeStart, rangeEnd-rangeStart); text = text.substr(rangeStart, rangeEnd-rangeStart);
CBotToken* bt = CBotToken::CompileTokens(text.c_str()); auto tokens = CBotToken::CompileTokens(text.c_str());
CBotToken* bt = tokens.get();
while ( bt != nullptr ) while ( bt != nullptr )
{ {
std::string token = bt->GetString(); std::string token = bt->GetString();
@ -684,8 +670,6 @@ void CScript::ColorizeScript(Ui::CEdit* edit, int rangeStart, int rangeEnd)
bt = bt->GetNext(); bt = bt->GetNext();
} }
CBotToken::Delete(bt);
} }