Numworks Epsilon  1.4.1
Graphing Calculator Operating System
objbool.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include <stdlib.h>
28 
29 #include "py/runtime.h"
30 
31 typedef struct _mp_obj_bool_t {
33  bool value;
35 
36 STATIC void bool_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
37  mp_obj_bool_t *self = MP_OBJ_TO_PTR(self_in);
38  if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
39  if (self->value) {
40  mp_print_str(print, "true");
41  } else {
42  mp_print_str(print, "false");
43  }
44  } else {
45  if (self->value) {
46  mp_print_str(print, "True");
47  } else {
48  mp_print_str(print, "False");
49  }
50  }
51 }
52 
53 STATIC mp_obj_t bool_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
54  (void)type_in;
55  mp_arg_check_num(n_args, n_kw, 0, 1, false);
56 
57  if (n_args == 0) {
58  return mp_const_false;
59  } else {
60  return mp_obj_new_bool(mp_obj_is_true(args[0]));
61  }
62 }
63 
65  if (op == MP_UNARY_OP_LEN) {
66  return MP_OBJ_NULL;
67  }
68  mp_obj_bool_t *self = MP_OBJ_TO_PTR(o_in);
69  return mp_unary_op(op, MP_OBJ_NEW_SMALL_INT(self->value));
70 }
71 
73  mp_obj_bool_t *self = MP_OBJ_TO_PTR(lhs_in);
74  return mp_binary_op(op, MP_OBJ_NEW_SMALL_INT(self->value), rhs_in);
75 }
76 
78  { &mp_type_type },
79  .name = MP_QSTR_bool,
80  .print = bool_print,
81  .make_new = bool_make_new,
82  .unary_op = bool_unary_op,
83  .binary_op = bool_binary_op,
84 };
85 
STATIC mp_obj_t bool_unary_op(mp_unary_op_t op, mp_obj_t o_in)
Definition: objbool.c:64
bool value
Definition: objbool.c:33
mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg)
Definition: runtime.c:216
STATIC void bool_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
Definition: objbool.c:36
int mp_print_str(const mp_print_t *print, const char *str)
Definition: mpprint.c:53
void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw)
Definition: argcheck.c:32
STATIC mp_obj_t bool_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args)
Definition: objbool.c:53
mp_obj_base_t base
Definition: objbool.c:32
mp_unary_op_t
Definition: runtime0.h:45
#define STATIC
Definition: mpconfig.h:1178
const mp_obj_bool_t mp_const_false_obj
Definition: objbool.c:86
#define MICROPY_PY_UJSON
Definition: mpconfig.h:1055
mp_print_kind_t
Definition: obj.h:412
#define MP_OBJ_NEW_SMALL_INT(small_int)
Definition: obj.h:87
#define MP_OBJ_NULL
Definition: obj.h:73
const mp_obj_type_t mp_type_bool
Definition: objbool.c:77
mp_binary_op_t
Definition: runtime0.h:67
mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs)
Definition: runtime.c:277
STATIC mp_obj_t bool_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in)
Definition: objbool.c:72
args
Definition: i18n.py:175
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
qstr name
Definition: obj.h:478
uint64_t mp_obj_t
Definition: obj.h:39
struct _mp_obj_bool_t mp_obj_bool_t
bool mp_obj_is_true(mp_obj_t arg)
Definition: obj.c:108
const mp_obj_bool_t mp_const_true_obj
Definition: objbool.c:87
#define mp_const_false
Definition: obj.h:615