blob: 6324c0e49bf97e2bbd90c9d1be03e43c4fc59c14 [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
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "mpconfig.h"
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020031#include "nlr.h"
32#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020034#include "obj.h"
35#include "runtime0.h"
36
Damien Georgee9906ac2014-01-04 18:44:46 +000037/******************************************************************************/
38/* ellipsis object, a singleton */
39
40typedef struct _mp_obj_ellipsis_t {
41 mp_obj_base_t base;
42} mp_obj_ellipsis_t;
43
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020044void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damien Georgee9906ac2014-01-04 18:44:46 +000045 print(env, "Ellipsis");
46}
47
Damien George07ddab52014-03-29 13:15:08 +000048const mp_obj_type_t mp_type_ellipsis = {
Damien Georgec5966122014-02-15 16:10:44 +000049 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000050 .name = MP_QSTR_Ellipsis,
Damien George97209d32014-01-07 15:58:30 +000051 .print = ellipsis_print,
Damien Georgee9906ac2014-01-04 18:44:46 +000052};
53
Damien George07ddab52014-03-29 13:15:08 +000054const mp_obj_ellipsis_t mp_const_ellipsis_obj = {{&mp_type_ellipsis}};
Damien Georgee9906ac2014-01-04 18:44:46 +000055
56/******************************************************************************/
57/* slice object */
58
Damien Georgefb510b32014-06-01 13:32:54 +010059#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020060
61// TODO: This implements only variant of slice with 2 integer args only.
62// CPython supports 3rd arg (step), plus args can be arbitrary Python objects.
63typedef struct _mp_obj_slice_t {
64 mp_obj_base_t base;
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030065 mp_obj_t start;
66 mp_obj_t stop;
67 mp_obj_t step;
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020068} mp_obj_slice_t;
69
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020070void slice_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020071 mp_obj_slice_t *o = o_in;
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030072 print(env, "slice(");
73 mp_obj_print_helper(print, env, o->start, PRINT_REPR);
74 print(env, ", ");
75 mp_obj_print_helper(print, env, o->stop, PRINT_REPR);
76 print(env, ", ");
77 mp_obj_print_helper(print, env, o->step, PRINT_REPR);
78 print(env, ")");
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