blob: b25ca42a14a909b0afffca84aabbc68f14b68d62 [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
Damien660365e2013-12-17 18:27:24 +000027#include <stdio.h>
Damien660365e2013-12-17 18:27:24 +000028#include <assert.h>
29
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "mpconfig.h"
Damien660365e2013-12-17 18:27:24 +000031#include "nlr.h"
32#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000034#include "obj.h"
Damien George897fe0c2014-04-15 22:03:55 +010035#include "objstr.h"
Damiend99b0522013-12-21 18:17:45 +000036#include "runtime0.h"
37#include "runtime.h"
Damien660365e2013-12-17 18:27:24 +000038#include "builtin.h"
39
Damien Georgefb510b32014-06-01 13:32:54 +010040#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000041#include <math.h>
42#endif
43
Damien Georgeb97669a2014-01-08 11:47:55 +000044// args[0] is function from class body
45// args[1] is class name
46// args[2:] are base objects
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020047STATIC mp_obj_t mp_builtin___build_class__(uint n_args, const mp_obj_t *args) {
Damien Georgeb97669a2014-01-08 11:47:55 +000048 assert(2 <= n_args);
49
Damien George7efc5b32014-04-05 22:36:42 +010050 // set the new classes __locals__ object
51 mp_obj_dict_t *old_locals = mp_locals_get();
Damien George062478e2014-01-09 20:57:50 +000052 mp_obj_t class_locals = mp_obj_new_dict(0);
Damien George7efc5b32014-04-05 22:36:42 +010053 mp_locals_set(class_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000054
55 // call the class code
Damien George882b3632014-04-02 15:56:31 +010056 mp_obj_t cell = mp_call_function_0(args[0]);
Damiena3dcd9e2013-12-17 21:35:38 +000057
58 // restore old __locals__ object
Damien Georged17926d2014-03-30 13:35:08 +010059 mp_locals_set(old_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000060
Damien Georgeb97669a2014-01-08 11:47:55 +000061 // get the class type (meta object) from the base objects
62 mp_obj_t meta;
63 if (n_args == 2) {
64 // no explicit bases, so use 'type'
Damien Georgec5966122014-02-15 16:10:44 +000065 meta = (mp_obj_t)&mp_type_type;
Damien Georgeb97669a2014-01-08 11:47:55 +000066 } else {
67 // use type of first base object
68 meta = mp_obj_get_type(args[2]);
69 }
Damien Georgeb97669a2014-01-08 11:47:55 +000070
71 // TODO do proper metaclass resolution for multiple base objects
72
Damien Georgeb97669a2014-01-08 11:47:55 +000073 // create the new class using a call to the meta object
Damien Georgeb97669a2014-01-08 11:47:55 +000074 mp_obj_t meta_args[3];
Damien George20006db2014-01-18 14:10:48 +000075 meta_args[0] = args[1]; // class name
Damien Georgeb97669a2014-01-08 11:47:55 +000076 meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases
Damien George20006db2014-01-18 14:10:48 +000077 meta_args[2] = class_locals; // dict of members
Damien Georged17926d2014-03-30 13:35:08 +010078 mp_obj_t new_class = mp_call_function_n_kw(meta, 3, 0, meta_args);
Damien Georgeb97669a2014-01-08 11:47:55 +000079
80 // store into cell if neede
81 if (cell != mp_const_none) {
82 mp_obj_cell_set(cell, new_class);
83 }
84
85 return new_class;
Damiena3dcd9e2013-12-17 21:35:38 +000086}
87
Damien Georgeb97669a2014-01-08 11:47:55 +000088MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
89
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020090STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
Damiend99b0522013-12-21 18:17:45 +000091 if (o != mp_const_none) {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020092 mp_obj_print(o, PRINT_REPR);
Damien660365e2013-12-17 18:27:24 +000093 printf("\n");
94 }
Damiend99b0522013-12-21 18:17:45 +000095 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +000096}
97
Damien George23005372014-01-13 19:39:01 +000098MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
99
Damiend99b0522013-12-21 18:17:45 +0000100mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
101 if (MP_OBJ_IS_SMALL_INT(o_in)) {
102 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
Damien660365e2013-12-17 18:27:24 +0000103 if (val < 0) {
104 val = -val;
105 }
Damiend99b0522013-12-21 18:17:45 +0000106 return MP_OBJ_NEW_SMALL_INT(val);
Damien Georgefb510b32014-06-01 13:32:54 +0100107#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000108 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000109 mp_float_t value = mp_obj_float_get(o_in);
Damien660365e2013-12-17 18:27:24 +0000110 // TODO check for NaN etc
Damiend99b0522013-12-21 18:17:45 +0000111 if (value < 0) {
112 return mp_obj_new_float(-value);
Damien660365e2013-12-17 18:27:24 +0000113 } else {
114 return o_in;
115 }
Damien George0c36da02014-03-08 15:24:39 +0000116 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000117 mp_float_t real, imag;
118 mp_obj_complex_get(o_in, &real, &imag);
Damien George0c36da02014-03-08 15:24:39 +0000119 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
Damien660365e2013-12-17 18:27:24 +0000120#endif
121 } else {
122 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000123 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +0000124 }
125}
126
Damien George23005372014-01-13 19:39:01 +0000127MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
128
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200129STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100130 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000131 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100132 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100133 if (!mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000134 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000135 }
136 }
Damiend99b0522013-12-21 18:17:45 +0000137 return mp_const_true;
Damien660365e2013-12-17 18:27:24 +0000138}
139
Damien George23005372014-01-13 19:39:01 +0000140MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
141
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200142STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100143 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000144 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100145 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100146 if (mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000147 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000148 }
149 }
Damiend99b0522013-12-21 18:17:45 +0000150 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000151}
152
Damien George23005372014-01-13 19:39:01 +0000153MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
154
Damien George897fe0c2014-04-15 22:03:55 +0100155STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
156 mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_b_brace_close_), o_in };
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200157 return mp_obj_str_format(MP_ARRAY_SIZE(args), args);
Damien George897fe0c2014-04-15 22:03:55 +0100158}
159
160MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);
161
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200162STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000163 if (mp_obj_is_callable(o_in)) {
164 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000165 } else {
Damiend99b0522013-12-21 18:17:45 +0000166 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000167 }
168}
169
Damien George23005372014-01-13 19:39:01 +0000170MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
171
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200172STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000173 int ord = mp_obj_get_int(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000174 if (0 <= ord && ord <= 0x10ffff) {
Damien George2617eeb2014-05-25 22:27:57 +0100175 char str[1] = {ord};
Damien George5fa93b62014-01-22 14:35:10 +0000176 return mp_obj_new_str(str, 1, true);
Damiena3dcd9e2013-12-17 21:35:38 +0000177 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100178 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
Damiena3dcd9e2013-12-17 21:35:38 +0000179 }
180}
181
Damien George23005372014-01-13 19:39:01 +0000182MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
183
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200184STATIC mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) {
Damien George4acb2452014-02-02 22:07:44 +0000185 // TODO make this function more general and less of a hack
186
Damien George7efc5b32014-04-05 22:36:42 +0100187 mp_obj_dict_t *dict = NULL;
Damien George4acb2452014-02-02 22:07:44 +0000188 if (n_args == 0) {
189 // make a list of names in the local name space
Damien George7efc5b32014-04-05 22:36:42 +0100190 dict = mp_locals_get();
Damien George4acb2452014-02-02 22:07:44 +0000191 } else { // n_args == 1
192 // make a list of names in the given object
Damien George6022d9d2014-03-26 22:35:00 +0000193 if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
Damien George7efc5b32014-04-05 22:36:42 +0100194 dict = mp_obj_module_get_globals(args[0]);
Damien George6022d9d2014-03-26 22:35:00 +0000195 } else {
196 mp_obj_type_t *type;
197 if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
198 type = args[0];
199 } else {
200 type = mp_obj_get_type(args[0]);
201 }
Damien George3e1a5c12014-03-29 13:43:38 +0000202 if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
Damien George7efc5b32014-04-05 22:36:42 +0100203 dict = type->locals_dict;
Damien George6022d9d2014-03-26 22:35:00 +0000204 }
Damien Georgebadc9d42014-03-23 00:03:11 +0000205 }
Damien George4acb2452014-02-02 22:07:44 +0000206 }
207
208 mp_obj_t dir = mp_obj_new_list(0, NULL);
Damien George7efc5b32014-04-05 22:36:42 +0100209 if (dict != NULL) {
210 for (uint i = 0; i < dict->map.alloc; i++) {
211 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
212 mp_obj_list_append(dir, dict->map.table[i].key);
Damien Georgebadc9d42014-03-23 00:03:11 +0000213 }
214 }
215 }
Damien George9b196cd2014-03-26 21:47:19 +0000216
Damien George4acb2452014-02-02 22:07:44 +0000217 return dir;
218}
219
220MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);
221
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200222STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
Damiend99b0522013-12-21 18:17:45 +0000223 if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
224 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
225 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
Damien George20006db2014-01-18 14:10:48 +0000226 mp_obj_t args[2];
227 args[0] = MP_OBJ_NEW_SMALL_INT(i1 / i2);
228 args[1] = MP_OBJ_NEW_SMALL_INT(i1 % i2);
Damien George15d18062014-03-31 16:28:13 +0100229 return mp_obj_new_tuple(2, args);
Damiena3dcd9e2013-12-17 21:35:38 +0000230 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100231 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unsupported operand type(s) for divmod(): '%s' and '%s'", mp_obj_get_type_str(o1_in), mp_obj_get_type_str(o2_in)));
Damiena3dcd9e2013-12-17 21:35:38 +0000232 }
233}
234
Damien George23005372014-01-13 19:39:01 +0000235MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
236
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200237STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000238 // TODO hash will generally overflow small integer; can we safely truncate it?
Damiend99b0522013-12-21 18:17:45 +0000239 return mp_obj_new_int(mp_obj_hash(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000240}
241
Damiend99b0522013-12-21 18:17:45 +0000242MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
243
Damien George58051112014-04-15 12:42:52 +0100244STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) {
Damien Georgeb013aea2014-04-15 12:50:21 +0100245 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_x), o_in);
Damien George58051112014-04-15 12:42:52 +0100246}
247
248MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex);
249
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200250STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100251 return mp_getiter(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000252}
253
Damiend99b0522013-12-21 18:17:45 +0000254MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000255
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200256STATIC mp_obj_t mp_builtin_len(mp_obj_t o_in) {
John R. Lenton4bee76e2014-01-10 11:25:03 +0000257 mp_obj_t len = mp_obj_len_maybe(o_in);
258 if (len == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100259 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
John R. Lenton4bee76e2014-01-10 11:25:03 +0000260 } else {
261 return len;
Damiena3dcd9e2013-12-17 21:35:38 +0000262 }
Damiena3dcd9e2013-12-17 21:35:38 +0000263}
264
Damien George23005372014-01-13 19:39:01 +0000265MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
266
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200267STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000268 if (n_args == 1) {
269 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100270 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000271 mp_obj_t max_obj = NULL;
272 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100273 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Andrew Scheller37067662014-05-01 21:21:43 +0100274 if (max_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, max_obj, item) == mp_const_true)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000275 max_obj = item;
276 }
277 }
278 if (max_obj == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100279 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "max() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000280 }
281 return max_obj;
282 } else {
283 // given many args
Damiend99b0522013-12-21 18:17:45 +0000284 mp_obj_t max_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000285 for (int i = 1; i < n_args; i++) {
Andrew Scheller37067662014-05-01 21:21:43 +0100286 if (mp_binary_op(MP_BINARY_OP_LESS, max_obj, args[i]) == mp_const_true) {
Damiena3dcd9e2013-12-17 21:35:38 +0000287 max_obj = args[i];
288 }
289 }
290 return max_obj;
291 }
292}
293
Damien George23005372014-01-13 19:39:01 +0000294MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max);
295
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200296STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000297 if (n_args == 1) {
298 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100299 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000300 mp_obj_t min_obj = NULL;
301 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100302 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Andrew Scheller37067662014-05-01 21:21:43 +0100303 if (min_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, item, min_obj) == mp_const_true)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000304 min_obj = item;
305 }
306 }
307 if (min_obj == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100308 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "min() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000309 }
310 return min_obj;
311 } else {
312 // given many args
Damiend99b0522013-12-21 18:17:45 +0000313 mp_obj_t min_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000314 for (int i = 1; i < n_args; i++) {
Andrew Scheller37067662014-05-01 21:21:43 +0100315 if (mp_binary_op(MP_BINARY_OP_LESS, args[i], min_obj) == mp_const_true) {
Damiena3dcd9e2013-12-17 21:35:38 +0000316 min_obj = args[i];
317 }
318 }
319 return min_obj;
320 }
321}
322
Damien George23005372014-01-13 19:39:01 +0000323MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
324
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200325STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
Damien Georged17926d2014-03-30 13:35:08 +0100326 mp_obj_t ret = mp_iternext_allow_raise(o);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100327 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100328 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000329 } else {
330 return ret;
331 }
Damiend99b0522013-12-21 18:17:45 +0000332}
333
334MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
335
Damien George897fe0c2014-04-15 22:03:55 +0100336STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
337 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in);
338}
339
340MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
341
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200342STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damien George55baff42014-01-21 21:40:13 +0000343 uint len;
Damien George698ec212014-02-08 18:17:23 +0000344 const char *str = mp_obj_str_get_data(o_in, &len);
Damien George55baff42014-01-21 21:40:13 +0000345 if (len == 1) {
Damien George698ec212014-02-08 18:17:23 +0000346 // don't sign extend when converting to ord
347 // TODO unicode
348 return mp_obj_new_int(((const byte*)str)[0]);
Damiena3dcd9e2013-12-17 21:35:38 +0000349 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100350 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "ord() expected a character, but string of length %d found", len));
Damiena3dcd9e2013-12-17 21:35:38 +0000351 }
352}
353
Damien George23005372014-01-13 19:39:01 +0000354MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
355
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200356STATIC mp_obj_t mp_builtin_pow(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000357 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000358 switch (n_args) {
Damien Georged17926d2014-03-30 13:35:08 +0100359 case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]);
360 default: return mp_binary_op(MP_BINARY_OP_MODULO, mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
Damiena3dcd9e2013-12-17 21:35:38 +0000361 }
362}
363
Damien George23005372014-01-13 19:39:01 +0000364MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
365
Damien George48815662014-04-02 10:34:44 +0100366STATIC mp_obj_t mp_builtin_print(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
367 mp_map_elem_t *sep_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_MAP_LOOKUP);
368 mp_map_elem_t *end_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_MAP_LOOKUP);
369 const char *sep_data = " ";
370 uint sep_len = 1;
371 const char *end_data = "\n";
372 uint end_len = 1;
373 if (sep_elem != NULL && sep_elem->value != mp_const_none) {
374 sep_data = mp_obj_str_get_data(sep_elem->value, &sep_len);
375 }
376 if (end_elem != NULL && end_elem->value != mp_const_none) {
377 end_data = mp_obj_str_get_data(end_elem->value, &end_len);
378 }
Damiena3dcd9e2013-12-17 21:35:38 +0000379 for (int i = 0; i < n_args; i++) {
380 if (i > 0) {
Damien George48815662014-04-02 10:34:44 +0100381 printf("%.*s", sep_len, sep_data);
Damiena3dcd9e2013-12-17 21:35:38 +0000382 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200383 mp_obj_print(args[i], PRINT_STR);
Damiena3dcd9e2013-12-17 21:35:38 +0000384 }
Damien George48815662014-04-02 10:34:44 +0100385 printf("%.*s", end_len, end_data);
Damiend99b0522013-12-21 18:17:45 +0000386 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000387}
388
Damien George48815662014-04-02 10:34:44 +0100389MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
Damien George23005372014-01-13 19:39:01 +0000390
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200391STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000392 vstr_t *vstr = vstr_new();
Damien George4899ff92014-01-15 22:39:03 +0000393 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
Damien George2617eeb2014-05-25 22:27:57 +0100394 mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
Damien George5fa93b62014-01-22 14:35:10 +0000395 vstr_free(vstr);
396 return s;
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000397}
398
399MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
400
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200401STATIC mp_obj_t mp_builtin_sum(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000402 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000403 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000404 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000405 case 1: value = mp_obj_new_int(0); break;
Damien George23005372014-01-13 19:39:01 +0000406 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000407 }
Damien Georged17926d2014-03-30 13:35:08 +0100408 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000409 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100410 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100411 value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
Damiena3dcd9e2013-12-17 21:35:38 +0000412 }
413 return value;
414}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000415
Damien George23005372014-01-13 19:39:01 +0000416MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000417
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200418STATIC mp_obj_t mp_builtin_sorted(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien George20006db2014-01-18 14:10:48 +0000419 assert(n_args >= 1);
420 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100421 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton5c768392014-01-13 05:12:50 +0000422 "must use keyword argument for key function"));
423 }
Damien George3e1a5c12014-03-29 13:43:38 +0000424 mp_obj_t self = mp_type_list.make_new((mp_obj_t)&mp_type_list, 1, 0, args);
Damien George20006db2014-01-18 14:10:48 +0000425 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000426
427 return self;
428}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000429
John R. Lenton88cb1e62014-01-13 19:55:18 +0000430MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200431
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200432STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) {
Damien George330cf6d2014-02-02 13:38:21 +0000433 return mp_obj_new_int((machine_int_t)o_in);
xbe0ebf8532014-02-01 19:00:41 -0800434}
435
436MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_builtin_id);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200437
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300438// See mp_load_attr() if making any changes
439STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
440 mp_obj_t dest[2];
441 // use load_method, raising or not raising exception
442 ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest);
443 if (dest[0] == MP_OBJ_NULL) {
444 return defval;
445 } else if (dest[1] == MP_OBJ_NULL) {
446 // load_method returned just a normal attribute
447 return dest[0];
448 } else {
449 // load_method returned a method, so build a bound method object
450 return mp_obj_new_bound_meth(dest[0], dest[1]);
451 }
452}
453
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300454STATIC mp_obj_t mp_builtin_getattr(uint n_args, const mp_obj_t *args) {
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300455 mp_obj_t attr = args[1];
456 if (MP_OBJ_IS_TYPE(attr, &mp_type_str)) {
457 attr = mp_obj_str_intern(attr);
458 } else if (!MP_OBJ_IS_QSTR(attr)) {
459 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "string required"));
460 }
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300461 mp_obj_t defval = MP_OBJ_NULL;
462 if (n_args > 2) {
463 defval = args[2];
464 }
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300465 return mp_load_attr_default(args[0], MP_OBJ_QSTR_VALUE(attr), defval);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200466}
467
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300468MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
Paul Sokolovskycc0af3d2014-04-06 01:00:46 +0300469
Paul Sokolovskyff306662014-05-11 19:05:42 +0300470STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
471 assert(MP_OBJ_IS_QSTR(attr_in));
472
473 mp_obj_t dest[2];
Chris Angelicodaf973a2014-06-06 03:51:03 +1000474 // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
Paul Sokolovskyff306662014-05-11 19:05:42 +0300475 // explicitly says "This is implemented by calling getattr(object, name) and seeing
476 // whether it raises an AttributeError or not.", so we should explicitly wrap this
477 // in nlr_push and handle exception.
478 mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
479
480 return MP_BOOL(dest[0] != MP_OBJ_NULL);
481}
482
483MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
484
Paul Sokolovsky080d99b2014-04-06 01:18:19 +0300485// These two are defined in terms of MicroPython API functions right away
486MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
487MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);