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", |
| 26 | ellipsis_print, // print |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 27 | NULL, // make_new |
Damien George | e9906ac | 2014-01-04 18:44:46 +0000 | [diff] [blame] | 28 | NULL, // call_n |
| 29 | NULL, // unary_op |
| 30 | NULL, // binary_op |
| 31 | NULL, // getiter |
| 32 | NULL, // iternext |
| 33 | {{NULL, NULL},}, // method list |
| 34 | }; |
| 35 | |
| 36 | static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}}; |
| 37 | const mp_obj_t mp_const_ellipsis = (mp_obj_t)&ellipsis_obj; |
| 38 | |
| 39 | /******************************************************************************/ |
| 40 | /* slice object */ |
| 41 | |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 42 | #if MICROPY_ENABLE_SLICE |
| 43 | |
| 44 | // TODO: This implements only variant of slice with 2 integer args only. |
| 45 | // CPython supports 3rd arg (step), plus args can be arbitrary Python objects. |
| 46 | typedef struct _mp_obj_slice_t { |
| 47 | mp_obj_base_t base; |
| 48 | machine_int_t start; |
| 49 | machine_int_t stop; |
| 50 | } mp_obj_slice_t; |
| 51 | |
| 52 | void slice_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) { |
| 53 | mp_obj_slice_t *o = o_in; |
| 54 | print(env, "slice(" INT_FMT ", " INT_FMT ")", o->start, o->stop); |
| 55 | } |
| 56 | |
| 57 | const mp_obj_type_t slice_type = { |
| 58 | { &mp_const_type }, |
| 59 | "slice", |
| 60 | slice_print, |
| 61 | NULL, // call_n |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 62 | NULL, // make_new |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 63 | NULL, // unary_op |
| 64 | NULL, // binary_op |
| 65 | NULL, // getiter |
| 66 | NULL, // iternext |
| 67 | { { NULL, NULL }, }, // method list |
| 68 | }; |
| 69 | |
| 70 | // TODO: Make sure to handle "empty" values, which are signified by None in CPython |
| 71 | mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) { |
| 72 | assert(ostep == NULL); |
Paul Sokolovsky | 59800af | 2014-01-03 23:35:32 +0200 | [diff] [blame] | 73 | machine_int_t start = 0, stop = 0; |
| 74 | if (ostart != mp_const_none) { |
| 75 | start = mp_obj_get_int(ostart); |
| 76 | } |
| 77 | if (ostop != mp_const_none) { |
| 78 | stop = mp_obj_get_int(ostop); |
| 79 | if (stop == 0) { |
| 80 | // [x:0] is a special case - in our slice object, stop = 0 means |
| 81 | // "end of sequence". Fortunately, [x:0] is an empty seqence for |
| 82 | // any x (including negative). [x:x] is also always empty sequence. |
| 83 | // but x also can be 0. But note that b""[x:x] is b"" for any x (i.e. |
| 84 | // no IndexError, at least in Python 3.3.3). So, we just use -1's to |
| 85 | // signify that. -1 is catchy "special" number in case someone will |
| 86 | // try to print [x:0] slice ever. |
| 87 | start = stop = -1; |
| 88 | } |
| 89 | } |
Paul Sokolovsky | 1c6de11 | 2014-01-03 02:41:17 +0200 | [diff] [blame] | 90 | mp_obj_slice_t *o = m_new(mp_obj_slice_t, 1); |
| 91 | o->base.type = &slice_type; |
| 92 | o->start = start; |
| 93 | o->stop = stop; |
| 94 | return (mp_obj_t)o; |
| 95 | } |
| 96 | |
| 97 | void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step) { |
| 98 | assert(MP_OBJ_IS_TYPE(self_in, &slice_type)); |
| 99 | mp_obj_slice_t *self = self_in; |
| 100 | *start = self->start; |
| 101 | *stop = self->stop; |
| 102 | *step = 1; |
| 103 | } |
| 104 | |
| 105 | #endif |