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