2015-11-15 17:59:15 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotCallMethode.h"
|
2015-11-15 17:59:15 +00:00
|
|
|
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotUtils.h"
|
|
|
|
#include "CBot/CBotStack.h"
|
|
|
|
#include "CBot/CBotCStack.h"
|
2015-11-15 17:59:15 +00:00
|
|
|
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotVar/CBotVar.h"
|
2015-11-15 22:18:47 +00:00
|
|
|
|
2015-11-15 17:59:15 +00:00
|
|
|
// Local include
|
|
|
|
|
|
|
|
// Global include
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2015-12-20 15:19:10 +00:00
|
|
|
CBotCallMethode::CBotCallMethode(const std::string& name,
|
|
|
|
bool rExec(CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
|
|
|
|
CBotTypResult rCompile(CBotVar* pThis, CBotVar*& pVar))
|
2015-11-15 17:59:15 +00:00
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
m_rExec = rExec;
|
|
|
|
m_rComp = rCompile;
|
|
|
|
m_next = nullptr;
|
|
|
|
m_nFuncIdent = CBotVar::NextUniqNum();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CBotCallMethode::~CBotCallMethode()
|
|
|
|
{
|
|
|
|
delete m_next;
|
|
|
|
m_next = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2015-12-20 15:19:10 +00:00
|
|
|
CBotTypResult CBotCallMethode::CompileCall(const std::string& name,
|
2015-11-15 17:59:15 +00:00
|
|
|
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 )
|
|
|
|
{
|
2015-12-20 18:16:01 +00:00
|
|
|
if (pVar2) pStack->SetError(static_cast<CBotError>(ret), pVar2->GetToken());
|
2015-11-15 17:59:15 +00:00
|
|
|
}
|
|
|
|
delete pVar;
|
|
|
|
nIdent = pt->m_nFuncIdent;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
pt = pt->m_next;
|
|
|
|
}
|
|
|
|
return CBotTypResult(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2015-12-20 15:19:10 +00:00
|
|
|
std::string CBotCallMethode::GetName()
|
2015-11-15 17:59:15 +00:00
|
|
|
{
|
|
|
|
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,
|
2015-12-20 15:19:10 +00:00
|
|
|
const std::string& name,
|
2015-11-15 17:59:15 +00:00
|
|
|
CBotVar* pThis,
|
|
|
|
CBotVar** ppVars,
|
2015-12-20 15:19:10 +00:00
|
|
|
CBotVar*& pResult,
|
2015-11-15 17:59:15 +00:00
|
|
|
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;
|
|
|
|
}
|