Moving CBotCallMethode class in its own header and source files.

dev-time-step
Grunaka 2015-11-15 18:59:15 +01:00
parent 6d340e80ab
commit 143eecd791
6 changed files with 296 additions and 179 deletions

View File

@ -490,37 +490,3 @@ extern float GetNumFloat( const char* p );
#if 0
extern void DEBUG( const char* text, int val, CBotStack* pile );
#endif
///////////////////////////////////////////
// class managing the methods declared by AddFunction on a class
class CBotCallMethode
{
private:
CBotString m_name;
bool (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user);
CBotTypResult
(*m_rComp) (CBotVar* pThis, CBotVar* &pVar);
CBotCallMethode* m_next;
friend class CBotClass;
long m_nFuncIdent;
public:
CBotCallMethode(const char* name,
bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar));
~CBotCallMethode();
CBotTypResult
CompileCall(const char* name, CBotVar* pThis,
CBotVar** ppVars, CBotCStack* pStack,
long& nIdent);
int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pFunc);
CBotString GetName();
CBotCallMethode* Next();
void AddNext(CBotCallMethode* p);
};

View File

@ -0,0 +1,179 @@
/*
* 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
*/
// Modules inlcude
#include "CBotCallMethode.h"
#include "CBotUtils.h"
#include "CBotStack.h"
// Local include
// Global include
////////////////////////////////////////////////////////////////////////////////
CBotCallMethode::CBotCallMethode(const char* name,
bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar))
{
m_name = name;
m_rExec = rExec;
m_rComp = rCompile;
m_next = nullptr;
m_nFuncIdent = CBotVar::NextUniqNum();
}
////////////////////////////////////////////////////////////////////////////////
CBotCallMethode::~CBotCallMethode()
{
delete m_next;
m_next = nullptr;
}
////////////////////////////////////////////////////////////////////////////////
CBotTypResult CBotCallMethode::CompileCall(const char* name,
CBotVar* pThis,
CBotVar** ppVar,
CBotCStack* pStack,
long& nIdent)
{
CBotCallMethode* pt = this;
nIdent = 0;
while ( pt != nullptr )
{
if ( pt->m_name == name )
{
CBotVar* pVar = MakeListVars(ppVar, true);
CBotVar* pVar2 = pVar;
CBotTypResult r = pt->m_rComp(pThis, pVar2);
int ret = r.GetType();
if ( ret > 20 )
{
if (pVar2) pStack->SetError(ret, pVar2->GetToken());
}
delete pVar;
nIdent = pt->m_nFuncIdent;
return r;
}
pt = pt->m_next;
}
return CBotTypResult(-1);
}
////////////////////////////////////////////////////////////////////////////////
CBotString CBotCallMethode::GetName()
{
return m_name;
}
////////////////////////////////////////////////////////////////////////////////
CBotCallMethode* CBotCallMethode::Next()
{
return m_next;
}
////////////////////////////////////////////////////////////////////////////////
void CBotCallMethode::AddNext(CBotCallMethode* pt)
{
CBotCallMethode* p = this;
while ( p->m_next != nullptr ) p = p->m_next;
p->m_next = pt;
}
////////////////////////////////////////////////////////////////////////////////
int CBotCallMethode::DoCall(long& nIdent,
const char* name,
CBotVar* pThis,
CBotVar** ppVars,
CBotVar* &pResult,
CBotStack* pStack,
CBotToken* pToken)
{
CBotCallMethode* pt = this;
// search by the identifier
if ( nIdent ) while ( pt != nullptr )
{
if ( pt->m_nFuncIdent == nIdent )
{
// lists the parameters depending on the contents of the stack (pStackVar)
CBotVar* pVar = MakeListVars(ppVars, true);
CBotVar* pVarToDelete = pVar;
// then calls the routine external to the module
int Exception = 0;
int res = pt->m_rExec(pThis, pVar, pResult, Exception, pStack->GetPUser());
pStack->SetVar(pResult);
if (res == false)
{
if (Exception!=0)
{
// pStack->SetError(Exception, pVar->GetToken());
pStack->SetError(Exception, pToken);
}
delete pVarToDelete;
return false;
}
delete pVarToDelete;
return true;
}
pt = pt->m_next;
}
// search by name
while ( pt != nullptr )
{
if ( pt->m_name == name )
{
// lists the parameters depending on the contents of the stack (pStackVar)
CBotVar* pVar = MakeListVars(ppVars, true);
CBotVar* pVarToDelete = pVar;
int Exception = 0;
int res = pt->m_rExec(pThis, pVar, pResult, Exception, pStack->GetPUser());
pStack->SetVar(pResult);
if (res == false)
{
if (Exception!=0)
{
// pStack->SetError(Exception, pVar->GetToken());
pStack->SetError(Exception, pToken);
}
delete pVarToDelete;
return false;
}
delete pVarToDelete;
nIdent = pt->m_nFuncIdent;
return true;
}
pt = pt->m_next;
}
return -1;
}

115
src/CBot/CBotCallMethode.h Normal file
View File

