blob: d95ccef06fc06544da34a1b72df58cfb975eb6ad [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"
9#include "obj.h"
10#include "runtime0.h"
11
Damien Georgee9906ac2014-01-04 18:44:46 +000012/******************************************************************************/
13/* ellipsis object, a singleton */
14
15typedef struct _mp_obj_ellipsis_t {
16 mp_obj_base_t base;
17} mp_obj_ellipsis_t;
18
19void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
20 print(env, "Ellipsis");
21}
22
23const mp_obj_type_t ellipsis_type = {
24 { &mp_const_type },
25 "ellipsis",
Damien George97209d32014-01-07 15:58:30 +000026 .print = ellipsis_print,
Damien Georgee9906ac2014-01-04 18:44:46 +000027};
28
29static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}};
30const mp_obj_t mp_const_ellipsis = (mp_obj_t)&ellipsis_obj;
31
32/******************************************************************************/
33/* slice object */
34
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020035#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.
39typedef 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
45void 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
50const mp_obj_type_t slice_type = {
51 { &mp_const_type },
52 "slice",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020053 .print = slice_print,
Paul Sokolovsky1c6de112014-01-03 02:41:17 +020054};
55
56// TODO: Make sure to handle "empty" values, which are signified by None in CPython
57mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
58 assert(ostep == NULL);
Paul Sokolovsky59800af2014-01-03 23:35:32 +020059 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 Sokolovsky1c6de112014-01-03 02:41:17 +020076 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
83void 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