From 6be1f562889d62758947d825924f737036aec6ac Mon Sep 17 00:00:00 2001 From: melex750 Date: Sun, 20 Mar 2016 20:55:22 -0400 Subject: [PATCH] Add error code no expression, remove some bad code --- src/CBot/CBotClass.cpp | 6 +-- src/CBot/CBotEnums.h | 1 + src/CBot/CBotInstr/CBotDefArray.cpp | 4 +- src/CBot/CBotInstr/CBotDefBoolean.cpp | 2 +- src/CBot/CBotInstr/CBotDefClass.cpp | 2 +- src/CBot/CBotInstr/CBotDefFloat.cpp | 2 +- src/CBot/CBotInstr/CBotDefInt.cpp | 2 +- src/CBot/CBotInstr/CBotDefString.cpp | 2 +- src/CBot/CBotInstr/CBotListArray.cpp | 67 +++++---------------------- src/CBot/CBotInstr/CBotListArray.h | 2 +- src/common/restext.cpp | 1 + 11 files changed, 25 insertions(+), 66 deletions(-) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 5f761453..aca2d2b0 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -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) ) diff --git a/src/CBot/CBotEnums.h b/src/CBot/CBotEnums.h index 475648a3..24e0095b 100644 --- a/src/CBot/CBotEnums.h +++ b/src/CBot/CBotEnums.h @@ -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 diff --git a/src/CBot/CBotInstr/CBotDefArray.cpp b/src/CBot/CBotInstr/CBotDefArray.cpp index c70c8f9b..473fdc8d 100644 --- a/src/CBot/CBotInstr/CBotDefArray.cpp +++ b/src/CBot/CBotInstr/CBotDefArray.cpp @@ -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())) ) diff --git a/src/CBot/CBotInstr/CBotDefBoolean.cpp b/src/CBot/CBotInstr/CBotDefBoolean.cpp index 63b9d346..d64a1860 100644 --- a/src/CBot/CBotInstr/CBotDefBoolean.cpp +++ b/src/CBot/CBotInstr/CBotDefBoolean.cpp @@ -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 ))) diff --git a/src/CBot/CBotInstr/CBotDefClass.cpp b/src/CBot/CBotInstr/CBotDefClass.cpp index 72ec71ec..ab4ea991 100644 --- a/src/CBot/CBotInstr/CBotDefClass.cpp +++ b/src/CBot/CBotInstr/CBotDefClass.cpp @@ -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) diff --git a/src/CBot/CBotInstr/CBotDefFloat.cpp b/src/CBot/CBotInstr/CBotDefFloat.cpp index 33398fa2..5348a529 100644 --- a/src/CBot/CBotInstr/CBotDefFloat.cpp +++ b/src/CBot/CBotInstr/CBotDefFloat.cpp @@ -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 ))) diff --git a/src/CBot/CBotInstr/CBotDefInt.cpp b/src/CBot/CBotInstr/CBotDefInt.cpp index 9f7dcb61..babd3fda 100644 --- a/src/CBot/CBotInstr/CBotDefInt.cpp +++ b/src/CBot/CBotInstr/CBotDefInt.cpp @@ -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 ))) diff --git a/src/CBot/CBotInstr/CBotDefString.cpp b/src/CBot/CBotInstr/CBotDefString.cpp index 77e0119f..cb4502d9 100644 --- a/src/CBot/CBotInstr/CBotDefString.cpp +++ b/src/CBot/CBotInstr/CBotDefString.cpp @@ -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 ))) diff --git a/src/CBot/CBotInstr/CBotListArray.cpp b/src/CBot/CBotInstr/CBotListArray.cpp index ab25e058..bc6ac28d 100644 --- a/src/CBot/CBotInstr/CBotListArray.cpp +++ b/src/CBot/CBotInstr/CBotListArray.cpp @@ -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; diff --git a/src/CBot/CBotInstr/CBotListArray.h b/src/CBot/CBotInstr/CBotListArray.h index 5c9eafdf..329e5877 100644 --- a/src/CBot/CBotInstr/CBotListArray.h +++ b/src/CBot/CBotInstr/CBotListArray.h @@ -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. diff --git a/src/common/restext.cpp b/src/common/restext.cpp index 0139c1ed..9b492678 100644 --- a/src/common/restext.cpp +++ b/src/common/restext.cpp @@ -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");