From 8eff62a78cf9ee1c45e226112844118e073773f1 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 19:53:00 +0100 Subject: [PATCH] Moving CBotBlock class in its own header and source files. --- src/CBot/CBot.cpp | 52 --------------------- src/CBot/CBot.h | 16 ------- src/CBot/CBotFunction.cpp | 2 + src/CBot/CBotIf.cpp | 2 + src/CBot/CBotInstr/CBotBlock.cpp | 69 ++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotBlock.h | 64 ++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotCatch.cpp | 1 + src/CBot/CBotInstr/CBotDo.cpp | 1 + src/CBot/CBotInstr/CBotFor.cpp | 1 + src/CBot/CBotInstr/CBotListInstr.cpp | 1 + src/CBot/CBotInstr/CBotSwitch.cpp | 1 + src/CBot/CBotInstr/CBotTry.cpp | 1 + src/CBot/CBotInstr/CBotWhile.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 14 files changed, 145 insertions(+), 68 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotBlock.cpp create mode 100644 src/CBot/CBotInstr/CBotBlock.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 98e04d9c..042e4c74 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -357,58 +357,6 @@ bool CBotInstr::CompCase(CBotStack* &pj, int val) return false; } -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compiles a statement block " { i ; i ; } " - -// this class have no constructor because there is never an instance of this -// class -// the object returned by Compile is usually of type CBotListInstr - - -CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) -{ - pStack->SetStartError(p->GetStart()); - - if (IsOfType(p, ID_OPBLK)) - { - CBotInstr* inst = CBotListInstr::Compile(p, pStack, bLocal); - - if (IsOfType(p, ID_CLBLK)) - { - return inst; - } - - pStack->SetError(TX_CLOSEBLK, p->GetStart()); // missing parenthesis - delete inst; - return nullptr; - } - - pStack->SetError(TX_OPENBLK, p->GetStart()); - return nullptr; -} - -CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal) -{ - // is this a new block - if (p->GetType() == ID_OPBLK) return CBotBlock::Compile(p, pStack); - - // otherwise, look for a single statement instead - - // to handle the case with local definition instruction (*) - CBotCStack* pStk = pStack->TokenStack(p, bLocal); - - return pStack->Return( CBotInstr::Compile(p, pStk), // a single instruction - pStk); -} - -// (*) is the case in the following statement -// if (1 == 1) int x = 0; -// where the variable x is known only in the block following the if - - ////////////////////////////////////////////////////////////////////////////////////// // defining an array of any type // int a[12]; diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 326a79ec..32cdb695 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -799,22 +799,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; - - - -// an instruction block { .... } -class CBotBlock : public CBotInstr -{ -public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); - static - CBotInstr* CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal = false); -private: - CBotBlock() = delete; - CBotBlock(const CBotBlock &) = delete; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 226add20..6ebad172 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -23,6 +23,8 @@ #include "CBot.h" +#include "CBotInstr/CBotBlock.h" + #include diff --git a/src/CBot/CBotIf.cpp b/src/CBot/CBotIf.cpp index 15770af4..781ec12c 100644 --- a/src/CBot/CBotIf.cpp +++ b/src/CBot/CBotIf.cpp @@ -22,6 +22,8 @@ #include "CBot.h" +#include "CBotInstr/CBotBlock.h" + // various constructors / destructors CBotIf::CBotIf() { diff --git a/src/CBot/CBotInstr/CBotBlock.cpp b/src/CBot/CBotInstr/CBotBlock.cpp new file mode 100644 index 00000000..ed87b53b --- /dev/null +++ b/src/CBot/CBotInstr/CBotBlock.cpp @@ -0,0 +1,69 @@ +/* + * 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 "CBotBlock.h" +#include "CBotListInstr.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) +{ + pStack->SetStartError(p->GetStart()); + + if (IsOfType(p, ID_OPBLK)) + { + CBotInstr* inst = CBotListInstr::Compile(p, pStack, bLocal); + + if (IsOfType(p, ID_CLBLK)) + { + return inst; + } + + pStack->SetError(TX_CLOSEBLK, p->GetStart()); // missing parenthesis + delete inst; + return nullptr; + } + + pStack->SetError(TX_OPENBLK, p->GetStart()); + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal) +{ + // is this a new block + if (p->GetType() == ID_OPBLK) return CBotBlock::Compile(p, pStack); + + // otherwise, look for a single statement instead + + // to handle the case with local definition instruction (*) + CBotCStack* pStk = pStack->TokenStack(p, bLocal); + + return pStack->Return( CBotInstr::Compile(p, pStk), // a single instruction + pStk); +} + +// (*) is the case in the following statement +// if (1 == 1) int x = 0; +// where the variable x is known only in the block following the if diff --git a/src/CBot/CBotInstr/CBotBlock.h b/src/CBot/CBotInstr/CBotBlock.h new file mode 100644 index 00000000..f413e756 --- /dev/null +++ b/src/CBot/CBotInstr/CBotBlock.h @@ -0,0 +1,64 @@ +/* + * 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 CBotBlock class An instruction block { .... }. + */ +class CBotBlock : public CBotInstr +{ +public: + + /*! + * \brief Compile Compiles a statement block " { i ; i ; } " + * \param p + * \param pStack + * \param bLocal + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); + + /*! + * \brief CompileBlkOrInst + * \param p + * \param pStack + * \param bLocal + * \return + */ + static CBotInstr* CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal = false); + +private: + + /*! + * \brief CBotBlock This class have no constructor because there is never an + * instance of this class the object returned by Compile is usually of type + * CBotListInstr + */ + CBotBlock() = delete; + CBotBlock(const CBotBlock &) = delete; +}; diff --git a/src/CBot/CBotInstr/CBotCatch.cpp b/src/CBot/CBotInstr/CBotCatch.cpp index 4c63e029..e4a5571e 100644 --- a/src/CBot/CBotInstr/CBotCatch.cpp +++ b/src/CBot/CBotInstr/CBotCatch.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotCatch.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotDo.cpp b/src/CBot/CBotInstr/CBotDo.cpp index a24e5dbe..94c281d4 100644 --- a/src/CBot/CBotInstr/CBotDo.cpp +++ b/src/CBot/CBotInstr/CBotDo.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotDo.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFor.cpp b/src/CBot/CBotInstr/CBotFor.cpp index 7dc07d4a..32caa3c5 100644 --- a/src/CBot/CBotInstr/CBotFor.cpp +++ b/src/CBot/CBotInstr/CBotFor.cpp @@ -20,6 +20,7 @@ // Modules inlcude #include "CBotFor.h" #include "CBotListExpression.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListInstr.cpp b/src/CBot/CBotInstr/CBotListInstr.cpp index 3d1d9c57..b2fcf49c 100644 --- a/src/CBot/CBotInstr/CBotListInstr.cpp +++ b/src/CBot/CBotInstr/CBotListInstr.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotListInstr.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotSwitch.cpp b/src/CBot/CBotInstr/CBotSwitch.cpp index c0c79975..70fd8033 100644 --- a/src/CBot/CBotInstr/CBotSwitch.cpp +++ b/src/CBot/CBotInstr/CBotSwitch.cpp @@ -22,6 +22,7 @@ // Modules inlcude #include "CBotSwitch.h" #include "CBotCase.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotTry.cpp b/src/CBot/CBotInstr/CBotTry.cpp index c2b2901a..89b689ea 100644 --- a/src/CBot/CBotInstr/CBotTry.cpp +++ b/src/CBot/CBotInstr/CBotTry.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotTry.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotWhile.cpp b/src/CBot/CBotInstr/CBotWhile.cpp index 92b276db..340cc239 100644 --- a/src/CBot/CBotInstr/CBotWhile.cpp +++ b/src/CBot/CBotInstr/CBotWhile.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotWhile.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 30e717be..ac6a0c09 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -32,6 +32,7 @@ set(SOURCES CBotInstr/CBotInstrMethode.cpp CBotInstr/CBotInstrCall.cpp CBotInstr/CBotListInstr.cpp + CBotInstr/CBotBlock.cpp ) # Includes