valaccodeifstatement.vala 2.54 KB
Newer Older
1 2
/* valaccodeifstatement.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 an if selection statement in the C code.
 */
public class Vala.CCodeIfStatement : CCodeStatement {
	/**
	 * The boolean condition to evaluate.
	 */
32
	public CCodeExpression condition { get; set; }
33 34 35 36
	
	/**
	 * The statement to be evaluated if the condition holds.
	 */
37
	public CCodeStatement true_statement { get; set; }
38 39 40 41
	
	/**
	 * The optional statement to be evaluated if the condition doesn't hold.
	 */
42
	public CCodeStatement? false_statement { get; set; }
43
	
44
	public CCodeIfStatement (CCodeExpression cond, CCodeStatement true_stmt, CCodeStatement? false_stmt = null) {
45 46 47 48 49
		condition = cond;
		true_statement = true_stmt;
		false_statement = false_stmt;
	}
	
50 51 52 53 54 55
	/**
	 * Specifies whether this if statement is part of an else if statement.
	 * This only affects the output formatting.
	 */
	public bool else_if { get; set; }
	
56
	public override void write (CCodeWriter writer) {
57
		if (!else_if) {
58
			writer.write_indent (line);
59 60 61 62 63 64 65 66
		} else {
			writer.write_string (" ");
		}
		writer.write_string ("if (");
		if (condition != null) {
			condition.write (writer);
		}
		writer.write_string (")");
67
		
68 69 70 71 72 73 74 75 76
		/* else shouldn't be on a separate line */
		if (false_statement != null && true_statement is CCodeBlock) {
			var cblock = (CCodeBlock) true_statement;
			cblock.suppress_newline = true;
		}
		
		true_statement.write (writer);
		if (false_statement != null) {
			if (writer.bol) {
77
				writer.write_indent ();
78
				writer.write_string ("else");
79
			} else {
80
				writer.write_string (" else");
81
			}
82
			
83 84 85 86
			/* else if should be on one line */
			if (false_statement is CCodeIfStatement) {
				var cif = (CCodeIfStatement) false_statement;
				cif.else_if = true;
87 88
			}
			
89
			false_statement.write (writer);
90 91 92
		}
	}
}