blob: 84aa5a781a733176cce11d73caa0f54f2b94dfc2 [file] [log] [blame]
Damien Georgee8208a72014-04-04 15:08:23 +01001#include <stdlib.h>
Damiend99b0522013-12-21 18:17:45 +00002
3#include "nlr.h"
4#include "misc.h"
5#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00006#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00007#include "obj.h"
Damien George1e708fe2014-01-23 18:27:51 +00008#include "runtime0.h"
Damien George71c51812014-01-04 20:21:15 +00009#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000010
11typedef struct _mp_obj_bool_t {
12 mp_obj_base_t base;
13 bool value;
14} mp_obj_bool_t;
15
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020016STATIC 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 +000017 mp_obj_bool_t *self = self_in;
18 if (self->value) {
19 print(env, "True");
20 } else {
21 print(env, "False");
22 }
23}
24
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020025STATIC 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 +000026 // TODO check n_kw == 0
27
Damien George71c51812014-01-04 20:21:15 +000028 switch (n_args) {
29 case 0: return mp_const_false;
Damien Georged17926d2014-03-30 13:35:08 +010030 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 +000031 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 +000032 }
33}
34
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020035STATIC mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
Damien George1e708fe2014-01-23 18:27:51 +000036 machine_int_t value = ((mp_obj_bool_t*)o_in)->value;
37 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010038 case MP_UNARY_OP_BOOL: return o_in;
39 case MP_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value);
40 case MP_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value);
41 case MP_UNARY_OP_INVERT:
Damien George1e708fe2014-01-23 18:27:51 +000042 default: // no other cases
43 return MP_OBJ_NEW_SMALL_INT(~value);
44 }
45}
46
Damien Georgee8208a72014-04-04 15:08:23 +010047STATIC mp_obj_t bool_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
48 if (MP_BINARY_OP_OR <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
49 return mp_binary_op(op, MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_is_true(lhs_in)), rhs_in);
50 }
51 // operation not supported
52 return MP_OBJ_NULL;
53}
54
Damien George07ddab52014-03-29 13:15:08 +000055const mp_obj_type_t mp_type_bool = {
Damien Georgec5966122014-02-15 16:10:44 +000056 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000057 .name = MP_QSTR_bool,
Damien George97209d32014-01-07 15:58:30 +000058 .print = bool_print,
59 .make_new = bool_make_new,
Damien George1e708fe2014-01-23 18:27:51 +000060 .unary_op = bool_unary_op,
Damien Georgee8208a72014-04-04 15:08:23 +010061 .binary_op = bool_binary_op,
Damiend99b0522013-12-21 18:17:45 +000062};
63
Damien George07ddab52014-03-29 13:15:08 +000064const mp_obj_bool_t mp_const_false_obj = {{&mp_type_bool}, false};
65const mp_obj_bool_t mp_const_true_obj = {{&mp_type_bool}, true};