blob: 70180b2db2b2f8cf2650985e8e113f164762ddf0 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020027#include <string.h>
28
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020029#include "mpconfig.h"
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "misc.h"
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020031#include "nlr.h"
32#include "qstr.h"
33#include "obj.h"
34#include "objtuple.h"
35
Damien George107c9fb2014-04-26 10:31:15 +010036#if MICROPY_ENABLE_MOD_COLLECTIONS
37
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020038typedef struct _mp_obj_namedtuple_type_t {
39 mp_obj_type_t base;
40 const char *fields;
41} mp_obj_namedtuple_type_t;
42
43typedef struct _mp_obj_namedtuple_t {
44 mp_obj_tuple_t tuple;
45} mp_obj_namedtuple_t;
46
47static inline bool is_end_tok(char c) {
48 return c == ' ' || c == ',';
49}
50
51static inline const char *skip_to_next(const char *p) {
52 while (!is_end_tok(*p)) {
53 if (*p == 0) {
54 return NULL;
55 }
56 p++;
57 }
58 while (is_end_tok(*p)) {
59 if (*p == 0) {
60 return NULL;
61 }
62 p++;
63 }
64 return p;
65}
66
67STATIC uint namedtuple_count_fields(const char *namedef) {
68 uint cnt = 0;
69 while (*namedef != 0) {
70 cnt++;
71 while (!is_end_tok(*namedef) && *namedef != 0) {
72 namedef++;
73 }
74 while (is_end_tok(*namedef) && *namedef != 0) {
75 namedef++;
76 }
77 }
78 return cnt;
79}
80
81STATIC int namedtuple_find_field(const char *name, const char *namedef) {
82 int id = 0;
83 int len = strlen(name);
84 while (namedef) {
85 if (memcmp(name, namedef, len) == 0) {
86 namedef += len;
87 if (*namedef == 0 || is_end_tok(*namedef)) {
88 return id;
89 }
90 }
91 namedef = skip_to_next(namedef);
92 id++;
93 }
94
95 return -1;
96}
97
98STATIC void namedtuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
99 mp_obj_namedtuple_t *o = o_in;
100 print(env, "%s(", qstr_str(o->tuple.base.type->name));
101 const char *fields = ((mp_obj_namedtuple_type_t*)o->tuple.base.type)->fields;
102
103 for (int i = 0; i < o->tuple.len; i++) {
104 if (i > 0) {
105 print(env, ", ");
106 }
107 const char *next = fields;
108
109 while (!is_end_tok(*next) && *next != 0) {
110 next++;
111 }
112 print(env, "%.*s=", next - fields, fields);
113 mp_obj_print_helper(print, env, o->tuple.items[i], PRINT_REPR);
114 while (is_end_tok(*next) && *next != 0) {
115 next++;
116 }
117 fields = next;
118 }
119 print(env, ")");
120}
121
122STATIC void namedtuple_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
123 mp_obj_namedtuple_t *self = self_in;
124 const char *fields = ((mp_obj_namedtuple_type_t*)self->tuple.base.type)->fields;
125 int id = namedtuple_find_field(qstr_str(attr), fields);
126 if (id < 0) {
127 return;
128 }
129 dest[0] = self->tuple.items[id];
130}
131
132STATIC bool namedtuple_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
Damien Georgeea13f402014-04-05 18:32:08 +0100133 nlr_raise(mp_obj_new_exception_msg(&mp_type_AttributeError, "can't set attribute"));
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200134}
135
136STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
137 mp_obj_namedtuple_type_t *type = type_in;
138 uint num_fields = namedtuple_count_fields(type->fields);
139 if (n_args != num_fields) {
140 // Counts include implicit "self"
Damien Georgeea13f402014-04-05 18:32:08 +0100141 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200142 "__new__() takes %d positional arguments but %d were given",
143 num_fields + 1, n_args + 1));
144 }
145 mp_obj_tuple_t *tuple = mp_obj_new_tuple(n_args, args);
146 tuple->base.type = type_in;
147 return tuple;
148}
149
Damien George07ddab52014-03-29 13:15:08 +0000150STATIC const mp_obj_tuple_t namedtuple_base_tuple = {{&mp_type_tuple}, 1, {(mp_obj_t)&mp_type_tuple}};
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200151
152mp_obj_t mp_obj_new_namedtuple_type(qstr name, const char *fields) {
153 mp_obj_namedtuple_type_t *o = m_new0(mp_obj_namedtuple_type_t, 1);
154 o->base.base.type = &mp_type_type;
155 o->base.name = name;
156 o->base.print = namedtuple_print;
157 o->base.make_new = namedtuple_make_new;
Paul Sokolovskye74f52b2014-02-27 22:49:47 +0200158 o->base.unary_op = tuple_unary_op;
159 o->base.binary_op = tuple_binary_op;
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200160 o->base.load_attr = namedtuple_load_attr;
161 o->base.store_attr = namedtuple_store_attr;
Damien George729f7b42014-04-17 22:10:53 +0100162 o->base.subscr = tuple_subscr;
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200163 o->base.bases_tuple = (mp_obj_t)&namedtuple_base_tuple;
164 o->fields = fields;
165 return o;
166}
167
168STATIC mp_obj_t new_namedtuple_type(mp_obj_t name_in, mp_obj_t fields_in) {
169 qstr name = MP_OBJ_QSTR_VALUE(name_in);
170 const char *fields = mp_obj_str_get_str(fields_in);
171 return mp_obj_new_namedtuple_type(name, fields);
172}
173MP_DEFINE_CONST_FUN_OBJ_2(mp_namedtuple_obj, new_namedtuple_type);
Damien George107c9fb2014-04-26 10:31:15 +0100174
175#endif // MICROPY_ENABLE_MOD_COLLECTIONS