Add error code no expression, remove some bad code

dev-time-step
melex750 2016-03-20 20:55:22 -04:00
parent 5b3da83715
commit 6be1f56288
11 changed files with 25 additions and 66 deletions

View File

@ -566,7 +566,7 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
if ( p->GetType() != ID_CLBRK )
{
i = CBotExpression::Compile( p, pStack ); // expression for the value
if (i == nullptr || pStack->GetType() >= CBotTypBoolean) // must be a number
if (i == nullptr || pStack->GetType() != CBotTypInt) // must be a number
{
pStack->SetError(CBotErrBadIndex, p->GetStart());
return false;
@ -681,9 +681,9 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
if ( IsOfType(p, ID_ASS ) )
{
pStack->SetStartError(p->GetStart());
if ( IsOfTypeList(p, TokenTypVar, ID_NEW, ID_SEP, 0) ) // no var, new, or ';'
if ( IsOfType(p, ID_SEP) )
{
pStack->SetError(CBotErrBadLeft, p->GetPrev());
pStack->SetError(CBotErrNoExpression, p->GetPrev());
return false;
}
if ( type.Eq(CBotTypArrayPointer) )

View File

@ -235,6 +235,7 @@ enum CBotError : int
CBotErrBadIndex = 5040, //!< wrong index type "[ false ]"
CBotErrPrivate = 5041, //!< protected item
CBotErrNoPublic = 5042, //!< missing word "public"
CBotErrNoExpression = 5043, //!< expression expected after =
// Runtime errors
CBotErrZeroDiv = 6000, //!< division by zero

View File

@ -76,7 +76,7 @@ CBotInstr* CBotDefArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResul
if (p->GetType() != ID_CLBRK)
{
i = CBotExpression::Compile(p, pStk); // expression for the value
if (i == nullptr || pStk->GetType() >= CBotTypBoolean) // must be a number
if (i == nullptr || pStk->GetType() != CBotTypInt) // must be a number
{
pStk->SetError(CBotErrBadIndex, p->GetStart());
goto error;
@ -106,7 +106,7 @@ CBotInstr* CBotDefArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResul
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
pStk->SetError(CBotErrNoExpression, p->GetPrev());
goto error;
}
if ( nullptr == (inst->m_listass = CBotListArray::Compile(p, pStk, type.GetTypElem())) )

View File

@ -90,7 +90,7 @@ CBotInstr* CBotDefBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont,
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
pStk->SetError(CBotErrNoExpression, p->GetPrev());
goto error;
}
if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))

View File

@ -157,7 +157,7 @@ CBotInstr* CBotDefClass::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* p
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
pStk->SetError(CBotErrNoExpression, p->GetPrev());
goto error;
}
if (inst->m_hasParams)

View File

@ -89,7 +89,7 @@ CBotInstr* CBotDefFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, b
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
pStk->SetError(CBotErrNoExpression, p->GetPrev());
goto error;
}
if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))

View File

@ -93,7 +93,7 @@ CBotInstr* CBotDefInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, boo
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
pStk->SetError(CBotErrNoExpression, p->GetPrev());
goto error;
}
if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))

View File

@ -93,7 +93,7 @@ CBotInstr* CBotDefString::Compile(CBotToken* &p, CBotCStack* pStack, bool cont,
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
pStk->SetError(CBotErrNoExpression, p->GetPrev());
goto error;
}
if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))

View File