@ -0,0 +1,115 @@
/*
* 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
/*!
* \brief The CBotCallMethode class Class managing the methods declared by
* AddFunction on a class.
*/
class CBotCallMethode
{
public:
/*!
* \brief CBotCallMethode
* \param name
* \param rExec
* \param rCompile
*/
CBotCallMethode(const char* name,
bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar));
/*!
* \brief ~CBotCallMethode
*/
~CBotCallMethode();
/*!
* \brief CompileCall Is acceptable by a call procedure name and given
* parameters.
* \param name
* \param pThis
* \param ppVars
* \param pStack
* \param nIdent
* \return
*/
CBotTypResult CompileCall(const char* name,
CBotVar* pThis,
CBotVar** ppVars,
CBotCStack* pStack,
long& nIdent);
/*!
* \brief DoCall
* \param nIdent
* \param name
* \param pThis
* \param ppVars
* \param pResult
* \param pStack
* \param pFunc
* \return
*/
int DoCall(long& nIdent,
const char* name,
CBotVar* pThis,
CBotVar** ppVars,
CBotVar* &pResult,
CBotStack* pStack,
CBotToken* pFunc);
/*!
* \brief GetName
* \return
*/
CBotString GetName();
/*!
* \brief Next
* \return
*/
CBotCallMethode* Next();
/*!
* \brief AddNext
* \param p
*/
void AddNext(CBotCallMethode* p);
private:
CBotString m_name;
bool (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user);
CBotTypResult (*m_rComp) (CBotVar* pThis, CBotVar* &pVar);
CBotCallMethode* m_next;
friend class CBotClass;
long m_nFuncIdent;
};

View File

@ -31,6 +31,7 @@
#include "CBotCall.h"
#include "CBotStack.h"
#include "CBotUtils.h"
#include "CBotCallMethode.h"
// Local include

View File

@ -641,148 +641,3 @@ void CBotProgram::Free()
CBotCall ::Free() ;
CBotClass::Free() ;
}
////////////////////////////////////////////////////////////////////////////////
CBotCallMethode::CBotCallMethode(const char* name,
bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar))
{
m_name = name;
m_rExec = rExec;
m_rComp = rCompile;
m_next = nullptr;
m_nFuncIdent = CBotVar::NextUniqNum();
}
////////////////////////////////////////////////////////////////////////////////
CBotCallMethode::~CBotCallMethode()
{
delete m_next;
m_next = nullptr;
}
// is acceptable by a call procedure name
// and given parameters
////////////////////////////////////////////////////////////////////////////////
CBotTypResult CBotCallMethode::CompileCall(const char* name, CBotVar* pThis,
CBotVar** ppVar, CBotCStack* pStack,
long& nIdent)
{
CBotCallMethode* pt = this;
nIdent = 0;
while ( pt != nullptr )
{
if ( pt->m_name == name )
{
CBotVar* pVar = MakeListVars(ppVar, true);
CBotVar* pVar2 = pVar;
CBotTypResult r = pt->m_rComp(pThis, pVar2);
int ret = r.GetType();
if ( ret > 20 )
{
if (pVar2) pStack->SetError(ret, pVar2->GetToken());
}
delete pVar;
nIdent = pt->m_nFuncIdent;
return r;
}
pt = pt->m_next;
}
return CBotTypResult(-1);
}
////////////////////////////////////////////////////////////////////////////////
CBotString CBotCallMethode::GetName()
{
return m_name;
}
////////////////////////////////////////////////////////////////////////////////
CBotCallMethode* CBotCallMethode::Next()
{
return m_next;
}
////////////////////////////////////////////////////////////////////////////////
void CBotCallMethode::AddNext(CBotCallMethode* pt)
{
CBotCallMethode* p = this;
while ( p->m_next != nullptr ) p = p->m_next;
p->m_next = pt;
}
////////////////////////////////////////////////////////////////////////////////
int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pToken)
{
CBotCallMethode* pt = this;
// search by the identifier
if ( nIdent ) while ( pt != nullptr )
{
if ( pt->m_nFuncIdent == nIdent )
{
// lists the parameters depending on the contents of the stack (pStackVar)
CBotVar* pVar = MakeListVars(ppVars, true);
CBotVar* pVarToDelete = pVar;
// then calls the routine external to the module
int Exception = 0;
int res = pt->m_rExec(pThis, pVar, pResult, Exception, pStack->GetPUser());
pStack->SetVar(pResult);
if (res == false)
{
if (Exception!=0)
{
// pStack->SetError(Exception, pVar->GetToken());
pStack->SetError(Exception, pToken);
}
delete pVarToDelete;
return false;
}
delete pVarToDelete;
return true;
}
pt = pt->m_next;
}
// search by name
while ( pt != nullptr )
{
if ( pt->m_name == name )
{
// lists the parameters depending on the contents of the stack (pStackVar)
CBotVar* pVar = MakeListVars(ppVars, true);
CBotVar* pVarToDelete = pVar;
int Exception = 0;
int res = pt->m_rExec(pThis, pVar, pResult, Exception, pStack->GetPUser());
pStack->SetVar(pResult);
if (res == false)
{
if (Exception!=0)
{
// pStack->SetError(Exception, pVar->GetToken());
pStack->SetError(Exception, pToken);
}
delete pVarToDelete;
return false;
}
delete pVarToDelete;
nIdent = pt->m_nFuncIdent;
return true;
}
pt = pt->m_next;
}
return -1;
}

View File

@ -9,6 +9,7 @@ set(SOURCES
CBotCall.cpp
CBotUtils.cpp
CBotDefParam.cpp
CBotCallMethode.cpp
CBotInstr/CBotWhile.cpp
CBotInstr/CBotDo.cpp
CBotInstr/CBotFor.cpp