Line data Source code
1 : /* valawhilestatement.vala
2 : *
3 : * Copyright (C) 2006-2010 Jürg Billeter
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Lesser General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2.1 of the License, or (at your option) any later version.
9 :
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 :
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with this library; if not, write to the Free Software
17 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * Represents a while iteration statement in the source code.
27 : */
28 3074 : public class Vala.WhileStatement : Loop, Statement {
29 : /**
30 : * Creates a new while statement.
31 : *
32 : * @param condition loop condition
33 : * @param body loop body
34 : * @param source_reference reference to source code
35 : * @return newly created while statement
36 : */
37 29472 : public WhileStatement (Expression condition, Block body, SourceReference? source_reference = null) {
38 9824 : base (condition, body, source_reference);
39 : }
40 :
41 8710 : public override void accept (CodeVisitor visitor) {
42 8710 : visitor.visit_while_statement (this);
43 : }
44 :
45 8710 : public override void accept_children (CodeVisitor visitor) {
46 8710 : condition.accept (visitor);
47 :
48 8710 : visitor.visit_end_full_expression (condition);
49 :
50 8710 : body.accept (visitor);
51 : }
52 :
53 9140 : public override bool check (CodeContext context) {
54 9140 : if (checked) {
55 0 : return !error;
56 : }
57 :
58 9140 : checked = true;
59 :
60 : // convert to simple loop
61 :
62 9140 : if (condition.is_always_true ()) {
63 : // do not generate if block if condition is always true
64 18226 : } else if (condition.is_always_false ()) {
65 : // do not generate if block if condition is always false
66 2 : body.insert_statement (0, new BreakStatement (condition.source_reference));
67 : } else {
68 9112 : var if_condition = new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, condition, condition.source_reference);
69 9112 : var true_block = new Block (condition.source_reference);
70 9112 : true_block.add_statement (new BreakStatement (condition.source_reference));
71 9112 : var if_stmt = new IfStatement (if_condition, true_block, null, condition.source_reference);
72 9112 : body.insert_statement (0, if_stmt);
73 : }
74 :
75 9140 : var loop = new LoopStatement (body, source_reference);
76 :
77 9140 : unowned Block parent_block = (Block) parent_node;
78 9140 : parent_block.replace_statement (this, loop);
79 :
80 9140 : if (!loop.check (context)) {
81 0 : error = true;
82 : }
83 :
84 9140 : return !error;
85 : }
86 : }
87 :
|