blob: 0c0163d005a0379af10d34d1b3783e625d9eaa7e [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 }
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) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000175 int c = mp_obj_get_int(o_in);
176 char str[4];
177 int len = 0;
178 if (c < 0x80) {
179 *str = c; len = 1;
180 } else if (c < 0x800) {
181 str[0] = (c >> 6) | 0xC0;
182 str[1] = (c & 0x3F) | 0x80;
183 len = 2;
184 } else if (c < 0x10000) {
185 str[0] = (c >> 12) | 0xE0;
186 str[1] = ((c >> 6) & 0x3F) | 0x80;
187 str[2] = (c & 0x3F) | 0x80;
188 len = 3;
189 } else if (c < 0x110000) {
190 str[0] = (c >> 18) | 0xF0;
191 str[1] = ((c >> 12) & 0x3F) | 0x80;
192 str[2] = ((c >> 6) & 0x3F) | 0x80;
193 str[3] = (c & 0x3F) | 0x80;
194 len = 4;
Damiena3dcd9e2013-12-17 21:35:38 +0000195 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100196 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
Damiena3dcd9e2013-12-17 21:35:38 +0000197 }
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000198 return mp_obj_new_str(str, len, true);
Damiena3dcd9e2013-12-17 21:35:38 +0000199}
200
Damien George23005372014-01-13 19:39:01 +0000201MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
202
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200203STATIC mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) {
Damien George4acb2452014-02-02 22:07:44 +0000204 // TODO make this function more general and less of a hack
205
Damien George7efc5b32014-04-05 22:36:42 +0100206 mp_obj_dict_t *dict = NULL;
Damien George4acb2452014-02-02 22:07:44 +0000207 if (n_args == 0) {
208 // make a list of names in the local name space
Damien George7efc5b32014-04-05 22:36:42 +0100209 dict = mp_locals_get();
Damien George4acb2452014-02-02 22:07:44 +0000210 } else { // n_args == 1
211 // make a list of names in the given object
Damien George6022d9d2014-03-26 22:35:00 +0000212 if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
Damien George7efc5b32014-04-05 22:36:42 +0100213 dict = mp_obj_module_get_globals(args[0]);
Damien George6022d9d2014-03-26 22:35:00 +0000214 } else {
215 mp_obj_type_t *type;
216 if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
217 type = args[0];
218 } else {
219 type = mp_obj_get_type(args[0]);
220 }
Damien George3e1a5c12014-03-29 13:43:38 +0000221 if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
Damien George7efc5b32014-04-05 22:36:42 +0100222 dict = type->locals_dict;
Damien George6022d9d2014-03-26 22:35:00 +0000223 }
Damien Georgebadc9d42014-03-23 00:03:11 +0000224 }
Damien George4acb2452014-02-02 22:07:44 +0000225 }
226
227 mp_obj_t dir = mp_obj_new_list(0, NULL);
Damien George7efc5b32014-04-05 22:36:42 +0100228 if (dict != NULL) {
229 for (uint i = 0; i < dict->map.alloc; i++) {
230 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
231 mp_obj_list_append(dir, dict->map.table[i].key);
Damien Georgebadc9d42014-03-23 00:03:11 +0000232 }
233 }
234 }
Damien George9b196cd2014-03-26 21:47:19 +0000235
Damien George4acb2452014-02-02 22:07:44 +0000236 return dir;
237}
238
239MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);
240
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200241STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
Damiend99b0522013-12-21 18:17:45 +0000242 if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
243 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
244 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
Damien George20006db2014-01-18 14:10:48 +0000245 mp_obj_t args[2];
246 args[0] = MP_OBJ_NEW_SMALL_INT(i1 / i2);
247 args[1] = MP_OBJ_NEW_SMALL_INT(i1 % i2);
Damien George15d18062014-03-31 16:28:13 +0100248 return mp_obj_new_tuple(2, args);
Damiena3dcd9e2013-12-17 21:35:38 +0000249 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100250 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 +0000251 }
252}
253
Damien George23005372014-01-13 19:39:01 +0000254MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
255
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200256STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000257 // TODO hash will generally overflow small integer; can we safely truncate it?
Damiend99b0522013-12-21 18:17:45 +0000258 return mp_obj_new_int(mp_obj_hash(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000259}
260
Damiend99b0522013-12-21 18:17:45 +0000261MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
262
Damien George58051112014-04-15 12:42:52 +0100263STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) {
Damien Georgeb013aea2014-04-15 12:50:21 +0100264 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 +0100265}
266
267MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex);
268
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200269STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100270 return mp_getiter(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000271}
272
Damiend99b0522013-12-21 18:17:45 +0000273MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000274
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200275STATIC mp_obj_t mp_builtin_len(mp_obj_t o_in) {
John R. Lenton4bee76e2014-01-10 11:25:03 +0000276 mp_obj_t len = mp_obj_len_maybe(o_in);
277 if (len == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100278 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 +0000279 } else {
280 return len;
Damiena3dcd9e2013-12-17 21:35:38 +0000281 }
Damiena3dcd9e2013-12-17 21:35:38 +0000282}
283
Damien George23005372014-01-13 19:39:01 +0000284MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
285
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200286STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000287 if (n_args == 1) {
288 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100289 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000290 mp_obj_t max_obj = NULL;
291 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100292 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Andrew Scheller37067662014-05-01 21:21:43 +0100293 if (max_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, max_obj, item) == mp_const_true)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000294 max_obj = item;
295 }
296 }
297 if (max_obj == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100298 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "max() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000299 }
300 return max_obj;
301 } else {
302 // given many args
Damiend99b0522013-12-21 18:17:45 +0000303 mp_obj_t max_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000304 for (int i = 1; i < n_args; i++) {
Andrew Scheller37067662014-05-01 21:21:43 +0100305 if (mp_binary_op(MP_BINARY_OP_LESS, max_obj, args[i]) == mp_const_true) {
Damiena3dcd9e2013-12-17 21:35:38 +0000306 max_obj = args[i];
307 }
308 }
309 return max_obj;
310 }
311}
312
Damien George23005372014-01-13 19:39:01 +0000313MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max);
314
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200315STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000316 if (n_args == 1) {
317 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100318 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000319 mp_obj_t min_obj = NULL;
320 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100321 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Andrew Scheller37067662014-05-01 21:21:43 +0100322 if (min_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, item, min_obj) == mp_const_true)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000323 min_obj = item;
324 }
325 }
326 if (min_obj == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100327 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "min() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000328 }
329 return min_obj;
330 } else {
331 // given many args
Damiend99b0522013-12-21 18:17:45 +0000332 mp_obj_t min_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000333 for (int i = 1; i < n_args; i++) {
Andrew Scheller37067662014-05-01 21:21:43 +0100334 if (mp_binary_op(MP_BINARY_OP_LESS, args[i], min_obj) == mp_const_true) {
Damiena3dcd9e2013-12-17 21:35:38 +0000335 min_obj = args[i];
336 }
337 }
338 return min_obj;
339 }
340}
341
Damien George23005372014-01-13 19:39:01 +0000342MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
343
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200344STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
Damien Georged17926d2014-03-30 13:35:08 +0100345 mp_obj_t ret = mp_iternext_allow_raise(o);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100346 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100347 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000348 } else {
349 return ret;
350 }
Damiend99b0522013-12-21 18:17:45 +0000351}
352
353MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
354
Damien George897fe0c2014-04-15 22:03:55 +0100355STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
356 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in);
357}
358
359MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
360
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200361STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damien George55baff42014-01-21 21:40:13 +0000362 uint len;
Damien George698ec212014-02-08 18:17:23 +0000363 const char *str = mp_obj_str_get_data(o_in, &len);
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000364 uint charlen = unichar_charlen(str, len);
365 if (charlen == 1) {
366 if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
367 machine_int_t ord = *str++ & 0x7F;
368 for (machine_int_t mask = 0x40; ord & mask; mask >>= 1) {
369 ord &= ~mask;
370 }
371 while (UTF8_IS_CONT(*str)) {
372 ord = (ord << 6) | (*str++ & 0x3F);
373 }
374 return mp_obj_new_int(ord);
375 } else {
376 return mp_obj_new_int(((const byte*)str)[0]);
377 }
Damiena3dcd9e2013-12-17 21:35:38 +0000378 } else {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000379 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 +0000380 }
381}
382
Damien George23005372014-01-13 19:39:01 +0000383MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
384
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200385STATIC mp_obj_t mp_builtin_pow(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000386 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000387 switch (n_args) {
Damien Georged17926d2014-03-30 13:35:08 +0100388 case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]);
389 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 +0000390 }
391}
392
Damien George23005372014-01-13 19:39:01 +0000393MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
394
Damien George48815662014-04-02 10:34:44 +0100395STATIC mp_obj_t mp_builtin_print(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
396 mp_map_elem_t *sep_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_MAP_LOOKUP);
397 mp_map_elem_t *end_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_MAP_LOOKUP);
398 const char *sep_data = " ";
399 uint sep_len = 1;
400 const char *end_data = "\n";
401 uint end_len = 1;
402 if (sep_elem != NULL && sep_elem->value != mp_const_none) {
403 sep_data = mp_obj_str_get_data(sep_elem->value, &sep_len);
404 }
405 if (end_elem != NULL && end_elem->value != mp_const_none) {
406 end_data = mp_obj_str_get_data(end_elem->value, &end_len);
407 }
Damiena3dcd9e2013-12-17 21:35:38 +0000408 for (int i = 0; i < n_args; i++) {
409 if (i > 0) {
Damien George48815662014-04-02 10:34:44 +0100410 printf("%.*s", sep_len, sep_data);
Damiena3dcd9e2013-12-17 21:35:38 +0000411 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200412 mp_obj_print(args[i], PRINT_STR);
Damiena3dcd9e2013-12-17 21:35:38 +0000413 }
Damien George48815662014-04-02 10:34:44 +0100414 printf("%.*s", end_len, end_data);
Damiend99b0522013-12-21 18:17:45 +0000415 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000416}
417
Damien George48815662014-04-02 10:34:44 +0100418MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
Damien George23005372014-01-13 19:39:01 +0000419
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200420STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000421 vstr_t *vstr = vstr_new();
Damien George4899ff92014-01-15 22:39:03 +0000422 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
Damien George2617eeb2014-05-25 22:27:57 +0100423 mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
Damien George5fa93b62014-01-22 14:35:10 +0000424 vstr_free(vstr);
425 return s;
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000426}
427
428MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
429
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200430STATIC mp_obj_t mp_builtin_sum(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000431 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000432 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000433 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000434 case 1: value = mp_obj_new_int(0); break;
Damien George23005372014-01-13 19:39:01 +0000435 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000436 }
Damien Georged17926d2014-03-30 13:35:08 +0100437 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000438 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100439 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100440 value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
Damiena3dcd9e2013-12-17 21:35:38 +0000441 }
442 return value;
443}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000444
Damien George23005372014-01-13 19:39:01 +0000445MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000446
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200447STATIC 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 +0000448 assert(n_args >= 1);
449 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100450 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton5c768392014-01-13 05:12:50 +0000451 "must use keyword argument for key function"));
452 }
Damien George3e1a5c12014-03-29 13:43:38 +0000453 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 +0000454 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000455
456 return self;
457}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000458
John R. Lenton88cb1e62014-01-13 19:55:18 +0000459MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200460
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200461STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) {
Damien George330cf6d2014-02-02 13:38:21 +0000462 return mp_obj_new_int((machine_int_t)o_in);
xbe0ebf8532014-02-01 19:00:41 -0800463}
464
465MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_builtin_id);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200466
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300467// See mp_load_attr() if making any changes
468STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
469 mp_obj_t dest[2];
470 // use load_method, raising or not raising exception
471 ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest);
472 if (dest[0] == MP_OBJ_NULL) {
473 return defval;
474 } else if (dest[1] == MP_OBJ_NULL) {
475 // load_method returned just a normal attribute
476 return dest[0];
477 } else {
478 // load_method returned a method, so build a bound method object
479 return mp_obj_new_bound_meth(dest[0], dest[1]);
480 }
481}
482
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300483STATIC mp_obj_t mp_builtin_getattr(uint n_args, const mp_obj_t *args) {
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300484 mp_obj_t attr = args[1];
485 if (MP_OBJ_IS_TYPE(attr, &mp_type_str)) {
486 attr = mp_obj_str_intern(attr);
487 } else if (!MP_OBJ_IS_QSTR(attr)) {
488 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "string required"));
489 }
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300490 mp_obj_t defval = MP_OBJ_NULL;
491 if (n_args > 2) {
492 defval = args[2];
493 }
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300494 return mp_load_attr_default(args[0], MP_OBJ_QSTR_VALUE(attr), defval);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200495}
496
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300497MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
Paul Sokolovskycc0af3d2014-04-06 01:00:46 +0300498
Paul Sokolovskyff306662014-05-11 19:05:42 +0300499STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
500 assert(MP_OBJ_IS_QSTR(attr_in));
501
502 mp_obj_t dest[2];
Chris Angelicodaf973a2014-06-06 03:51:03 +1000503 // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
Paul Sokolovskyff306662014-05-11 19:05:42 +0300504 // explicitly says "This is implemented by calling getattr(object, name) and seeing
505 // whether it raises an AttributeError or not.", so we should explicitly wrap this
506 // in nlr_push and handle exception.
507 mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
508
509 return MP_BOOL(dest[0] != MP_OBJ_NULL);
510}
511
512MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
513
Paul Sokolovsky080d99b2014-04-06 01:18:19 +0300514// These two are defined in terms of MicroPython API functions right away
515MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
516MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);