blob: 924927db5f330cd74dee69529b92d6bc82133392 [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
Damien George07ddab52014-03-29 13:15:08 +000022const mp_obj_type_t mp_type_ellipsis = {
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
Damien George07ddab52014-03-29 13:15:08 +000028const mp_obj_ellipsis_t mp_const_ellipsis_obj = {{&mp_type_ellipsis}};
Damien Georgee9906ac2014-01-04 18:44:46 +000029
30/******************************************************************************/
31/* slice object */
32
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020033#if MICROPY_ENABLE_SLICE
34
35// TODO: This implements only variant of slice with 2 integer args only.
36// CPython supports 3rd arg (step), plus args can be arbitrary Python objects.
37typedef struct _mp_obj_slice_t {
38 mp_obj_base_t base;
39 machine_int_t start;
40 machine_int_t stop;
41} mp_obj_slice_t;
42
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020043void 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 +020044 mp_obj_slice_t *o = o_in;
45 print(env, "slice(" INT_FMT ", " INT_FMT ")", o->start, o->stop);
46}
47
Damien George3e1a5c12014-03-29 13:43:38 +000048const mp_obj_type_t mp_type_slice = {
Damien Georgec5966122014-02-15 16:10:44 +000049 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000050 .name = MP_QSTR_slice,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020051 .print = slice_print,
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020052};
53
54// TODO: Make sure to handle "empty" values, which are signified by None in CPython
55mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
56 assert(ostep == NULL);
Paul Sokolovsky59800af2014-01-03 23:35:32 +020057 machine_int_t start = 0, stop = 0;
58 if (ostart != mp_const_none) {
59 start = mp_obj_get_int(ostart);
60 }
61 if (ostop != mp_const_none) {
62 stop = mp_obj_get_int(ostop);
63 if (stop == 0) {
64 // [x:0] is a special case - in our slice object, stop = 0 means
65 // "end of sequence". Fortunately, [x:0] is an empty seqence for
66 // any x (including negative). [x:x] is also always empty sequence.
67 // but x also can be 0. But note that b""[x:x] is b"" for any x (i.e.
68 // no IndexError, at least in Python 3.3.3). So, we just use -1's to
69 // signify that. -1 is catchy "special" number in case someone will
70 // try to print [x:0] slice ever.
71 start = stop = -1;
72 }
73 }
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020074 mp_obj_slice_t *o = m_new(mp_obj_slice_t, 1);
Damien George3e1a5c12014-03-29 13:43:38 +000075 o->base.type = &mp_type_slice;
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020076 o->start = start;
77 o->stop = stop;
78 return (mp_obj_t)o;
79}
80
81void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step) {
Damien George3e1a5c12014-03-29 13:43:38 +000082 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_slice));
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020083 mp_obj_slice_t *self = self_in;
84 *start = self->start;
85 *stop = self->stop;
86 *step = 1;
87}
88
89#endif