blob: 729ffb4e6d487fe7b1425eabd87025e69ac3521d [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 George55baff42014-01-21 21:40:13 +00007#include "qstr.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 George20006db2014-01-18 14:10:48 +000025static mp_obj_t bool_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
26 // TODO check n_kw == 0
27
Damien George71c51812014-01-04 20:21:15 +000028 switch (n_args) {
29 case 0: return mp_const_false;
30 case 1: if (rt_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
Damien George6c73ca12014-01-08 18:11:23 +000031 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 +000032 }
33}
34
Damiend99b0522013-12-21 18:17:45 +000035const mp_obj_type_t bool_type = {
36 { &mp_const_type },
37 "bool",
Damien George97209d32014-01-07 15:58:30 +000038 .print = bool_print,
39 .make_new = bool_make_new,
Damiend99b0522013-12-21 18:17:45 +000040};
41
42static const mp_obj_bool_t false_obj = {{&bool_type}, false};
43static const mp_obj_bool_t true_obj = {{&bool_type}, true};
44
45const mp_obj_t mp_const_false = (mp_obj_t)&false_obj;
46const mp_obj_t mp_const_true = (mp_obj_t)&true_obj;