From 6d340e80ab2197ca0850052a774e31c2a16e4a82 Mon Sep 17 00:00:00 2001 From: Grunaka <dev@romainbreton.fr> Date: Sun, 15 Nov 2015 18:51:28 +0100 Subject: [PATCH] Moving CBotDefParam class in its own header and source files. --- src/CBot/CBot.h | 26 ---- src/CBot/CBotDefParam.cpp | 207 ++++++++++++++++++++++++++++ src/CBot/CBotDefParam.h | 115 ++++++++++++++++ src/CBot/CBotInstr/CBotFunction.cpp | 185 +------------------------ src/CBot/CMakeLists.txt | 1 + 5 files changed, 324 insertions(+), 210 deletions(-) create mode 100644 src/CBot/CBotDefParam.cpp create mode 100644 src/CBot/CBotDefParam.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 33efc71f..56e483c0 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -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(); -}; diff --git a/src/CBot/CBotDefParam.cpp b/src/CBot/CBotDefParam.cpp new file mode 100644 index 00000000..3a91f604 --- /dev/null +++ b/src/CBot/CBotDefParam.cpp @@ -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; +} diff --git a/src/CBot/CBotDefParam.h b/src/CBot/CBotDefParam.h new file mode 100644 index 00000000..705660e3 --- /dev/null +++ b/src/CBot/CBotDefParam.h @@ -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; +}; diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index 94df0e7a..fd81a441 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -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; -} diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 7aee901b..c2a953f6 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -8,6 +8,7 @@ set(SOURCES CBotVar.cpp CBotCall.cpp CBotUtils.cpp + CBotDefParam.cpp CBotInstr/CBotWhile.cpp CBotInstr/CBotDo.cpp CBotInstr/CBotFor.cpp