diff options
author | Piotr Dziwinski <piotrdz@gmail.com> | 2012-08-08 22:35:17 +0200 |
---|---|---|
committer | Piotr Dziwinski <piotrdz@gmail.com> | 2012-08-08 22:35:17 +0200 |
commit | bc24b9f9e516e657fcc0034808e010287fc2e393 (patch) | |
tree | e9c7c57316dc8b49efec1d8038de71381be61085 /src/CBot/CBotWhile.cpp | |
parent | beca66071c6a2d82da63fb238cdc86e68ff96bdb (diff) | |
download | colobot-bc24b9f9e516e657fcc0034808e010287fc2e393.tar.gz colobot-bc24b9f9e516e657fcc0034808e010287fc2e393.tar.bz2 colobot-bc24b9f9e516e657fcc0034808e010287fc2e393.zip |
Whitespace fix
Diffstat (limited to 'src/CBot/CBotWhile.cpp')
-rw-r--r-- | src/CBot/CBotWhile.cpp | 2012 |
1 files changed, 1006 insertions, 1006 deletions
diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index 36923ae..51310a9 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -16,15 +16,15 @@ /////////////////////////////////////////////////////////////////////// // This file defined the following statements: -// CBotWhile "while (condition) {instructions}" -// CBotDo "do {instructions} while (condition)" -// CBotFor "for (init, condition, incr) {instructions}" -// CBotSwitch "switch (val) {instructions}" -// CBotCase "case val:" -// CBotBreak "break", "break label", "continu", "continu label" -// CBotTry "try {instructions}" -// CBotCatch "catch (condition) {instructions}" or "finally" -// CBotThrow "throw execption" +// CBotWhile "while (condition) {instructions}" +// CBotDo "do {instructions} while (condition)" +// CBotFor "for (init, condition, incr) {instructions}" +// CBotSwitch "switch (val) {instructions}" +// CBotCase "case val:" +// CBotBreak "break", "break label", "continu", "continu label" +// CBotTry "try {instructions}" +// CBotCatch "catch (condition) {instructions}" or "finally" +// CBotThrow "throw execption" #include "CBot.h" @@ -36,122 +36,122 @@ CBotWhile::CBotWhile() { - m_Condition = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotWhile"; // debug + m_Condition = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotWhile"; // debug } CBotWhile::~CBotWhile() { - delete m_Condition; // frees the condition - delete m_Block; // releases the block instruction + delete m_Condition; // frees the condition + delete m_Block; // releases the block instruction } CBotInstr* CBotWhile::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotWhile* inst = new CBotWhile(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) + CBotWhile* inst = new CBotWhile(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // records the name of the label - } + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // records the name of the label + } - inst->SetToken(p); - if (!IsOfType(p, ID_WHILE)) return NULL; // should never happen + inst->SetToken(p); + if (!IsOfType(p, ID_WHILE)) return NULL; // should never happen - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp // a bit of battery please (??) - if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) - { - // the condition exists + if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) + { + // the condition exists - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); - if ( pStk->IsOk() ) - { - // the statement block is ok (it may be empty! + if ( pStk->IsOk() ) + { + // the statement block is ok (it may be empty! - return pStack->Return(inst, pStk); // return an object to the application + return pStack->Return(inst, pStk); // return an object to the application // makes the object to which the application - } - } + } + } - delete inst; // error, frees the place - return pStack->Return(NULL, pStk); // no object, the error is on the stack + delete inst; // error, frees the place + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // executes a "while" instruction bool CBotWhile :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); // adds an item to the stack - // or find in case of recovery -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or find in case of recovery +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - while( true ) switch( pile->GivState() ) // executes the loop - { // there are two possible states (depending on recovery) - case 0: - // evaluates the condition - if ( !m_Condition->Execute(pile) ) return false; // interrupted here? + while( true ) switch( pile->GivState() ) // executes the loop + { // there are two possible states (depending on recovery) + case 0: + // evaluates the condition + if ( !m_Condition->Execute(pile) ) return false; // interrupted here? - // the result of the condition is on the stack + // the result of the condition is on the stack - // terminates if an error or if the condition is false - if ( !pile->IsOk() || pile->GivVal() != true ) - { - return pj->Return(pile); // sends the results and releases the stack - } + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GivVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } - // the condition is true, pass in the second mode + // the condition is true, pass in the second mode - if (!pile->SetState(1)) return false; // ready for further + if (!pile->SetState(1)) return false; // ready for further - case 1: - // evaluates the associated statement block - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(0, m_label)) continue; // if continued, will return to test - return pj->BreakReturn(pile, m_label); // sends the results and releases the stack - } + case 1: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(0, m_label)) continue; // if continued, will return to test + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - // returns to the test again - if (!pile->SetState(0, 0)) return false; - continue; - } + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // returns to the test again + if (!pile->SetState(0, 0)) return false; + continue; + } } void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // there are two possible states (depending on recovery) - case 0: - // evaluates the condition - m_Condition->RestoreState(pile, bMain); - return; + if ( !bMain ) return; + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are two possible states (depending on recovery) + case 0: + // evaluates the condition + m_Condition->RestoreState(pile, bMain); + return; - case 1: - // evaluates the associated statement block - if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); - return; - } + case 1: + // evaluates the associated statement block + if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); + return; + } } @@ -162,140 +162,140 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) CBotRepeat::CBotRepeat() { - m_NbIter = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotRepeat"; // debug + m_NbIter = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotRepeat"; // debug } CBotRepeat::~CBotRepeat() { - delete m_NbIter; // frees the condition - delete m_Block; // frees the instruction block + delete m_NbIter; // frees the condition + delete m_Block; // frees the instruction block } CBotInstr* CBotRepeat::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotRepeat* inst = new CBotRepeat(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // register the name of label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_REPEAT)) return NULL; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - if ( IsOfType(p, ID_OPENPAR ) ) - { - CBotToken* ppp = p; // preserves the ^ token (starting position) - if ( NULL != (inst->m_NbIter = CBotExpression::Compile( p, pStk )) ) - { - if ( pStk->GivType() < CBotTypLong ) - { - if ( IsOfType(p, ID_CLOSEPAR ) ) - { - - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - - if ( pStk->IsOk() ) - { - // the statement block is ok (it may be empty! - - return pStack->Return(inst, pStk); // return an object to the application - } - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - pStk->SetStartError(ppp->GivStart()); - pStk->SetError( TX_BADTYPE, p->GivStart() ); - } - pStack->SetError(TX_ENDOF, p); - } - pStack->SetError(TX_OPENPAR, p->GivStart()); // missing parenthesis - - delete inst; // error, frees up - return pStack->Return(NULL, pStk); // no object, the error is on the stack + CBotRepeat* inst = new CBotRepeat(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_REPEAT)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + if ( IsOfType(p, ID_OPENPAR ) ) + { + CBotToken* ppp = p; // preserves the ^ token (starting position) + if ( NULL != (inst->m_NbIter = CBotExpression::Compile( p, pStk )) ) + { + if ( pStk->GivType() < CBotTypLong ) + { + if ( IsOfType(p, ID_CLOSEPAR ) ) + { + + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + + if ( pStk->IsOk() ) + { + // the statement block is ok (it may be empty! + + return pStack->Return(inst, pStk); // return an object to the application + } + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + pStk->SetStartError(ppp->GivStart()); + pStk->SetError( TX_BADTYPE, p->GivStart() ); + } + pStack->SetError(TX_ENDOF, p); + } + pStack->SetError(TX_OPENPAR, p->GivStart()); // missing parenthesis + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // execution of intruction "repeat" bool CBotRepeat :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); // adds an item to the stack - // or find in case of recovery -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or find in case of recovery +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - while( true ) switch( pile->GivState() ) // executes the loop - { // there are two possible states (depending on recovery) - case 0: - // evaluates the number of iterations - if ( !m_NbIter->Execute(pile) ) return false; // interrupted here ? + while( true ) switch( pile->GivState() ) // executes the loop + { // there are two possible states (depending on recovery) + case 0: + // evaluates the number of iterations + if ( !m_NbIter->Execute(pile) ) return false; // interrupted here ? - // the result of the condition is on the stack + // the result of the condition is on the stack - // terminates if an error or if the condition is false - int n; - if ( !pile->IsOk() || ( n = pile->GivVal() ) < 1 ) - { - return pj->Return(pile); // sends the results and releases the stack - } + // terminates if an error or if the condition is false + int n; + if ( !pile->IsOk() || ( n = pile->GivVal() ) < 1 ) + { + return pj->Return(pile); // sends the results and releases the stack + } - // puts the number of iterations +1 to the "state" + // puts the number of iterations +1 to the "state" - if (!pile->SetState(n+1)) return false; // ready for further - continue; // continue as a result + if (!pile->SetState(n+1)) return false; // ready for further + continue; // continue as a result - case 1: - // normal end of the loop - return pj->Return(pile); // sends the results and releases the stack + case 1: + // normal end of the loop + return pj->Return(pile); // sends the results and releases the stack - default: - // evaluates the associated statement block - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(pile->GivState()-1, m_label)) continue; // if continued, will return to test - return pj->BreakReturn(pile, m_label); // sends the results and releases the stack - } + default: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(pile->GivState()-1, m_label)) continue; // if continued, will return to test + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - // returns to the test again - if (!pile->SetState(pile->GivState()-1, 0)) return false; - continue; - } + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // returns to the test again + if (!pile->SetState(pile->GivState()-1, 0)) return false; + continue; + } } void CBotRepeat :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // there are two possible states (depending on recovery) - case 0: - // evaluates the condition - m_NbIter->RestoreState(pile, bMain); - return; + if ( !bMain ) return; + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are two possible states (depending on recovery) + case 0: + // evaluates the condition + m_NbIter->RestoreState(pile, bMain); + return; - case 1: - // evaluates the associated statement block - if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); - return; - } + case 1: + // evaluates the associated statement block + if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); + return; + } } /////////////////////////////////////////////////////////////////////////// @@ -305,127 +305,127 @@ void CBotRepeat :: RestoreState(CBotStack* &pj, bool bMain) CBotDo::CBotDo() { - m_Condition = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotDo"; // debug + m_Condition = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotDo"; // debug } CBotDo::~CBotDo() { - delete m_Condition; // frees the condition - delete m_Block; // frees the instruction block + delete m_Condition; // frees the condition + delete m_Block; // frees the instruction block } CBotInstr* CBotDo::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotDo* inst = new CBotDo(); // creates the object - - CBotToken* pp = p; // preserves at the ^ token (starting position) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // register the name of label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_DO)) return NULL; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - - // looking for a statement block after the do - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - - if ( pStk->IsOk() ) - { - if (IsOfType(p, ID_WHILE)) - { - if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) - { - // the condition exists - if (IsOfType(p, ID_SEP)) - { - return pStack->Return(inst, pStk); // return an object to the application - } - pStk->SetError(TX_ENDOF, p->GivStart()); - } - } - pStk->SetError(TX_WHILE, p->GivStart()); - } - - delete inst; // error, frees up - return pStack->Return(NULL, pStk); // no object, the error is on the stack + CBotDo* inst = new CBotDo(); // creates the object + + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_DO)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + + // looking for a statement block after the do + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + + if ( pStk->IsOk() ) + { + if (IsOfType(p, ID_WHILE)) + { + if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) + { + // the condition exists + if (IsOfType(p, ID_SEP)) + { + return pStack->Return(inst, pStk); // return an object to the application + } + pStk->SetError(TX_ENDOF, p->GivStart()); + } + } + pStk->SetError(TX_WHILE, p->GivStart()); + } + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // executes instruction "do" bool CBotDo :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); // adds an item to the stack - // or find in case of recovery -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - while( true ) switch( pile->GivState() ) // executes the loop - { // there are two possible states (depending on recovery) - case 0: - // evaluates the associated statement block - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(1, m_label)) continue; // if continued, will return to test - return pj->BreakReturn(pile, m_label); // sends the results and releases the stack - } + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or find in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + while( true ) switch( pile->GivState() ) // executes the loop + { // there are two possible states (depending on recovery) + case 0: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(1, m_label)) continue; // if continued, will return to test + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // sends the results and releases the stack - } + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } - if (!pile->SetState(1)) return false; // ready for further + if (!pile->SetState(1)) return false; // ready for further - case 1: - // evaluates the condition - if ( !m_Condition->Execute(pile) ) return false; // interrupted here ? - - // the result of the condition is on the stack - - // terminates if an error or if the condition is false - if ( !pile->IsOk() || pile->GivVal() != true ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - // returns to instruction block to start - if (!pile->SetState(0, 0)) return false; - continue; - } + case 1: + // evaluates the condition + if ( !m_Condition->Execute(pile) ) return false; // interrupted here ? + + // the result of the condition is on the stack + + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GivVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // returns to instruction block to start + if (!pile->SetState(0, 0)) return false; + continue; + } } void CBotDo :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // there are two possible states (depending on recovery) - case 0: - // restores the assosiated statement's block - if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); - return; - - case 1: - // restores the condition - m_Condition->RestoreState(pile, bMain); - return; - } + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are two possible states (depending on recovery) + case 0: + // restores the assosiated statement's block + if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); + return; + + case 1: + // restores the condition + m_Condition->RestoreState(pile, bMain); + return; + } } @@ -436,181 +436,181 @@ void CBotDo :: RestoreState(CBotStack* &pj, bool bMain) CBotFor::CBotFor() { - m_Init = - m_Test = - m_Incr = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotFor"; // debug + m_Init = + m_Test = + m_Incr = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotFor"; // debug } CBotFor::~CBotFor() { - delete m_Init; - delete m_Test; - delete m_Incr; - delete m_Block; // frees the instruction block + delete m_Init; + delete m_Test; + delete m_Incr; + delete m_Block; // frees the instruction block } CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotFor* inst = new CBotFor(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // register the name of label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_FOR)) return NULL; // should never happen - - if ( !IsOfType(p, ID_OPENPAR)) // missing parenthesis ? - { - pStack->SetError(TX_OPENPAR, p->GivStart()); - return NULL; - } - - CBotCStack* pStk = pStack->TokenStack(pp, true); // un petit bout de pile svp - - // compiles instructions for initialization - inst->m_Init = CBotListExpression::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( !IsOfType(p, ID_SEP)) // lack the semicolon? - { - pStack->SetError(TX_OPENPAR, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); // no object, the error is on the stack - } - inst->m_Test = CBotBoolExpr::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( !IsOfType(p, ID_SEP)) // lack the semicolon? - { - pStack->SetError(TX_OPENPAR, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); // no object, the error is on the stack - } - inst->m_Incr = CBotListExpression::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( IsOfType(p, ID_CLOSEPAR)) // missing parenthesis ? - { - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - if ( pStk->IsOk() ) - return pStack->Return(inst, pStk);; - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - } - } - - delete inst; // error, frees up - return pStack->Return(NULL, pStk); // no object, the error is on the stack + CBotFor* inst = new CBotFor(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_FOR)) return NULL; // should never happen + + if ( !IsOfType(p, ID_OPENPAR)) // missing parenthesis ? + { + pStack->SetError(TX_OPENPAR, p->GivStart()); + return NULL; + } + + CBotCStack* pStk = pStack->TokenStack(pp, true); // un petit bout de pile svp + + // compiles instructions for initialization + inst->m_Init = CBotListExpression::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( !IsOfType(p, ID_SEP)) // lack the semicolon? + { + pStack->SetError(TX_OPENPAR, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); // no object, the error is on the stack + } + inst->m_Test = CBotBoolExpr::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( !IsOfType(p, ID_SEP)) // lack the semicolon? + { + pStack->SetError(TX_OPENPAR, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); // no object, the error is on the stack + } + inst->m_Incr = CBotListExpression::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( IsOfType(p, ID_CLOSEPAR)) // missing parenthesis ? + { + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + if ( pStk->IsOk() ) + return pStack->Return(inst, pStk);; + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + } + } + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // execution of instruction "for" bool CBotFor :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this, true); // adds an item to the stack (variables locales) - // or find in case of recovery -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - while( true ) switch( pile->GivState() ) // executes the loop - { // there are four possible states (depending on recovery) - case 0: - // initialize - if ( m_Init != NULL && - !m_Init->Execute(pile) ) return false; // interrupted here ? - if (!pile->SetState(1)) return false; // ready for further + CBotStack* pile = pj->AddStack(this, true); // adds an item to the stack (variables locales) + // or find in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + while( true ) switch( pile->GivState() ) // executes the loop + { // there are four possible states (depending on recovery) + case 0: + // initialize + if ( m_Init != NULL && + !m_Init->Execute(pile) ) return false; // interrupted here ? + if (!pile->SetState(1)) return false; // ready for further - case 1: - // evaluates the condition - if ( m_Test != NULL ) // no strings attached? -> True! - { - if (!m_Test->Execute(pile) ) return false; // interrupted here ? - - // the result of the condition is on the stack - - // terminates if an error or if the condition is false - if ( !pile->IsOk() || pile->GivVal() != true ) - { - return pj->Return(pile); // sends the results and releases the stack - } - } - - // la condition est vrai, passe à la suite - if (!pile->SetState(2)) return false; // ready for further - - case 2: - // evaluates the associated statement block - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(3, m_label)) continue; // if continued, going on to incrementation - return pj->BreakReturn(pile, m_label); // sends the results and releases the stack - } - - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - if (!pile->SetState(3)) return false; // ready for further - - case 3: - // evalutate the incrementation - if ( m_Incr != NULL && - !m_Incr->Execute(pile) ) return false; // interrupted here ? - - // returns to the test again - if (!pile->SetState(1, 0)) return false; // returns to the test - continue; - } + case 1: + // evaluates the condition + if ( m_Test != NULL ) // no strings attached? -> True! + { + if (!m_Test->Execute(pile) ) return false; // interrupted here ? + + // the result of the condition is on the stack + + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GivVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } + } + + // la condition est vrai, passe à la suite + if (!pile->SetState(2)) return false; // ready for further + + case 2: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(3, m_label)) continue; // if continued, going on to incrementation + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } + + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + if (!pile->SetState(3)) return false; // ready for further + + case 3: + // evalutate the incrementation + if ( m_Incr != NULL && + !m_Incr->Execute(pile) ) return false; // interrupted here ? + + // returns to the test again + if (!pile->SetState(1, 0)) return false; // returns to the test + continue; + } } void CBotFor :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack (variables locales) - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // there are four possible states (depending on recovery) - case 0: - // initialize - if ( m_Init != NULL ) m_Init->RestoreState(pile, true); // interrupted here ! - return; - - case 1: - if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variables definitions - - // evaluates the condition - if ( m_Test != NULL ) m_Test->RestoreState(pile, true); // interrupted here ! - return; - - case 2: - if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variable definitions - - // evaluates the associated statement block - if ( m_Block != NULL ) m_Block->RestoreState(pile, true); - return; - - case 3: - if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variable definitions - - // evaluate the incrementation - if ( m_Incr != NULL ) m_Incr->RestoreState(pile, true); // interrupted here ! - return; - } + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack (variables locales) + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are four possible states (depending on recovery) + case 0: + // initialize + if ( m_Init != NULL ) m_Init->RestoreState(pile, true); // interrupted here ! + return; + + case 1: + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variables definitions + + // evaluates the condition + if ( m_Test != NULL ) m_Test->RestoreState(pile, true); // interrupted here ! + return; + + case 2: + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variable definitions + + // evaluates the associated statement block + if ( m_Block != NULL ) m_Block->RestoreState(pile, true); + return; + + case 3: + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variable definitions + + // evaluate the incrementation + if ( m_Incr != NULL ) m_Incr->RestoreState(pile, true); // interrupted here ! + return; + } } ////////////////////////////////////////////////////////////////////////////////////// @@ -620,92 +620,92 @@ void CBotFor :: RestoreState(CBotStack* &pj, bool bMain) CBotListExpression::CBotListExpression() { - m_Expr = NULL; - name = "CBotListExpression"; + m_Expr = NULL; + name = "CBotListExpression"; } CBotListExpression::~CBotListExpression() { - delete m_Expr; + delete m_Expr; } // seeks a declaration of variable or expression static CBotInstr* CompileInstrOrDefVar(CBotToken* &p, CBotCStack* pStack) { - CBotInstr* i = CBotInt::Compile( p, pStack, false, true ); // Is this a declaration of an integer? - if ( i== NULL ) i = CBotFloat::Compile( p, pStack, false, true ); // or a real number? - if ( i== NULL ) i = CBotBoolean::Compile( p, pStack, false, true ); // or a boolean? - if ( i== NULL ) i = CBotIString::Compile( p, pStack, false, true ); // ar a string? - if ( i== NULL ) i = CBotExpression::Compile( p, pStack ); // compiles an expression - return i; + CBotInstr* i = CBotInt::Compile( p, pStack, false, true ); // Is this a declaration of an integer? + if ( i== NULL ) i = CBotFloat::Compile( p, pStack, false, true ); // or a real number? + if ( i== NULL ) i = CBotBoolean::Compile( p, pStack, false, true ); // or a boolean? + if ( i== NULL ) i = CBotIString::Compile( p, pStack, false, true ); // ar a string? + if ( i== NULL ) i = CBotExpression::Compile( p, pStack ); // compiles an expression + return i; } CBotInstr* CBotListExpression::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotListExpression* inst = new CBotListExpression(); - - inst->m_Expr = CompileInstrOrDefVar( p, pStack ); // compile the first expression in a list - if (pStack->IsOk()) - { - while ( IsOfType(p, ID_COMMA) ) // more instructions? - { - CBotInstr* i = CompileInstrOrDefVar( p, pStack ); // Is this a declaration of an integer? - inst->m_Expr->AddNext(i); // added after - if ( !pStack->IsOk() ) - { - delete inst; - return NULL; // no object, the error is on the stack - } - } - return inst; - } - delete inst; - return NULL; + CBotListExpression* inst = new CBotListExpression(); + + inst->m_Expr = CompileInstrOrDefVar( p, pStack ); // compile the first expression in a list + if (pStack->IsOk()) + { + while ( IsOfType(p, ID_COMMA) ) // more instructions? + { + CBotInstr* i = CompileInstrOrDefVar( p, pStack ); // Is this a declaration of an integer? + inst->m_Expr->AddNext(i); // added after + if ( !pStack->IsOk() ) + { + delete inst; + return NULL; // no object, the error is on the stack + } + } + return inst; + } + delete inst; + return NULL; } bool CBotListExpression::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(); // essential - CBotInstr* p = m_Expr; // the first expression - - int state = pile->GivState(); - while (state-->0) p = p->GivNext(); // returns to the interrupted operation - - if ( p != NULL ) while (true) - { - if ( !p->Execute(pile) ) return false; - p = p->GivNext(); - if ( p == NULL ) break; - if (!pile->IncState()) return false; // ready for next - } - return pj->Return(pile); + CBotStack* pile = pj->AddStack(); // essential + CBotInstr* p = m_Expr; // the first expression + + int state = pile->GivState(); + while (state-->0) p = p->GivNext(); // returns to the interrupted operation + + if ( p != NULL ) while (true) + { + if ( !p->Execute(pile) ) return false; + p = p->GivNext(); + if ( p == NULL ) break; + if (!pile->IncState()) return false; // ready for next + } + return pj->Return(pile); } void CBotListExpression::RestoreState(CBotStack* &pj, bool bMain) { - CBotStack* pile = pj; - int state = 0x7000; - - if ( bMain ) - { - pile = pj->RestoreStack(); - if ( pile == NULL ) return; - state = pile->GivState(); - } - - CBotInstr* p = m_Expr; // the first expression - - while (p != NULL && state-->0) - { - p->RestoreState(pile, false); - p = p->GivNext(); // returns to the interrupted operation - } - - if ( p != NULL ) - { - p->RestoreState(pile, bMain); - } + CBotStack* pile = pj; + int state = 0x7000; + + if ( bMain ) + { + pile = pj->RestoreStack(); + if ( pile == NULL ) return; + state = pile->GivState(); + } + + CBotInstr* p = m_Expr; // the first expression + + while (p != NULL && state-->0) + { + p->RestoreState(pile, false); + p = p->GivNext(); // returns to the interrupted operation + } + + if ( p != NULL ) + { + p->RestoreState(pile, bMain); + } } /////////////////////////////////////////////////////////////////////////// @@ -715,186 +715,186 @@ void CBotListExpression::RestoreState(CBotStack* &pj, bool bMain) CBotSwitch::CBotSwitch() { - m_Value = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotSwitch"; // debug + m_Value = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotSwitch"; // debug } CBotSwitch::~CBotSwitch() { - delete m_Value; // frees the value - delete m_Block; // frees the instruction block + delete m_Value; // frees the value + delete m_Block; // frees the instruction block } CBotInstr* CBotSwitch::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotSwitch* inst = new CBotSwitch(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - inst->SetToken(p); - if (!IsOfType(p, ID_SWITCH)) return NULL; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - if ( IsOfType(p, ID_OPENPAR ) ) - { - if ( NULL != (inst->m_Value = CBotExpression::Compile( p, pStk )) ) - { - if ( pStk->GivType() < CBotTypLong ) - { - if ( IsOfType(p, ID_CLOSEPAR ) ) - { - if ( IsOfType(p, ID_OPBLK ) ) - { - IncLvl(); - - while( !IsOfType( p, ID_CLBLK ) ) - { - if ( p->GivType() == ID_CASE || p->GivType() == ID_DEFAULT) - { - CBotCStack* pStk2 = pStk->TokenStack(p); // un petit bout de pile svp - - CBotInstr* i = CBotCase::Compile( p, pStk2 ); - if (i == NULL) - { - delete inst; - return pStack->Return(NULL, pStk2); - } - delete pStk2; - if ( inst->m_Block == NULL ) inst->m_Block = i; - else inst->m_Block->AddNext(i); - continue; - } - - if ( inst->m_Block == NULL ) - { - pStk->SetError(TX_NOCASE, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); - } - - CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true ); - if ( !pStk->IsOk() ) - { - delete inst; - return pStack->Return(NULL, pStk); - } - inst->m_Block->AddNext(i); - - if ( p == NULL ) - { - pStk->SetError(TX_CLOSEBLK, -1); - delete inst; - return pStack->Return(NULL, pStk); - } - } - DecLvl(); - - if ( inst->m_Block == NULL ) - { - pStk->SetError(TX_NOCASE, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); - } - // the statement block is ok - return pStack->Return(inst, pStk); // return an object to the application - } - pStk->SetError( TX_OPENBLK, p->GivStart() ); - } - pStk->SetError( TX_CLOSEPAR, p->GivStart() ); - } - pStk->SetError( TX_BADTYPE, p->GivStart() ); - } - } - pStk->SetError( TX_OPENPAR, p->GivStart()); - - delete inst; // error, frees up - return pStack->Return(NULL, pStk); // no object, the error is on the stack + CBotSwitch* inst = new CBotSwitch(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_SWITCH)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + if ( IsOfType(p, ID_OPENPAR ) ) + { + if ( NULL != (inst->m_Value = CBotExpression::Compile( p, pStk )) ) + { + if ( pStk->GivType() < CBotTypLong ) + { + if ( IsOfType(p, ID_CLOSEPAR ) ) + { + if ( IsOfType(p, ID_OPBLK ) ) + { + IncLvl(); + + while( !IsOfType( p, ID_CLBLK ) ) + { + if ( p->GivType() == ID_CASE || p->GivType() == ID_DEFAULT) + { + CBotCStack* pStk2 = pStk->TokenStack(p); // un petit bout de pile svp + + CBotInstr* i = CBotCase::Compile( p, pStk2 ); + if (i == NULL) + { + delete inst; + return pStack->Return(NULL, pStk2); + } + delete pStk2; + if ( inst->m_Block == NULL ) inst->m_Block = i; + else inst->m_Block->AddNext(i); + continue; + } + + if ( inst->m_Block == NULL ) + { + pStk->SetError(TX_NOCASE, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); + } + + CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true ); + if ( !pStk->IsOk() ) + { + delete inst; + return pStack->Return(NULL, pStk); + } + inst->m_Block->AddNext(i); + + if ( p == NULL ) + { + pStk->SetError(TX_CLOSEBLK, -1); + delete inst; + return pStack->Return(NULL, pStk); + } + } + DecLvl(); + + if ( inst->m_Block == NULL ) + { + pStk->SetError(TX_NOCASE, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); + } + // the statement block is ok + return pStack->Return(inst, pStk); // return an object to the application + } + pStk->SetError( TX_OPENBLK, p->GivStart() ); + } + pStk->SetError( TX_CLOSEPAR, p->GivStart() ); + } + pStk->SetError( TX_BADTYPE, p->GivStart() ); + } + } + pStk->SetError( TX_OPENPAR, p->GivStart()); + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // executes instruction "switch" bool CBotSwitch :: Execute(CBotStack* &pj) { - CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack -// if ( pile1 == EOX ) return true; - - CBotInstr* p = m_Block; // first expression - - int state = pile1->GivState(); - if (state == 0) - { - if ( !m_Value->Execute(pile1) ) return false; - pile1->SetState(state = -1); - } - - if ( pile1->IfStep() ) return false; - - if ( state == -1 ) - { - state = 0; - int val = pile1->GivVal(); // result of the value - - CBotStack* pile2 = pile1->AddStack(); - while ( p != NULL ) // search for the corresponding case in a list - { - state++; - if ( p->CompCase( pile2, val ) ) break; // found the case - p = p->GivNext(); - } - pile2->Delete(); - - if ( p == NULL ) return pj->Return(pile1); // completed if nothing - - if ( !pile1->SetState(state) ) return false; - } - - p = m_Block; // returns to the beginning - while (state-->0) p = p->GivNext(); // advance in the list - - while( p != NULL ) - { - if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1); - if ( !pile1->IncState() ) return false; - p = p->GivNext(); - } - return pj->Return(pile1); + CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack +// if ( pile1 == EOX ) return true; + + CBotInstr* p = m_Block; // first expression + + int state = pile1->GivState(); + if (state == 0) + { + if ( !m_Value->Execute(pile1) ) return false; + pile1->SetState(state = -1); + } + + if ( pile1->IfStep() ) return false; + + if ( state == -1 ) + { + state = 0; + int val = pile1->GivVal(); // result of the value + + CBotStack* pile2 = pile1->AddStack(); + while ( p != NULL ) // search for the corresponding case in a list + { + state++; + if ( p->CompCase( pile2, val ) ) break; // found the case + p = p->GivNext(); + } + pile2->Delete(); + + if ( p == NULL ) return pj->Return(pile1); // completed if nothing + + if ( !pile1->SetState(state) ) return false; + } + + p = m_Block; // returns to the beginning + while (state-->0) p = p->GivNext(); // advance in the list + + while( p != NULL ) + { + if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1); + if ( !pile1->IncState() ) return false; + p = p->GivNext(); + } + return pj->Return(pile1); } void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - - CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack - if ( pile1 == NULL ) return; - - CBotInstr* p = m_Block; // first expression - - int state = pile1->GivState(); - if (state == 0) - { - m_Value->RestoreState(pile1, bMain); - return; - } - - if ( state == -1 ) - { - return; - } - -// p = m_Block; // returns to the beginning - while ( p != NULL && state-- > 0 ) - { - p->RestoreState(pile1, false); - p = p->GivNext(); // advance in the list - } - - if( p != NULL ) - { - p->RestoreState(pile1, true); - return; - } + if ( !bMain ) return; + + CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack + if ( pile1 == NULL ) return; + + CBotInstr* p = m_Block; // first expression + + int state = pile1->GivState(); + if (state == 0) + { + m_Value->RestoreState(pile1, bMain); + return; + } + + if ( state == -1 ) + { + return; + } + +// p = m_Block; // returns to the beginning + while ( p != NULL && state-- > 0 ) + { + p->RestoreState(pile1, false); + p = p->GivNext(); // advance in the list + } + + if( p != NULL ) + { + p->RestoreState(pile1, true); + return; + } } /////////////////////////////////////////////////////////////////////////// @@ -905,50 +905,50 @@ void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain) CBotCase::CBotCase() { - m_Value = NULL; // NULL so that delete is not possible further - name = "CBotCase"; // debug + m_Value = NULL; // NULL so that delete is not possible further + name = "CBotCase"; // debug } CBotCase::~CBotCase() { - delete m_Value; // frees the value + delete m_Value; // frees the value } CBotInstr* CBotCase::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotCase* inst = new CBotCase(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - inst->SetToken(p); - if (!IsOfType(p, ID_CASE, ID_DEFAULT)) return NULL; // should never happen - - if ( pp->GivType() == ID_CASE ) - { - pp = p; - inst->m_Value = CBotExprNum::Compile(p, pStack); - if ( inst->m_Value == NULL ) - { - pStack->SetError( TX_BADNUM, pp ); - delete inst; - return NULL; - } - } - if ( !IsOfType( p, ID_DOTS )) - { - pStack->SetError( TX_MISDOTS, p->GivStart() ); - delete inst; - return NULL; - } - - return inst; + CBotCase* inst = new CBotCase(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_CASE, ID_DEFAULT)) return NULL; // should never happen + + if ( pp->GivType() == ID_CASE ) + { + pp = p; + inst->m_Value = CBotExprNum::Compile(p, pStack); + if ( inst->m_Value == NULL ) + { + pStack->SetError( TX_BADNUM, pp ); + delete inst; + return NULL; + } + } + if ( !IsOfType( p, ID_DOTS )) + { + pStack->SetError( TX_MISDOTS, p->GivStart() ); + delete inst; + return NULL; + } + + return inst; } // execution of instruction "case" bool CBotCase::Execute(CBotStack* &pj) { - return true; // the "case" statement does nothing! + return true; // the "case" statement does nothing! } void CBotCase::RestoreState(CBotStack* &pj, bool bMain) @@ -960,10 +960,10 @@ void CBotCase::RestoreState(CBotStack* &pj, bool bMain) bool CBotCase::CompCase(CBotStack* &pile, int val) { - if ( m_Value == NULL ) return true; // "default" case + if ( m_Value == NULL ) return true; // "default" case - while (!m_Value->Execute(pile)); // puts the value on the correspondent stack (without interruption) - return (pile->GivVal() == val); // compared with the given value + while (!m_Value->Execute(pile)); // puts the value on the correspondent stack (without interruption) + return (pile->GivVal() == val); // compared with the given value } /////////////////////////////////////////////////////////////////////////// @@ -973,7 +973,7 @@ bool CBotCase::CompCase(CBotStack* &pile, int val) CBotBreak::CBotBreak() { - name = "CBotBreak"; // debug + name = "CBotBreak"; // debug } CBotBreak::~CBotBreak() @@ -982,58 +982,58 @@ CBotBreak::~CBotBreak() CBotInstr* CBotBreak::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotToken* pp = p; // preserves at the ^ token (starting position) - int type = p->GivType(); - - if (!IsOfType(p, ID_BREAK, ID_CONTINUE)) return NULL; // should never happen - - if ( !ChkLvl(CBotString(), type ) ) - { - pStack->SetError(TX_BREAK, pp); - return NULL; // no object, the error is on the stack - } - - CBotBreak* inst = new CBotBreak(); // creates the object - inst->SetToken(pp); // keeps the operation - - pp = p; - if ( IsOfType( p, TokenTypVar ) ) - { - inst->m_label = pp->GivString(); // register the name of label - if ( !ChkLvl(inst->m_label, type ) ) - { - delete inst; - pStack->SetError(TX_NOLABEL, pp); - return NULL; // no object, the error is on the stack - } - } - - if (IsOfType(p, ID_SEP)) - { - return inst; // return what it wants - } - delete inst; - - pStack->SetError(TX_ENDOF, p->GivStart()); - return NULL; // no object, the error is on the stack + CBotToken* pp = p; // preserves at the ^ token (starting position) + int type = p->GivType(); + + if (!IsOfType(p, ID_BREAK, ID_CONTINUE)) return NULL; // should never happen + + if ( !ChkLvl(CBotString(), type ) ) + { + pStack->SetError(TX_BREAK, pp); + return NULL; // no object, the error is on the stack + } + + CBotBreak* inst = new CBotBreak(); // creates the object + inst->SetToken(pp); // keeps the operation + + pp = p; + if ( IsOfType( p, TokenTypVar ) ) + { + inst->m_label = pp->GivString(); // register the name of label + if ( !ChkLvl(inst->m_label, type ) ) + { + delete inst; + pStack->SetError(TX_NOLABEL, pp); + return NULL; // no object, the error is on the stack + } + } + + if (IsOfType(p, ID_SEP)) + { + return inst; // return what it wants + } + delete inst; + + pStack->SetError(TX_ENDOF, p->GivStart()); + return NULL; // no object, the error is on the stack } // execution of statement "break" or "continu" bool CBotBreak :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - pile->SetBreak(m_token.GivType()==ID_BREAK ? 1 : 2, m_label); - return pj->Return(pile); + pile->SetBreak(m_token.GivType()==ID_BREAK ? 1 : 2, m_label); + return pj->Return(pile); } void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if ( bMain ) pj->RestoreStack(this); } @@ -1044,51 +1044,51 @@ void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain) CBotTry::CBotTry() { - m_ListCatch = NULL; - m_FinalInst = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotTry"; // debug + m_ListCatch = NULL; + m_FinalInst = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotTry"; // debug } CBotTry::~CBotTry() { - delete m_ListCatch; // frees the list - delete m_Block; // frees the instruction block - delete m_FinalInst; -} + delete m_ListCatch; // frees the list + delete m_Block; // frees the instruction block + delete m_FinalInst; +} CBotInstr* CBotTry::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotTry* inst = new CBotTry(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - inst->SetToken(p); - if (!IsOfType(p, ID_TRY)) return NULL; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk ); - CBotCatch** pn = &inst->m_ListCatch; - - while (pStk->IsOk() && p->GivType() == ID_CATCH) - { - CBotCatch* i = CBotCatch::Compile(p, pStk); - *pn = i; - pn = &i->m_next; - } - - if (pStk->IsOk() && IsOfType( p, ID_FINALLY) ) - { - inst->m_FinalInst = CBotBlock::CompileBlkOrInst( p, pStk ); - } - - if (pStk->IsOk()) - { - return pStack->Return(inst, pStk); // return an object to the application - } - - delete inst; // error, frees up - return pStack->Return(NULL, pStk); // no object, the error is on the stack + CBotTry* inst = new CBotTry(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_TRY)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk ); + CBotCatch** pn = &inst->m_ListCatch; + + while (pStk->IsOk() && p->GivType() == ID_CATCH) + { + CBotCatch* i = CBotCatch::Compile(p, pStk); + *pn = i; + pn = &i->m_next; + } + + if (pStk->IsOk() && IsOfType( p, ID_FINALLY) ) + { + inst->m_FinalInst = CBotBlock::CompileBlkOrInst( p, pStk ); + } + + if (pStk->IsOk()) + { + return pStack->Return(inst, pStk); // return an object to the application + } + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // execution of instruction Try @@ -1098,145 +1098,145 @@ CBotInstr* CBotTry::Compile(CBotToken* &p, CBotCStack* pStack) bool CBotTry :: Execute(CBotStack* &pj) { - int val; - - CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack -// if ( pile1 == EOX ) return true; - - if ( pile1->IfStep() ) return false; - // or find in case of recovery - CBotStack* pile0 = pj->AddStack2(); // adds an element to the secondary stack - CBotStack* pile2 = pile0->AddStack(); - - if ( pile1->GivState() == 0 ) - { - if ( m_Block->Execute(pile1) ) - { - if ( m_FinalInst == NULL ) return pj->Return(pile1); - pile1->SetState(-2); // passes final - } - - val = pile1->GivError(); - if ( val == 0 && CBotStack::m_initimer == 0 ) // mode step? - return false; // does not make the catch - - pile1->IncState(); - pile2->SetState(val); // stores the error number - pile1->SetError(0); // for now there is are more errors! - - if ( val == 0 && CBotStack::m_initimer < 0 ) // mode step? - return false; // does not make the catch - } - - // there was an interruption - // see what it returns - - CBotCatch* pc = m_ListCatch; - int state = (short)pile1->GivState(); // where were we? - val = pile2->GivState(); // what error? - pile0->SetState(1); // marking the GetRunPos - - if ( val >= 0 && state > 0 ) while ( pc != NULL ) - { - if ( --state <= 0 ) - { + int val; + + CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack +// if ( pile1 == EOX ) return true; + + if ( pile1->IfStep() ) return false; + // or find in case of recovery + CBotStack* pile0 = pj->AddStack2(); // adds an element to the secondary stack + CBotStack* pile2 = pile0->AddStack(); + + if ( pile1->GivState() == 0 ) + { + if ( m_Block->Execute(pile1) ) + { + if ( m_FinalInst == NULL ) return pj->Return(pile1); + pile1->SetState(-2); // passes final + } + + val = pile1->GivError(); + if ( val == 0 && CBotStack::m_initimer == 0 ) // mode step? + return false; // does not make the catch + + pile1->IncState(); + pile2->SetState(val); // stores the error number + pile1->SetError(0); // for now there is are more errors! + + if ( val == 0 && CBotStack::m_initimer < 0 ) // mode step? + return false; // does not make the catch + } + + // there was an interruption + // see what it returns + + CBotCatch* pc = m_ListCatch; + int state = (short)pile1->GivState(); // where were we? + val = pile2->GivState(); // what error? + pile0->SetState(1); // marking the GetRunPos + + if ( val >= 0 && state > 0 ) while ( pc != NULL ) + { + if ( --state <= 0 ) + { // request to the catch block if they feel concerned - // demande au bloc catch s'il se sent concerné - if ( !pc->TestCatch(pile2, val) ) return false; // suspend ! - pile1->IncState(); - } - if ( --state <= 0 ) - { - if ( pile2->GivVal() == true ) - { -// pile0->SetState(1); - - if ( !pc->Execute(pile2) ) return false; // performs the operation - if ( m_FinalInst == NULL ) - return pj->Return(pile2); // ends the try - - pile1->SetState(-2); // passes final - break; - } - pile1->IncState(); - } - pc = pc->m_next; - } - if ( m_FinalInst != NULL && - pile1->GivState() > 0 && val != 0 ) pile1->SetState(-1);// if stop then made the final - - if (pile1->GivState() <= -1) - { -// pile0->SetState(1); - - if (!m_FinalInst->Execute(pile2) && pile2->IsOk()) return false; - if (!pile2->IsOk()) return pj->Return(pile2); // keep this exception - pile2->SetError(pile1->GivState()==-1 ? val : 0); // gives the initial error - return pj->Return(pile2); - } - - pile1->SetState(0); // returns to the evaluation - pile0->SetState(0); // returns to the evaluation - if ( val != 0 && m_ListCatch == NULL && m_FinalInst == NULL ) - return pj->Return(pile2); // ends the try without exception - - pile1->SetError(val); // gives the error - return false; // it's not for us + // demande au bloc catch s'il se sent concerné + if ( !pc->TestCatch(pile2, val) ) return false; // suspend ! + pile1->IncState(); + } + if ( --state <= 0 ) + { + if ( pile2->GivVal() == true ) + { +// pile0->SetState(1); + + if ( !pc->Execute(pile2) ) return false; // performs the operation + if ( m_FinalInst == NULL ) + return pj->Return(pile2); // ends the try + + pile1->SetState(-2); // passes final + break; + } + pile1->IncState(); + } + pc = pc->m_next; + } + if ( m_FinalInst != NULL && + pile1->GivState() > 0 && val != 0 ) pile1->SetState(-1);// if stop then made the final + + if (pile1->GivState() <= -1) + { +// pile0->SetState(1); + + if (!m_FinalInst->Execute(pile2) && pile2->IsOk()) return false; + if (!pile2->IsOk()) return pj->Return(pile2); // keep this exception + pile2->SetError(pile1->GivState()==-1 ? val : 0); // gives the initial error + return pj->Return(pile2); + } + + pile1->SetState(0); // returns to the evaluation + pile0->SetState(0); // returns to the evaluation + if ( val != 0 && m_ListCatch == NULL && m_FinalInst == NULL ) + return pj->Return(pile2); // ends the try without exception + + pile1->SetError(val); // gives the error + return false; // it's not for us } void CBotTry :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - - int val; - CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack - if ( pile1 == NULL ) return; - // or find in case of recovery - CBotStack* pile0 = pj->AddStack2(); // adds an item to the secondary stack - if ( pile0 == NULL ) return; - - CBotStack* pile2 = pile0->RestoreStack(); - if ( pile2 == NULL ) return; - - m_Block->RestoreState(pile1, bMain); - if ( pile0->GivState() == 0 ) - { - return; - } - - // there was an interruption - // see what it returns - - CBotCatch* pc = m_ListCatch; - int state = pile1->GivState(); // where were we ? - val = pile2->GivState(); // what error ? - - if ( val >= 0 && state > 0 ) while ( pc != NULL ) - { - if ( --state <= 0 ) - { + if ( !bMain ) return; + + int val; + CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack + if ( pile1 == NULL ) return; + // or find in case of recovery + CBotStack* pile0 = pj->AddStack2(); // adds an item to the secondary stack + if ( pile0 == NULL ) return; + + CBotStack* pile2 = pile0->RestoreStack(); + if ( pile2 == NULL ) return; + + m_Block->RestoreState(pile1, bMain); + if ( pile0->GivState() == 0 ) + { + return; + } + + // there was an interruption + // see what it returns + + CBotCatch* pc = m_ListCatch; + int state = pile1->GivState(); // where were we ? + val = pile2->GivState(); // what error ? + + if ( val >= 0 && state > 0 ) while ( pc != NULL ) + { + if ( --state <= 0 ) + { // request to the catch block if they feel concerned - // demande au bloc catch s'il se sent concerné - pc->RestoreCondState(pile2, bMain); // suspend ! - return; - } - if ( --state <= 0 ) - { - if ( pile2->GivVal() == true ) - { - pc->RestoreState(pile2, bMain); // execute the operation - return; - } - } - pc = pc->m_next; - } - - if (pile1->GivState() <= -1) - { - m_FinalInst->RestoreState(pile2, bMain); - return; - } + // demande au bloc catch s'il se sent concerné + pc->RestoreCondState(pile2, bMain); // suspend ! + return; + } + if ( --state <= 0 ) + { + if ( pile2->GivVal() == true ) + { + pc->RestoreState(pile2, bMain); // execute the operation + return; + } + } + pc = pc->m_next; + } + + if (pile1->GivState() <= -1) + { + m_FinalInst->RestoreState(pile2, bMain); + return; + } } /////////////////////////////////////////////////////////////////////////// @@ -1246,81 +1246,81 @@ void CBotTry :: RestoreState(CBotStack* &pj, bool bMain) CBotCatch::CBotCatch() { - m_Cond = - m_Block = NULL; // NULL so that delete is not possible further - m_next = NULL; + m_Cond = + m_Block = NULL; // NULL so that delete is not possible further + m_next = NULL; - name = "CBotCatch"; // debug + name = "CBotCatch"; // debug } CBotCatch::~CBotCatch() { - delete m_Cond; // frees the list - delete m_Block; // frees the instruction block - delete m_next; // and subsequent + delete m_Cond; // frees the list + delete m_Block; // frees the instruction block + delete m_next; // and subsequent } CBotCatch* CBotCatch::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotCatch* inst = new CBotCatch(); // creates the object - pStack->SetStartError(p->GivStart()); - - inst->SetToken(p); - if (!IsOfType(p, ID_CATCH)) return NULL; // should never happen - - if (IsOfType(p, ID_OPENPAR)) - { - inst->m_Cond = CBotExpression::Compile(p, pStack); - if (( pStack->GivType() < CBotTypLong || - pStack->GivTypResult().Eq(CBotTypBoolean) )&& pStack->IsOk() ) - { - if (IsOfType(p, ID_CLOSEPAR)) - { - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStack ); - if ( pStack->IsOk() ) - return inst; // return an object to the application - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - pStack->SetError(TX_BADTYPE, p->GivStart()); - } - pStack->SetError(TX_OPENPAR, p->GivStart()); - delete inst; // error, frees up - return NULL; // no object, the error is on the stack + CBotCatch* inst = new CBotCatch(); // creates the object + pStack->SetStartError(p->GivStart()); + + inst->SetToken(p); + if (!IsOfType(p, ID_CATCH)) return NULL; // should never happen + + if (IsOfType(p, ID_OPENPAR)) + { + inst->m_Cond = CBotExpression::Compile(p, pStack); + if (( pStack->GivType() < CBotTypLong || + pStack->GivTypResult().Eq(CBotTypBoolean) )&& pStack->IsOk() ) + { + if (IsOfType(p, ID_CLOSEPAR)) + { + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStack ); + if ( pStack->IsOk() ) + return inst; // return an object to the application + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + pStack->SetError(TX_BADTYPE, p->GivStart()); + } + pStack->SetError(TX_OPENPAR, p->GivStart()); + delete inst; // error, frees up + return NULL; // no object, the error is on the stack } // execution of "catch" bool CBotCatch :: Execute(CBotStack* &pj) { - if ( m_Block == NULL ) return true; - return m_Block->Execute(pj); // executes the associated block + if ( m_Block == NULL ) return true; + return m_Block->Execute(pj); // executes the associated block } void CBotCatch :: RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain && m_Block != NULL ) m_Block->RestoreState(pj, bMain); + if ( bMain && m_Block != NULL ) m_Block->RestoreState(pj, bMain); } void CBotCatch :: RestoreCondState(CBotStack* &pj, bool bMain) { - m_Cond->RestoreState(pj, bMain); + m_Cond->RestoreState(pj, bMain); } // routine to see if the catch is to do or not bool CBotCatch :: TestCatch(CBotStack* &pile, int val) { - if ( !m_Cond->Execute(pile) ) return false; - - if ( val > 0 || pile->GivType() != CBotTypBoolean ) - { - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); - var->SetValInt( pile->GivVal() == val ); - pile->SetVar(var); // calls on the stack - } - - return true; + if ( !m_Cond->Execute(pile) ) return false; + + if ( val > 0 || pile->GivType() != CBotTypBoolean ) + { + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); + var->SetValInt( pile->GivVal() == val ); + pile->SetVar(var); // calls on the stack + } + + return true; } /////////////////////////////////////////////////////////////////////////// @@ -1330,72 +1330,72 @@ bool CBotCatch :: TestCatch(CBotStack* &pile, int val) CBotThrow::CBotThrow() { - m_Value = NULL; // NULL so that delete is not possible further + m_Value = NULL; // NULL so that delete is not possible further - name = "CBotThrow"; // debug + name = "CBotThrow"; // debug } CBotThrow::~CBotThrow() { - delete m_Value; + delete m_Value; } CBotInstr* CBotThrow::Compile(CBotToken* &p, CBotCStack* pStack) { - pStack->SetStartError(p->GivStart()); + pStack->SetStartError(p->GivStart()); - CBotThrow* inst = new CBotThrow(); // creates the object - inst->SetToken(p); + CBotThrow* inst = new CBotThrow(); // creates the object + inst->SetToken(p); - CBotToken* pp = p; // preserves at the ^ token (starting position) + CBotToken* pp = p; // preserves at the ^ token (starting position) - if (!IsOfType(p, ID_THROW)) return NULL; // should never happen + if (!IsOfType(p, ID_THROW)) return NULL; // should never happen - inst->m_Value = CBotExpression::Compile( p, pStack ); + inst->m_Value = CBotExpression::Compile( p, pStack ); - if (pStack->GivType() < CBotTypLong && pStack->IsOk()) - { - return inst; // return an object to the application - } - pStack->SetError(TX_BADTYPE, pp); + if (pStack->GivType() < CBotTypLong && pStack->IsOk()) + { + return inst; // return an object to the application + } + pStack->SetError(TX_BADTYPE, pp); - delete inst; // error, frees up - return NULL; // no object, the error is on the stack + delete inst; // error, frees up + return NULL; // no object, the error is on the stack } // execution of instruction "throw" bool CBotThrow :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->GivState() == 0 ) - { - if ( !m_Value->Execute(pile) ) return false; - pile->IncState(); - } + if ( pile->GivState() == 0 ) + { + if ( !m_Value->Execute(pile) ) return false; + pile->IncState(); + } - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - int val = pile->GivVal(); - if ( val < 0 ) val = TX_BADTHROW; - pile->SetError( val, &m_token ); - return pj->Return( pile ); + int val = pile->GivVal(); + if ( val < 0 ) val = TX_BADTHROW; + pile->SetError( val, &m_token ); + return pj->Return( pile ); } void CBotThrow :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - if ( pile->GivState() == 0 ) - { - m_Value->RestoreState(pile, bMain); - return; - } + if ( pile->GivState() == 0 ) + { + m_Value->RestoreState(pile, bMain); + return; + } } @@ -1405,7 +1405,7 @@ void CBotThrow :: RestoreState(CBotStack* &pj, bool bMain) CBotStartDebugDD::CBotStartDebugDD() { - name = "CBotStartDebugDD"; // debug + name = "CBotStartDebugDD"; // debug } CBotStartDebugDD::~CBotStartDebugDD() @@ -1415,9 +1415,9 @@ CBotStartDebugDD::~CBotStartDebugDD() CBotInstr* CBotStartDebugDD::Compile(CBotToken* &p, CBotCStack* pStack) { - if (!IsOfType(p, ID_DEBUGDD)) return NULL; // should never happen + if (!IsOfType(p, ID_DEBUGDD)) return NULL; // should never happen - return new CBotStartDebugDD(); // creates the object + return new CBotStartDebugDD(); // creates the object } @@ -1425,9 +1425,9 @@ CBotInstr* CBotStartDebugDD::Compile(CBotToken* &p, CBotCStack* pStack) bool CBotStartDebugDD :: Execute(CBotStack* &pj) { - CBotProgram* p = pj->GivBotCall(); - p->m_bDebugDD = true; + CBotProgram* p = pj->GivBotCall(); + p->m_bDebugDD = true; - return true; + return true; } |