1 /*
2 Copyright (C) 2002-2009, Parrot Foundation.
3 License:  Artistic 2.0, see README.pod and LICENSE for details
4 
5 =head1 NAME
6 
7 src/datatypes.c - Parrot and native data types functions
8 
9 =head1 DESCRIPTION
10 
11 The functions in this file are used in .ops files to access the C<enum>
12 and C string constants for Parrot and native data types defined in
13 F<include/parrot/datatypes.h>.
14 
15 =head2 Functions
16 
17 =over 4
18 
19 =cut
20 
21 */
22 
23 #include "parrot/parrot.h"
24 
25 /* HEADERIZER HFILE: include/parrot/datatypes.h */
26 
27 /*
28 
29 =item C<INTVAL Parrot_dt_get_datatype_enum(PARROT_INTERP, STRING *type_name)>
30 
31 Return datatype C<enum> for C<STRING*> type_name.
32 
33 =cut
34 
35 */
36 
37 PARROT_EXPORT
38 PARROT_WARN_UNUSED_RESULT
39 INTVAL
Parrot_dt_get_datatype_enum(PARROT_INTERP,ARGIN (STRING * type_name))40 Parrot_dt_get_datatype_enum(PARROT_INTERP, ARGIN(STRING *type_name))
41 {
42     ASSERT_ARGS(Parrot_dt_get_datatype_enum)
43     char *type;
44     int flags = 0;
45     int i;
46 
47     if (STRING_IS_NULL(type_name) || STRING_IS_EMPTY(type_name))
48         return enum_type_undef;
49 
50     if ('&' == Parrot_str_indexed(interp, type_name, Parrot_str_length(interp, type_name) - 1)) {
51         type_name  = Parrot_str_substr(interp, type_name, 0,
52                                         Parrot_str_length(interp, type_name) - 1);
53         flags     |= enum_type_ref_flag;
54     }
55 
56     type = Parrot_str_to_cstring(interp, type_name);
57 
58     for (i = enum_first_type; i < enum_last_type; ++i) {
59         if (STREQ(data_types[i - enum_first_type].name, type)) {
60             Parrot_str_free_cstring(type);
61             return i | flags;
62         }
63     }
64 
65     Parrot_str_free_cstring(type);
66 
67     return enum_type_undef;
68 }
69 
70 /*
71 
72 =item C<STRING * Parrot_dt_get_datatype_name(PARROT_INTERP, INTVAL type)>
73 
74 Return datatype name for C<type>.
75 
76 =cut
77 
78 */
79 
80 PARROT_EXPORT
81 PARROT_WARN_UNUSED_RESULT
82 PARROT_CANNOT_RETURN_NULL
83 STRING *
Parrot_dt_get_datatype_name(PARROT_INTERP,INTVAL type)84 Parrot_dt_get_datatype_name(PARROT_INTERP, INTVAL type)
85 {
86     ASSERT_ARGS(Parrot_dt_get_datatype_name)
87     const char *s;
88     STRING *str;
89 
90     const int is_ref = type & enum_type_ref_flag;
91 
92     type &= ~enum_type_ref_flag;
93 
94     s = (type < enum_first_type || type >= enum_last_type)
95             ? "illegal"
96             : data_types[type - enum_first_type].name;
97 
98     str = Parrot_str_new_init(interp, s, strlen(s),
99             Parrot_default_encoding_ptr, PObj_external_FLAG);
100 
101     return is_ref ? Parrot_str_concat(interp, str, Parrot_str_new(interp, "&", 0)) : str;
102 }
103 
104 /*
105 
106 =item C<FLOATVAL Parrot_dt_divide_floatval_by_zero(PARROT_INTERP, FLOATVAL num)>
107 
108 Only used to generate Infinity and NaN constants in our corresponding
109 header file.
110 
111 =cut
112 
113 */
114 
115 #ifndef PARROT_HAS_INF_NAN
116 
117 PARROT_EXPORT
118 FLOATVAL
Parrot_dt_divide_floatval_by_zero(SHIM_INTERP,FLOATVAL num)119 Parrot_dt_divide_floatval_by_zero(SHIM_INTERP, FLOATVAL num)
120 {
121     ASSERT_ARGS(Parrot_dt_divide_floatval_by_zero)
122     const FLOATVAL zero = 0.0;
123     return num / zero;
124 }
125 
126 #endif
127 
128 
129 /*
130 
131 =back
132 
133 =head1 SEE ALSO
134 
135 F<include/parrot/datatypes.h>.
136 
137 =cut
138 
139 */
140 
141 
142 /*
143  * Local variables:
144  *   c-file-style: "parrot"
145  * End:
146  * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
147  */
148