blob: e723fad334cb99395f07172141b95b7e95e8518c [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)) {
Damien George40f3c022014-07-03 13:25:24 +0100102 mp_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 }
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300116#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +0000117 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000118 mp_float_t real, imag;
119 mp_obj_complex_get(o_in, &real, &imag);
Damien George0c36da02014-03-08 15:24:39 +0000120 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
Damien660365e2013-12-17 18:27:24 +0000121#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300122#endif
Damien660365e2013-12-17 18:27:24 +0000123 } else {
124 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000125 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +0000126 }
127}
128
Damien George23005372014-01-13 19:39:01 +0000129MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
130
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200131STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100132 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000133 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100134 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100135 if (!mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000136 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000137 }
138 }
Damiend99b0522013-12-21 18:17:45 +0000139 return mp_const_true;
Damien660365e2013-12-17 18:27:24 +0000140}
141
Damien George23005372014-01-13 19:39:01 +0000142MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
143
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200144STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100145 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000146 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100147 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100148 if (mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000149 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000150 }
151 }
Damiend99b0522013-12-21 18:17:45 +0000152 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000153}
154
Damien George23005372014-01-13 19:39:01 +0000155MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
156
Damien George897fe0c2014-04-15 22:03:55 +0100157STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
158 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 +0200159 return mp_obj_str_format(MP_ARRAY_SIZE(args), args);
Damien George897fe0c2014-04-15 22:03:55 +0100160}
161
162MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);
163
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200164STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000165 if (mp_obj_is_callable(o_in)) {
166 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000167 } else {
Damiend99b0522013-12-21 18:17:45 +0000168 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000169 }
170}
171
Damien George23005372014-01-13 19:39:01 +0000172MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
173
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200174STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300175 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien George40f3c022014-07-03 13:25:24 +0100176 mp_int_t c = mp_obj_get_int(o_in);
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000177 char str[4];
178 int len = 0;
179 if (c < 0x80) {
180 *str = c; len = 1;
181 } else if (c < 0x800) {
182 str[0] = (c >> 6) | 0xC0;
183 str[1] = (c & 0x3F) | 0x80;
184 len = 2;
185 } else if (c < 0x10000) {
186 str[0] = (c >> 12) | 0xE0;
187 str[1] = ((c >> 6) & 0x3F) | 0x80;
188 str[2] = (c & 0x3F) | 0x80;
189 len = 3;
190 } else if (c < 0x110000) {
191 str[0] = (c >> 18) | 0xF0;
192 str[1] = ((c >> 12) & 0x3F) | 0x80;
193 str[2] = ((c >> 6) & 0x3F) | 0x80;
194 str[3] = (c & 0x3F) | 0x80;
195 len = 4;
Damiena3dcd9e2013-12-17 21:35:38 +0000196 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100197 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
Damiena3dcd9e2013-12-17 21:35:38 +0000198 }
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000199 return mp_obj_new_str(str, len, true);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300200 #else
Damien George40f3c022014-07-03 13:25:24 +0100201 mp_int_t ord = mp_obj_get_int(o_in);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300202 if (0 <= ord && ord <= 0x10ffff) {
203 char str[1] = {ord};
204 return mp_obj_new_str(str, 1, true);
205 } else {
206 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
207 }
208 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000209}
210
Damien George23005372014-01-13 19:39:01 +0000211MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
212
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200213STATIC mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) {
Damien George4acb2452014-02-02 22:07:44 +0000214 // TODO make this function more general and less of a hack
215
Damien George7efc5b32014-04-05 22:36:42 +0100216 mp_obj_dict_t *dict = NULL;
Damien George4acb2452014-02-02 22:07:44 +0000217 if (n_args == 0) {
218 // make a list of names in the local name space
Damien George7efc5b32014-04-05 22:36:42 +0100219 dict = mp_locals_get();
Damien George4acb2452014-02-02 22:07:44 +0000220 } else { // n_args == 1
221 // make a list of names in the given object
Damien George6022d9d2014-03-26 22:35:00 +0000222 if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
Damien George7efc5b32014-04-05 22:36:42 +0100223 dict = mp_obj_module_get_globals(args[0]);
Damien George6022d9d2014-03-26 22:35:00 +0000224 } else {
225 mp_obj_type_t *type;
226 if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
227 type = args[0];
228 } else {
229 type = mp_obj_get_type(args[0]);
230 }
Damien George3e1a5c12014-03-29 13:43:38 +0000231 if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
Damien George7efc5b32014-04-05 22:36:42 +0100232 dict = type->locals_dict;
Damien George6022d9d2014-03-26 22:35:00 +0000233 }
Damien Georgebadc9d42014-03-23 00:03:11 +0000234 }
Damien George4acb2452014-02-02 22:07:44 +0000235 }
236
237 mp_obj_t dir = mp_obj_new_list(0, NULL);
Damien George7efc5b32014-04-05 22:36:42 +0100238 if (dict != NULL) {
239 for (uint i = 0; i < dict->map.alloc; i++) {
240 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
241 mp_obj_list_append(dir, dict->map.table[i].key);
Damien Georgebadc9d42014-03-23 00:03:11 +0000242 }
243 }
244 }
Damien George9b196cd2014-03-26 21:47:19 +0000245
Damien George4acb2452014-02-02 22:07:44 +0000246 return dir;
247}
248
249MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);
250
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200251STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
Damiend99b0522013-12-21 18:17:45 +0000252 if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
Damien George40f3c022014-07-03 13:25:24 +0100253 mp_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
254 mp_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
Damien George20006db2014-01-18 14:10:48 +0000255 mp_obj_t args[2];
256 args[0] = MP_OBJ_NEW_SMALL_INT(i1 / i2);
257 args[1] = MP_OBJ_NEW_SMALL_INT(i1 % i2);
Damien George15d18062014-03-31 16:28:13 +0100258 return mp_obj_new_tuple(2, args);
Damiena3dcd9e2013-12-17 21:35:38 +0000259 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100260 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 +0000261 }
262}
263
Damien George23005372014-01-13 19:39:01 +0000264MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
265
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200266STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000267 // TODO hash will generally overflow small integer; can we safely truncate it?
Damiend99b0522013-12-21 18:17:45 +0000268 return mp_obj_new_int(mp_obj_hash(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000269}
270
Damiend99b0522013-12-21 18:17:45 +0000271MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
272
Damien George58051112014-04-15 12:42:52 +0100273STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) {
Damien Georgeb013aea2014-04-15 12:50:21 +0100274 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 +0100275}
276
277MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex);
278
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200279STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100280 return mp_getiter(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000281}
282
Damiend99b0522013-12-21 18:17:45 +0000283MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000284
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200285STATIC mp_obj_t mp_builtin_len(mp_obj_t o_in) {
John R. Lenton4bee76e2014-01-10 11:25:03 +0000286 mp_obj_t len = mp_obj_len_maybe(o_in);
287 if (len == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100288 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 +0000289 } else {
290 return len;
Damiena3dcd9e2013-12-17 21:35:38 +0000291 }
Damiena3dcd9e2013-12-17 21:35:38 +0000292}
293
Damien George23005372014-01-13 19:39:01 +0000294MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
295
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200296STATIC mp_obj_t mp_builtin_max(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 max_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 (max_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, max_obj, item) == mp_const_true)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000304 max_obj = item;
305 }
306 }
307 if (max_obj == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100308 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "max() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000309 }
310 return max_obj;
311 } else {
312 // given many args
Damiend99b0522013-12-21 18:17:45 +0000313 mp_obj_t max_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, max_obj, args[i]) == mp_const_true) {
Damiena3dcd9e2013-12-17 21:35:38 +0000316 max_obj = args[i];
317 }
318 }
319 return max_obj;
320 }
321}
322
Damien George23005372014-01-13 19:39:01 +0000323MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max);
324
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200325STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000326 if (n_args == 1) {
327 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100328 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000329 mp_obj_t min_obj = NULL;
330 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100331 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Andrew Scheller37067662014-05-01 21:21:43 +0100332 if (min_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, item, min_obj) == mp_const_true)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000333 min_obj = item;
334 }
335 }
336 if (min_obj == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100337 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "min() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000338 }
339 return min_obj;
340 } else {
341 // given many args
Damiend99b0522013-12-21 18:17:45 +0000342 mp_obj_t min_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000343 for (int i = 1; i < n_args; i++) {
Andrew Scheller37067662014-05-01 21:21:43 +0100344 if (mp_binary_op(MP_BINARY_OP_LESS, args[i], min_obj) == mp_const_true) {
Damiena3dcd9e2013-12-17 21:35:38 +0000345 min_obj = args[i];
346 }
347 }
348 return min_obj;
349 }
350}
351
Damien George23005372014-01-13 19:39:01 +0000352MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
353
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200354STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
Damien Georged17926d2014-03-30 13:35:08 +0100355 mp_obj_t ret = mp_iternext_allow_raise(o);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100356 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100357 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000358 } else {
359 return ret;
360 }
Damiend99b0522013-12-21 18:17:45 +0000361}
362
363MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
364
Damien George897fe0c2014-04-15 22:03:55 +0100365STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
366 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in);
367}
368
369MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
370
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200371STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damien George55baff42014-01-21 21:40:13 +0000372 uint len;
Damien George698ec212014-02-08 18:17:23 +0000373 const char *str = mp_obj_str_get_data(o_in, &len);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300374 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien George40f3c022014-07-03 13:25:24 +0100375 mp_uint_t charlen = unichar_charlen(str, len);
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000376 if (charlen == 1) {
377 if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
Damien George40f3c022014-07-03 13:25:24 +0100378 mp_int_t ord = *str++ & 0x7F;
379 for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000380 ord &= ~mask;
381 }
382 while (UTF8_IS_CONT(*str)) {
383 ord = (ord << 6) | (*str++ & 0x3F);
384 }
385 return mp_obj_new_int(ord);
386 } else {
387 return mp_obj_new_int(((const byte*)str)[0]);
388 }
Damiena3dcd9e2013-12-17 21:35:38 +0000389 } else {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000390 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "ord() expected a character, but string of length %d found", charlen));
Damiena3dcd9e2013-12-17 21:35:38 +0000391 }
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300392 #else
393 if (len == 1) {
394 // don't sign extend when converting to ord
395 return mp_obj_new_int(((const byte*)str)[0]);
396 } else {
397 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "ord() expected a character, but string of length %d found", len));
398 }
399 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000400}
401
Damien George23005372014-01-13 19:39:01 +0000402MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
403
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200404STATIC mp_obj_t mp_builtin_pow(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000405 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000406 switch (n_args) {
Damien Georged17926d2014-03-30 13:35:08 +0100407 case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]);
408 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 +0000409 }
410}
411
Damien George23005372014-01-13 19:39:01 +0000412MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
413
Damien George48815662014-04-02 10:34:44 +0100414STATIC mp_obj_t mp_builtin_print(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
415 mp_map_elem_t *sep_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_MAP_LOOKUP);
416 mp_map_elem_t *end_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_MAP_LOOKUP);
417 const char *sep_data = " ";
418 uint sep_len = 1;
419 const char *end_data = "\n";
420 uint end_len = 1;
421 if (sep_elem != NULL && sep_elem->value != mp_const_none) {
422 sep_data = mp_obj_str_get_data(sep_elem->value, &sep_len);
423 }
424 if (end_elem != NULL && end_elem->value != mp_const_none) {
425 end_data = mp_obj_str_get_data(end_elem->value, &end_len);
426 }
Damiena3dcd9e2013-12-17 21:35:38 +0000427 for (int i = 0; i < n_args; i++) {
428 if (i > 0) {
Damien George48815662014-04-02 10:34:44 +0100429 printf("%.*s", sep_len, sep_data);
Damiena3dcd9e2013-12-17 21:35:38 +0000430 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200431 mp_obj_print(args[i], PRINT_STR);
Damiena3dcd9e2013-12-17 21:35:38 +0000432 }
Damien George48815662014-04-02 10:34:44 +0100433 printf("%.*s", end_len, end_data);
Damiend99b0522013-12-21 18:17:45 +0000434 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000435}
436
Damien George48815662014-04-02 10:34:44 +0100437MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
Damien George23005372014-01-13 19:39:01 +0000438
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200439STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000440 vstr_t *vstr = vstr_new();
Damien George4899ff92014-01-15 22:39:03 +0000441 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
Damien George2617eeb2014-05-25 22:27:57 +0100442 mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
Damien George5fa93b62014-01-22 14:35:10 +0000443 vstr_free(vstr);
444 return s;
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000445}
446
447MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
448
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200449STATIC mp_obj_t mp_builtin_sum(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000450 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000451 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000452 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000453 case 1: value = mp_obj_new_int(0); break;
Damien George23005372014-01-13 19:39:01 +0000454 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000455 }
Damien Georged17926d2014-03-30 13:35:08 +0100456 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000457 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100458 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100459 value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
Damiena3dcd9e2013-12-17 21:35:38 +0000460 }
461 return value;
462}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000463
Damien George23005372014-01-13 19:39:01 +0000464MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000465
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200466STATIC 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 +0000467 assert(n_args >= 1);
468 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100469 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton5c768392014-01-13 05:12:50 +0000470 "must use keyword argument for key function"));
471 }
Damien George3e1a5c12014-03-29 13:43:38 +0000472 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 +0000473 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000474
475 return self;
476}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000477
John R. Lenton88cb1e62014-01-13 19:55:18 +0000478MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200479
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200480STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) {
Damien George40f3c022014-07-03 13:25:24 +0100481 return mp_obj_new_int((mp_int_t)o_in);
xbe0ebf8532014-02-01 19:00:41 -0800482}
483
484MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_builtin_id);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200485
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300486// See mp_load_attr() if making any changes
487STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
488 mp_obj_t dest[2];
489 // use load_method, raising or not raising exception
490 ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest);
491 if (dest[0] == MP_OBJ_NULL) {
492 return defval;
493 } else if (dest[1] == MP_OBJ_NULL) {
494 // load_method returned just a normal attribute
495 return dest[0];
496 } else {
497 // load_method returned a method, so build a bound method object
498 return mp_obj_new_bound_meth(dest[0], dest[1]);
499 }
500}
501
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300502STATIC mp_obj_t mp_builtin_getattr(uint n_args, const mp_obj_t *args) {
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300503 mp_obj_t attr = args[1];
504 if (MP_OBJ_IS_TYPE(attr, &mp_type_str)) {
505 attr = mp_obj_str_intern(attr);
506 } else if (!MP_OBJ_IS_QSTR(attr)) {
507 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "string required"));
508 }
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300509 mp_obj_t defval = MP_OBJ_NULL;
510 if (n_args > 2) {
511 defval = args[2];
512 }
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300513 return mp_load_attr_default(args[0], MP_OBJ_QSTR_VALUE(attr), defval);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200514}
515
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300516MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
Paul Sokolovskycc0af3d2014-04-06 01:00:46 +0300517
Paul Sokolovskyff306662014-05-11 19:05:42 +0300518STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
519 assert(MP_OBJ_IS_QSTR(attr_in));
520
521 mp_obj_t dest[2];
Chris Angelicodaf973a2014-06-06 03:51:03 +1000522 // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
Paul Sokolovskyff306662014-05-11 19:05:42 +0300523 // explicitly says "This is implemented by calling getattr(object, name) and seeing
524 // whether it raises an AttributeError or not.", so we should explicitly wrap this
525 // in nlr_push and handle exception.
526 mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
527
528 return MP_BOOL(dest[0] != MP_OBJ_NULL);
529}
530
531MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
532
Paul Sokolovsky080d99b2014-04-06 01:18:19 +0300533// These two are defined in terms of MicroPython API functions right away
534MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
535MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);