blob: 7983864b2c7f27d8601b605c7eb7ad266b804af6 [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/******************************************************************************/
35/* ellipsis object, a singleton */
36
37typedef struct _mp_obj_ellipsis_t {
38 mp_obj_base_t base;
39} mp_obj_ellipsis_t;
40
Damien George7f9d1d62015-04-09 23:56:15 +010041STATIC void ellipsis_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000042 (void)self_in;
43 (void)kind;
Damien George7f9d1d62015-04-09 23:56:15 +010044 mp_print_str(print, "Ellipsis");
Damien Georgee9906ac2014-01-04 18:44:46 +000045}
46
Damien George07ddab52014-03-29 13:15:08 +000047const mp_obj_type_t mp_type_ellipsis = {
Damien Georgec5966122014-02-15 16:10:44 +000048 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000049 .name = MP_QSTR_Ellipsis,
Damien George97209d32014-01-07 15:58:30 +000050 .print = ellipsis_print,
Damien Georgee9906ac2014-01-04 18:44:46 +000051};
52
Damien George07ddab52014-03-29 13:15:08 +000053const mp_obj_ellipsis_t mp_const_ellipsis_obj = {{&mp_type_ellipsis}};
Damien Georgee9906ac2014-01-04 18:44:46 +000054
55/******************************************************************************/
56/* slice object */
57
Damien Georgefb510b32014-06-01 13:32:54 +010058#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020059
60// TODO: This implements only variant of slice with 2 integer args only.
61// CPython supports 3rd arg (step), plus args can be arbitrary Python objects.
62typedef struct _mp_obj_slice_t {
63 mp_obj_base_t base;
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030064 mp_obj_t start;
65 mp_obj_t stop;
66 mp_obj_t step;
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020067} mp_obj_slice_t;
68
Damien George7f9d1d62015-04-09 23:56:15 +010069STATIC 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 +000070 (void)kind;
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020071 mp_obj_slice_t *o = o_in;
Damien George7f9d1d62015-04-09 23:56:15 +010072 mp_print_str(print, "slice(");
73 mp_obj_print_helper(print, o->start, PRINT_REPR);
74 mp_print_str(print, ", ");
75 mp_obj_print_helper(print, o->stop, PRINT_REPR);
76 mp_print_str(print, ", ");
77 mp_obj_print_helper(print, o->step, PRINT_REPR);
78 mp_print_str(print, ")");
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020079}
80
Damien George3e1a5c12014-03-29 13:43:38 +000081const mp_obj_type_t mp_type_slice = {
Damien Georgec5966122014-02-15 16:10:44 +000082 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000083 .name = MP_QSTR_slice,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020084 .print = slice_print,
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020085};
86
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020087mp_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 +030088 mp_obj_slice_t *o = m_new_obj(mp_obj_slice_t);
Damien George3e1a5c12014-03-29 13:43:38 +000089 o->base.type = &mp_type_slice;
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030090 o->start = ostart;
91 o->stop = ostop;
92 o->step = ostep;
93 return o;
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020094}
95
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030096void 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 +000097 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_slice));
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020098 mp_obj_slice_t *self = self_in;
99 *start = self->start;
100 *stop = self->stop;
Paul Sokolovskyafaaf532014-05-25 01:39:27 +0300101 *step = self->step;
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200102}
103
104#endif