Moving CBotDefParam class in its own header and source files.

dev-time-step
Grunaka 2015-11-15 18:51:28 +01:00
parent 0a1b7da2a8
commit 6d340e80ab
5 changed files with 324 additions and 210 deletions

View File

@ -524,29 +524,3 @@ public:
};
// a list of parameters
class CBotDefParam
{
private:
CBotToken m_token; // name of the parameter
CBotString m_typename; // type name
CBotTypResult m_type; // type of paramteter
CBotDefParam* m_next; // next parameter
long m_nIdent;
public:
CBotDefParam();
~CBotDefParam();
static
CBotDefParam* Compile(CBotToken* &p, CBotCStack* pStack);
bool Execute(CBotVar** ppVars, CBotStack* &pj);
void RestoreState(CBotStack* &pj, bool bMain);
void AddNext(CBotDefParam* p);
int GetType();
CBotTypResult GetTypResult();
CBotDefParam* GetNext();
CBotString GetParamString();
};

207
src/CBot/CBotDefParam.cpp Normal file
View File

@ -0,0 +1,207 @@
/*
* 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 "CBotDefParam.h"
#include "CBot.h"
#include "CBotUtils.h"
// Local include
// Global include
#include <cassert>
////////////////////////////////////////////////////////////////////////////////
CBotDefParam::CBotDefParam()
{
m_next = nullptr;
m_nIdent = 0;
}
////////////////////////////////////////////////////////////////////////////////
CBotDefParam::~CBotDefParam()
{
delete m_next;
}
////////////////////////////////////////////////////////////////////////////////
CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack)
{
// mainly not pStack->TokenStack here
// declared variables must remain visible thereafter
pStack->SetStartError(p->GetStart());
if (IsOfType(p, ID_OPENPAR))
{
CBotDefParam* list = nullptr;
while (!IsOfType(p, ID_CLOSEPAR))
{
CBotDefParam* param = new CBotDefParam();
if (list == nullptr) list = param;
else list->AddNext(param); // added to the list
// CBotClass* pClass = nullptr;//= CBotClass::Find(p);
param->m_typename = p->GetString();
CBotTypResult type = param->m_type = TypeParam(p, pStack);
// if ( type == CBotTypPointer ) type = CBotTypClass; // we must create a new object
if (param->m_type.GetType() > 0)
{
CBotToken* pp = p;
param->m_token = *p;
if (pStack->IsOk() && IsOfType(p, TokenTypVar) )
{
// variable already declared?
if (pStack->CheckVarLocal(pp))
{
pStack->SetError(TX_REDEFVAR, pp);
break;
}
if ( type.Eq(CBotTypArrayPointer) ) type.SetType(CBotTypArrayBody);
CBotVar* var = CBotVar::Create(pp->GetString(), type); // creates the variable
// if ( pClass ) var->SetClass(pClass);
var->SetInit(CBotVar::InitType::IS_POINTER); // mark initialized
param->m_nIdent = CBotVar::NextUniqNum();
var->SetUniqNum(param->m_nIdent);
pStack->AddVar(var); // place on the stack
if (IsOfType(p, ID_COMMA) || p->GetType() == ID_CLOSEPAR)
continue;
}
pStack->SetError(TX_CLOSEPAR, p->GetStart());
}
pStack->SetError(TX_NOTYP, p);
delete list;
return nullptr;
}
return list;
}
pStack->SetError(TX_OPENPAR, p->GetStart());
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
void CBotDefParam::AddNext(CBotDefParam* p)
{
CBotDefParam* pp = this;
while (pp->m_next != nullptr) pp = pp->m_next;
pp->m_next = p;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj)
{
int i = 0;
CBotDefParam* p = this;
while ( p != nullptr )
{
// creates a local variable on the stack
CBotVar* newvar = CBotVar::Create(p->m_token.GetString(), p->m_type);
// serves to make the transformation of types:
if ( ppVars != nullptr && ppVars[i] != nullptr )
{
switch (p->m_type.GetType())
{
case CBotTypInt:
newvar->SetValInt(ppVars[i]->GetValInt());
break;
case CBotTypFloat:
newvar->SetValFloat(ppVars[i]->GetValFloat());
break;
case CBotTypString:
newvar->SetValString(ppVars[i]->GetValString());
break;
case CBotTypBoolean:
newvar->SetValInt(ppVars[i]->GetValInt());
break;
case CBotTypIntrinsic:
(static_cast<CBotVarClass*>(newvar))->Copy(ppVars[i], false);
break;
case CBotTypPointer:
case CBotTypArrayPointer:
{
newvar->SetPointer(ppVars[i]->GetPointer());
}
break;
default:
assert(0);
}
}
newvar->SetUniqNum(p->m_nIdent);
pj->AddVar(newvar); // add a variable
p = p->m_next;
i++;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
void CBotDefParam::RestoreState(CBotStack* &pj, bool bMain)
{
// int i = 0;
CBotDefParam* p = this;
while ( p != nullptr )
{
// creates a local variable on the stack
CBotVar* var = pj->FindVar(p->m_token.GetString());
var->SetUniqNum(p->m_nIdent);
p = p->m_next;
}
}
////////////////////////////////////////////////////////////////////////////////
int CBotDefParam::GetType()
{
return m_type.GetType();
}
////////////////////////////////////////////////////////////////////////////////
CBotTypResult CBotDefParam::GetTypResult()
{
return m_type;
}
////////////////////////////////////////////////////////////////////////////////
CBotDefParam* CBotDefParam::GetNext()
{
return m_next;
}
////////////////////////////////////////////////////////////////////////////////
CBotString CBotDefParam::GetParamString()
{
CBotString param;
param = m_typename;
param += ' ';
param += m_token.GetString();
return param;
}

115
src/CBot/CBotDefParam.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 "CBotDll.h"
#include "CBotToken.h"
#include "CBotStack.h"
// Local include
// Global include
class CBotCStack;
class CBotStack;
class CBotVar;
/*!
* \brief The CBotDefParam class A list of parameters.
*/
class CBotDefParam
{
public:
/*!
* \brief CBotDefParam
*/
CBotDefParam();
/*!
* \brief ~CBotDefParam
*/
~CBotDefParam();
/*!
* \brief Compile Compiles a list of parameters.
* \param p
* \param pStack
* \return
*/
static CBotDefParam* Compile(CBotToken* &p, CBotCStack* pStack);
/*!
* \brief Execute
* \param ppVars
* \param pj
* \return
*/
bool Execute(CBotVar** ppVars, CBotStack* &pj);
/*!
* \brief RestoreState
* \param pj
* \param bMain
*/
void RestoreState(CBotStack* &pj, bool bMain);
/*!
* \brief AddNext
* \param p
*/
void AddNext(CBotDefParam* p);
/*!
* \brief GetType
* \return
*/
int GetType();
/*!
* \brief GetTypResult
* \return
*/
CBotTypResult GetTypResult();
/*!
* \brief GetNext
* \return
*/
CBotDefParam* GetNext();
/*!
* \brief GetParamString
* \return
*/
CBotString GetParamString();
private:
//! Name of the parameter.
CBotToken m_token;
//! Type name.
CBotString m_typename;
//! Type of paramteter.
CBotTypResult m_type;
//! Next parameter.
CBotDefParam* m_next;
long m_nIdent;
};

