colobot/src/CBot/CBotInstr/CBotWhile.cpp

166 lines
5.5 KiB
C++
Raw Normal View History

/*
* This file is part of the Colobot: Gold Edition source code
2016-02-13 13:11:30 +00:00
* Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
2015-08-22 14:40:02 +00:00
* 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
*/
2012-08-02 22:50:25 +00:00
#include "CBot/CBotInstr/CBotWhile.h"
#include "CBot/CBotInstr/CBotBlock.h"
#include "CBot/CBotInstr/CBotCondition.h"
2012-08-02 22:50:25 +00:00
#include "CBot/CBotStack.h"
#include "CBot/CBotCStack.h"
2015-12-26 13:19:24 +00:00
namespace CBot
{
2012-08-02 22:50:25 +00:00
////////////////////////////////////////////////////////////////////////////////
2012-08-02 22:50:25 +00:00
CBotWhile::CBotWhile()
{
m_condition = nullptr;
m_block = nullptr;
2012-08-02 22:50:25 +00:00
}
////////////////////////////////////////////////////////////////////////////////
2012-08-02 22:50:25 +00:00
CBotWhile::~CBotWhile()
{
delete m_condition; // frees the condition
delete m_block; // releases the block instruction
2012-08-02 22:50:25 +00:00
}
////////////////////////////////////////////////////////////////////////////////
2012-08-02 22:50:25 +00:00
CBotInstr* CBotWhile::Compile(CBotToken* &p, CBotCStack* pStack)
{
2012-08-08 20:35:17 +00:00
CBotWhile* inst = new CBotWhile(); // creates the object
CBotToken* pp = p; // preserves at the ^ token (starting position)
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
if ( IsOfType( p, TokenTypVar ) &&
IsOfType( p, ID_DOTS ) )
{
2012-08-11 18:59:35 +00:00
inst->m_label = pp->GetString(); // records the name of the label
2012-08-08 20:35:17 +00:00
}
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
inst->SetToken(p);
2015-08-16 10:43:42 +00:00
if (!IsOfType(p, ID_WHILE)) return nullptr; // should never happen
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp
2012-08-02 22:50:25 +00:00
// a bit of battery please (??)
if ( nullptr != (inst->m_condition = CBotCondition::Compile(p, pStk )) )
2012-08-08 20:35:17 +00:00
{
// the condition exists
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
IncLvl(inst->m_label);
inst->m_block = CBotBlock::CompileBlkOrInst(p, pStk, true );
2012-08-08 20:35:17 +00:00
DecLvl();
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
if ( pStk->IsOk() )
{
// the statement block is ok (it may be empty!
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
return pStack->Return(inst, pStk); // return an object to the application
2012-08-02 22:50:25 +00:00
// makes the object to which the application
2012-08-08 20:35:17 +00:00
}
}
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
delete inst; // error, frees the place
2015-08-16 10:43:42 +00:00
return pStack->Return(nullptr, pStk); // no object, the error is on the stack
2012-08-02 22:50:25 +00:00
}
////////////////////////////////////////////////////////////////////////////////
bool CBotWhile::Execute(CBotStack* &pj)
2012-08-02 22:50:25 +00:00
{
2012-08-08 20:35:17 +00:00
CBotStack* pile = pj->AddStack(this); // adds an item to the stack
// or find in case of recovery
// if ( pile == EOX ) return true;
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
if ( pile->IfStep() ) return false;
2012-08-02 22:50:25 +00:00
2012-08-11 18:59:35 +00:00
while( true ) switch( pile->GetState() ) // executes the loop
2012-08-08 20:35:17 +00:00
{ // there are two possible states (depending on recovery)
case 0:
// evaluates the condition
if ( !m_condition->Execute(pile) ) return false; // interrupted here?
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
// the result of the condition is on the stack
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
// terminates if an error or if the condition is false
2012-08-11 18:59:35 +00:00
if ( !pile->IsOk() || pile->GetVal() != true )
2012-08-08 20:35:17 +00:00
{
return pj->Return(pile); // sends the results and releases the stack
}
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
// the condition is true, pass in the second mode
2012-08-02 22:50:25 +00:00
2012-08-08 20:35:17 +00:00
if (!pile->SetState(1)) return false; // ready for further
2012-08-08 20:35:17 +00:00
case 1:
// evaluates the associated statement block
if (m_block != nullptr &&
!m_block->Execute(pile) )
2012-08-08 20:35:17 +00:00
{
if (pile->IfContinue(0, m_label)) continue; // if continued, will return to test
return pj->BreakReturn(pile, m_label); // sends the results and releases the stack
}
2012-08-08 20:35:17 +00:00
// terminates if there is an error
if ( !pile->IsOk() )
{
return pj->Return(pile); // sends the results and releases the stack
}
// returns to the test again
if (!pile->SetState(0, 0)) return false;
continue;
2012-08-08 20:35:17 +00:00
}
2012-08-02 22:50:25 +00:00
}
////////////////////////////////////////////////////////////////////////////////
void CBotWhile::RestoreState(CBotStack* &pj, bool bMain)
2012-08-02 22:50:25 +00:00
{
2012-08-08 20:35:17 +00:00
if ( !bMain ) return;
CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack
2015-08-16 10:43:42 +00:00
if ( pile == nullptr ) return;
2012-08-08 20:35:17 +00:00
switch( pile->GetState() )
2012-08-08 20:35:17 +00:00
{ // there are two possible states (depending on recovery)
case 0:
// evaluates the condition
m_condition->RestoreState(pile, bMain);
2012-08-08 20:35:17 +00:00
return;
2012-08-08 20:35:17 +00:00
case 1:
// evaluates the associated statement block
if (m_block != nullptr ) m_block->RestoreState(pile, bMain);
2012-08-08 20:35:17 +00:00
return;
}
2012-08-02 22:50:25 +00:00
}
2015-12-26 13:19:24 +00:00
std::string CBotWhile::GetDebugData()
{
return !m_label.empty() ? "m_label = "+m_label : "";
}
std::map<std::string, CBotInstr*> CBotWhile::GetDebugLinks()
{
auto links = CBotInstr::GetDebugLinks();
links["m_condition"] = m_condition;
links["m_block"] = m_block;
return links;
}
2015-12-26 13:19:24 +00:00
} // namespace CBot