2015-11-08 18:12:03 +00:00
|
|
|
/*
|
|
|
|
* 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-11-08 18:12:03 +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
|
|
|
|
*/
|
|
|
|
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotInstr/CBotCase.h"
|
2015-12-31 13:44:19 +00:00
|
|
|
|
|
|
|
#include "CBot/CBotInstr/CBotExprLitNum.h"
|
2015-11-14 11:56:16 +00:00
|
|
|
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotStack.h"
|
|
|
|
#include "CBot/CBotCStack.h"
|
2015-11-08 18:12:03 +00:00
|
|
|
|
2015-12-26 13:19:24 +00:00
|
|
|
namespace CBot
|
|
|
|
{
|
2015-11-08 18:12:03 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CBotCase::CBotCase()
|
|
|
|
{
|
2015-12-27 15:51:57 +00:00
|
|
|
m_value = nullptr;
|
2015-11-08 18:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CBotCase::~CBotCase()
|
|
|
|
{
|
2015-12-27 15:51:57 +00:00
|
|
|
delete m_value;
|
2015-11-08 18:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CBotInstr* CBotCase::Compile(CBotToken* &p, CBotCStack* pStack)
|
|
|
|
{
|
|
|
|
CBotCase* inst = new CBotCase(); // creates the object
|
|
|
|
CBotToken* pp = p; // preserves at the ^ token (starting position)
|
|
|
|
|
|
|
|
inst->SetToken(p);
|
|
|
|
if (!IsOfType(p, ID_CASE, ID_DEFAULT)) return nullptr; // should never happen
|
|
|
|
|
|
|
|
if ( pp->GetType() == ID_CASE )
|
|
|
|
{
|
|
|
|
pp = p;
|
2015-12-31 13:44:19 +00:00
|
|
|
inst->m_value = CBotExprLitNum::Compile(p, pStack);
|
2015-12-27 15:51:57 +00:00
|
|
|
if (inst->m_value == nullptr )
|
2015-11-08 18:12:03 +00:00
|
|
|
{
|
2015-12-20 18:01:03 +00:00
|
|
|
pStack->SetError( CBotErrBadNum, pp );
|
2015-11-08 18:12:03 +00:00
|
|
|
delete inst;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !IsOfType( p, ID_DOTS ))
|
|
|
|
{
|
2015-12-20 18:01:03 +00:00
|
|
|
pStack->SetError( CBotErrNoDoubleDots, p->GetStart() );
|
2015-11-08 18:12:03 +00:00
|
|
|
delete inst;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool CBotCase::Execute(CBotStack* &pj)
|
|
|
|
{
|
|
|
|
return true; // the "case" statement does nothing!
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void CBotCase::RestoreState(CBotStack* &pj, bool bMain)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool CBotCase::CompCase(CBotStack* &pile, int val)
|
|
|
|
{
|
2015-12-27 15:51:57 +00:00
|
|
|
if (m_value == nullptr ) return true; // "default" case
|
2015-11-08 18:12:03 +00:00
|
|
|
|
2015-12-27 15:51:57 +00:00
|
|
|
while (!m_value->Execute(pile)); // puts the value on the correspondent stack (without interruption)
|
2015-11-08 18:12:03 +00:00
|
|
|
return (pile->GetVal() == val); // compared with the given value
|
|
|
|
}
|
2015-12-26 13:19:24 +00:00
|
|
|
|
2015-12-27 15:51:57 +00:00
|
|
|
std::map<std::string, CBotInstr*> CBotCase::GetDebugLinks()
|
|
|
|
{
|
|
|
|
auto links = CBotInstr::GetDebugLinks();
|
|
|
|
links["m_value"] = m_value;
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
2015-12-26 13:19:24 +00:00
|
|
|
} // namespace CBot
|