Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 27 | #include <stdlib.h> |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 30 | #include "py/nlr.h" |
| 31 | #include "py/obj.h" |
| 32 | #include "py/runtime0.h" |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 33 | |
Damien George | e9906ac | 2014-01-04 18:44:46 +0000 | [diff] [blame] | 34 | /******************************************************************************/ |
Damien George | e9906ac | 2014-01-04 18:44:46 +0000 | [diff] [blame] | 35 | /* slice object */ |
| 36 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 37 | #if MICROPY_PY_BUILTINS_SLICE |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 38 | |
| 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. |
| 41 | typedef struct _mp_obj_slice_t { |
| 42 | mp_obj_base_t base; |
Paul Sokolovsky | afaaf53 | 2014-05-25 01:39:27 +0300 | [diff] [blame] | 43 | mp_obj_t start; |
| 44 | mp_obj_t stop; |
| 45 | mp_obj_t step; |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 46 | } mp_obj_slice_t; |
| 47 | |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 48 | STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 49 | (void)kind; |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 50 | mp_obj_slice_t *o = o_in; |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 51 | 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 Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Tom Soulanille | aeb62f9 | 2015-09-11 14:31:32 -0700 | [diff] [blame] | 60 | #if MICROPY_PY_BUILTINS_SLICE_ATTRS |
| 61 | STATIC 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 | } |
| 66 | mp_obj_slice_t *self = self_in; |
| 67 | 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 George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 77 | const mp_obj_type_t mp_type_slice = { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 78 | { &mp_type_type }, |
Damien George | a71c83a | 2014-02-15 11:34:50 +0000 | [diff] [blame] | 79 | .name = MP_QSTR_slice, |
Paul Sokolovsky | 860ffb0 | 2014-01-05 22:34:09 +0200 | [diff] [blame] | 80 | .print = slice_print, |
Tom Soulanille | aeb62f9 | 2015-09-11 14:31:32 -0700 | [diff] [blame] | 81 | #if MICROPY_PY_BUILTINS_SLICE_ATTRS |
Tom Soulanille | 661d9d1 | 2015-09-15 14:30:52 -0700 | [diff] [blame^] | 82 | .attr = slice_attr, |
Tom Soulanille | aeb62f9 | 2015-09-11 14:31:32 -0700 | [diff] [blame] | 83 | #endif |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 86 | mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) { |
Paul Sokolovsky | afaaf53 | 2014-05-25 01:39:27 +0300 | [diff] [blame] | 87 | mp_obj_slice_t *o = m_new_obj(mp_obj_slice_t); |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 88 | o->base.type = &mp_type_slice; |
Paul Sokolovsky | afaaf53 | 2014-05-25 01:39:27 +0300 | [diff] [blame] | 89 | o->start = ostart; |
| 90 | o->stop = ostop; |
| 91 | o->step = ostep; |
| 92 | return o; |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Paul Sokolovsky | afaaf53 | 2014-05-25 01:39:27 +0300 | [diff] [blame] | 95 | void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step) { |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 96 | assert(MP_OBJ_IS_TYPE(self_in, &mp_type_slice)); |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 97 | mp_obj_slice_t *self = self_in; |
| 98 | *start = self->start; |
| 99 | *stop = self->stop; |
Paul Sokolovsky | afaaf53 | 2014-05-25 01:39:27 +0300 | [diff] [blame] | 100 | *step = self->step; |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | #endif |