2015-11-11 21:16:38 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
2020-07-07 08:19:36 +00:00
|
|
|
* Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
|
2015-11-11 21:16:38 +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-12-27 15:51:57 +00:00
|
|
|
#include <sstream>
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotInstr/CBotLeftExpr.h"
|
|
|
|
#include "CBot/CBotInstr/CBotFieldExpr.h"
|
|
|
|
#include "CBot/CBotInstr/CBotIndexExpr.h"
|
|
|
|
#include "CBot/CBotInstr/CBotExpression.h"
|
2015-11-11 21:16:38 +00:00
|
|
|
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotStack.h"
|
|
|
|
#include "CBot/CBotCStack.h"
|
|
|
|
#include "CBot/CBotClass.h"
|
2015-11-14 11:56:16 +00:00
|
|
|
|
2015-11-23 20:59:56 +00:00
|
|
|
#include "CBot/CBotVar/CBotVarArray.h"
|
2015-11-15 16:04:27 +00:00
|
|
|
|
2015-12-26 13:19:24 +00:00
|
|
|
namespace CBot
|
|
|
|
{
|
2015-11-11 21:16:38 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CBotLeftExpr::CBotLeftExpr()
|
|
|
|
{
|
|
|
|
m_nIdent = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CBotLeftExpr::~CBotLeftExpr()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack)
|
|
|
|
{
|
|
|
|
CBotCStack* pStk = pStack->TokenStack();
|
|
|
|
|
|
|
|
pStk->SetStartError(p->GetStart());
|
|
|
|
|
|
|
|
// is it a variable name?
|
|
|
|
if (p->GetType() == TokenTypVar)
|
|
|
|
{
|
|
|
|
CBotLeftExpr* inst = new CBotLeftExpr(); // creates the object
|
|
|
|
|
|
|
|
inst->SetToken(p);
|
|
|
|
|
|
|
|
CBotVar* var;
|
|
|
|
|
|
|
|
if (nullptr != (var = pStk->FindVar(p))) // seek if known variable
|
|
|
|
{
|
|
|
|
inst->m_nIdent = var->GetUniqNum();
|
|
|
|
if (inst->m_nIdent > 0 && inst->m_nIdent < 9000)
|
|
|
|
{
|
2017-01-16 16:38:34 +00:00
|
|
|
if (CBotFieldExpr::CheckProtectionError(pStk, nullptr, var, true))
|
2015-11-11 21:16:38 +00:00
|
|
|
{
|
2015-12-20 18:01:03 +00:00
|
|
|
pStk->SetError(CBotErrPrivate, p);
|
2015-11-11 21:16:38 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
// this is an element of the current class
|
|
|
|
// adds the equivalent of this. before
|
|
|
|
CBotToken pthis("this");
|
2017-10-24 08:46:07 +00:00
|
|
|
// invisible 'this.' highlights member token on error
|
|
|
|
pthis.SetPos(p->GetStart(), p->GetEnd());
|
2015-11-11 21:16:38 +00:00
|
|
|
inst->SetToken(&pthis);
|
|
|
|
inst->m_nIdent = -2; // indent for this
|
|
|
|
|
|
|
|
CBotFieldExpr* i = new CBotFieldExpr(); // new element
|
|
|
|
i->SetToken(p); // keeps the name of the token
|
|
|
|
inst->AddNext3(i); // add after
|
|
|
|
|
|
|
|
var = pStk->FindVar(pthis);
|
|
|
|
var = var->GetItem(p->GetString());
|
|
|
|
i->SetUniqNum(var->GetUniqNum());
|
|
|
|
}
|
|
|
|
p = p->GetNext(); // next token
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (var->GetType() == CBotTypArrayPointer)
|
|
|
|
{
|
|
|
|
if (IsOfType( p, ID_OPBRK ))
|
|
|
|
{
|
|
|
|
CBotIndexExpr* i = new CBotIndexExpr();
|
|
|
|
i->m_expr = CBotExpression::Compile(p, pStk);
|
|
|
|
inst->AddNext3(i); // add to the chain
|
|
|
|
|
|
|
|
var = (static_cast<CBotVarArray*>(var))->GetItem(0,true); // gets the component [0]
|
|
|
|
|
|
|
|
if (i->m_expr == nullptr)
|
|
|
|
{
|
2015-12-20 18:01:03 +00:00
|
|
|
pStk->SetError(CBotErrBadIndex, p->GetStart());
|
2015-11-11 21:16:38 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK ))
|
|
|
|
{
|
2015-12-20 18:01:03 +00:00
|
|
|
pStk->SetError(CBotErrCloseIndex, p->GetStart());
|
2015-11-11 21:16:38 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 18:16:54 +00:00
|
|
|
if (var->GetType(CBotVar::GetTypeMode::CLASS_AS_POINTER) == CBotTypPointer) // for classes
|
2015-11-11 21:16:38 +00:00
|
|
|
{
|
|
|
|
if (IsOfType(p, ID_DOT))
|
|
|
|
{
|
|
|
|
CBotToken* pp = p;
|
|
|
|
|
|
|
|
CBotFieldExpr* i = new CBotFieldExpr(); // new element
|
|
|
|
i->SetToken(pp); // keeps the name of the token
|
|
|
|
inst->AddNext3(i); // adds after
|
|
|
|
|
|
|
|
if (p->GetType() == TokenTypVar) // must be a name
|
|
|
|
{
|
2016-09-17 12:00:34 +00:00
|
|
|
CBotVar* preVar = var;
|
2015-11-11 21:16:38 +00:00
|
|
|
var = var->GetItem(p->GetString()); // get item correspondent
|
|
|
|
if (var != nullptr)
|
|
|
|
{
|
2017-01-16 16:38:34 +00:00
|
|
|
if (CBotFieldExpr::CheckProtectionError(pStk, preVar, var, true))
|
2015-11-11 21:16:38 +00:00
|
|
|
{
|
2015-12-20 18:01:03 +00:00
|
|
|
pStk->SetError(CBotErrPrivate, pp);
|
2015-11-11 21:16:38 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
i->SetUniqNum(var->GetUniqNum());
|
|
|
|
p = p->GetNext(); // skips the name
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-20 18:01:03 +00:00
|
|
|
pStk->SetError(CBotErrUndefItem, p);
|
2015-11-11 21:16:38 +00:00
|
|
|
}
|
2015-12-20 18:01:03 +00:00
|
|
|
pStk->SetError(CBotErrUndefClass, p->GetStart());
|
2015-11-11 21:16:38 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (pStk->IsOk()) return static_cast<CBotLeftExpr*> (pStack->Return(inst, pStk));
|
|
|
|
}
|
2015-12-20 18:01:03 +00:00
|
|
|
pStk->SetError(CBotErrUndefVar, p);
|
2015-11-11 21:16:38 +00:00
|
|
|
err:
|
|
|
|
delete inst;
|
|
|
|
return static_cast<CBotLeftExpr*> ( pStack->Return(nullptr, pStk));
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<CBotLeftExpr*> ( pStack->Return(nullptr, pStk));
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array)
|
|
|
|
{
|
|
|
|
CBotStack* pile = pj->AddStack();
|
|
|
|
|
|
|
|
CBotVar* var1 = nullptr;
|
|
|
|
CBotVar* var2 = nullptr;
|
|
|
|
// fetch a variable (not copy)
|
|
|
|
if (!ExecuteVar(var1, array, nullptr, false)) return false;
|
|
|
|
|
|
|
|
if (pile->IfStep()) return false;
|
|
|
|
|
|
|
|
if (var1)
|
|
|
|
{
|
|
|
|
var2 = pj->GetVar(); // result on the input stack
|
|
|
|
if (var2)
|
|
|
|
{
|
|
|
|
CBotTypResult t1 = var1->GetTypResult();
|
|
|
|
CBotTypResult t2 = var2->GetTypResult();
|
|
|
|
if (t2.Eq(CBotTypPointer))
|
|
|
|
{
|
|
|
|
CBotClass* c1 = t1.GetClass();
|
2016-08-04 05:06:37 +00:00
|
|
|
CBotClass* c2 = var2->GetClass();
|
2015-11-11 21:16:38 +00:00
|
|
|
if ( !c2->IsChildOf(c1))
|
|
|
|
{
|
|
|
|
CBotToken* pt = &m_token;
|
2015-12-20 18:01:03 +00:00
|
|
|
pile->SetError(CBotErrBadType1, pt);
|
2015-11-11 21:16:38 +00:00
|
|
|
return pj->Return(pile); // operation performed
|
|
|
|
}
|
2016-08-04 05:06:37 +00:00
|
|
|
var1->SetVal(var2); // set pointer
|
|
|
|
var1->SetType(t1); // keep pointer type
|
2015-11-11 21:16:38 +00:00
|
|
|
}
|
2016-08-04 05:06:37 +00:00
|
|
|
else
|
|
|
|
var1->SetVal(var2); // do assignment
|
2015-11-11 21:16:38 +00:00
|
|
|
}
|
|
|
|
pile->SetCopyVar(var1); // replace the stack with the copy of the variable
|
|
|
|
// (for name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pj->Return(pile); // operation performed
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile)
|
|
|
|
{
|
|
|
|
pVar = pile->FindVar(m_token);
|
|
|
|
if (pVar == nullptr) return false;
|
|
|
|
|
|
|
|
if ( m_next3 != nullptr &&
|
|
|
|
!m_next3->ExecuteVar(pVar, pile) ) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep)
|
|
|
|
{
|
|
|
|
pile = pile->AddStack(this);
|
|
|
|
|
2015-12-24 13:39:38 +00:00
|
|
|
pVar = pile->FindVar(m_nIdent, false);
|
2015-11-11 21:16:38 +00:00
|
|
|
if (pVar == nullptr)
|
|
|
|
{
|
2015-12-31 15:10:47 +00:00
|
|
|
assert(false);
|
|
|
|
//pile->SetError(static_cast<CBotError>(2), &m_token); // TODO: yup, another unknown error ~krzys_h
|
2015-11-11 21:16:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bStep && m_next3 == nullptr && pile->IfStep()) return false;
|
|
|
|
|
|
|
|
if ( m_next3 != nullptr &&
|
|
|
|
!m_next3->ExecuteVar(pVar, pile, &m_token, bStep, true) ) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain)
|
|
|
|
{
|
|
|
|
pile = pile->RestoreStack(this);
|
|
|
|
if (pile == nullptr) return;
|
|
|
|
|
|
|
|
if (m_next3 != nullptr)
|
|
|
|
m_next3->RestoreStateVar(pile, bMain);
|
|
|
|
}
|
2015-12-26 13:19:24 +00:00
|
|
|
|
2015-12-27 15:51:57 +00:00
|
|
|
std::string CBotLeftExpr::GetDebugData()
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << m_token.GetString();
|
|
|
|
//ss << "VarID = " << m_nIdent;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2015-12-26 13:19:24 +00:00
|
|
|
} // namespace CBot
|