blob: 88a724fcd6d6d7d95f7658b0c20a1c4318d530b9 [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"
Paul Sokolovskycb66f412014-07-13 23:07:42 +030039#include "stream.h"
40#include "pfenv.h"
Damien660365e2013-12-17 18:27:24 +000041
Damien Georgefb510b32014-06-01 13:32:54 +010042#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000043#include <math.h>
44#endif
45
Damien Georgeb97669a2014-01-08 11:47:55 +000046// args[0] is function from class body
47// args[1] is class name
48// args[2:] are base objects
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020049STATIC mp_obj_t mp_builtin___build_class__(uint n_args, const mp_obj_t *args) {
Damien Georgeb97669a2014-01-08 11:47:55 +000050 assert(2 <= n_args);
51
Damien George7efc5b32014-04-05 22:36:42 +010052 // set the new classes __locals__ object
53 mp_obj_dict_t *old_locals = mp_locals_get();
Damien George062478e2014-01-09 20:57:50 +000054 mp_obj_t class_locals = mp_obj_new_dict(0);
Damien George7efc5b32014-04-05 22:36:42 +010055 mp_locals_set(class_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000056
57 // call the class code
Damien George882b3632014-04-02 15:56:31 +010058 mp_obj_t cell = mp_call_function_0(args[0]);
Damiena3dcd9e2013-12-17 21:35:38 +000059
60 // restore old __locals__ object
Damien Georged17926d2014-03-30 13:35:08 +010061 mp_locals_set(old_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000062
Damien Georgeb97669a2014-01-08 11:47:55 +000063 // get the class type (meta object) from the base objects
64 mp_obj_t meta;
65 if (n_args == 2) {
66 // no explicit bases, so use 'type'
Damien Georgec5966122014-02-15 16:10:44 +000067 meta = (mp_obj_t)&mp_type_type;
Damien Georgeb97669a2014-01-08 11:47:55 +000068 } else {
69 // use type of first base object
70 meta = mp_obj_get_type(args[2]);
71 }
Damien Georgeb97669a2014-01-08 11:47:55 +000072
73 // TODO do proper metaclass resolution for multiple base objects
74
Damien Georgeb97669a2014-01-08 11:47:55 +000075 // create the new class using a call to the meta object
Damien Georgeb97669a2014-01-08 11:47:55 +000076 mp_obj_t meta_args[3];
Damien George20006db2014-01-18 14:10:48 +000077 meta_args[0] = args[1]; // class name
Damien Georgeb97669a2014-01-08 11:47:55 +000078 meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases
Damien George20006db2014-01-18 14:10:48 +000079 meta_args[2] = class_locals; // dict of members
Damien Georged17926d2014-03-30 13:35:08 +010080 mp_obj_t new_class = mp_call_function_n_kw(meta, 3, 0, meta_args);
Damien Georgeb97669a2014-01-08 11:47:55 +000081
82 // store into cell if neede
83 if (cell != mp_const_none) {
84 mp_obj_cell_set(cell, new_class);
85 }
86
87 return new_class;
Damiena3dcd9e2013-12-17 21:35:38 +000088}
89
Damien Georgeb97669a2014-01-08 11:47:55 +000090MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
91
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020092STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
Damiend99b0522013-12-21 18:17:45 +000093 if (o != mp_const_none) {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020094 mp_obj_print(o, PRINT_REPR);
Damien660365e2013-12-17 18:27:24 +000095 printf("\n");
96 }
Damiend99b0522013-12-21 18:17:45 +000097 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +000098}
99
Damien George23005372014-01-13 19:39:01 +0000100MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
101
Damiend99b0522013-12-21 18:17:45 +0000102mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
103 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien George40f3c022014-07-03 13:25:24 +0100104 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
Damien660365e2013-12-17 18:27:24 +0000105 if (val < 0) {
106 val = -val;
107 }
Damiend99b0522013-12-21 18:17:45 +0000108 return MP_OBJ_NEW_SMALL_INT(val);
Damien Georgefb510b32014-06-01 13:32:54 +0100109#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000110 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000111 mp_float_t value = mp_obj_float_get(o_in);
Damien660365e2013-12-17 18:27:24 +0000112 // TODO check for NaN etc
Damiend99b0522013-12-21 18:17:45 +0000113 if (value < 0) {
114 return mp_obj_new_float(-value);
Damien660365e2013-12-17 18:27:24 +0000115 } else {
116 return o_in;
117 }
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300118#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +0000119 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000120 mp_float_t real, imag;
121 mp_obj_complex_get(o_in, &real, &imag);
Damien George0c36da02014-03-08 15:24:39 +0000122 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
Damien660365e2013-12-17 18:27:24 +0000123#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300124#endif
Damien660365e2013-12-17 18:27:24 +0000125 } else {
126 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000127 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +0000128 }
129}
130
Damien George23005372014-01-13 19:39:01 +0000131MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
132
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200133STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100134 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000135 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100136 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100137 if (!mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000138 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000139 }
140 }
Damiend99b0522013-12-21 18:17:45 +0000141 return mp_const_true;
Damien660365e2013-12-17 18:27:24 +0000142}
143
Damien George23005372014-01-13 19:39:01 +0000144MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
145
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200146STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100147 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000148 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100149 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100150 if (mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000151 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000152 }
153 }
Damiend99b0522013-12-21 18:17:45 +0000154 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000155}
156
Damien George23005372014-01-13 19:39:01 +0000157MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
158
Damien George897fe0c2014-04-15 22:03:55 +0100159STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
160 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 +0200161 return mp_obj_str_format(MP_ARRAY_SIZE(args), args);
Damien George897fe0c2014-04-15 22:03:55 +0100162}
163
164MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);
165
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200166STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000167 if (mp_obj_is_callable(o_in)) {
168 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000169 } else {
Damiend99b0522013-12-21 18:17:45 +0000170 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000171 }
172}
173
Damien George23005372014-01-13 19:39:01 +0000174MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
175
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200176STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300177 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgec9aa58e2014-07-31 13:41:43 +0000178 mp_uint_t c = mp_obj_get_int(o_in);
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000179 char str[4];
180 int len = 0;
181 if (c < 0x80) {
182 *str = c; len = 1;
183 } else if (c < 0x800) {
184 str[0] = (c >> 6) | 0xC0;
185 str[1] = (c & 0x3F) | 0x80;
186 len = 2;
187 } else if (c < 0x10000) {
188 str[0] = (c >> 12) | 0xE0;
189 str[1] = ((c >> 6) & 0x3F) | 0x80;
190 str[2] = (c & 0x3F) | 0x80;
191 len = 3;
192 } else if (c < 0x110000) {
193 str[0] = (c >> 18) | 0xF0;
194 str[1] = ((c >> 12) & 0x3F) | 0x80;
195 str[2] = ((c >> 6) & 0x3F) | 0x80;
196 str[3] = (c & 0x3F) | 0x80;
197 len = 4;
Damiena3dcd9e2013-12-17 21:35:38 +0000198 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100199 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
Damiena3dcd9e2013-12-17 21:35:38 +0000200 }
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000201 return mp_obj_new_str(str, len, true);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300202 #else
Damien George40f3c022014-07-03 13:25:24 +0100203 mp_int_t ord = mp_obj_get_int(o_in);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300204 if (0 <= ord && ord <= 0x10ffff) {
205 char str[1] = {ord};
206 return mp_obj_new_str(str, 1, true);
207 } else {
208 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
209 }
210 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000211}
212
Damien George23005372014-01-13 19:39:01 +0000213MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
214
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200215STATIC mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) {
Damien George4acb2452014-02-02 22:07:44 +0000216 // TODO make this function more general and less of a hack
217
Damien George7efc5b32014-04-05 22:36:42 +0100218 mp_obj_dict_t *dict = NULL;
Damien George4acb2452014-02-02 22:07:44 +0000219 if (n_args == 0) {
220 // make a list of names in the local name space
Damien George7efc5b32014-04-05 22:36:42 +0100221 dict = mp_locals_get();
Damien George4acb2452014-02-02 22:07:44 +0000222 } else { // n_args == 1
223 // make a list of names in the given object
Damien George6022d9d2014-03-26 22:35:00 +0000224 if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
Damien George7efc5b32014-04-05 22:36:42 +0100225 dict = mp_obj_module_get_globals(args[0]);
Damien George6022d9d2014-03-26 22:35:00 +0000226 } else {
227 mp_obj_type_t *type;
228 if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
229 type = args[0];
230 } else {
231 type = mp_obj_get_type(args[0]);
232 }
Damien George3e1a5c12014-03-29 13:43:38 +0000233 if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
Damien George7efc5b32014-04-05 22:36:42 +0100234 dict = type->locals_dict;
Damien George6022d9d2014-03-26 22:35:00 +0000235 }
Damien Georgebadc9d42014-03-23 00:03:11 +0000236 }
Damien George4acb2452014-02-02 22:07:44 +0000237 }
238
239 mp_obj_t dir = mp_obj_new_list(0, NULL);
Damien George7efc5b32014-04-05 22:36:42 +0100240 if (dict != NULL) {
241 for (uint i = 0; i < dict->map.alloc; i++) {
242 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
243 mp_obj_list_append(dir, dict->map.table[i].key);
Damien Georgebadc9d42014-03-23 00:03:11 +0000244 }
245 }
246 }
Damien George9b196cd2014-03-26 21:47:19 +0000247
Damien George4acb2452014-02-02 22:07:44 +0000248 return dir;
249}
250
251MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);
252
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200253STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
Damiend99b0522013-12-21 18:17:45 +0000254 if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
Damien George40f3c022014-07-03 13:25:24 +0100255 mp_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
256 mp_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
Damien George20006db2014-01-18 14:10:48 +0000257 mp_obj_t args[2];
258 args[0] = MP_OBJ_NEW_SMALL_INT(i1 / i2);
259 args[1] = MP_OBJ_NEW_SMALL_INT(i1 % i2);
Damien George15d18062014-03-31 16:28:13 +0100260 return mp_obj_new_tuple(2, args);
Damiena3dcd9e2013-12-17 21:35:38 +0000261 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100262 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 +0000263 }
264}
265
Damien George23005372014-01-13 19:39:01 +0000266MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
267
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200268STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000269 // TODO hash will generally overflow small integer; can we safely truncate it?
Damiend99b0522013-12-21 18:17:45 +0000270 return mp_obj_new_int(mp_obj_hash(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000271}
272
Damiend99b0522013-12-21 18:17:45 +0000273MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
274
Damien George58051112014-04-15 12:42:52 +0100275STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) {
Damien Georgeb013aea2014-04-15 12:50:21 +0100276 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 +0100277}
278
279MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex);
280
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200281STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100282 return mp_getiter(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000283}
284
Damiend99b0522013-12-21 18:17:45 +0000285MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000286
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200287STATIC mp_obj_t mp_builtin_len(mp_obj_t o_in) {
John R. Lenton4bee76e2014-01-10 11:25:03 +0000288 mp_obj_t len = mp_obj_len_maybe(o_in);
289 if (len == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100290 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 +0000291 } else {
292 return len;
Damiena3dcd9e2013-12-17 21:35:38 +0000293 }
Damiena3dcd9e2013-12-17 21:35:38 +0000294}
295
Damien George23005372014-01-13 19:39:01 +0000296MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
297
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200298STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000299 if (n_args == 1) {
300 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100301 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000302 mp_obj_t max_obj = NULL;
303 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100304 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Andrew Scheller37067662014-05-01 21:21:43 +0100305 if (max_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, max_obj, item) == mp_const_true)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000306 max_obj = item;
307 }
308 }
309 if (max_obj == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100310 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "max() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000311 }
312 return max_obj;
313 } else {
314 // given many args
Damiend99b0522013-12-21 18:17:45 +0000315 mp_obj_t max_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000316 for (int i = 1; i < n_args; i++) {
Andrew Scheller37067662014-05-01 21:21:43 +0100317 if (mp_binary_op(MP_BINARY_OP_LESS, max_obj, args[i]) == mp_const_true) {
Damiena3dcd9e2013-12-17 21:35:38 +0000318 max_obj = args[i];
319 }
320 }
321 return max_obj;
322 }
323}
324
Damien George23005372014-01-13 19:39:01 +0000325MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max);
326
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200327STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000328 if (n_args == 1) {
329 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100330 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000331 mp_obj_t min_obj = NULL;
332 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100333 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Andrew Scheller37067662014-05-01 21:21:43 +0100334 if (min_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, item, min_obj) == mp_const_true)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000335 min_obj = item;
336 }
337 }
338 if (min_obj == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100339 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "min() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000340 }
341 return min_obj;
342 } else {
343 // given many args
Damiend99b0522013-12-21 18:17:45 +0000344 mp_obj_t min_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000345 for (int i = 1; i < n_args; i++) {
Andrew Scheller37067662014-05-01 21:21:43 +0100346 if (mp_binary_op(MP_BINARY_OP_LESS, args[i], min_obj) == mp_const_true) {
Damiena3dcd9e2013-12-17 21:35:38 +0000347 min_obj = args[i];
348 }
349 }
350 return min_obj;
351 }
352}
353
Damien George23005372014-01-13 19:39:01 +0000354MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
355
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200356STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
Damien Georged17926d2014-03-30 13:35:08 +0100357 mp_obj_t ret = mp_iternext_allow_raise(o);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100358 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100359 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000360 } else {
361 return ret;
362 }
Damiend99b0522013-12-21 18:17:45 +0000363}
364
365MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
366
Damien George897fe0c2014-04-15 22:03:55 +0100367STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
368 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in);
369}
370
371MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
372
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200373STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damien George55baff42014-01-21 21:40:13 +0000374 uint len;
Damien George698ec212014-02-08 18:17:23 +0000375 const char *str = mp_obj_str_get_data(o_in, &len);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300376 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien George40f3c022014-07-03 13:25:24 +0100377 mp_uint_t charlen = unichar_charlen(str, len);
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000378 if (charlen == 1) {
379 if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
Damien George40f3c022014-07-03 13:25:24 +0100380 mp_int_t ord = *str++ & 0x7F;
381 for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000382 ord &= ~mask;
383 }
384 while (UTF8_IS_CONT(*str)) {
385 ord = (ord << 6) | (*str++ & 0x3F);
386 }
387 return mp_obj_new_int(ord);
388 } else {
389 return mp_obj_new_int(((const byte*)str)[0]);
390 }
Damiena3dcd9e2013-12-17 21:35:38 +0000391 } else {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000392 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 +0000393 }
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300394 #else
395 if (len == 1) {
396 // don't sign extend when converting to ord
397 return mp_obj_new_int(((const byte*)str)[0]);
398 } else {
399 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "ord() expected a character, but string of length %d found", len));
400 }
401 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000402}
403
Damien George23005372014-01-13 19:39:01 +0000404MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
405
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200406STATIC mp_obj_t mp_builtin_pow(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000407 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000408 switch (n_args) {
Damien Georged17926d2014-03-30 13:35:08 +0100409 case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]);
410 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 +0000411 }
412}
413
Damien George23005372014-01-13 19:39:01 +0000414MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
415
Damien George48815662014-04-02 10:34:44 +0100416STATIC mp_obj_t mp_builtin_print(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
417 mp_map_elem_t *sep_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_MAP_LOOKUP);
418 mp_map_elem_t *end_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_MAP_LOOKUP);
419 const char *sep_data = " ";
420 uint sep_len = 1;
421 const char *end_data = "\n";
422 uint end_len = 1;
423 if (sep_elem != NULL && sep_elem->value != mp_const_none) {
424 sep_data = mp_obj_str_get_data(sep_elem->value, &sep_len);
425 }
426 if (end_elem != NULL && end_elem->value != mp_const_none) {
427 end_data = mp_obj_str_get_data(end_elem->value, &end_len);
428 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300429 #if MICROPY_PY_IO
430 mp_obj_t stream_obj = &mp_sys_stdout_obj;
431 mp_map_elem_t *file_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_file), MP_MAP_LOOKUP);
432 if (file_elem != NULL && file_elem->value != mp_const_none) {
433 stream_obj = file_elem->value;
434 }
435
436 pfenv_t pfenv;
437 pfenv.data = stream_obj;
438 pfenv.print_strn = (void (*)(void *, const char *, unsigned int))mp_stream_write;
439 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000440 for (int i = 0; i < n_args; i++) {
441 if (i > 0) {
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300442 #if MICROPY_PY_IO
443 mp_stream_write(stream_obj, sep_data, sep_len);
444 #else
Damien George48815662014-04-02 10:34:44 +0100445 printf("%.*s", sep_len, sep_data);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300446 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000447 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300448 #if MICROPY_PY_IO
449 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))pfenv_printf, &pfenv, args[i], PRINT_STR);
450 #else
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200451 mp_obj_print(args[i], PRINT_STR);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300452 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000453 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300454 #if MICROPY_PY_IO
455 mp_stream_write(stream_obj, end_data, end_len);
456 #else
Damien George48815662014-04-02 10:34:44 +0100457 printf("%.*s", end_len, end_data);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300458 #endif
Damiend99b0522013-12-21 18:17:45 +0000459 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000460}
461
Damien George48815662014-04-02 10:34:44 +0100462MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
Damien George23005372014-01-13 19:39:01 +0000463
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200464STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000465 vstr_t *vstr = vstr_new();
Damien George4899ff92014-01-15 22:39:03 +0000466 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
Damien George2617eeb2014-05-25 22:27:57 +0100467 mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
Damien George5fa93b62014-01-22 14:35:10 +0000468 vstr_free(vstr);
469 return s;
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000470}
471
472MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
473
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200474STATIC mp_obj_t mp_builtin_sum(uint n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000475 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000476 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000477 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000478 case 1: value = mp_obj_new_int(0); break;
Damien George23005372014-01-13 19:39:01 +0000479 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000480 }
Damien Georged17926d2014-03-30 13:35:08 +0100481 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000482 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100483 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100484 value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
Damiena3dcd9e2013-12-17 21:35:38 +0000485 }
486 return value;
487}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000488
Damien George23005372014-01-13 19:39:01 +0000489MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000490
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200491STATIC 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 +0000492 assert(n_args >= 1);
493 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100494 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton5c768392014-01-13 05:12:50 +0000495 "must use keyword argument for key function"));
496 }
Damien George3e1a5c12014-03-29 13:43:38 +0000497 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 +0000498 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000499
500 return self;
501}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000502
John R. Lenton88cb1e62014-01-13 19:55:18 +0000503MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200504
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200505STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) {
Paul Sokolovsky645582f2014-07-23 01:38:19 +0300506 mp_int_t id = (mp_int_t)o_in;
507 if (!MP_OBJ_IS_OBJ(o_in)) {
508 return mp_obj_new_int(id);
509 } else if (id >= 0) {
510 // Many OSes and CPUs have affinity for putting "user" memories
511 // into low half of address space, and "system" into upper half.
512 // We're going to take advantage of that and return small int
513 // (signed) for such "user" addresses.
514 return MP_OBJ_NEW_SMALL_INT(id);
515 } else {
516 // If that didn't work, well, let's return long int, just as
517 // a (big) positve value, so it will never clash with the range
518 // of small int returned in previous case.
519 return mp_obj_new_int_from_uint((mp_uint_t)id);
520 }
xbe0ebf8532014-02-01 19:00:41 -0800521}
522
523MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_builtin_id);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200524
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300525// See mp_load_attr() if making any changes
526STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
527 mp_obj_t dest[2];
528 // use load_method, raising or not raising exception
529 ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest);
530 if (dest[0] == MP_OBJ_NULL) {
531 return defval;
532 } else if (dest[1] == MP_OBJ_NULL) {
533 // load_method returned just a normal attribute
534 return dest[0];
535 } else {
536 // load_method returned a method, so build a bound method object
537 return mp_obj_new_bound_meth(dest[0], dest[1]);
538 }
539}
540
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300541STATIC mp_obj_t mp_builtin_getattr(uint n_args, const mp_obj_t *args) {
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300542 mp_obj_t attr = args[1];
543 if (MP_OBJ_IS_TYPE(attr, &mp_type_str)) {
544 attr = mp_obj_str_intern(attr);
545 } else if (!MP_OBJ_IS_QSTR(attr)) {
546 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "string required"));
547 }
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300548 mp_obj_t defval = MP_OBJ_NULL;
549 if (n_args > 2) {
550 defval = args[2];
551 }
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300552 return mp_load_attr_default(args[0], MP_OBJ_QSTR_VALUE(attr), defval);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200553}
554
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300555MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
Paul Sokolovskycc0af3d2014-04-06 01:00:46 +0300556
Paul Sokolovskyff306662014-05-11 19:05:42 +0300557STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
558 assert(MP_OBJ_IS_QSTR(attr_in));
559
560 mp_obj_t dest[2];
Chris Angelicodaf973a2014-06-06 03:51:03 +1000561 // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
Paul Sokolovskyff306662014-05-11 19:05:42 +0300562 // explicitly says "This is implemented by calling getattr(object, name) and seeing
563 // whether it raises an AttributeError or not.", so we should explicitly wrap this
564 // in nlr_push and handle exception.
565 mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
566
567 return MP_BOOL(dest[0] != MP_OBJ_NULL);
568}
569
570MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
571
Paul Sokolovsky080d99b2014-04-06 01:18:19 +0300572// These two are defined in terms of MicroPython API functions right away
573MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
574MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);