Line data Source code
1 : /* valarealliteral.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 real literal in the source code.
27 : */
28 62 : public class Vala.RealLiteral : Literal {
29 : /**
30 : * The literal value.
31 : */
32 1544 : public string value { get; private set; }
33 :
34 : /**
35 : * Creates a new real literal.
36 : *
37 : * @param r literal value
38 : * @param source reference to source code
39 : * @return newly created real literal
40 : */
41 1218 : public RealLiteral (string r, SourceReference? source = null) {
42 406 : value = r;
43 406 : source_reference = source;
44 : }
45 :
46 1275 : public override void accept (CodeVisitor visitor) {
47 1275 : visitor.visit_real_literal (this);
48 :
49 1275 : visitor.visit_expression (this);
50 : }
51 :
52 0 : public override bool is_pure () {
53 0 : return true;
54 : }
55 :
56 0 : public override string to_string () {
57 0 : return value;
58 : }
59 :
60 603 : public override bool check (CodeContext context) {
61 603 : if (checked) {
62 199 : return !error;
63 : }
64 :
65 404 : checked = true;
66 :
67 : string type_name;
68 404 : int suf_len = 0;
69 :
70 404 : switch (value[value.length - 1]) {
71 : case 'f':
72 : case 'F':
73 101 : suf_len = 1;
74 101 : type_name = "float";
75 101 : break;
76 : case 'd':
77 : case 'D':
78 26 : suf_len = 1;
79 26 : type_name = "double";
80 26 : break;
81 : default:
82 277 : type_name = "double";
83 277 : break;
84 : }
85 :
86 404 : switch (value[value.length - suf_len - 1]) {
87 : case 'e':
88 : case 'E':
89 : case 'p':
90 : case 'P':
91 : case '+':
92 : case '-':
93 5 : Report.error (source_reference, "exponent has no digits");
94 5 : error = true;
95 5 : break;
96 : }
97 :
98 404 : var st = (Struct) context.root.scope.lookup (type_name);
99 : // ensure attributes are already processed
100 404 : st.check (context);
101 :
102 404 : value_type = new FloatingType (st);
103 :
104 404 : return !error;
105 : }
106 :
107 321 : public override void emit (CodeGenerator codegen) {
108 321 : codegen.visit_real_literal (this);
109 :
110 321 : codegen.visit_expression (this);
111 : }
112 : }
|