Fix syntax+type checking, base types+CBotDefArray

dev-time-step
melex750 2016-03-20 07:54:41 -04:00
parent accfc93573
commit 707ef97626
6 changed files with 66 additions and 51 deletions

View File

@ -72,19 +72,26 @@ CBotInstr* CBotDefArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResul
CBotInstr* i;
while (IsOfType(p, ID_OPBRK))
{
pStk->SetStartError(p->GetStart());
if (p->GetType() != ID_CLBRK)
i = CBotExpression::Compile(p, pStk); // expression for the value
{
i = CBotExpression::Compile(p, pStk); // expression for the value
if (i == nullptr || pStk->GetType() >= CBotTypBoolean) // must be a number
{
pStk->SetError(CBotErrBadIndex, p->GetStart());
goto error;
}
}
else
i = new CBotEmpty(); // if no special formula
inst->AddNext3b(i); // construct a list
type = CBotTypResult(CBotTypArrayPointer, type);
if (!pStk->IsOk() || !IsOfType(p, ID_CLBRK ))
{
pStk->SetError(CBotErrCloseIndex, p->GetStart());
goto error;
}
if (IsOfType(p, ID_CLBRK)) continue;
pStk->SetError(CBotErrCloseIndex, p->GetStart());
goto error;
}
CBotVar* var = CBotVar::Create(*vartoken, type); // create an instance
@ -96,17 +103,23 @@ CBotInstr* CBotDefArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResul
if (IsOfType(p, ID_ASS)) // with an assignment
{
if ((inst->m_listass = CBotTwoOpExpr::Compile(p, pStk)) != nullptr)
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
if (!pStk->GetTypResult().Compare(type)) // compatible type ?
{
pStk->SetError(CBotErrBadType1, p->GetStart());
goto error;
}
pStk->SetError(CBotErrBadLeft, p->GetPrev());
goto error;
}
else
if ( nullptr == (inst->m_listass = CBotListArray::Compile(p, pStk, type.GetTypElem())) )
{
inst->m_listass = CBotListArray::Compile(p, pStk, type.GetTypElem());
if (pStk->IsOk())
{
inst->m_listass = CBotTwoOpExpr::Compile(p, pStk);
if (inst->m_listass == nullptr || !pStk->GetTypResult().Compare(type)) // compatible type ?
{
pStk->SetError(CBotErrBadType1, p->GetStart());
goto error;
}
}
}
}

View File

