Moving CBotBreak class in its own header and source files.

dev-time-step
Grunaka 2015-11-08 19:20:06 +01:00
parent 6d2fbf3ea4
commit 8da5c675a4
6 changed files with 170 additions and 81 deletions

View File

@ -39,6 +39,7 @@
#include "CBotInstr/CBotDo.h"
#include "CBotInstr/CBotFor.h"
#include "CBotInstr/CBotSwitch.h"
#include "CBotInstr/CBotBreak.h"
// Local include

View File

@ -483,20 +483,6 @@ public:
void RestoreState(CBotStack* &pj, bool bMain) override;
};
class CBotBreak : public CBotInstr
{
private:
CBotString m_label; // a label if there is
public:
CBotBreak();
~CBotBreak();
static
CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack);
bool Execute(CBotStack* &pj) override;
void RestoreState(CBotStack* &pj, bool bMain) override;
};
class CBotReturn : public CBotInstr
{
private:

View File

@ -0,0 +1,93 @@
/*
* 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 "CBotBreak.h"
// Local include
// Global include
////////////////////////////////////////////////////////////////////////////////
CBotBreak::CBotBreak()
{
name = "CBotBreak"; // debug
}
////////////////////////////////////////////////////////////////////////////////
CBotBreak::~CBotBreak()
{
}
////////////////////////////////////////////////////////////////////////////////
CBotInstr* CBotBreak::Compile(CBotToken* &p, CBotCStack* pStack)
{
CBotToken* pp = p; // preserves at the ^ token (starting position)
int type = p->GetType();
if (!IsOfType(p, ID_BREAK, ID_CONTINUE)) return nullptr; // should never happen
if ( !ChkLvl(CBotString(), type ) )
{
pStack->SetError(TX_BREAK, pp);
return nullptr; // no object, the error is on the stack
}
CBotBreak* inst = new CBotBreak(); // creates the object
inst->SetToken(pp); // keeps the operation
pp = p;
if ( IsOfType( p, TokenTypVar ) )
{
inst->m_label = pp->GetString(); // register the name of label
if ( !ChkLvl(inst->m_label, type ) )
{
delete inst;
pStack->SetError(TX_NOLABEL, pp);
return nullptr; // no object, the error is on the stack
}
}
if (IsOfType(p, ID_SEP))
{
return inst; // return what it wants
}
delete inst;
pStack->SetError(TX_ENDOF, p->GetStart());
return nullptr; // no object, the error is on the stack
}
////////////////////////////////////////////////////////////////////////////////
bool CBotBreak :: Execute(CBotStack* &pj)
{
CBotStack* pile = pj->AddStack(this);
// if ( pile == EOX ) return true;
if ( pile->IfStep() ) return false;
pile->SetBreak(m_token.GetType()==ID_BREAK ? 1 : 2, m_label);
return pj->Return(pile);
}
////////////////////////////////////////////////////////////////////////////////
void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain)
{
if ( bMain ) pj->RestoreStack(this);
}

View File

@ -0,0 +1,75 @@
/*
* 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 CBotBreak class Compiles instruction "break" or "continu".
*/
class CBotBreak : public CBotInstr
{
public:
/*!
* \brief CBotBreak
*/
CBotBreak();
/*!
* \brief CBotBreak
*/
~CBotBreak();
/*!
* \brief Compile
* \param p
* \param pStack
* \return
*/
static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack);
/*!
* \brief Execute Execution of statement "break" or "continu"
* \param pj
* \return
*/
bool Execute(CBotStack* &pj) override;
/*!
* \brief RestoreState
* \param pj
* \param bMain
*/
void RestoreState(CBotStack* &pj, bool bMain) override;
private:
//! A label if there is
CBotString m_label;
};

View File

@ -157,73 +157,6 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain)
}
}
///////////////////////////////////////////////////////////////////////////
// compiles instruction "break" or "continu"
CBotBreak::CBotBreak()
{
name = "CBotBreak"; // debug
}
CBotBreak::~CBotBreak()
{
}
CBotInstr* CBotBreak::Compile(CBotToken* &p, CBotCStack* pStack)
{
CBotToken* pp = p; // preserves at the ^ token (starting position)
int type = p->GetType();
if (!IsOfType(p, ID_BREAK, ID_CONTINUE)) return nullptr; // should never happen
if ( !ChkLvl(CBotString(), type ) )
{
pStack->SetError(TX_BREAK, pp);
return nullptr; // no object, the error is on the stack
}
CBotBreak* inst = new CBotBreak(); // creates the object
inst->SetToken(pp); // keeps the operation
pp = p;
if ( IsOfType( p, TokenTypVar ) )
{
inst->m_label = pp->GetString(); // register the name of label
if ( !ChkLvl(inst->m_label, type ) )
{
delete inst;
pStack->SetError(TX_NOLABEL, pp);
return nullptr; // no object, the error is on the stack
}
}
if (IsOfType(p, ID_SEP))
{
return inst; // return what it wants
}
delete inst;
pStack->SetError(TX_ENDOF, p->GetStart());
return nullptr; // no object, the error is on the stack
}
// execution of statement "break" or "continu"
bool CBotBreak :: Execute(CBotStack* &pj)
{
CBotStack* pile = pj->AddStack(this);
// if ( pile == EOX ) return true;
if ( pile->IfStep() ) return false;
pile->SetBreak(m_token.GetType()==ID_BREAK ? 1 : 2, m_label);
return pj->Return(pile);
}
void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain)
{
if ( bMain ) pj->RestoreStack(this);
}
///////////////////////////////////////////////////////////////////////////

View File

@ -15,6 +15,7 @@ set(SOURCES
CBotInstr/CBotListExpression.cpp
CBotInstr/CBotSwitch.cpp
CBotInstr/CBotCase.cpp
CBotInstr/CBotBreak.cpp
)
# Includes