summaryrefslogtreecommitdiff
path: root/ccode/valaccodeunaryexpression.vala
blob: c3e2c6b9daea31094795cb2f7a89c06abdccc6a5 (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
/* valaccodeunaryexpression.vala
 *
 * Copyright (C) 2006  Jürg Billeter
 *
 * 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
 * version 2.1 of the License, or (at your option) any later version.

 * 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;

/**
 * Represents an expression with one operand in the C code.
 */
public class Vala.CCodeUnaryExpression : CCodeExpression {
	/**
	 * The unary operator.
	 */
	public CCodeUnaryOperator operator { get; set; }
	
	/**
	 * The operand.
	 */
	public CCodeExpression! inner { get; set construct; }
	
	public CCodeUnaryExpression (CCodeUnaryOperator op, CCodeExpression! expr) {
		operator = op;
		inner = expr;
	}
	
	public override void write (CCodeWriter! writer) {
		if (operator == CCodeUnaryOperator.PLUS) {
			writer.write_string ("+");
		} else if (operator == CCodeUnaryOperator.MINUS) {
			writer.write_string ("-");
		} else if (operator == CCodeUnaryOperator.LOGICAL_NEGATION) {
			writer.write_string ("!");
		} else if (operator == CCodeUnaryOperator.BITWISE_COMPLEMENT) {
			writer.write_string ("~");
		} else if (operator == CCodeUnaryOperator.POINTER_INDIRECTION) {
			writer.write_string ("*");
		} else if (operator == CCodeUnaryOperator.ADDRESS_OF) {
			writer.write_string ("&");
		} else if (operator == CCodeUnaryOperator.PREFIX_INCREMENT) {
			writer.write_string ("++");
		} else if (operator == CCodeUnaryOperator.PREFIX_DECREMENT) {
			writer.write_string ("--");
		}

		inner.write (writer);

		if (operator == CCodeUnaryOperator.POSTFIX_INCREMENT) {
			writer.write_string ("++");
		} else if (operator == CCodeUnaryOperator.POSTFIX_DECREMENT) {
			writer.write_string ("--");
		}
	}
}

public enum Vala.CCodeUnaryOperator {
	PLUS,
	MINUS,
	LOGICAL_NEGATION,
	BITWISE_COMPLEMENT,
	POINTER_INDIRECTION,
	ADDRESS_OF,
	PREFIX_INCREMENT,
	PREFIX_DECREMENT,
	POSTFIX_INCREMENT,
	POSTFIX_DECREMENT
}