Numworks Epsilon  1.4.1
Graphing Calculator Operating System
objslice.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 #include <assert.h>
29 
30 #include "py/obj.h"
31 
32 /******************************************************************************/
33 /* slice object */
34 
35 #if MICROPY_PY_BUILTINS_SLICE
36 
37 // TODO: This implements only variant of slice with 2 integer args only.
38 // CPython supports 3rd arg (step), plus args can be arbitrary Python objects.
39 typedef struct _mp_obj_slice_t {
40  mp_obj_base_t base;
42  mp_obj_t stop;
43  mp_obj_t step;
44 } mp_obj_slice_t;
45 
46 STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
47  (void)kind;
48  mp_obj_slice_t *o = MP_OBJ_TO_PTR(o_in);
49  mp_print_str(print, "slice(");
50  mp_obj_print_helper(print, o->start, PRINT_REPR);
51  mp_print_str(print, ", ");
52  mp_obj_print_helper(print, o->stop, PRINT_REPR);
53  mp_print_str(print, ", ");
54  mp_obj_print_helper(print, o->step, PRINT_REPR);
55  mp_print_str(print, ")");
56 }
57 
58 #if MICROPY_PY_BUILTINS_SLICE_ATTRS
59 STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
60  if (dest[0] != MP_OBJ_NULL) {
61  // not load attribute
62  return;
63  }
64  mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
65  if (attr == MP_QSTR_start) {
66  dest[0] = self->start;
67  } else if (attr == MP_QSTR_stop) {
68  dest[0] = self->stop;
69  } else if (attr == MP_QSTR_step) {
70  dest[0] = self->step;
71  }
72 }
73 #endif
74 
76  { &mp_type_type },
77  .name = MP_QSTR_slice,
78  .print = slice_print,
79 #if MICROPY_PY_BUILTINS_SLICE_ATTRS
80  .attr = slice_attr,
81 #endif
82 };
83 
84 mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
85  mp_obj_slice_t *o = m_new_obj(mp_obj_slice_t);
86  o->base.type = &mp_type_slice;
87  o->start = ostart;
88  o->stop = ostop;
89  o->step = ostep;
90  return MP_OBJ_FROM_PTR(o);
91 }
92 
93 void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step) {
95  mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
96  *start = self->start;
97  *stop = self->stop;
98  *step = self->step;
99 }
100 
101 #endif
mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step)
#define assert(e)
Definition: assert.h:9
#define MP_OBJ_IS_TYPE(o, t)
Definition: obj.h:254
STATIC const uint8_t attr[]
Definition: unicode.c:51
int mp_print_str(const mp_print_t *print, const char *str)
Definition: mpprint.c:53
#define MP_OBJ_FROM_PTR(p)
Definition: obj.h:233
#define STATIC
Definition: mpconfig.h:1178
mp_print_kind_t
Definition: obj.h:412
#define MP_OBJ_NULL
Definition: obj.h:73
void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind)
Definition: obj.c:59
size_t qstr
Definition: qstr.h:48
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
void start()
Definition: rt0.cpp:31
const mp_obj_type_t mp_type_slice
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step)
qstr name
Definition: obj.h:478
uint64_t mp_obj_t
Definition: obj.h:39
#define m_new_obj(type)
Definition: misc.h:60