blob: 928be6dab1427f188fb60e25580087f8587a3680 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python 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
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020027#include <stdlib.h>
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020028#include <assert.h>
29
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/nlr.h"
31#include "py/obj.h"
32#include "py/runtime0.h"
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020033
Damien Georgee9906ac2014-01-04 18:44:46 +000034/******************************************************************************/
Damien Georgee9906ac2014-01-04 18:44:46 +000035/* slice object */
36
Damien Georgefb510b32014-06-01 13:32:54 +010037#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020038
39// TODO: This implements only variant of slice with 2 integer args only.
40// CPython supports 3rd arg (step), plus args can be arbitrary Python objects.
41typedef struct _mp_obj_slice_t {
42 mp_obj_base_t base;
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030043 mp_obj_t start;
44 mp_obj_t stop;
45 mp_obj_t step;
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020046} mp_obj_slice_t;
47
Damien George7f9d1d62015-04-09 23:56:15 +010048STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000049 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000050 mp_obj_slice_t *o = MP_OBJ_TO_PTR(o_in);
Damien George7f9d1d62015-04-09 23:56:15 +010051 mp_print_str(print, "slice(");
52 mp_obj_print_helper(print, o->start, PRINT_REPR);
53 mp_print_str(print, ", ");
54 mp_obj_print_helper(print, o->stop, PRINT_REPR);
55 mp_print_str(print, ", ");
56 mp_obj_print_helper(print, o->step, PRINT_REPR);
57 mp_print_str(print, ")");
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020058}
59
Tom Soulanilleaeb62f92015-09-11 14:31:32 -070060#if MICROPY_PY_BUILTINS_SLICE_ATTRS
61STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
62 if (dest[0] != MP_OBJ_NULL) {
63 // not load attribute
64 return;
65 }
Damien George999cedb2015-11-27 17:01:44 +000066 mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
Tom Soulanilleaeb62f92015-09-11 14:31:32 -070067 if (attr == MP_QSTR_start) {
68 dest[0] = self->start;
69 } else if (attr == MP_QSTR_stop) {
70 dest[0] = self->stop;
71 } else if (attr == MP_QSTR_step) {
72 dest[0] = self->step;
73 }
74}
75#endif
76
Damien George3e1a5c12014-03-29 13:43:38 +000077const mp_obj_type_t mp_type_slice = {
Damien Georgec5966122014-02-15 16:10:44 +000078 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000079 .name = MP_QSTR_slice,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020080 .print = slice_print,
Tom Soulanilleaeb62f92015-09-11 14:31:32 -070081#if MICROPY_PY_BUILTINS_SLICE_ATTRS
Tom Soulanille661d9d12015-09-15 14:30:52 -070082 .attr = slice_attr,
Tom Soulanilleaeb62f92015-09-11 14:31:32 -070083#endif
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020084};
85
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020086mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030087 mp_obj_slice_t *o = m_new_obj(mp_obj_slice_t);
Damien George3e1a5c12014-03-29 13:43:38 +000088 o->base.type = &mp_type_slice;
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030089 o->start = ostart;
90 o->stop = ostop;
91 o->step = ostep;
Damien George999cedb2015-11-27 17:01:44 +000092 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020093}
94
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030095void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step) {
Damien George3e1a5c12014-03-29 13:43:38 +000096 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_slice));
Damien George999cedb2015-11-27 17:01:44 +000097 mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020098 *start = self->start;
99 *stop = self->stop;
Paul Sokolovskyafaaf532014-05-25 01:39:27 +0300100 *step = self->step;
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200101}
102
103#endif