Ticket #1770: boolean.pmc

File boolean.pmc, 4.3 KB (added by Paul C. Anagnostopoulos, 4 years ago)

new boolean.pmc as of ticket creation

Line 
1/* Copyright (c) 2010, Parrot Foundation.
2$Id$
3
4=head1 NAME
5
6src/pmc/boolean.pmc - Boolean PMC
7
8=head1 DESCRIPTION
9
10This PMC implements a Boolean type with a single true/false value.
11A C<Boolean> does not morph to other types when its value is set; it simply
12changes its value.
13
14This implementation of C<Boolean> inherits from the C<Scalar> PMC.
15Unlike the previous implementation, it does I<not> inherit
16from C<Integer>.
17
18=head2 Functions
19
20=over
21
22=cut
23
24*/
25
26/* This new Boolean PMC stores its boolean value in a private PObj flag. */
27
28#define boolean_FLAG PObj_private0_FLAG
29
30#define get_boolean_FLAG(pmc) \
31    ((PObj_get_FLAGS(pmc) & boolean_FLAG) != 0)
32
33#define set_boolean_FLAG(pmc, val) \
34     if (val) \
35         PObj_get_FLAGS(pmc) |= boolean_FLAG; \
36     else \
37         PObj_get_FLAGS(pmc) &= ~boolean_FLAG;
38
39#define flip_boolean_FLAG(pmc) \
40     PObj_get_FLAGS(pmc) ^= boolean_FLAG
41
42
43pmclass Boolean extends scalar provides boolean provides scalar manual_attrs {
44
45/*
46
47=item C<void init()>
48
49Create a new C<Boolean> with initial value C<FALSE>.
50
51=item C<void init_pmc(PMC *value)>
52
53Create a new C<Boolean> with the given initial value interpreted
54as a Boolean.
55
56=cut
57
58*/
59
60    /* These two init functions set the boolean flag directly. */
61
62    VTABLE void init () {
63        set_boolean_FLAG(SELF, FALSE);
64    }
65
66    VTABLE void init_pmc (PMC *value) {
67        INTVAL v = PMC_IS_NULL(value) ? FALSE : VTABLE_get_bool(INTERP, value);
68        set_boolean_FLAG(SELF, v);
69    }
70
71/*
72
73=item C<INTVAL get_bool()>
74
75Obtain the value of the C<Boolean> as an integer: 1 = C<TRUE>, 0 = C<FALSE>.
76
77=item C<INTVAL get_integer()>
78
79Same as C<get_bool()>.
80
81=item C<FLOATVAL get_number()>
82
83Obtain the value of the C<Boolean> as a float: 1.0 = C<TRUE>, 0.0 = C<FALSE>.
84
85=item C<STRING *get_string()>
86
87Obtain the value of the C<Boolean> as a string: "1" = C<TRUE>, "0" = C<FALSE>.
88
89=cut
90
91*/
92
93    VTABLE INTVAL get_bool () {
94        return get_boolean_FLAG(SELF);
95    }
96
97    VTABLE INTVAL get_integer () {
98        return SELF.get_bool();
99    }     
100
101    VTABLE FLOATVAL get_number () {
102        INTVAL value = SELF.get_bool();
103        return (FLOATVAL)value;
104    }     
105
106    VTABLE STRING *get_string () {
107        return Parrot_str_from_int(INTERP, SELF.get_integer());
108    }
109
110/*
111
112=item C<void set_bool(INTVAL value)>
113
114Sets the value of the Boolean to the specified integer value: 0 = C<FALSE>, non-0 =
115C<TRUE>.
116
117=item C<void set_integer_native(INTVAL value)>
118
119Same as C<set_bool()>.
120
121=item C<void set_number_native(FLOATVAL value)>
122
123Sets the value of the Boolean to the specified float value: 0.0 = C<FALSE>, non-0.0 =
124C<TRUE>.
125
126=item C<void set_string_native(STRING *value)>
127
128Sets the Boolean to the value represented by the specified string. All values are
129considered C<TRUE> except for C<""> and C<"0>", which are considered
130C<FALSE>.
131
132=cut
133
134*/
135
136    VTABLE void set_bool (INTVAL value) {
137        set_boolean_FLAG(SELF, value);
138    }
139
140    VTABLE void set_integer_native (INTVAL value) {
141        SELF.set_bool(value);
142    }
143
144    VTABLE void set_number_native (FLOATVAL value) {
145        SELF.set_bool(!FLOAT_IS_ZERO(value));
146    }
147
148    VTABLE void set_string_native (STRING *value) {
149        SELF.set_bool(Parrot_str_boolean(INTERP, value));
150    }
151
152/**    VTABLE PMC *neg (PMC *dest) {
153        dest = Parrot_pmc_new_init_int(INTERP, VTABLE_type(INTERP, SELF),
154                                       SELF.get_bool());
155        return dest;
156    }
157
158    VTABLE void i_neg () {
159        /* Nothing to do for an in-place negate.
160    }**/
161
162    /* No POD documentation, since the reader should see Scalar. */
163
164    VTABLE void i_logical_not () {
165        flip_boolean_FLAG(SELF);
166    }
167
168/*
169
170=item C<void freeze(PMC *info)>
171
172Used to archive the C<Boolean>.
173
174=item C<void thaw(PMC *info)>
175
176Used to unarchive the C<Boolean>.
177
178=cut
179
180*/
181
182    VTABLE void freeze (PMC *info) {
183        SUPER(info);
184        VTABLE_push_integer(INTERP, info, SELF.get_bool());
185    }
186
187    VTABLE void thaw (PMC *info) {
188        SUPER(info);
189        SELF.set_bool(VTABLE_shift_integer(INTERP, info));
190    }
191
192}
193
194/*
195
196=back
197
198See also the C<Scalar> PMC.
199
200=cut
201
202*/
203
204/* Local variables:
205 *   c-file-style: "parrot"
206 * End:
207 * vim: expandtab shiftwidth=4:
208 */