valaccodeforstatement.vala 2.53 KB
Newer Older
1 2
/* valaccodeforstatement.vala
 *
3
 * Copyright (C) 2006-2008  Jürg Billeter
4 5 6 7
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * 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 11 12 13 14 15 16 17 18 19 20 21 22 23 24

 * This library 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
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 */

using GLib;

25 26 27 28 29 30 31
/**
 * Represents a for iteration statement in the C code.
 */
public class Vala.CCodeForStatement : CCodeStatement {
	/**
	 * The loop condition.
	 */
32
	public CCodeExpression? condition { get; set; }
33

34 35 36 37
	/**
	 * The loop body.
	 */
	public CCodeStatement body { get; set; }
38

39 40
	private List<CCodeExpression> initializer = new ArrayList<CCodeExpression> ();
	private List<CCodeExpression> iterator = new ArrayList<CCodeExpression> ();
41

42
	public CCodeForStatement (CCodeExpression? condition, CCodeStatement? body = null) {
43
		this.condition = condition;
44
		this.body = body;
45
	}
46

47 48 49 50 51
	/**
	 * Appends the specified expression to the list of initializers.
	 *
	 * @param expr an initializer expression
	 */
52
	public void add_initializer (CCodeExpression expr) {
53
		initializer.add (expr);
54
	}
55

56 57 58 59 60
	/**
	 * Appends the specified expression to the iterator.
	 *
	 * @param expr an iterator expression
	 */
61
	public void add_iterator (CCodeExpression expr) {
62
		iterator.add (expr);
63
	}
64

65
	public override void write (CCodeWriter writer) {
66
		bool first;
67

68
		writer.write_indent (line);
69
		writer.write_string ("for (");
70

71 72 73 74 75 76
		first = true;
		foreach (CCodeExpression init_expr in initializer) {
			if (!first) {
				writer.write_string (", ");
			} else {
				first = false;
77
			}
78 79 80 81
			if (init_expr != null) {
				init_expr.write (writer);
			}
		}
82

83 84 85 86 87
		writer.write_string ("; ");
		if (condition != null) {
			condition.write (writer);
		}
		writer.write_string ("; ");
88

89 90 91 92 93 94
		first = true;
		foreach (CCodeExpression it_expr in iterator) {
			if (!first) {
				writer.write_string (", ");
			} else {
				first = false;
95
			}
96 97
			if (it_expr != null) {
				it_expr.write (writer);
98 99
			}
		}
100 101 102

		writer.write_string (")");
		body.write (writer);
103 104
	}
}