~ubuntu-branches/ubuntu/raring/vala-0.20/raring-proposed

« back to all changes in this revision

Viewing changes to vala/valacharacterliteral.vala

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2013-04-05 13:45:05 UTC
  • Revision ID: package-import@ubuntu.com-20130405134505-yyk3rec9904i7p8o
Tags: upstream-0.20.1
Import upstream version 0.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* valacharacterliteral.vala
 
2
 *
 
3
 * Copyright (C) 2006-2010  Jürg Billeter
 
4
 * Copyright (C) 2006-2009  Raffaele Sandrini
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 
19
 *
 
20
 * Author:
 
21
 *      Jürg Billeter <j@bitron.ch>
 
22
 *      Raffaele Sandrini <raffaele@sandrini.ch>
 
23
 */
 
24
 
 
25
using GLib;
 
26
 
 
27
/**
 
28
 * Represents a single literal character.
 
29
 */
 
30
public class Vala.CharacterLiteral : Literal {
 
31
        /**
 
32
         * The literal value.
 
33
         */
 
34
        public string value {
 
35
                get {
 
36
                        return _value;
 
37
                }
 
38
                set {
 
39
                        _value = value;
 
40
                        
 
41
                        if (!value.validate ()) {
 
42
                                error = true;
 
43
                        }
 
44
                }
 
45
        }
 
46
        
 
47
        private string _value;
 
48
 
 
49
        /**
 
50
         * Creates a new character literal.
 
51
         *
 
52
         * @param c      character
 
53
         * @param source reference to source code
 
54
         * @return       newly created character literal
 
55
         */
 
56
        public CharacterLiteral (string c, SourceReference? source = null) {
 
57
                value = c;
 
58
                source_reference = source;
 
59
 
 
60
        }
 
61
 
 
62
        public override void accept (CodeVisitor visitor) {
 
63
                visitor.visit_character_literal (this);
 
64
 
 
65
                visitor.visit_expression (this);
 
66
        }
 
67
        
 
68
        /**
 
69
         * Returns the unicode character value this character literal
 
70
         * represents.
 
71
         *
 
72
         * @return unicode character value
 
73
         */
 
74
        public unichar get_char () {
 
75
                return value.next_char ().get_char ();
 
76
        }
 
77
 
 
78
        public override bool is_pure () {
 
79
                return true;
 
80
        }
 
81
 
 
82
        public override string to_string () {
 
83
                return value;
 
84
        }
 
85
 
 
86
        public override bool check (CodeContext context) {
 
87
                if (checked) {
 
88
                        return !error;
 
89
                }
 
90
 
 
91
                checked = true;
 
92
 
 
93
                if (get_char () < 128) {
 
94
                        value_type = new IntegerType ((Struct) context.analyzer.root_symbol.scope.lookup ("char"));
 
95
                } else {
 
96
                        value_type = new IntegerType ((Struct) context.analyzer.root_symbol.scope.lookup ("unichar"));
 
97
                }
 
98
 
 
99
                return !error;
 
100
        }
 
101
 
 
102
        public override void emit (CodeGenerator codegen) {
 
103
                codegen.visit_character_literal (this);
 
104
 
 
105
                codegen.visit_expression (this);
 
106
        }
 
107
}