summaryrefslogtreecommitdiffstats
path: root/src/CBot/CBotCompExpr.cpp
blob: c9a977cdac5953cd364523fab93d743d15eef14e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// * This file is part of the COLOBOT source code
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see  http://www.gnu.org/licenses/.

///////////////////////////////////////////////////
// expression of type Opérande1 > Opérande2
//                     Opérande1 != Opérande2
// etc.

#include "CBot.h"

// various constructeurs

CBotCompExpr::CBotCompExpr()
{
    m_leftop    =
    m_rightop   = NULL;
    name = "CBotCompExpr";
}

CBotCompExpr::~CBotCompExpr()
{
    delete  m_leftop;
    delete  m_rightop;
}

fichier plus utilise;

// compile instruction of type A < B

CBotInstr* CBotCompExpr::Compile(CBotToken* &p, CBotCStack* pStack)
{
    CBotCStack* pStk = pStack->AddStack();

    CBotInstr*  left = CBotAddExpr::Compile( p, pStk );     // expression A + B left
    if (left == NULL) return pStack->Return(NULL, pStk);    // error

    if ( p->GetType() == ID_HI ||
         p->GetType() == ID_LO ||
         p->GetType() == ID_HS ||
         p->GetType() == ID_LS ||
         p->GetType() == ID_EQ ||
         p->GetType() == ID_NE)                             // the various comparisons
    {
        CBotCompExpr* inst = new CBotCompExpr();            // element for operation
        inst->SetToken(p);                                  // stores the operation

        int          type1, type2;
        type1 = pStack->GetType();

        p = p->Next();
        if ( NULL != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) )  // expression A + B right
        {
            type2 = pStack->GetType();
            // are the results compatible
            if ( type1 == type2 )
            {
                inst->m_leftop = left;
                pStk->SetVar(new CBotVar(NULL, CBotTypBoolean));
                                                            // the result is a boolean
                return pStack->Return(inst, pStk);
            }
        }

        delete left;
        delete inst;
        return pStack->Return(NULL, pStk);
    }

    return pStack->Return(left, pStk);
}


// perform the operation

bool CBotCompExpr::Execute(CBotStack* &pStack)
{
    CBotStack* pStk1 = pStack->AddStack(this);
//  if ( pStk1 == EOX ) return TRUE;

    if ( pStk1->GetState() == 0 && !m_leftop->Execute(pStk1) ) return FALSE; // interrupted here ?

    pStk1->SetState(1);     // finished

    // requires a little more stack to not touch the result of the left
    CBotStack* pStk2 = pStk1->AddStack();

    if ( !m_rightop->Execute(pStk2) ) return FALSE; // interrupted here ?

    int     type1 = pStk1->GetType();
    int     type2 = pStk2->GetType();

    CBotVar*    result = new CBotVar( NULL, CBotTypBoolean );

    switch (GetTokenType())
    {
    case ID_LO:
        result->Lo(pStk1->GetVar(), pStk2->GetVar());       // lower
        break;
    case ID_HI:
        result->Hi(pStk1->GetVar(), pStk2->GetVar());       // higher
        break;
    case ID_LS:
        result->Ls(pStk1->GetVar(), pStk2->GetVar());       // lower or equal
        break;
    case ID_HS:
        result->Hs(pStk1->GetVar(), pStk2->GetVar());       // higher of equal
        break;
    case ID_EQ:
        result->Eq(pStk1->GetVar(), pStk2->GetVar());       // equal
        break;
    case ID_NE:
        result->Ne(pStk1->GetVar(), pStk2->GetVar());       // not equal
        break;
    }
    pStk2->SetVar(result);              // puts the result on the stack

    pStk1->Return(pStk2);               // frees the stack
    return pStack->Return(pStk1);       // transmit the result
}