diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 1ad497e4..976789cf 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -727,25 +727,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotLogicExpr : public CBotInstr -{ -private: - CBotInstr* m_condition; // test to evaluate - CBotInstr* m_op1; // left element - CBotInstr* m_op2; // right element - friend class CBotTwoOpExpr; - -public: - CBotLogicExpr(); - ~CBotLogicExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - - - // all operations with two operands diff --git a/src/CBot/CBotInstr/CBotLogicExpr.cpp b/src/CBot/CBotInstr/CBotLogicExpr.cpp new file mode 100644 index 00000000..bfa6af1a --- /dev/null +++ b/src/CBot/CBotInstr/CBotLogicExpr.cpp @@ -0,0 +1,92 @@ +/* + * 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 "CBotLogicExpr.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotLogicExpr::CBotLogicExpr() +{ + m_condition = + m_op1 = + m_op2 = nullptr; // nullptr to be able to delete without other + name = "CBotLogicExpr"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotLogicExpr::~CBotLogicExpr() +{ + delete m_condition; + delete m_op1; + delete m_op2; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotLogicExpr::Execute(CBotStack* &pStack) +{ + CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack + // or return in case of recovery +// if ( pStk1 == EOX ) return true; + + if ( pStk1->GetState() == 0 ) + { + if ( !m_condition->Execute(pStk1) ) return false; + if (!pStk1->SetState(1)) return false; + } + + if ( pStk1->GetVal() == true ) + { + if ( !m_op1->Execute(pStk1) ) return false; + } + else + { + if ( !m_op2->Execute(pStk1) ) return false; + } + + return pStack->Return(pStk1); // transmits the result +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotLogicExpr::RestoreState(CBotStack* &pStack, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pStk1 = pStack->RestoreStack(this); // adds an item to the stack + if ( pStk1 == nullptr ) return; + + if ( pStk1->GetState() == 0 ) + { + m_condition->RestoreState(pStk1, bMain); + return; + } + + if ( pStk1->GetVal() == true ) + { + m_op1->RestoreState(pStk1, bMain); + } + else + { + m_op2->RestoreState(pStk1, bMain); + } +} + diff --git a/src/CBot/CBotInstr/CBotLogicExpr.h b/src/CBot/CBotInstr/CBotLogicExpr.h new file mode 100644 index 00000000..2bffc243 --- /dev/null +++ b/src/CBot/CBotInstr/CBotLogicExpr.h @@ -0,0 +1,70 @@ +/* + * 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 CBotLogicExpr class + */ +class CBotLogicExpr : public CBotInstr +{ +public: + + /*! + * \brief CBotLogicExpr + */ + CBotLogicExpr(); + + /*! + * \brief ~CBotLogicExpr + */ + ~CBotLogicExpr(); + + /*! + * \brief Execute + * \param pStack + * \return + */ + bool Execute(CBotStack* &pStack) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + + //! Test to evaluate + CBotInstr* m_condition; + //! Left element + CBotInstr* m_op1; + //! Right element + CBotInstr* m_op2; + friend class CBotTwoOpExpr; +}; diff --git a/src/CBot/CBotTwoOpExpr.cpp b/src/CBot/CBotTwoOpExpr.cpp index 9f4cb2c5..d9173d60 100644 --- a/src/CBot/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotTwoOpExpr.cpp @@ -24,6 +24,7 @@ #include "CBot.h" #include "CBotInstr/CBotParExpr.h" +#include "CBotInstr/CBotLogicExpr.h" #include @@ -50,22 +51,6 @@ CBotTwoOpExpr::~CBotTwoOpExpr() delete m_rightop; } -CBotLogicExpr::CBotLogicExpr() -{ - m_condition = - m_op1 = - m_op2 = nullptr; // nullptr to be able to delete without other - name = "CBotLogicExpr"; // debug -} - -CBotLogicExpr::~CBotLogicExpr() -{ - delete m_condition; - delete m_op1; - delete m_op2; -} - - // type of operands accepted by operations #define ENTIER ((1<AddStack(this); // adds an item to the stack - // or return in case of recovery -// if ( pStk1 == EOX ) return true; - - if ( pStk1->GetState() == 0 ) - { - if ( !m_condition->Execute(pStk1) ) return false; - if (!pStk1->SetState(1)) return false; - } - - if ( pStk1->GetVal() == true ) - { - if ( !m_op1->Execute(pStk1) ) return false; - } - else - { - if ( !m_op2->Execute(pStk1) ) return false; - } - - return pStack->Return(pStk1); // transmits the result -} - -void CBotLogicExpr::RestoreState(CBotStack* &pStack, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pStk1 = pStack->RestoreStack(this); // adds an item to the stack - if ( pStk1 == nullptr ) return; - - if ( pStk1->GetState() == 0 ) - { - m_condition->RestoreState(pStk1, bMain); - return; - } - - if ( pStk1->GetVal() == true ) - { - m_op1->RestoreState(pStk1, bMain); - } - else - { - m_op2->RestoreState(pStk1, bMain); - } -} - #if 0 void t() { diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 697dd019..150724a9 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -36,6 +36,7 @@ set(SOURCES CBotInstr/CBotExprUnaire.cpp CBotInstr/CBotParExpr.cpp CBotInstr/CBotBoolExpr.cpp + CBotInstr/CBotLogicExpr.cpp ) # Includes