Permalink
Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
222 lines (139 sloc) 4.28 KB
/*
Copyright (C) 2001-2014, Parrot Foundation.
=head1 NAME
src/pmc/boolean.pmc - Boolean PMC
=head1 DESCRIPTION
This PMC implements a Boolean type with a single true/false value.
A C<Boolean> does not morph to other types when its value is set; it simply
changes its value.
This implementation of C<Boolean> inherits from the C<Scalar> PMC.
Unlike the previous implementation, it does I<not> inherit
from C<Integer>.
=head2 Functions
=over
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
/* This new Boolean PMC stores its boolean value in a private PObj flag. */
#define boolean_FLAG PObj_private0_FLAG
#define get_boolean_FLAG(pmc) \
((PObj_get_FLAGS(pmc) & boolean_FLAG) != 0)
#define set_boolean_FLAG(pmc, val) \
if (val) \
PObj_get_FLAGS(pmc) |= boolean_FLAG; \
else \
PObj_get_FLAGS(pmc) &= ~boolean_FLAG;
#define flip_boolean_FLAG(pmc) \
PObj_get_FLAGS(pmc) ^= boolean_FLAG
pmclass Boolean extends scalar provides boolean provides scalar manual_attrs {
/*
=item C<void init()>
Create a new C<Boolean> with initial value C<FALSE>.
=item C<void init_pmc(PMC *value)>
Create a new C<Boolean> with the given initial value interpreted
as a Boolean.
=item C<void init_int(INTVAL value)>
Create a new C<Boolean> with the given initial value interpreted
as a Boolean.
=cut
*/
/* These init functions set the boolean flag directly. */
VTABLE void init() {
set_boolean_FLAG(SELF, 0);
}
VTABLE void init_pmc(PMC *value) {
const INTVAL v = PMC_IS_NULL(value) ? 0 : VTABLE_get_bool(INTERP, value);
set_boolean_FLAG(SELF, v);
}
VTABLE void init_int(INTVAL value) {
set_boolean_FLAG(SELF, value);
}
/*
=item C<INTVAL get_bool()>
Obtain the value of the C<Boolean> as an integer: 1 = C<TRUE>, 0 = C<FALSE>.
=item C<INTVAL get_integer()>
Same as C<get_bool()>.
=item C<FLOATVAL get_number()>
Obtain the value of the C<Boolean> as a float: 1.0 = C<TRUE>, 0.0 = C<FALSE>.
=item C<STRING *get_string()>
Obtain the value of the C<Boolean> as a string: "1" = C<TRUE>, "0" = C<FALSE>.
=cut
*/
VTABLE INTVAL get_bool() :no_wb {
UNUSED(INTERP);
return get_boolean_FLAG(SELF);
}
VTABLE INTVAL get_integer() :no_wb {
return SELF.get_bool();
}
VTABLE FLOATVAL get_number() :no_wb {
const INTVAL value = SELF.get_bool();
return (FLOATVAL)value;
}
VTABLE STRING *get_string() :no_wb {
return Parrot_str_from_int(INTERP, SELF.get_integer());
}
/*
=item C<void set_bool(INTVAL value)>
Sets the value of the Boolean to the specified integer value: 0 = C<FALSE>, non-0 =
C<TRUE>.
=item C<void set_integer_native(INTVAL value)>
Same as C<set_bool()>.
=item C<void set_number_native(FLOATVAL value)>
Sets the value of the Boolean to the specified float value: 0.0 = C<FALSE>, non-0.0 =
C<TRUE>.
=item C<void set_string_native(STRING *value)>
Sets the Boolean to the value represented by the specified string. All values are
considered C<TRUE> except for C<""> and C<"0>", which are considered
C<FALSE>.
=cut
*/
VTABLE void set_bool(INTVAL value) {
set_boolean_FLAG(SELF, value);
}
VTABLE void set_integer_native(INTVAL value) :manual_wb {
SELF.set_bool(value);
}
VTABLE void set_number_native(FLOATVAL value) :manual_wb {
SELF.set_bool(!FLOAT_IS_ZERO(value));
}
VTABLE void set_string_native(STRING *value) :manual_wb {
SELF.set_bool(Parrot_str_boolean(INTERP, value));
}
/*
=item C<INTVAL is_equal(PMC *value)>
The C<==> operation.
=cut
*/
VTABLE INTVAL is_equal(PMC *value) :no_wb {
return (INTVAL)(get_boolean_FLAG(SELF) == VTABLE_get_bool(INTERP, value));
}
/*
=item C<void freeze(PMC *info)>
Used to archive the C<Boolean>.
=item C<void thaw(PMC *info)>
Used to unarchive the C<Boolean>.
=cut
*/
VTABLE void freeze(PMC *info) :no_wb {
SUPER(info);
VTABLE_push_integer(INTERP, info, SELF.get_bool());
}
VTABLE void thaw(PMC *info) :manual_wb {
SUPER(info);
SELF.set_bool(VTABLE_shift_integer(INTERP, info));
}
}
/*
=back
See also the C<Scalar> PMC.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/