@ -45,7 +45,7 @@ CBotListArray::~CBotListArray()
}
////////////////////////////////////////////////////////////////////////////////
CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool classItem)
CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type)
{
CBotCStack* pStk = pStack->TokenStack(p);
@ -69,58 +69,33 @@ CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResu
if (type.Eq( CBotTypArrayPointer ))
{
pStk->SetStartError(p->GetStart());
if (p->GetType() == TokenTypVar)
if ( nullptr == (inst->m_expr = CBotListArray::Compile(p, pStk, type.GetTypElem())) )
{
if (!classItem)
if (pStk->IsOk())
{
inst->m_expr = CBotTwoOpExpr::Compile(p, pStk);
if (!pStk->GetTypResult().Compare(type)) // compatible type ?
if (inst->m_expr == nullptr || !pStk->GetTypResult().Compare(type)) // compatible type ?
{
pStk->SetError(CBotErrBadType1, p->GetStart());
goto error;
}
}
else
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
goto error;
}
}
else
{
if (nullptr == ( inst->m_expr = CBotListArray::Compile( p, pStk, type.GetTypElem() ) ))
{
goto error;
}
}
while (IsOfType( p, ID_COMMA )) // other elements?
{
pStk->SetStartError(p->GetStart());
CBotInstr* i = nullptr;
if (p->GetType() == TokenTypVar)
if ( nullptr == (i = CBotListArray::Compile(p, pStk, type.GetTypElem())) )
{
if (!classItem)
if (pStk->IsOk())
{
i = CBotTwoOpExpr::Compile(p, pStk);
if (nullptr == i || !pStk->GetTypResult().Compare(type)) // compatible type ?
if (i == nullptr || !pStk->GetTypResult().Compare(type)) // compatible type ?
{
pStk->SetError(CBotErrBadType1, p->GetStart());
goto error;
}
}
else
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
goto error;
}
}
else
{
i = CBotListArray::Compile(p, pStk, type.GetTypElem());
if (nullptr == i)
{
goto error;
}
}
inst->m_expr->AddNext3(i);
if ( p->GetType() == ID_COMMA ) continue;
@ -133,23 +108,15 @@ CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResu
else
{
pStk->SetStartError(p->GetStart());
if (classItem && IsOfType(p, TokenTypVar, ID_NEW)) // don't allow func() or var between "{ }"
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
goto error;
}
if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))
{
goto error;
}
CBotVar* pv = pStk->GetVar(); // result of the expression
CBotTypResult retType;
if (pv != nullptr) retType = pv->GetTypResult(CBotVar::GetTypeMode::CLASS_AS_INTRINSIC);
if (pv == nullptr || (type.Eq(CBotTypString) && !retType.Eq(CBotTypString)) ||
(!(type.Eq(CBotTypPointer) && retType.Eq(CBotTypNullPointer)) &&
!TypesCompatibles( type, pv->GetTypResult()) &&
!TypeCompatible(type, retType, ID_ASS) ) ) // compatible type?
if (pv == nullptr || (!TypesCompatibles( type, pv->GetTypResult()) &&
!(type.Eq(CBotTypPointer) && pv->GetTypResult().Eq(CBotTypNullPointer))) ) // compatible type?
{
pStk->SetError(CBotErrBadType1, p->GetStart());
goto error;
@ -159,12 +126,6 @@ CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResu
{
pStk->SetStartError(p->GetStart());
if (classItem && IsOfType(p, TokenTypVar, ID_NEW)) // don't allow func() or var between "{ }"
{
pStk->SetError(CBotErrBadLeft, p);
goto error;
}
CBotInstr* i = CBotTwoOpExpr::Compile(p, pStk) ;
if (nullptr == i)
{
@ -172,13 +133,9 @@ CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResu
}
CBotVar* pv = pStk->GetVar(); // result of the expression
CBotTypResult retType;
if (pv != nullptr) retType = pv->GetTypResult(CBotVar::GetTypeMode::CLASS_AS_INTRINSIC);
if (pv == nullptr || (type.Eq(CBotTypString) && !retType.Eq(CBotTypString)) ||
(!(type.Eq(CBotTypPointer) && retType.Eq(CBotTypNullPointer)) &&
!TypesCompatibles( type, pv->GetTypResult()) &&
!TypeCompatible(type, retType, ID_ASS) ) ) // compatible type?
if (pv == nullptr || (!TypesCompatibles( type, pv->GetTypResult()) &&
!(type.Eq(CBotTypPointer) && pv->GetTypResult().Eq(CBotTypNullPointer))) ) // compatible type?
{
pStk->SetError(CBotErrBadType1, p->GetStart());
goto error;

View File

@ -41,7 +41,7 @@ public:
* \param classItem
* \return
*/
static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool classItem = false);
static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type);
/*!
* \brief Execute Executes the definition of an array.

View File

@ -715,6 +715,7 @@ void InitializeRestext()
stringsCbot[CBot::CBotErrBadIndex] = TR("Incorrect index type");
stringsCbot[CBot::CBotErrPrivate] = TR("Private element");
stringsCbot[CBot::CBotErrNoPublic] = TR("Public required");
stringsCbot[CBot::CBotErrNoExpression] = TR("expression expected after =");
stringsCbot[CBot::CBotErrZeroDiv] = TR("Dividing by zero");
stringsCbot[CBot::CBotErrNotInit] = TR("Variable not initialized");