From accfc9357372fd87a71723ebdc3d0210cce2a92c Mon Sep 17 00:00:00 2001 From: melex750 Date: Sun, 20 Mar 2016 07:50:42 -0400 Subject: [PATCH] Add syntax and type checking for class member vars --- src/CBot/CBotClass.cpp | 44 ++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index d5007d83..5f761453 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -19,6 +19,7 @@ #include "CBot/CBotClass.h" +#include "CBot/CBotInstr/CBotInstrUtils.h" #include "CBot/CBotInstr/CBotNew.h" #include "CBot/CBotInstr/CBotLeftExprVar.h" #include "CBot/CBotInstr/CBotTwoOpExpr.h" @@ -561,29 +562,27 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) while ( IsOfType( p, ID_OPBRK ) ) // a table? { CBotInstr* i = nullptr; - + pStack->SetStartError( p->GetStart() ); if ( p->GetType() != ID_CLBRK ) + { i = CBotExpression::Compile( p, pStack ); // expression for the value + if (i == nullptr || pStack->GetType() >= CBotTypBoolean) // must be a number + { + pStack->SetError(CBotErrBadIndex, p->GetStart()); + return false; + } + } else i = new CBotEmpty(); // special if not a formula type = CBotTypResult(CBotTypArrayPointer, type); - if (!pStack->IsOk() || !IsOfType( p, ID_CLBRK ) ) - { - pStack->SetError(CBotErrCloseIndex, p->GetStart()); - return false; - } - -/* CBotVar* pv = pStack->GetVar(); - if ( pv->GetType()>= CBotTypBoolean ) - { - pStack->SetError(CBotErrBadType1, p->GetStart()); - return false; - }*/ - if (limites == nullptr) limites = i; else limites->AddNext3(i); + + if (IsOfType(p, ID_CLBRK)) continue; + pStack->SetError(CBotErrCloseIndex, p->GetStart()); + return false; } if ( p->GetType() == ID_OPENPAR ) @@ -681,6 +680,12 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) CBotInstr* i = nullptr; if ( IsOfType(p, ID_ASS ) ) { + pStack->SetStartError(p->GetStart()); + if ( IsOfTypeList(p, TokenTypVar, ID_NEW, ID_SEP, 0) ) // no var, new, or ';' + { + pStack->SetError(CBotErrBadLeft, p->GetPrev()); + return false; + } if ( type.Eq(CBotTypArrayPointer) ) { i = CBotListArray::Compile(p, pStack, type.GetTypElem()); @@ -689,6 +694,17 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) { // it has an assignmet to calculate i = CBotTwoOpExpr::Compile(p, pStack); + + CBotTypResult retType = pStack->GetTypResult(CBotVar::GetTypeMode::CLASS_AS_INTRINSIC); + + if ( (type.Eq(CBotTypString) && !retType.Eq(CBotTypString)) || + (!(type.Eq(CBotTypPointer) && retType.Eq(CBotTypNullPointer)) && + !TypeCompatible( type, retType, ID_ASS)) ) + + { + pStack->SetError(CBotErrBadType1, p->GetPrev()); + return false; + } } if ( !pStack->IsOk() ) return false; }