blob: 6bcde4364590c5222c62464587346b0f970add98 [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"
Damien Georgee72be1b2014-10-22 23:05:50 +010036#include "smallint.h"
Damiend99b0522013-12-21 18:17:45 +000037#include "runtime0.h"
38#include "runtime.h"
Damien660365e2013-12-17 18:27:24 +000039#include "builtin.h"
Paul Sokolovskycb66f412014-07-13 23:07:42 +030040#include "stream.h"
41#include "pfenv.h"
Damien660365e2013-12-17 18:27:24 +000042
Damien Georgefb510b32014-06-01 13:32:54 +010043#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000044#include <math.h>
45#endif
46
Damien Georgeb97669a2014-01-08 11:47:55 +000047// args[0] is function from class body
48// args[1] is class name
49// args[2:] are base objects
Damien Georged182b982014-08-30 14:19:41 +010050STATIC mp_obj_t mp_builtin___build_class__(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgeb97669a2014-01-08 11:47:55 +000051 assert(2 <= n_args);
52
Damien George7efc5b32014-04-05 22:36:42 +010053 // set the new classes __locals__ object
54 mp_obj_dict_t *old_locals = mp_locals_get();
Damien George062478e2014-01-09 20:57:50 +000055 mp_obj_t class_locals = mp_obj_new_dict(0);
Damien George7efc5b32014-04-05 22:36:42 +010056 mp_locals_set(class_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000057
58 // call the class code
Damien George882b3632014-04-02 15:56:31 +010059 mp_obj_t cell = mp_call_function_0(args[0]);
Damiena3dcd9e2013-12-17 21:35:38 +000060
61 // restore old __locals__ object
Damien Georged17926d2014-03-30 13:35:08 +010062 mp_locals_set(old_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000063
Damien Georgeb97669a2014-01-08 11:47:55 +000064 // get the class type (meta object) from the base objects
65 mp_obj_t meta;
66 if (n_args == 2) {
67 // no explicit bases, so use 'type'
Damien Georgec5966122014-02-15 16:10:44 +000068 meta = (mp_obj_t)&mp_type_type;
Damien Georgeb97669a2014-01-08 11:47:55 +000069 } else {
70 // use type of first base object
71 meta = mp_obj_get_type(args[2]);
72 }
Damien Georgeb97669a2014-01-08 11:47:55 +000073
74 // TODO do proper metaclass resolution for multiple base objects
75
Damien Georgeb97669a2014-01-08 11:47:55 +000076 // create the new class using a call to the meta object
Damien Georgeb97669a2014-01-08 11:47:55 +000077 mp_obj_t meta_args[3];
Damien George20006db2014-01-18 14:10:48 +000078 meta_args[0] = args[1]; // class name
Damien Georgeb97669a2014-01-08 11:47:55 +000079 meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases
Damien George20006db2014-01-18 14:10:48 +000080 meta_args[2] = class_locals; // dict of members
Damien Georged17926d2014-03-30 13:35:08 +010081 mp_obj_t new_class = mp_call_function_n_kw(meta, 3, 0, meta_args);
Damien Georgeb97669a2014-01-08 11:47:55 +000082
83 // store into cell if neede
84 if (cell != mp_const_none) {
85 mp_obj_cell_set(cell, new_class);
86 }
87
88 return new_class;
Damiena3dcd9e2013-12-17 21:35:38 +000089}
Damien Georgeb97669a2014-01-08 11:47:55 +000090MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
91
Damien George69c5fe12014-08-12 18:13:44 +010092STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000093 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien George40f3c022014-07-03 13:25:24 +010094 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
Damien660365e2013-12-17 18:27:24 +000095 if (val < 0) {
96 val = -val;
97 }
Damiend99b0522013-12-21 18:17:45 +000098 return MP_OBJ_NEW_SMALL_INT(val);
Damien Georgefb510b32014-06-01 13:32:54 +010099#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000100 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000101 mp_float_t value = mp_obj_float_get(o_in);
Damien660365e2013-12-17 18:27:24 +0000102 // TODO check for NaN etc
Damiend99b0522013-12-21 18:17:45 +0000103 if (value < 0) {
104 return mp_obj_new_float(-value);
Damien660365e2013-12-17 18:27:24 +0000105 } else {
106 return o_in;
107 }
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300108#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +0000109 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000110 mp_float_t real, imag;
111 mp_obj_complex_get(o_in, &real, &imag);
Damien George0c36da02014-03-08 15:24:39 +0000112 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
Damien660365e2013-12-17 18:27:24 +0000113#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300114#endif
Damien660365e2013-12-17 18:27:24 +0000115 } else {
116 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000117 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +0000118 }
119}
Damien George23005372014-01-13 19:39:01 +0000120MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
121
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200122STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100123 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000124 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100125 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100126 if (!mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000127 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000128 }
129 }
Damiend99b0522013-12-21 18:17:45 +0000130 return mp_const_true;
Damien660365e2013-12-17 18:27:24 +0000131}
Damien George23005372014-01-13 19:39:01 +0000132MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
133
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200134STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100135 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000136 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100137 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100138 if (mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000139 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000140 }
141 }
Damiend99b0522013-12-21 18:17:45 +0000142 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000143}
Damien George23005372014-01-13 19:39:01 +0000144MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
145
Damien George897fe0c2014-04-15 22:03:55 +0100146STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
147 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 +0200148 return mp_obj_str_format(MP_ARRAY_SIZE(args), args);
Damien George897fe0c2014-04-15 22:03:55 +0100149}
Damien George897fe0c2014-04-15 22:03:55 +0100150MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);
151
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200152STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000153 if (mp_obj_is_callable(o_in)) {
154 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000155 } else {
Damiend99b0522013-12-21 18:17:45 +0000156 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000157 }
158}
Damien George23005372014-01-13 19:39:01 +0000159MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
160
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200161STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300162 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgec9aa58e2014-07-31 13:41:43 +0000163 mp_uint_t c = mp_obj_get_int(o_in);
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000164 char str[4];
165 int len = 0;
166 if (c < 0x80) {
167 *str = c; len = 1;
168 } else if (c < 0x800) {
169 str[0] = (c >> 6) | 0xC0;
170 str[1] = (c & 0x3F) | 0x80;
171 len = 2;
172 } else if (c < 0x10000) {
173 str[0] = (c >> 12) | 0xE0;
174 str[1] = ((c >> 6) & 0x3F) | 0x80;
175 str[2] = (c & 0x3F) | 0x80;
176 len = 3;
177 } else if (c < 0x110000) {
178 str[0] = (c >> 18) | 0xF0;
179 str[1] = ((c >> 12) & 0x3F) | 0x80;
180 str[2] = ((c >> 6) & 0x3F) | 0x80;
181 str[3] = (c & 0x3F) | 0x80;
182 len = 4;
Damiena3dcd9e2013-12-17 21:35:38 +0000183 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100184 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
Damiena3dcd9e2013-12-17 21:35:38 +0000185 }
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000186 return mp_obj_new_str(str, len, true);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300187 #else
Damien George40f3c022014-07-03 13:25:24 +0100188 mp_int_t ord = mp_obj_get_int(o_in);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300189 if (0 <= ord && ord <= 0x10ffff) {
190 char str[1] = {ord};
191 return mp_obj_new_str(str, 1, true);
192 } else {
193 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
194 }
195 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000196}
Damien George23005372014-01-13 19:39:01 +0000197MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
198
Damien Georged182b982014-08-30 14:19:41 +0100199STATIC mp_obj_t mp_builtin_dir(mp_uint_t n_args, const mp_obj_t *args) {
Damien George4acb2452014-02-02 22:07:44 +0000200 // TODO make this function more general and less of a hack
201
Damien George7efc5b32014-04-05 22:36:42 +0100202 mp_obj_dict_t *dict = NULL;
Damien George4acb2452014-02-02 22:07:44 +0000203 if (n_args == 0) {
204 // make a list of names in the local name space
Damien George7efc5b32014-04-05 22:36:42 +0100205 dict = mp_locals_get();
Damien George4acb2452014-02-02 22:07:44 +0000206 } else { // n_args == 1
207 // make a list of names in the given object
Damien George6022d9d2014-03-26 22:35:00 +0000208 if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
Damien George7efc5b32014-04-05 22:36:42 +0100209 dict = mp_obj_module_get_globals(args[0]);
Damien George6022d9d2014-03-26 22:35:00 +0000210 } else {
211 mp_obj_type_t *type;
212 if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
213 type = args[0];
214 } else {
215 type = mp_obj_get_type(args[0]);
216 }
Damien George3e1a5c12014-03-29 13:43:38 +0000217 if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
Damien George7efc5b32014-04-05 22:36:42 +0100218 dict = type->locals_dict;
Damien George6022d9d2014-03-26 22:35:00 +0000219 }
Damien Georgebadc9d42014-03-23 00:03:11 +0000220 }
Damien George4acb2452014-02-02 22:07:44 +0000221 }
222
223 mp_obj_t dir = mp_obj_new_list(0, NULL);
Damien George7efc5b32014-04-05 22:36:42 +0100224 if (dict != NULL) {
Damien Georged182b982014-08-30 14:19:41 +0100225 for (mp_uint_t i = 0; i < dict->map.alloc; i++) {
Damien George7efc5b32014-04-05 22:36:42 +0100226 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
227 mp_obj_list_append(dir, dict->map.table[i].key);
Damien Georgebadc9d42014-03-23 00:03:11 +0000228 }
229 }
230 }
Damien George9b196cd2014-03-26 21:47:19 +0000231
Damien George4acb2452014-02-02 22:07:44 +0000232 return dir;
233}
Damien George4acb2452014-02-02 22:07:44 +0000234MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);
235
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200236STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
Damien George8594ce22014-09-13 18:43:09 +0100237 // TODO handle big int
Damiend99b0522013-12-21 18:17:45 +0000238 if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
Damien George40f3c022014-07-03 13:25:24 +0100239 mp_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
240 mp_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
Damien George8594ce22014-09-13 18:43:09 +0100241 if (i2 == 0) {
Damien George83695592014-09-13 19:58:18 +0100242 #if MICROPY_PY_BUILTINS_FLOAT
Damien George8594ce22014-09-13 18:43:09 +0100243 zero_division_error:
Damien George83695592014-09-13 19:58:18 +0100244 #endif
Damien George8594ce22014-09-13 18:43:09 +0100245 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
246 }
Damien George20006db2014-01-18 14:10:48 +0000247 mp_obj_t args[2];
Damien Georgee72be1b2014-10-22 23:05:50 +0100248 args[0] = MP_OBJ_NEW_SMALL_INT(mp_small_int_floor_divide(i1, i2));
249 args[1] = MP_OBJ_NEW_SMALL_INT(mp_small_int_modulo(i1, i2));
Damien George15d18062014-03-31 16:28:13 +0100250 return mp_obj_new_tuple(2, args);
Damien George83695592014-09-13 19:58:18 +0100251 #if MICROPY_PY_BUILTINS_FLOAT
Damien George8594ce22014-09-13 18:43:09 +0100252 } else if (MP_OBJ_IS_TYPE(o1_in, &mp_type_float) || MP_OBJ_IS_TYPE(o2_in, &mp_type_float)) {
253 mp_float_t f1 = mp_obj_get_float(o1_in);
254 mp_float_t f2 = mp_obj_get_float(o2_in);
255 if (f2 == 0.0) {
256 goto zero_division_error;
257 }
258 mp_obj_float_divmod(&f1, &f2);
259 mp_obj_t tuple[2] = {
260 mp_obj_new_float(f1),
261 mp_obj_new_float(f2),
262 };
263 return mp_obj_new_tuple(2, tuple);
Damien George83695592014-09-13 19:58:18 +0100264 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000265 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000266 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
267 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
268 "unsupported operand type(s) for divmod()"));
269 } else {
270 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
271 "unsupported operand type(s) for divmod(): '%s' and '%s'",
272 mp_obj_get_type_str(o1_in), mp_obj_get_type_str(o2_in)));
273 }
Damiena3dcd9e2013-12-17 21:35:38 +0000274 }
275}
Damien George23005372014-01-13 19:39:01 +0000276MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
277
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200278STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000279 // TODO hash will generally overflow small integer; can we safely truncate it?
Damiend99b0522013-12-21 18:17:45 +0000280 return mp_obj_new_int(mp_obj_hash(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000281}
Damiend99b0522013-12-21 18:17:45 +0000282MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
283
Damien George58051112014-04-15 12:42:52 +0100284STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) {
Damien Georgeb013aea2014-04-15 12:50:21 +0100285 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 +0100286}
Damien George58051112014-04-15 12:42:52 +0100287MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex);
288
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200289STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100290 return mp_getiter(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000291}
Damiend99b0522013-12-21 18:17:45 +0000292MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000293
Damien Georged182b982014-08-30 14:19:41 +0100294STATIC mp_obj_t mp_builtin_min_max(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs, mp_uint_t op) {
Damien George7310fd42014-08-24 19:14:09 +0100295 mp_map_elem_t *key_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP);
296 mp_obj_t key_fn = key_elem == NULL ? MP_OBJ_NULL : key_elem->value;
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]);
Damien George7310fd42014-08-24 19:14:09 +0100300 mp_obj_t best_key = MP_OBJ_NULL;
301 mp_obj_t best_obj = MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000302 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100303 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George7310fd42014-08-24 19:14:09 +0100304 mp_obj_t key = key_fn == MP_OBJ_NULL ? item : mp_call_function_1(key_fn, item);
305 if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) {
306 best_key = key;
307 best_obj = item;
Damiena3dcd9e2013-12-17 21:35:38 +0000308 }
309 }
Damien George7310fd42014-08-24 19:14:09 +0100310 if (best_obj == MP_OBJ_NULL) {
311 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000312 }
Damien George7310fd42014-08-24 19:14:09 +0100313 return best_obj;
Damiena3dcd9e2013-12-17 21:35:38 +0000314 } else {
315 // given many args
Damien George7310fd42014-08-24 19:14:09 +0100316 mp_obj_t best_key = MP_OBJ_NULL;
317 mp_obj_t best_obj = MP_OBJ_NULL;
318 for (mp_uint_t i = 0; i < n_args; i++) {
319 mp_obj_t key = key_fn == MP_OBJ_NULL ? args[i] : mp_call_function_1(key_fn, args[i]);
320 if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) {
321 best_key = key;
322 best_obj = args[i];
Damiena3dcd9e2013-12-17 21:35:38 +0000323 }
324 }
Damien George7310fd42014-08-24 19:14:09 +0100325 return best_obj;
Damiena3dcd9e2013-12-17 21:35:38 +0000326 }
327}
328
Damien Georged182b982014-08-30 14:19:41 +0100329STATIC mp_obj_t mp_builtin_max(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien George7310fd42014-08-24 19:14:09 +0100330 return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_MORE);
Damiena3dcd9e2013-12-17 21:35:38 +0000331}
Damien George7310fd42014-08-24 19:14:09 +0100332MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_max_obj, 1, mp_builtin_max);
Damiena3dcd9e2013-12-17 21:35:38 +0000333
Damien Georged182b982014-08-30 14:19:41 +0100334STATIC mp_obj_t mp_builtin_min(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien George7310fd42014-08-24 19:14:09 +0100335 return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_LESS);
336}
337MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_min_obj, 1, mp_builtin_min);
Damien George23005372014-01-13 19:39:01 +0000338
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200339STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
Damien Georged17926d2014-03-30 13:35:08 +0100340 mp_obj_t ret = mp_iternext_allow_raise(o);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100341 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100342 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000343 } else {
344 return ret;
345 }
Damiend99b0522013-12-21 18:17:45 +0000346}
Damiend99b0522013-12-21 18:17:45 +0000347MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
348
Damien George897fe0c2014-04-15 22:03:55 +0100349STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
350 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in);
351}
Damien George897fe0c2014-04-15 22:03:55 +0100352MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
353
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200354STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damien Georged182b982014-08-30 14:19:41 +0100355 mp_uint_t len;
Damien George698ec212014-02-08 18:17:23 +0000356 const char *str = mp_obj_str_get_data(o_in, &len);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300357 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien George1e9a92f2014-11-06 17:36:16 +0000358 len = unichar_charlen(str, len);
359 if (len == 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000360 if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
Damien George40f3c022014-07-03 13:25:24 +0100361 mp_int_t ord = *str++ & 0x7F;
362 for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000363 ord &= ~mask;
364 }
365 while (UTF8_IS_CONT(*str)) {
366 ord = (ord << 6) | (*str++ & 0x3F);
367 }
368 return mp_obj_new_int(ord);
369 } else {
370 return mp_obj_new_int(((const byte*)str)[0]);
371 }
Damiena3dcd9e2013-12-17 21:35:38 +0000372 }
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300373 #else
374 if (len == 1) {
375 // don't sign extend when converting to ord
376 return mp_obj_new_int(((const byte*)str)[0]);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300377 }
378 #endif
Damien George1e9a92f2014-11-06 17:36:16 +0000379
380 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
381 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
382 "ord expects a character"));
383 } else {
384 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
385 "ord() expected a character, but string of length %d found", len));
386 }
Damiena3dcd9e2013-12-17 21:35:38 +0000387}
Damien George23005372014-01-13 19:39:01 +0000388MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
389
Damien Georged182b982014-08-30 14:19:41 +0100390STATIC mp_obj_t mp_builtin_pow(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000391 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000392 switch (n_args) {
Damien Georged17926d2014-03-30 13:35:08 +0100393 case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]);
394 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 +0000395 }
396}
Damien George23005372014-01-13 19:39:01 +0000397MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
398
Damien Georged182b982014-08-30 14:19:41 +0100399STATIC mp_obj_t mp_builtin_print(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien George48815662014-04-02 10:34:44 +0100400 mp_map_elem_t *sep_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_MAP_LOOKUP);
401 mp_map_elem_t *end_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_MAP_LOOKUP);
402 const char *sep_data = " ";
Damien Georged182b982014-08-30 14:19:41 +0100403 mp_uint_t sep_len = 1;
Damien George48815662014-04-02 10:34:44 +0100404 const char *end_data = "\n";
Damien Georged182b982014-08-30 14:19:41 +0100405 mp_uint_t end_len = 1;
Damien George48815662014-04-02 10:34:44 +0100406 if (sep_elem != NULL && sep_elem->value != mp_const_none) {
407 sep_data = mp_obj_str_get_data(sep_elem->value, &sep_len);
408 }
409 if (end_elem != NULL && end_elem->value != mp_const_none) {
410 end_data = mp_obj_str_get_data(end_elem->value, &end_len);
411 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300412 #if MICROPY_PY_IO
413 mp_obj_t stream_obj = &mp_sys_stdout_obj;
414 mp_map_elem_t *file_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_file), MP_MAP_LOOKUP);
415 if (file_elem != NULL && file_elem->value != mp_const_none) {
416 stream_obj = file_elem->value;
417 }
418
419 pfenv_t pfenv;
420 pfenv.data = stream_obj;
Damien George42f3de92014-10-03 17:44:14 +0000421 pfenv.print_strn = (void (*)(void *, const char *, mp_uint_t))mp_stream_write;
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300422 #endif
Damien George42f3de92014-10-03 17:44:14 +0000423 for (mp_uint_t i = 0; i < n_args; i++) {
Damiena3dcd9e2013-12-17 21:35:38 +0000424 if (i > 0) {
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300425 #if MICROPY_PY_IO
426 mp_stream_write(stream_obj, sep_data, sep_len);
427 #else
Damien George4abff752014-08-30 14:59:21 +0100428 printf("%.*s", (int)sep_len, sep_data);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300429 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000430 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300431 #if MICROPY_PY_IO
432 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))pfenv_printf, &pfenv, args[i], PRINT_STR);
433 #else
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200434 mp_obj_print(args[i], PRINT_STR);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300435 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000436 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300437 #if MICROPY_PY_IO
438 mp_stream_write(stream_obj, end_data, end_len);
439 #else
Damien George4abff752014-08-30 14:59:21 +0100440 printf("%.*s", (int)end_len, end_data);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300441 #endif
Damiend99b0522013-12-21 18:17:45 +0000442 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000443}
Damien George48815662014-04-02 10:34:44 +0100444MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
Damien George23005372014-01-13 19:39:01 +0000445
Paul Sokolovsky1eca3282014-11-26 21:17:16 +0200446STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
447 if (o != mp_const_none) {
448 mp_builtin_print(1, &o, (mp_map_t*)&mp_const_empty_map);
449 }
450 return mp_const_none;
451}
452MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
453
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200454STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000455 vstr_t *vstr = vstr_new();
Damien George4899ff92014-01-15 22:39:03 +0000456 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
Damien George2617eeb2014-05-25 22:27:57 +0100457 mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
Damien George5fa93b62014-01-22 14:35:10 +0000458 vstr_free(vstr);
459 return s;
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000460}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000461MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
462
Damien George1559a972014-10-31 11:28:50 +0000463STATIC mp_obj_t mp_builtin_round(mp_obj_t o_in) {
464 // TODO support second arg
465 if (MP_OBJ_IS_INT(o_in)) {
466 return o_in;
467 }
468#if MICROPY_PY_BUILTINS_FLOAT
469 mp_float_t val = mp_obj_get_float(o_in);
470 mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val);
471 mp_int_t r = rounded;
472 // make rounded value even if it was halfway between ints
473 if (val - rounded == 0.5) {
474 r = (r + 1) & (~1);
475 } else if (val - rounded == -0.5) {
476 r &= ~1;
477 }
478#else
479 mp_int_t r = mp_obj_get_int(o_in);
480#endif
481 return mp_obj_new_int(r);
482}
483MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_round_obj, mp_builtin_round);
484
Damien Georged182b982014-08-30 14:19:41 +0100485STATIC mp_obj_t mp_builtin_sum(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000486 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000487 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000488 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000489 case 1: value = mp_obj_new_int(0); break;
Damien George23005372014-01-13 19:39:01 +0000490 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000491 }
Damien Georged17926d2014-03-30 13:35:08 +0100492 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000493 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100494 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100495 value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
Damiena3dcd9e2013-12-17 21:35:38 +0000496 }
497 return value;
498}
Damien George23005372014-01-13 19:39:01 +0000499MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000500
Damien Georged182b982014-08-30 14:19:41 +0100501STATIC mp_obj_t mp_builtin_sorted(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien George20006db2014-01-18 14:10:48 +0000502 assert(n_args >= 1);
503 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100504 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton5c768392014-01-13 05:12:50 +0000505 "must use keyword argument for key function"));
506 }
Damien George3e1a5c12014-03-29 13:43:38 +0000507 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 +0000508 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000509
510 return self;
511}
John R. Lenton88cb1e62014-01-13 19:55:18 +0000512MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200513
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300514// See mp_load_attr() if making any changes
515STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
516 mp_obj_t dest[2];
517 // use load_method, raising or not raising exception
518 ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest);
519 if (dest[0] == MP_OBJ_NULL) {
520 return defval;
521 } else if (dest[1] == MP_OBJ_NULL) {
522 // load_method returned just a normal attribute
523 return dest[0];
524 } else {
525 // load_method returned a method, so build a bound method object
526 return mp_obj_new_bound_meth(dest[0], dest[1]);
527 }
528}
529
Damien Georged182b982014-08-30 14:19:41 +0100530STATIC mp_obj_t mp_builtin_getattr(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300531 mp_obj_t attr = args[1];
532 if (MP_OBJ_IS_TYPE(attr, &mp_type_str)) {
533 attr = mp_obj_str_intern(attr);
534 } else if (!MP_OBJ_IS_QSTR(attr)) {
535 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "string required"));
536 }
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300537 mp_obj_t defval = MP_OBJ_NULL;
538 if (n_args > 2) {
539 defval = args[2];
540 }
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300541 return mp_load_attr_default(args[0], MP_OBJ_QSTR_VALUE(attr), defval);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200542}
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300543MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
Paul Sokolovskycc0af3d2014-04-06 01:00:46 +0300544
Paul Sokolovskyff306662014-05-11 19:05:42 +0300545STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
546 assert(MP_OBJ_IS_QSTR(attr_in));
547
548 mp_obj_t dest[2];
Chris Angelicodaf973a2014-06-06 03:51:03 +1000549 // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
Paul Sokolovskyff306662014-05-11 19:05:42 +0300550 // explicitly says "This is implemented by calling getattr(object, name) and seeing
551 // whether it raises an AttributeError or not.", so we should explicitly wrap this
552 // in nlr_push and handle exception.
553 mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
554
555 return MP_BOOL(dest[0] != MP_OBJ_NULL);
556}
Paul Sokolovskyff306662014-05-11 19:05:42 +0300557MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
558
Damien George4c03b3a2014-08-12 18:33:40 +0100559// These are defined in terms of MicroPython API functions right away
Damien Georgec7687ad2014-08-22 21:48:30 +0100560MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_obj_id);
Damien George4c03b3a2014-08-12 18:33:40 +0100561MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len);
Paul Sokolovsky080d99b2014-04-06 01:18:19 +0300562MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
563MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);