[colobot] 62/377: Moving CBotSwitch class in its own header and source files.
Didier Raboud
odyx at moszumanska.debian.org
Wed Mar 30 13:33:58 UTC 2016
This is an automated email from the git hooks/post-receive script.
odyx pushed a commit to branch debian/master
in repository colobot.
commit 5629bce950470348f293e49250eb54299d80cfb4
Author: Grunaka <dev at romainbreton.fr>
Date: Sun Nov 8 19:04:49 2015 +0100
Moving CBotSwitch class in its own header and source files.
---
src/CBot/CBot.cpp | 1 +
src/CBot/CBot.h | 17 ---
src/CBot/CBotInstr/CBotSwitch.cpp | 214 ++++++++++++++++++++++++++++++++++++++
src/CBot/CBotInstr/CBotSwitch.h | 76 ++++++++++++++
src/CBot/CBotWhile.cpp | 188 ---------------------------------
src/CBot/CMakeLists.txt | 1 +
6 files changed, 292 insertions(+), 205 deletions(-)
diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp
index dad1ccc..97bcc77 100644
--- a/src/CBot/CBot.cpp
+++ b/src/CBot/CBot.cpp
@@ -38,6 +38,7 @@
#include "CBot.h"
#include "CBotInstr/CBotDo.h"
#include "CBotInstr/CBotFor.h"
+#include "CBotInstr/CBotSwitch.h"
// Local include
diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h
index 4d70132..01a874d 100644
--- a/src/CBot/CBot.h
+++ b/src/CBot/CBot.h
@@ -511,23 +511,6 @@ public:
void RestoreState(CBotStack* &pj, bool bMain) override;
};
-
-class CBotSwitch : public CBotInstr
-{
-private:
- CBotInstr* m_Value; // value to seek
- CBotInstr* m_Block; // instructions
-
-public:
- CBotSwitch();
- ~CBotSwitch();
- static
- CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack);
- bool Execute(CBotStack* &pj) override;
- void RestoreState(CBotStack* &pj, bool bMain) override;
-};
-
-
class CBotCase : public CBotInstr
{
private:
diff --git a/src/CBot/CBotInstr/CBotSwitch.cpp b/src/CBot/CBotInstr/CBotSwitch.cpp
new file mode 100644
index 0000000..f9510d9
--- /dev/null
+++ b/src/CBot/CBotInstr/CBotSwitch.cpp
@@ -0,0 +1,214 @@
+
+
+/*
+ * 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 "CBotSwitch.h"
+
+// Local include
+
+// Global include
+
+
+////////////////////////////////////////////////////////////////////////////////
+CBotSwitch::CBotSwitch()
+{
+ m_Value =
+ m_Block = nullptr; // nullptr so that delete is not possible further
+ name = "CBotSwitch"; // debug
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CBotSwitch::~CBotSwitch()
+{
+ delete m_Value; // frees the value
+ delete m_Block; // frees the instruction block
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CBotInstr* CBotSwitch::Compile(CBotToken* &p, CBotCStack* pStack)
+{
+ CBotSwitch* inst = new CBotSwitch(); // creates the object
+ CBotToken* pp = p; // preserves at the ^ token (starting position)
+
+ inst->SetToken(p);
+ if (!IsOfType(p, ID_SWITCH)) return nullptr; // should never happen
+
+ CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp
+
+ if ( IsOfType(p, ID_OPENPAR ) )
+ {
+ if ( nullptr != (inst->m_Value = CBotExpression::Compile( p, pStk )) )
+ {
+ if ( pStk->GetType() < CBotTypLong )
+ {
+ if ( IsOfType(p, ID_CLOSEPAR ) )
+ {
+ if ( IsOfType(p, ID_OPBLK ) )
+ {
+ IncLvl();
+
+ while( !IsOfType( p, ID_CLBLK ) )
+ {
+ if ( p->GetType() == ID_CASE || p->GetType() == ID_DEFAULT)
+ {
+ CBotCStack* pStk2 = pStk->TokenStack(p); // un petit bout de pile svp
+
+ CBotInstr* i = CBotCase::Compile( p, pStk2 );
+ if (i == nullptr)
+ {
+ delete inst;
+ return pStack->Return(nullptr, pStk2);
+ }
+ delete pStk2;
+ if ( inst->m_Block == nullptr ) inst->m_Block = i;
+ else inst->m_Block->AddNext(i);
+ continue;
+ }
+
+ if ( inst->m_Block == nullptr )
+ {
+ pStk->SetError(TX_NOCASE, p->GetStart());
+ delete inst;
+ return pStack->Return(nullptr, pStk);
+ }
+
+ CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true );
+ if ( !pStk->IsOk() )
+ {
+ delete inst;
+ return pStack->Return(nullptr, pStk);
+ }
+ inst->m_Block->AddNext(i);
+
+ if ( p == nullptr )
+ {
+ pStk->SetError(TX_CLOSEBLK, -1);
+ delete inst;
+ return pStack->Return(nullptr, pStk);
+ }
+ }
+ DecLvl();
+
+ if ( inst->m_Block == nullptr )
+ {
+ pStk->SetError(TX_NOCASE, p->GetStart());
+ delete inst;
+ return pStack->Return(nullptr, pStk);
+ }
+ // the statement block is ok
+ return pStack->Return(inst, pStk); // return an object to the application
+ }
+ pStk->SetError( TX_OPENBLK, p->GetStart() );
+ }
+ pStk->SetError( TX_CLOSEPAR, p->GetStart() );
+ }
+ pStk->SetError( TX_BADTYPE, p->GetStart() );
+ }
+ }
+ pStk->SetError( TX_OPENPAR, p->GetStart());
+
+ delete inst; // error, frees up
+ return pStack->Return(nullptr, pStk); // no object, the error is on the stack
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool CBotSwitch :: Execute(CBotStack* &pj)
+{
+ CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack
+// if ( pile1 == EOX ) return true;
+
+ CBotInstr* p = m_Block; // first expression
+
+ int state = pile1->GetState();
+ if (state == 0)
+ {
+ if ( !m_Value->Execute(pile1) ) return false;
+ pile1->SetState(state = -1);
+ }
+
+ if ( pile1->IfStep() ) return false;
+
+ if ( state == -1 )
+ {
+ state = 0;
+ int val = pile1->GetVal(); // result of the value
+
+ CBotStack* pile2 = pile1->AddStack();
+ while ( p != nullptr ) // search for the corresponding case in a list
+ {
+ state++;
+ if ( p->CompCase( pile2, val ) ) break; // found the case
+ p = p->GetNext();
+ }
+ pile2->Delete();
+
+ if ( p == nullptr ) return pj->Return(pile1); // completed if nothing
+
+ if ( !pile1->SetState(state) ) return false;
+ }
+
+ p = m_Block; // returns to the beginning
+ while (state-->0) p = p->GetNext(); // advance in the list
+
+ while( p != nullptr )
+ {
+ if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1);
+ if ( !pile1->IncState() ) return false;
+ p = p->GetNext();
+ }
+ return pj->Return(pile1);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain)
+{
+ if ( !bMain ) return;
+
+ CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack
+ if ( pile1 == nullptr ) return;
+
+ CBotInstr* p = m_Block; // first expression
+
+ int state = pile1->GetState();
+ if (state == 0)
+ {
+ m_Value->RestoreState(pile1, bMain);
+ return;
+ }
+
+ if ( state == -1 )
+ {
+ return;
+ }
+
+// p = m_Block; // returns to the beginning
+ while ( p != nullptr && state-- > 0 )
+ {
+ p->RestoreState(pile1, false);
+ p = p->GetNext(); // advance in the list
+ }
+
+ if( p != nullptr )
+ {
+ p->RestoreState(pile1, true);
+ return;
+ }
+}
diff --git a/src/CBot/CBotInstr/CBotSwitch.h b/src/CBot/CBotInstr/CBotSwitch.h
new file mode 100644
index 0000000..c3f5e02
--- /dev/null
+++ b/src/CBot/CBotInstr/CBotSwitch.h
@@ -0,0 +1,76 @@
+/*
+ * 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 CBotSwitch class. Compiles instruction "switch"
+ */
+class CBotSwitch : public CBotInstr
+{
+public:
+
+ /*!
+ * \brief CBotSwitch
+ */
+ CBotSwitch();
+
+ /*!
+ * \brief CBotSwitch
+ */
+ ~CBotSwitch();
+
+ /*!
+ * \brief Compile
+ * \param p
+ * \param pStack
+ * \return
+ */
+ static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack);
+
+ /*!
+ * \brief Execute Executes instruction "switch".
+ * \param pj
+ * \return
+ */
+ bool Execute(CBotStack* &pj) override;
+
+ /*!
+ * \brief RestoreState
+ * \param pj
+ * \param bMain
+ */
+ void RestoreState(CBotStack* &pj, bool bMain) override;
+
+private:
+
+ //! Value to seek
+ CBotInstr* m_Value;
+ //! Instructions
+ CBotInstr* m_Block;
+
+};
diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp
index a691c5a..3f77dbe 100644
--- a/src/CBot/CBotWhile.cpp
+++ b/src/CBot/CBotWhile.cpp
@@ -157,194 +157,6 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain)
}
}
-///////////////////////////////////////////////////////////////////////////
-// compiles instruction "switch"
-
-CBotSwitch::CBotSwitch()
-{
- m_Value =
- m_Block = nullptr; // nullptr so that delete is not possible further
- name = "CBotSwitch"; // debug
-}
-
-CBotSwitch::~CBotSwitch()
-{
- delete m_Value; // frees the value
- delete m_Block; // frees the instruction block
-}
-
-
-CBotInstr* CBotSwitch::Compile(CBotToken* &p, CBotCStack* pStack)
-{
- CBotSwitch* inst = new CBotSwitch(); // creates the object
- CBotToken* pp = p; // preserves at the ^ token (starting position)
-
- inst->SetToken(p);
- if (!IsOfType(p, ID_SWITCH)) return nullptr; // should never happen
-
- CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp
-
- if ( IsOfType(p, ID_OPENPAR ) )
- {
- if ( nullptr != (inst->m_Value = CBotExpression::Compile( p, pStk )) )
- {
- if ( pStk->GetType() < CBotTypLong )
- {
- if ( IsOfType(p, ID_CLOSEPAR ) )
- {
- if ( IsOfType(p, ID_OPBLK ) )
- {
- IncLvl();
-
- while( !IsOfType( p, ID_CLBLK ) )
- {
- if ( p->GetType() == ID_CASE || p->GetType() == ID_DEFAULT)
- {
- CBotCStack* pStk2 = pStk->TokenStack(p); // un petit bout de pile svp
-
- CBotInstr* i = CBotCase::Compile( p, pStk2 );
- if (i == nullptr)
- {
- delete inst;
- return pStack->Return(nullptr, pStk2);
- }
- delete pStk2;
- if ( inst->m_Block == nullptr ) inst->m_Block = i;
- else inst->m_Block->AddNext(i);
- continue;
- }
-
- if ( inst->m_Block == nullptr )
- {
- pStk->SetError(TX_NOCASE, p->GetStart());
- delete inst;
- return pStack->Return(nullptr, pStk);
- }
-
- CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true );
- if ( !pStk->IsOk() )
- {
- delete inst;
- return pStack->Return(nullptr, pStk);
- }
- inst->m_Block->AddNext(i);
-
- if ( p == nullptr )
- {
- pStk->SetError(TX_CLOSEBLK, -1);
- delete inst;
- return pStack->Return(nullptr, pStk);
- }
- }
- DecLvl();
-
- if ( inst->m_Block == nullptr )
- {
- pStk->SetError(TX_NOCASE, p->GetStart());
- delete inst;
- return pStack->Return(nullptr, pStk);
- }
- // the statement block is ok
- return pStack->Return(inst, pStk); // return an object to the application
- }
- pStk->SetError( TX_OPENBLK, p->GetStart() );
- }
- pStk->SetError( TX_CLOSEPAR, p->GetStart() );
- }
- pStk->SetError( TX_BADTYPE, p->GetStart() );
- }
- }
- pStk->SetError( TX_OPENPAR, p->GetStart());
-
- delete inst; // error, frees up
- return pStack->Return(nullptr, pStk); // no object, the error is on the stack
-}
-
-// executes instruction "switch"
-
-bool CBotSwitch :: Execute(CBotStack* &pj)
-{
- CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack
-// if ( pile1 == EOX ) return true;
-
- CBotInstr* p = m_Block; // first expression
-
- int state = pile1->GetState();
- if (state == 0)
- {
- if ( !m_Value->Execute(pile1) ) return false;
- pile1->SetState(state = -1);
- }
-
- if ( pile1->IfStep() ) return false;
-
- if ( state == -1 )
- {
- state = 0;
- int val = pile1->GetVal(); // result of the value
-
- CBotStack* pile2 = pile1->AddStack();
- while ( p != nullptr ) // search for the corresponding case in a list
- {
- state++;
- if ( p->CompCase( pile2, val ) ) break; // found the case
- p = p->GetNext();
- }
- pile2->Delete();
-
- if ( p == nullptr ) return pj->Return(pile1); // completed if nothing
-
- if ( !pile1->SetState(state) ) return false;
- }
-
- p = m_Block; // returns to the beginning
- while (state-->0) p = p->GetNext(); // advance in the list
-
- while( p != nullptr )
- {
- if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1);
- if ( !pile1->IncState() ) return false;
- p = p->GetNext();
- }
- return pj->Return(pile1);
-}
-
-void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain)
-{
- if ( !bMain ) return;
-
- CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack
- if ( pile1 == nullptr ) return;
-
- CBotInstr* p = m_Block; // first expression
-
- int state = pile1->GetState();
- if (state == 0)
- {
- m_Value->RestoreState(pile1, bMain);
- return;
- }
-
- if ( state == -1 )
- {
- return;
- }
-
-// p = m_Block; // returns to the beginning
- while ( p != nullptr && state-- > 0 )
- {
- p->RestoreState(pile1, false);
- p = p->GetNext(); // advance in the list
- }
-
- if( p != nullptr )
- {
- p->RestoreState(pile1, true);
- return;
- }
-}
-
-///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// compiles instruction "case"
diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt
index 030c4de..212a6dc 100644
--- a/src/CBot/CMakeLists.txt
+++ b/src/CBot/CMakeLists.txt
@@ -13,6 +13,7 @@ set(SOURCES
CBotInstr/CBotDo.cpp
CBotInstr/CBotFor.cpp
CBotInstr/CBotListExpression.cpp
+ CBotInstr/CBotSwitch.cpp
)
# Includes
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git
More information about the Pkg-games-commits
mailing list