blob: 66f9e902a6c8f767f23771fd3982c332fce298d9 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
2#include <stdint.h>
3
4#include "nlr.h"
5#include "misc.h"
6#include "mpconfig.h"
Damien George71c51812014-01-04 20:21:15 +00007#include "mpqstr.h"
Damiend99b0522013-12-21 18:17:45 +00008#include "obj.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 Sokolovsky76d982e2014-01-13 19:19:16 +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
Damien George71c51812014-01-04 20:21:15 +000025// args are reverse in the array
26static mp_obj_t bool_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
27 switch (n_args) {
28 case 0: return mp_const_false;
29 case 1: if (rt_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
Damien George6c73ca12014-01-08 18:11:23 +000030 default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "bool takes at most 1 argument, %d given", n_args));
Damien George71c51812014-01-04 20:21:15 +000031 }
32}
33
Damiend99b0522013-12-21 18:17:45 +000034const mp_obj_type_t bool_type = {
35 { &mp_const_type },
36 "bool",
Damien George97209d32014-01-07 15:58:30 +000037 .print = bool_print,
38 .make_new = bool_make_new,
Damiend99b0522013-12-21 18:17:45 +000039};
40
41static const mp_obj_bool_t false_obj = {{&bool_type}, false};
42static const mp_obj_bool_t true_obj = {{&bool_type}, true};
43
44const mp_obj_t mp_const_false = (mp_obj_t)&false_obj;
45const mp_obj_t mp_const_true = (mp_obj_t)&true_obj;