@ -82,16 +82,17 @@ CBotInstr* CBotDefBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont,
inst = static_cast<CBotDefBoolean*>(CBotDefArray::Compile(p, pStk, CBotTypBoolean));
if (!pStk->IsOk() )
{
pStk->SetError(CBotErrCloseIndex, p->GetStart());
goto error;
}
goto suite; // no assignment, variable already created
}
if (IsOfType(p, ID_ASS))
{
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
goto error;
}
if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))
{
goto error;
@ -109,7 +110,7 @@ CBotInstr* CBotDefBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont,
(static_cast<CBotLeftExprVar*>(inst->m_var))->m_nIdent = CBotVar::NextUniqNum());
pStack->AddVar(var);
suite:
if (IsOfType(p, ID_COMMA))
if (pStk->IsOk() && IsOfType(p, ID_COMMA))
{
if (nullptr != ( inst->m_next2b = CBotDefBoolean::Compile(p, pStk, true, noskip)))
{

View File

@ -101,11 +101,6 @@ CBotInstr* CBotDefClass::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* p
inst = static_cast<CBotDefClass*>(CBotDefArray::Compile(p, pStk, type ));
if (!pStk->IsOk() )
{
pStk->SetError(CBotErrCloseIndex, p->GetStart());
goto error;
}
goto suite; // no assignment, variable already created
}
@ -159,6 +154,12 @@ CBotInstr* CBotDefClass::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* p
if (IsOfType(p, ID_ASS)) // with a assignment?
{
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
goto error;
}
if (inst->m_hasParams)
{
pStk->SetError(CBotErrNoTerminator, p->GetStart());
@ -200,7 +201,7 @@ CBotInstr* CBotDefClass::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* p
var->SetInit(CBotVar::InitType::IS_POINTER); // marks the pointer as init
}
suite:
if (IsOfType(p, ID_COMMA)) // several chained definitions
if (pStk->IsOk() && IsOfType(p, ID_COMMA)) // several chained definitions
{
if ( nullptr != ( inst->m_next = CBotDefClass::Compile(p, pStk, pClass) )) // compiles the following
{
@ -208,7 +209,7 @@ suite:
}
}
if (IsOfType(p, ID_SEP)) // complete instruction
if (!pStk->IsOk() || IsOfType(p, ID_SEP)) // complete instruction
{
return pStack->Return(inst, pStk);
}

View File

@ -81,16 +81,17 @@ CBotInstr* CBotDefFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, b
p = vartoken;
inst = static_cast<CBotDefFloat*>(CBotDefArray::Compile(p, pStk, CBotTypFloat));
if (!pStk->IsOk() )
{
pStk->SetError(CBotErrCloseIndex, p->GetStart());
goto error;
}
goto suite; // no assignment, variable already created
}
if (IsOfType(p, ID_ASS))
{
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
goto error;
}
if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))
{
goto error;
@ -108,7 +109,7 @@ CBotInstr* CBotDefFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, b
(static_cast<CBotLeftExprVar*>(inst->m_var))->m_nIdent = CBotVar::NextUniqNum());
pStack->AddVar(var);
suite:
if (IsOfType(p, ID_COMMA))
if (pStk->IsOk() && IsOfType(p, ID_COMMA))
{
if (nullptr != ( inst->m_next2b = CBotDefFloat::Compile(p, pStk, true, noskip)))
{

View File

@ -84,25 +84,18 @@ CBotInstr* CBotDefInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, boo
CBotInstr* inst2 = CBotDefArray::Compile(p, pStk, CBotTypInt);
if (!pStk->IsOk() )
{
pStk->SetError(CBotErrCloseIndex, p->GetStart());
goto error;
}
if (IsOfType(p, ID_COMMA)) // several definition chained
{
if (nullptr != ( inst2->m_next2b = CBotDefInt::Compile(p, pStk, true, noskip))) // compile the next one
{
return pStack->Return(inst2, pStk);
}
}
inst = static_cast<CBotDefInt*>(inst2);
goto suite; // no assignment, variable already created
}
if (IsOfType(p, ID_ASS)) // with an assignment?
{
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
goto error;
}
if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))
{
goto error;
@ -121,15 +114,15 @@ CBotInstr* CBotDefInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, boo
(static_cast<CBotLeftExprVar*>(inst->m_var))->m_nIdent = CBotVar::NextUniqNum());
pStack->AddVar(var); // place it on the stack
}
if (IsOfType(p, ID_COMMA)) // chained several definitions
suite:
if (pStk->IsOk() && IsOfType(p, ID_COMMA)) // chained several definitions
{
if (nullptr != ( inst->m_next2b = CBotDefInt::Compile(p, pStk, true, noskip))) // compile next one
{
return pStack->Return(inst, pStk);
}
}
suite:
if (noskip || IsOfType(p, ID_SEP)) // instruction is completed
{
return pStack->Return(inst, pStk);

View File

@ -75,15 +75,21 @@ CBotInstr* CBotDefString::Compile(CBotToken* &p, CBotCStack* pStack, bool cont,
if (IsOfType(p, ID_ASS))
{
pStk->SetStartError(p->GetStart());
if ( IsOfType(p, ID_SEP) )
{
pStk->SetError(CBotErrBadLeft, p->GetPrev());
goto error;
}
if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))
{
goto error;
}
/* if (!pStk->GetTypResult().Eq(CBotTypString)) // type compatible ?
if (!pStk->GetTypResult().Eq(CBotTypString)) // type compatible ?
{
pStk->SetError(CBotErrBadType1, p->GetStart());
goto error;
}*/
}
}
CBotVar* var = CBotVar::Create(*vartoken, CBotTypString);