blob: 6afb6e950dad7e570b5a80acd88ccfd598da080c [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001
2#include "nlr.h"
3#include "misc.h"
4#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00005#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00006#include "obj.h"
Damien George1e708fe2014-01-23 18:27:51 +00007#include "runtime0.h"
Damien George71c51812014-01-04 20:21:15 +00008#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +00009
10typedef struct _mp_obj_bool_t {
11 mp_obj_base_t base;
12 bool value;
13} mp_obj_bool_t;
14
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020015STATIC void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000016 mp_obj_bool_t *self = self_in;
17 if (self->value) {
18 print(env, "True");
19 } else {
20 print(env, "False");
21 }
22}
23
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020024STATIC mp_obj_t bool_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +000025 // TODO check n_kw == 0
26
Damien George71c51812014-01-04 20:21:15 +000027 switch (n_args) {
28 case 0: return mp_const_false;
Damien Georged17926d2014-03-30 13:35:08 +010029 case 1: if (mp_obj_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
Damien Georgec5966122014-02-15 16:10:44 +000030 default: nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bool takes at most 1 argument, %d given", n_args));
Damien George71c51812014-01-04 20:21:15 +000031 }
32}
33
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020034STATIC mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
Damien George1e708fe2014-01-23 18:27:51 +000035 machine_int_t value = ((mp_obj_bool_t*)o_in)->value;
36 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010037 case MP_UNARY_OP_BOOL: return o_in;
38 case MP_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value);
39 case MP_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value);
40 case MP_UNARY_OP_INVERT:
Damien George1e708fe2014-01-23 18:27:51 +000041 default: // no other cases
42 return MP_OBJ_NEW_SMALL_INT(~value);
43 }
44}
45
Damien George07ddab52014-03-29 13:15:08 +000046const mp_obj_type_t mp_type_bool = {
Damien Georgec5966122014-02-15 16:10:44 +000047 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000048 .name = MP_QSTR_bool,
Damien George97209d32014-01-07 15:58:30 +000049 .print = bool_print,
50 .make_new = bool_make_new,
Damien George1e708fe2014-01-23 18:27:51 +000051 .unary_op = bool_unary_op,
Damiend99b0522013-12-21 18:17:45 +000052};
53
Damien George07ddab52014-03-29 13:15:08 +000054const mp_obj_bool_t mp_const_false_obj = {{&mp_type_bool}, false};
55const mp_obj_bool_t mp_const_true_obj = {{&mp_type_bool}, true};