blob: 46cce264a9f2560260a140dcf72d04cf7300edcd [file] [log] [blame]
Paul Sokolovsky1c6de112014-01-03 02:41:17 +02001#include <stdlib.h>
Paul Sokolovsky1c6de112014-01-03 02:41:17 +02002#include <assert.h>
3
4#include "nlr.h"
5#include "misc.h"
6#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00007#include "qstr.h"
Paul Sokolovsky1c6de112014-01-03 02:41:17 +02008#include "obj.h"
9#include "runtime0.h"
10
Damien Georgee9906ac2014-01-04 18:44:46 +000011/******************************************************************************/
12/* ellipsis object, a singleton */
13
14typedef struct _mp_obj_ellipsis_t {
15 mp_obj_base_t base;
16} mp_obj_ellipsis_t;
17
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020018void 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 +000019 print(env, "Ellipsis");
20}
21
22const mp_obj_type_t ellipsis_type = {
Damien Georgec5966122014-02-15 16:10:44 +000023 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000024 .name = MP_QSTR_Ellipsis,
Damien George97209d32014-01-07 15:58:30 +000025 .print = ellipsis_print,
Damien Georgee9906ac2014-01-04 18:44:46 +000026};
27
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020028STATIC const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}};
Damien Georgee9906ac2014-01-04 18:44:46 +000029const mp_obj_t mp_const_ellipsis = (mp_obj_t)&ellipsis_obj;
30
31/******************************************************************************/
32/* slice object */
33
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020034#if MICROPY_ENABLE_SLICE
35
36// TODO: This implements only variant of slice with 2 integer args only.
37// CPython supports 3rd arg (step), plus args can be arbitrary Python objects.
38typedef struct _mp_obj_slice_t {
39 mp_obj_base_t base;
40 machine_int_t start;
41 machine_int_t stop;
42} mp_obj_slice_t;
43
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020044void 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 +020045 mp_obj_slice_t *o = o_in;
46 print(env, "slice(" INT_FMT ", " INT_FMT ")", o->start, o->stop);
47}
48
49const mp_obj_type_t slice_type = {
Damien Georgec5966122014-02-15 16:10:44 +000050 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000051 .name = MP_QSTR_slice,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020052 .print = slice_print,
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020053};
54
55// TODO: Make sure to handle "empty" values, which are signified by None in CPython
56mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
57 assert(ostep == NULL);
Paul Sokolovsky59800af2014-01-03 23:35:32 +020058 machine_int_t start = 0, stop = 0;
59 if (ostart != mp_const_none) {
60 start = mp_obj_get_int(ostart);
61 }
62 if (ostop != mp_const_none) {
63 stop = mp_obj_get_int(ostop);
64 if (stop == 0) {
65 // [x:0] is a special case - in our slice object, stop = 0 means
66 // "end of sequence". Fortunately, [x:0] is an empty seqence for
67 // any x (including negative). [x:x] is also always empty sequence.
68 // but x also can be 0. But note that b""[x:x] is b"" for any x (i.e.
69 // no IndexError, at least in Python 3.3.3). So, we just use -1's to
70 // signify that. -1 is catchy "special" number in case someone will
71 // try to print [x:0] slice ever.
72 start = stop = -1;
73 }
74 }
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020075 mp_obj_slice_t *o = m_new(mp_obj_slice_t, 1);
76 o->base.type = &slice_type;
77 o->start = start;
78 o->stop = stop;
79 return (mp_obj_t)o;
80}
81
82void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step) {
83 assert(MP_OBJ_IS_TYPE(self_in, &slice_type));
84 mp_obj_slice_t *self = self_in;
85 *start = self->start;
86 *stop = self->stop;
87 *step = 1;
88}
89
90#endif