View File

@ -31,7 +31,7 @@
#include "CBotStack.h"
#include "CBotClass.h"
#include "CBotDefParam.h"
#include "CBotUtils.h"
// Local include
@ -888,186 +888,3 @@ void CBotFunction::AddPublic(CBotFunction* func)
}
m_listPublic = func;
}
/////////////////////////////////////////////////////////////////////////
// management of parameters
////////////////////////////////////////////////////////////////////////////////
CBotDefParam::CBotDefParam()
{
m_next = nullptr;
m_nIdent = 0;
}
////////////////////////////////////////////////////////////////////////////////
CBotDefParam::~CBotDefParam()
{
delete m_next;
}
////////////////////////////////////////////////////////////////////////////////
// compiles a list of parameters
CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack)
{
// mainly not pStack->TokenStack here
// declared variables must remain visible thereafter
pStack->SetStartError(p->GetStart());
if (IsOfType(p, ID_OPENPAR))
{
CBotDefParam* list = nullptr;
while (!IsOfType(p, ID_CLOSEPAR))
{
CBotDefParam* param = new CBotDefParam();
if (list == nullptr) list = param;
else list->AddNext(param); // added to the list
// CBotClass* pClass = nullptr;//= CBotClass::Find(p);
param->m_typename = p->GetString();
CBotTypResult type = param->m_type = TypeParam(p, pStack);
// if ( type == CBotTypPointer ) type = CBotTypClass; // we must create a new object
if (param->m_type.GetType() > 0)
{
CBotToken* pp = p;
param->m_token = *p;
if (pStack->IsOk() && IsOfType(p, TokenTypVar) )
{
// variable already declared?
if (pStack->CheckVarLocal(pp))
{
pStack->SetError(TX_REDEFVAR, pp);
break;
}
if ( type.Eq(CBotTypArrayPointer) ) type.SetType(CBotTypArrayBody);
CBotVar* var = CBotVar::Create(pp->GetString(), type); // creates the variable
// if ( pClass ) var->SetClass(pClass);
var->SetInit(CBotVar::InitType::IS_POINTER); // mark initialized
param->m_nIdent = CBotVar::NextUniqNum();
var->SetUniqNum(param->m_nIdent);
pStack->AddVar(var); // place on the stack
if (IsOfType(p, ID_COMMA) || p->GetType() == ID_CLOSEPAR)
continue;
}
pStack->SetError(TX_CLOSEPAR, p->GetStart());
}
pStack->SetError(TX_NOTYP, p);
delete list;
return nullptr;
}
return list;
}
pStack->SetError(TX_OPENPAR, p->GetStart());
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
void CBotDefParam::AddNext(CBotDefParam* p)
{
CBotDefParam* pp = this;
while (pp->m_next != nullptr) pp = pp->m_next;
pp->m_next = p;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj)
{
int i = 0;
CBotDefParam* p = this;
while ( p != nullptr )
{
// creates a local variable on the stack
CBotVar* newvar = CBotVar::Create(p->m_token.GetString(), p->m_type);
// serves to make the transformation of types:
if ( ppVars != nullptr && ppVars[i] != nullptr )
{
switch (p->m_type.GetType())
{
case CBotTypInt:
newvar->SetValInt(ppVars[i]->GetValInt());
break;
case CBotTypFloat:
newvar->SetValFloat(ppVars[i]->GetValFloat());
break;
case CBotTypString:
newvar->SetValString(ppVars[i]->GetValString());
break;
case CBotTypBoolean:
newvar->SetValInt(ppVars[i]->GetValInt());
break;
case CBotTypIntrinsic:
(static_cast<CBotVarClass*>(newvar))->Copy(ppVars[i], false);
break;
case CBotTypPointer:
case CBotTypArrayPointer:
{
newvar->SetPointer(ppVars[i]->GetPointer());
}
break;
default:
assert(0);
}
}
newvar->SetUniqNum(p->m_nIdent);
pj->AddVar(newvar); // add a variable
p = p->m_next;
i++;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
void CBotDefParam::RestoreState(CBotStack* &pj, bool bMain)
{
// int i = 0;
CBotDefParam* p = this;
while ( p != nullptr )
{
// creates a local variable on the stack
CBotVar* var = pj->FindVar(p->m_token.GetString());
var->SetUniqNum(p->m_nIdent);
p = p->m_next;
}
}
////////////////////////////////////////////////////////////////////////////////
int CBotDefParam::GetType()
{
return m_type.GetType();
}
////////////////////////////////////////////////////////////////////////////////
CBotTypResult CBotDefParam::GetTypResult()
{
return m_type;
}
////////////////////////////////////////////////////////////////////////////////
CBotDefParam* CBotDefParam::GetNext()
{
return m_next;
}
////////////////////////////////////////////////////////////////////////////////
CBotString CBotDefParam::GetParamString()
{
CBotString param;
param = m_typename;
param += ' ';
param += m_token.GetString();
return param;
}

View File

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