Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdint.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "nlr.h" |
| 7 | #include "misc.h" |
| 8 | #include "mpconfig.h" |
| 9 | #include "obj.h" |
| 10 | #include "runtime0.h" |
| 11 | |
Damien George | e9906ac | 2014-01-04 18:44:46 +0000 | [diff] [blame] | 12 | /******************************************************************************/ |
| 13 | /* ellipsis object, a singleton */ |
| 14 | |
| 15 | typedef struct _mp_obj_ellipsis_t { |
| 16 | mp_obj_base_t base; |
| 17 | } mp_obj_ellipsis_t; |
| 18 | |
| 19 | void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { |
| 20 | print(env, "Ellipsis"); |
| 21 | } |
| 22 | |
| 23 | const mp_obj_type_t ellipsis_type = { |
| 24 | { &mp_const_type }, |
| 25 | "ellipsis", |
Damien George | 97209d3 | 2014-01-07 15:58:30 +0000 | [diff] [blame] | 26 | .print = ellipsis_print, |
Damien George | e9906ac | 2014-01-04 18:44:46 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}}; |
| 30 | const mp_obj_t mp_const_ellipsis = (mp_obj_t)&ellipsis_obj; |
| 31 | |
| 32 | /******************************************************************************/ |
| 33 | /* slice object */ |
| 34 | |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 35 | #if MICROPY_ENABLE_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; |
| 41 | machine_int_t start; |
| 42 | machine_int_t stop; |
| 43 | } mp_obj_slice_t; |
| 44 | |
| 45 | void slice_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) { |
| 46 | mp_obj_slice_t *o = o_in; |
| 47 | print(env, "slice(" INT_FMT ", " INT_FMT ")", o->start, o->stop); |
| 48 | } |
| 49 | |
| 50 | const mp_obj_type_t slice_type = { |
| 51 | { &mp_const_type }, |
| 52 | "slice", |
Paul Sokolovsky | 860ffb0 | 2014-01-05 22:34:09 +0200 | [diff] [blame] | 53 | .print = slice_print, |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | // TODO: Make sure to handle "empty" values, which are signified by None in CPython |
| 57 | mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) { |
| 58 | assert(ostep == NULL); |
Paul Sokolovsky | 59800af | 2014-01-03 23:35:32 +0200 | [diff] [blame] | 59 | machine_int_t start = 0, stop = 0; |
| 60 | if (ostart != mp_const_none) { |
| 61 | start = mp_obj_get_int(ostart); |
| 62 | } |
| 63 | if (ostop != mp_const_none) { |
| 64 | stop = mp_obj_get_int(ostop); |
| 65 | if (stop == 0) { |
| 66 | // [x:0] is a special case - in our slice object, stop = 0 means |
| 67 | // "end of sequence". Fortunately, [x:0] is an empty seqence for |
| 68 | // any x (including negative). [x:x] is also always empty sequence. |
| 69 | // but x also can be 0. But note that b""[x:x] is b"" for any x (i.e. |
| 70 | // no IndexError, at least in Python 3.3.3). So, we just use -1's to |
| 71 | // signify that. -1 is catchy "special" number in case someone will |
| 72 | // try to print [x:0] slice ever. |
| 73 | start = stop = -1; |
| 74 | } |
| 75 | } |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 76 | mp_obj_slice_t *o = m_new(mp_obj_slice_t, 1); |
| 77 | o->base.type = &slice_type; |
| 78 | o->start = start; |
| 79 | o->stop = stop; |
| 80 | return (mp_obj_t)o; |
| 81 | } |
| 82 | |
| 83 | void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step) { |
| 84 | assert(MP_OBJ_IS_TYPE(self_in, &slice_type)); |
| 85 | mp_obj_slice_t *self = self_in; |
| 86 | *start = self->start; |
| 87 | *stop = self->stop; |
| 88 | *step = 1; |
| 89 | } |
| 90 | |
| 91 | #endif |