1 /*
2  * The Doomsday Engine Project -- libcore
3  *
4  * Copyright © 2004-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
5  *
6  * @par License
7  * LGPL: http://www.gnu.org/licenses/lgpl.html
8  *
9  * <small>This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or (at your
12  * option) any later version. This program is distributed in the hope that it
13  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
15  * General Public License for more details. You should have received a copy of
16  * the GNU Lesser General Public License along with this program; if not, see:
17  * http://www.gnu.org/licenses</small>
18  */
19 
20 #include "de/WhileStatement"
21 #include "de/Expression"
22 #include "de/Evaluator"
23 #include "de/Context"
24 #include "de/Value"
25 #include "de/Writer"
26 #include "de/Reader"
27 
28 using namespace de;
29 
~WhileStatement()30 WhileStatement::~WhileStatement()
31 {
32     delete _loopCondition;
33 }
34 
execute(Context & context) const35 void WhileStatement::execute(Context &context) const
36 {
37     Evaluator &eval = context.evaluator();
38 
39     if (eval.evaluate(_loopCondition).isTrue())
40     {
41         // Continue and break jump points are defined within a while compound.
42         context.start(_compound.firstStatement(), this, this, this);
43     }
44     else
45     {
46         context.proceed();
47     }
48 }
49 
operator >>(Writer & to) const50 void WhileStatement::operator >> (Writer &to) const
51 {
52     to << dbyte(SerialId::While) << *_loopCondition << _compound;
53 }
54 
operator <<(Reader & from)55 void WhileStatement::operator << (Reader &from)
56 {
57     SerialId id;
58     from.readAs<dbyte>(id);
59     if (id != SerialId::While)
60     {
61         /// @throw DeserializationError The identifier that species the type of the
62         /// serialized statement was invalid.
63         throw DeserializationError("WhileStatement::operator <<", "Invalid ID");
64     }
65     delete _loopCondition;
66     _loopCondition = 0;
67     _loopCondition = Expression::constructFrom(from);
68 
69     from >> _compound;
70 }
71