Пример #1
0
CBotInstr* CBotAddExpr::Compile(CBotToken* &p, CBotStack* pStack)
{
    CBotStack* pStk = pStack->TokenStack();                 // un bout de pile svp

    // cherche des instructions qui peuvent convenir à gauche de l'opération + ou -

    CBotInstr*  left = CBotMulExpr::Compile( p, pStk );     // expression A * B à gauche
    if (left == NULL) return pStack->Return(NULL, pStk);    // si erreur, la transmet

    // est-ce qu'on a le token + ou - ensuite ?

    if ( p->GetType() == ID_ADD ||
         p->GetType() == ID_SUB)                            // plus ou moins
    {
        CBotAddExpr* inst = new CBotAddExpr();              // élément pour opération
        inst->SetToken(p);                                  // mémorise l'opération

        int          type1, type2;
        type1 = pStack->GetType();                          // de quel type le premier opérande ?

        p = p->Next();                                      // saute le token de l'opération

        // cherche des instructions qui peuvent convenir à droite

        if ( NULL != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) )  // expression (...) à droite
        {
            // il y a un second opérande acceptable

            type2 = pStack->GetType();                      // de quel type le résultat ?

            if ( type1 == type2 )                           // les résultats sont-ils compatibles
            {
                // si ok, enregistre l'opérande dans l'objet
                inst->m_leftop = left;
                // et rend l'object à qui l'a demandé
                return pStack->Return(inst, pStk);
            }
        }

        // en cas d'erreur, libère les éléments
        delete left;
        delete inst;
        // et transmet l'erreur qui se trouve sur la pile
        return pStack->Return(NULL, pStk);
    }

    // si on n'a pas affaire à une opération + ou -
    // rend à qui l'a demandé, l'opérande (de gauche) trouvé
    // à la place de l'objet "addition"
    return pStack->Return(left, pStk);
}
Пример #2
0
CBotInstr* CBotAddExpr::Compile(CBotToken* &p, CBotStack* pStack)
{
    CBotStack* pStk = pStack->TokenStack();                 // one end of stack please

    // looking statements that may be suitable to the left of the operation + or -

    CBotInstr*  left = CBotMulExpr::Compile( p, pStk );     // expression A * B left
    if (left == nullptr) return pStack->Return(nullptr, pStk);    // if error, transmit

    // do we have the token + or - next?

    if ( p->GetType() == ID_ADD ||
         p->GetType() == ID_SUB)                            // more or less
    {
        CBotAddExpr* inst = new CBotAddExpr();              // element for operation
        inst->SetToken(p);                                  // stores the operation

        int          type1, type2;
        type1 = pStack->GetType();                          // what kind of the first operand?

        p = p->Next();                                      // skip the token of the operation

        // looking statements that may be suitable for right

        if ( nullptr != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) )  // expression (...) rigth
        {
            // there is an acceptable second operand

            type2 = pStack->GetType();                      // what kind of results?

            if ( type1 == type2 )                           // are the results consistent ?
            {
                // ok so, saves the operand in the object
                inst->m_leftop = left;
                // and makes the object on demand
                return pStack->Return(inst, pStk);
            }
        }

        // in case of error, free the elements
        delete left;
        delete inst;
        // and transmits the error that is on the stack
        return pStack->Return(nullptr, pStk);
    }

    // if we are not dealing with an operation + or -
    // goes to that requested, the operand (left) found
    // place the object "addition"
    return pStack->Return(left, pStk);
}