blob: 54f2b5da1e095bad6d7e21b18935f625bdb7db7f [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;
ian-v5fd8fd22014-01-06 13:51:53 -080013 bool value;
Damiend99b0522013-12-21 18:17:45 +000014} mp_obj_bool_t;
15
Damien George71c51812014-01-04 20:21:15 +000016static void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
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; }
30 default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "bool takes at most 1 argument, %d given", (void*)(machine_int_t)n_args));
31 }
32}
33
Damiend99b0522013-12-21 18:17:45 +000034const mp_obj_type_t bool_type = {
35 { &mp_const_type },
36 "bool",
37 bool_print, // print
Damien George71c51812014-01-04 20:21:15 +000038 bool_make_new, // make_new
Damiend99b0522013-12-21 18:17:45 +000039 NULL, // call_n
40 NULL, // unary_op
41 NULL, // binary_op
42 NULL, // getiter
43 NULL, // iternext
ian-v7a16fad2014-01-06 09:52:29 -080044 .methods = NULL,
Damiend99b0522013-12-21 18:17:45 +000045};
46
ian-v5fd8fd22014-01-06 13:51:53 -080047static const mp_obj_bool_t false_obj = {{&bool_type}, false};
48static const mp_obj_bool_t true_obj = {{&bool_type}, true};
Damiend99b0522013-12-21 18:17:45 +000049
50const mp_obj_t mp_const_false = (mp_obj_t)&false_obj;
51const mp_obj_t mp_const_true = (mp_obj_t)&true_obj;