2015-11-08 18:44:56 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotInstr/CBotCatch.h"
|
|
|
|
#include "CBot/CBotInstr/CBotBlock.h"
|
|
|
|
#include "CBot/CBotInstr/CBotExpression.h"
|
2015-11-08 18:44:56 +00:00
|
|
|
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotStack.h"
|
|
|
|
#include "CBot/CBotCStack.h"
|
2015-11-14 11:56:16 +00:00
|
|
|
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotVar/CBotVar.h"
|
2015-11-15 22:18:47 +00:00
|
|
|
|
2015-11-08 18:44:56 +00:00
|
|
|
// Local include
|
|
|
|
|
|
|
|
// Global include
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CBotCatch::CBotCatch()
|
|
|
|
{
|
|
|
|
m_Cond =
|
|
|
|
m_Block = nullptr; // nullptr so that delete is not possible further
|
|
|
|
m_next = nullptr;
|
|
|
|
|
|
|
|
name = "CBotCatch"; // debug
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CBotCatch::~CBotCatch()
|
|
|
|
{
|
|
|
|
delete m_Cond; // frees the list
|
|
|
|
delete m_Block; // frees the instruction block
|
|
|
|
delete m_next; // and subsequent
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CBotCatch* CBotCatch::Compile(CBotToken* &p, CBotCStack* pStack)
|
|
|
|
{
|
|
|
|
CBotCatch* inst = new CBotCatch(); // creates the object
|
|
|
|
pStack->SetStartError(p->GetStart());
|
|
|
|
|
|
|
|
inst->SetToken(p);
|
|
|
|
if (!IsOfType(p, ID_CATCH)) return nullptr; // should never happen
|
|
|
|
|
|
|
|
if (IsOfType(p, ID_OPENPAR))
|
|
|
|
{
|
|
|
|
inst->m_Cond = CBotExpression::Compile(p, pStack);
|
|
|
|
if (( pStack->GetType() < CBotTypLong ||
|
|
|
|
pStack->GetTypResult().Eq(CBotTypBoolean) )&& pStack->IsOk() )
|
|
|
|
{
|
|
|
|
if (IsOfType(p, ID_CLOSEPAR))
|
|
|
|
{
|
|
|
|
inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStack );
|
|
|
|
if ( pStack->IsOk() )
|
|
|
|
return inst; // return an object to the application
|
|
|
|
}
|
2015-12-20 18:01:03 +00:00
|
|
|
pStack->SetError(CBotErrClosePar, p->GetStart());
|
2015-11-08 18:44:56 +00:00
|
|
|
}
|
2015-12-20 18:01:03 +00:00
|
|
|
pStack->SetError(CBotErrBadType1, p->GetStart());
|
2015-11-08 18:44:56 +00:00
|
|
|
}
|
2015-12-20 18:01:03 +00:00
|
|
|
pStack->SetError(CBotErrOpenPar, p->GetStart());
|
2015-11-08 18:44:56 +00:00
|
|
|
delete inst; // error, frees up
|
|
|
|
return nullptr; // no object, the error is on the stack
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool CBotCatch :: Execute(CBotStack* &pj)
|
|
|
|
{
|
|
|
|
if ( m_Block == nullptr ) return true;
|
|
|
|
return m_Block->Execute(pj); // executes the associated block
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void CBotCatch :: RestoreState(CBotStack* &pj, bool bMain)
|
|
|
|
{
|
|
|
|
if ( bMain && m_Block != nullptr ) m_Block->RestoreState(pj, bMain);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void CBotCatch :: RestoreCondState(CBotStack* &pj, bool bMain)
|
|
|
|
{
|
|
|
|
m_Cond->RestoreState(pj, bMain);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool CBotCatch :: TestCatch(CBotStack* &pile, int val)
|
|
|
|
{
|
|
|
|
if ( !m_Cond->Execute(pile) ) return false;
|
|
|
|
|
|
|
|
if ( val > 0 || pile->GetType() != CBotTypBoolean )
|
|
|
|
{
|
|
|
|
CBotVar* var = CBotVar::Create(static_cast<CBotToken*>(nullptr), CBotTypBoolean);
|
|
|
|
var->SetValInt( pile->GetVal() == val );
|
|
|
|
pile->SetVar(var); // calls on the stack
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|