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; CBotInstr* i;
while (IsOfType(p, ID_OPBRK)) while (IsOfType(p, ID_OPBRK))
{ {
pStk->SetStartError(p->GetStart());
if (p->GetType() != ID_CLBRK) 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 else
i = new CBotEmpty(); // if no special formula i = new CBotEmpty(); // if no special formula
inst->AddNext3b(i); // construct a list inst->AddNext3b(i); // construct a list
type = CBotTypResult(CBotTypArrayPointer, type); type = CBotTypResult(CBotTypArrayPointer, type);
if (!pStk->IsOk() || !IsOfType(p, ID_CLBRK )) if (IsOfType(p, ID_CLBRK)) continue;
{
pStk->SetError(CBotErrCloseIndex, p->GetStart()); pStk->SetError(CBotErrCloseIndex, p->GetStart());
goto error; goto error;
}
} }
CBotVar* var = CBotVar::Create(*vartoken, type); // create an instance 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 (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(CBotErrBadLeft, p->GetPrev());
{ goto error;
pStk->SetError(CBotErrBadType1, p->GetStart());
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)); 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 goto suite; // no assignment, variable already created
} }
if (IsOfType(p, ID_ASS)) 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 ))) if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))
{ {
goto error; 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()); (static_cast<CBotLeftExprVar*>(inst->m_var))->m_nIdent = CBotVar::NextUniqNum());
pStack->AddVar(var); pStack->AddVar(var);
suite: suite:
if (IsOfType(p, ID_COMMA)) if (pStk->IsOk() && IsOfType(p, ID_COMMA))
{ {
if (nullptr != ( inst->m_next2b = CBotDefBoolean::Compile(p, pStk, true, noskip))) 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 )); 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 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? 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) if (inst->m_hasParams)
{ {
pStk->SetError(CBotErrNoTerminator, p->GetStart()); 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 var->SetInit(CBotVar::InitType::IS_POINTER); // marks the pointer as init
} }
suite: 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 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); return pStack->Return(inst, pStk);
} }

View File

@ -81,16 +81,17 @@ CBotInstr* CBotDefFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, b
p = vartoken; p = vartoken;
inst = static_cast<CBotDefFloat*>(CBotDefArray::Compile(p, pStk, CBotTypFloat)); 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 goto suite; // no assignment, variable already created
} }
if (IsOfType(p, ID_ASS)) 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 ))) if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))
{ {
goto error; 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()); (static_cast<CBotLeftExprVar*>(inst->m_var))->m_nIdent = CBotVar::NextUniqNum());
pStack->AddVar(var); pStack->AddVar(var);
suite: suite:
if (IsOfType(p, ID_COMMA)) if (pStk->IsOk() && IsOfType(p, ID_COMMA))
{ {
if (nullptr != ( inst->m_next2b = CBotDefFloat::Compile(p, pStk, true, noskip))) 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); 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); inst = static_cast<CBotDefInt*>(inst2);
goto suite; // no assignment, variable already created goto suite; // no assignment, variable already created
} }
if (IsOfType(p, ID_ASS)) // with an assignment? 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 ))) if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))
{ {
goto error; 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()); (static_cast<CBotLeftExprVar*>(inst->m_var))->m_nIdent = CBotVar::NextUniqNum());
pStack->AddVar(var); // place it on the stack pStack->AddVar(var); // place it on the stack
} }
suite:
if (IsOfType(p, ID_COMMA)) // chained several definitions if (pStk->IsOk() && IsOfType(p, ID_COMMA)) // chained several definitions
{ {
if (nullptr != ( inst->m_next2b = CBotDefInt::Compile(p, pStk, true, noskip))) // compile next one if (nullptr != ( inst->m_next2b = CBotDefInt::Compile(p, pStk, true, noskip))) // compile next one
{ {
return pStack->Return(inst, pStk); return pStack->Return(inst, pStk);
} }
} }
suite:
if (noskip || IsOfType(p, ID_SEP)) // instruction is completed if (noskip || IsOfType(p, ID_SEP)) // instruction is completed
{ {
return pStack->Return(inst, pStk); 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)) 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 ))) if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )))
{ {
goto error; goto error;
} }
/* if (!pStk->GetTypResult().Eq(CBotTypString)) // type compatible ? if (!pStk->GetTypResult().Eq(CBotTypString)) // type compatible ?
{ {
pStk->SetError(CBotErrBadType1, p->GetStart()); pStk->SetError(CBotErrBadType1, p->GetStart());
goto error; goto error;
}*/ }
} }
CBotVar* var = CBotVar::Create(*vartoken, CBotTypString); CBotVar* var = CBotVar::Create(*vartoken, CBotTypString);