blob: a45b1463d92afa9b5dcacf0831b8a4a33ee54a73 [file] [log] [blame]
Damien660365e2013-12-17 18:27:24 +00001#include <stdint.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include <stdarg.h>
5#include <string.h>
6#include <assert.h>
7
8#include "nlr.h"
9#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "mpconfig.h"
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000011#include "mpqstr.h"
Damien660365e2013-12-17 18:27:24 +000012#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "runtime0.h"
14#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "map.h"
Damien660365e2013-12-17 18:27:24 +000016#include "builtin.h"
17
Damien Georgeb97669a2014-01-08 11:47:55 +000018// args[0] is function from class body
19// args[1] is class name
20// args[2:] are base objects
Damien George23005372014-01-13 19:39:01 +000021static mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
Damien Georgeb97669a2014-01-08 11:47:55 +000022 assert(2 <= n_args);
23
Damiena3dcd9e2013-12-17 21:35:38 +000024 // we differ from CPython: we set the new __locals__ object here
Damien George66028ab2014-01-03 14:03:48 +000025 mp_map_t *old_locals = rt_locals_get();
Damien George062478e2014-01-09 20:57:50 +000026 mp_obj_t class_locals = mp_obj_new_dict(0);
27 rt_locals_set(mp_obj_dict_get_map(class_locals));
Damiena3dcd9e2013-12-17 21:35:38 +000028
29 // call the class code
Damien Georgeb97669a2014-01-08 11:47:55 +000030 mp_obj_t cell = rt_call_function_1(args[0], (mp_obj_t)0xdeadbeef);
Damiena3dcd9e2013-12-17 21:35:38 +000031
32 // restore old __locals__ object
Damien George66028ab2014-01-03 14:03:48 +000033 rt_locals_set(old_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000034
Damien Georgeb97669a2014-01-08 11:47:55 +000035 // get the class type (meta object) from the base objects
36 mp_obj_t meta;
37 if (n_args == 2) {
38 // no explicit bases, so use 'type'
39 meta = (mp_obj_t)&mp_const_type;
40 } else {
41 // use type of first base object
42 meta = mp_obj_get_type(args[2]);
43 }
Damien Georgeb97669a2014-01-08 11:47:55 +000044
45 // TODO do proper metaclass resolution for multiple base objects
46
Damien Georgeb97669a2014-01-08 11:47:55 +000047 // create the new class using a call to the meta object
48 // (arguments must be backwards in the array)
49 mp_obj_t meta_args[3];
50 meta_args[2] = args[1]; // class name
51 meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases
Damien George062478e2014-01-09 20:57:50 +000052 meta_args[0] = class_locals; // dict of members
Damien Georgeb97669a2014-01-08 11:47:55 +000053 mp_obj_t new_class = rt_call_function_n(meta, 3, meta_args);
Damien Georgeb97669a2014-01-08 11:47:55 +000054
55 // store into cell if neede
56 if (cell != mp_const_none) {
57 mp_obj_cell_set(cell, new_class);
58 }
59
60 return new_class;
Damiena3dcd9e2013-12-17 21:35:38 +000061}
62
Damien Georgeb97669a2014-01-08 11:47:55 +000063MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
64
Damien George23005372014-01-13 19:39:01 +000065static mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
Damiend99b0522013-12-21 18:17:45 +000066 if (o != mp_const_none) {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020067 mp_obj_print(o, PRINT_REPR);
Damien660365e2013-12-17 18:27:24 +000068 printf("\n");
69 }
Damiend99b0522013-12-21 18:17:45 +000070 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +000071}
72
Damien George23005372014-01-13 19:39:01 +000073MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
74
Damiend99b0522013-12-21 18:17:45 +000075mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
76 if (MP_OBJ_IS_SMALL_INT(o_in)) {
77 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
Damien660365e2013-12-17 18:27:24 +000078 if (val < 0) {
79 val = -val;
80 }
Damiend99b0522013-12-21 18:17:45 +000081 return MP_OBJ_NEW_SMALL_INT(val);
Damien660365e2013-12-17 18:27:24 +000082#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +000083 } else if (MP_OBJ_IS_TYPE(o_in, &float_type)) {
84 mp_float_t value = mp_obj_float_get(o_in);
Damien660365e2013-12-17 18:27:24 +000085 // TODO check for NaN etc
Damiend99b0522013-12-21 18:17:45 +000086 if (value < 0) {
87 return mp_obj_new_float(-value);
Damien660365e2013-12-17 18:27:24 +000088 } else {
89 return o_in;
90 }
Damiend99b0522013-12-21 18:17:45 +000091 } else if (MP_OBJ_IS_TYPE(o_in, &complex_type)) {
92 mp_float_t real, imag;
93 mp_obj_complex_get(o_in, &real, &imag);
94 return mp_obj_new_float(machine_sqrt(real*real + imag*imag));
Damien660365e2013-12-17 18:27:24 +000095#endif
96 } else {
97 assert(0);
Damiend99b0522013-12-21 18:17:45 +000098 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +000099 }
100}
101
Damien George23005372014-01-13 19:39:01 +0000102MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
103
104static mp_obj_t mp_builtin_all(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000105 mp_obj_t iterable = rt_getiter(o_in);
106 mp_obj_t item;
107 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
Damiena3dcd9e2013-12-17 21:35:38 +0000108 if (!rt_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000109 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000110 }
111 }
Damiend99b0522013-12-21 18:17:45 +0000112 return mp_const_true;
Damien660365e2013-12-17 18:27:24 +0000113}
114
Damien George23005372014-01-13 19:39:01 +0000115MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
116
117static mp_obj_t mp_builtin_any(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000118 mp_obj_t iterable = rt_getiter(o_in);
119 mp_obj_t item;
120 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
Damiena3dcd9e2013-12-17 21:35:38 +0000121 if (rt_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000122 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000123 }
124 }
Damiend99b0522013-12-21 18:17:45 +0000125 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000126}
127
Damien George23005372014-01-13 19:39:01 +0000128MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
129
130static mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000131 if (mp_obj_is_callable(o_in)) {
132 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000133 } else {
Damiend99b0522013-12-21 18:17:45 +0000134 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000135 }
136}
137
Damien George23005372014-01-13 19:39:01 +0000138MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
139
140static mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000141 int ord = mp_obj_get_int(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000142 if (0 <= ord && ord <= 0x10ffff) {
143 char *str = m_new(char, 2);
144 str[0] = ord;
145 str[1] = '\0';
Damien732407f2013-12-29 19:33:23 +0000146 return mp_obj_new_str(qstr_from_str_take(str, 2));
Damiena3dcd9e2013-12-17 21:35:38 +0000147 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000148 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "chr() arg not in range(0x110000)"));
Damiena3dcd9e2013-12-17 21:35:38 +0000149 }
150}
151
Damien George23005372014-01-13 19:39:01 +0000152MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
153
154static mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
Damiend99b0522013-12-21 18:17:45 +0000155 if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
156 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
157 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
158 mp_obj_t revs_args[2];
159 revs_args[1] = MP_OBJ_NEW_SMALL_INT(i1 / i2);
160 revs_args[0] = MP_OBJ_NEW_SMALL_INT(i1 % i2);
Damiena3dcd9e2013-12-17 21:35:38 +0000161 return rt_build_tuple(2, revs_args);
162 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000163 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_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 +0000164 }
165}
166
Damien George23005372014-01-13 19:39:01 +0000167MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
168
Damiend99b0522013-12-21 18:17:45 +0000169static mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000170 // TODO hash will generally overflow small integer; can we safely truncate it?
Damiend99b0522013-12-21 18:17:45 +0000171 return mp_obj_new_int(mp_obj_hash(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000172}
173
Damiend99b0522013-12-21 18:17:45 +0000174MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
175
176static mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000177 return rt_getiter(o_in);
178}
179
Damiend99b0522013-12-21 18:17:45 +0000180MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000181
Damien George23005372014-01-13 19:39:01 +0000182static mp_obj_t mp_builtin_len(mp_obj_t o_in) {
John R. Lenton4bee76e2014-01-10 11:25:03 +0000183 mp_obj_t len = mp_obj_len_maybe(o_in);
184 if (len == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000185 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
John R. Lenton4bee76e2014-01-10 11:25:03 +0000186 } else {
187 return len;
Damiena3dcd9e2013-12-17 21:35:38 +0000188 }
Damiena3dcd9e2013-12-17 21:35:38 +0000189}
190
Damien George23005372014-01-13 19:39:01 +0000191MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
192
193static mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000194 if (n_args == 1) {
195 // given an iterable
Damiend99b0522013-12-21 18:17:45 +0000196 mp_obj_t iterable = rt_getiter(args[0]);
197 mp_obj_t max_obj = NULL;
198 mp_obj_t item;
199 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
200 if (max_obj == NULL || mp_obj_less(max_obj, item)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000201 max_obj = item;
202 }
203 }
204 if (max_obj == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000205 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "max() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000206 }
207 return max_obj;
208 } else {
209 // given many args
Damiend99b0522013-12-21 18:17:45 +0000210 mp_obj_t max_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000211 for (int i = 1; i < n_args; i++) {
Damiend99b0522013-12-21 18:17:45 +0000212 if (mp_obj_less(max_obj, args[i])) {
Damiena3dcd9e2013-12-17 21:35:38 +0000213 max_obj = args[i];
214 }
215 }
216 return max_obj;
217 }
218}
219
Damien George23005372014-01-13 19:39:01 +0000220MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max);
221
222static mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000223 if (n_args == 1) {
224 // given an iterable
Damiend99b0522013-12-21 18:17:45 +0000225 mp_obj_t iterable = rt_getiter(args[0]);
226 mp_obj_t min_obj = NULL;
227 mp_obj_t item;
228 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
229 if (min_obj == NULL || mp_obj_less(item, min_obj)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000230 min_obj = item;
231 }
232 }
233 if (min_obj == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000234 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "min() arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000235 }
236 return min_obj;
237 } else {
238 // given many args
Damiend99b0522013-12-21 18:17:45 +0000239 mp_obj_t min_obj = args[0];
Damiena3dcd9e2013-12-17 21:35:38 +0000240 for (int i = 1; i < n_args; i++) {
Damiend99b0522013-12-21 18:17:45 +0000241 if (mp_obj_less(args[i], min_obj)) {
Damiena3dcd9e2013-12-17 21:35:38 +0000242 min_obj = args[i];
243 }
244 }
245 return min_obj;
246 }
247}
248
Damien George23005372014-01-13 19:39:01 +0000249MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
250
Damiend9d62012013-12-21 18:38:03 +0000251static mp_obj_t mp_builtin_next(mp_obj_t o) {
252 mp_obj_t ret = rt_iternext(o);
253 if (ret == mp_const_stop_iteration) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000254 nlr_jump(mp_obj_new_exception(MP_QSTR_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000255 } else {
256 return ret;
257 }
Damiend99b0522013-12-21 18:17:45 +0000258}
259
260MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
261
Damien George23005372014-01-13 19:39:01 +0000262static mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000263 const char *str = qstr_str(mp_obj_get_qstr(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000264 if (strlen(str) == 1) {
Damiend99b0522013-12-21 18:17:45 +0000265 return mp_obj_new_int(str[0]);
Damiena3dcd9e2013-12-17 21:35:38 +0000266 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000267 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "ord() expected a character, but string of length %d found", strlen(str)));
Damiena3dcd9e2013-12-17 21:35:38 +0000268 }
269}
270
Damien George23005372014-01-13 19:39:01 +0000271MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
272
273static mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) {
274 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000275 switch (n_args) {
276 case 2: return rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]);
Damien George23005372014-01-13 19:39:01 +0000277 default: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
Damiena3dcd9e2013-12-17 21:35:38 +0000278 }
279}
280
Damien George23005372014-01-13 19:39:01 +0000281MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
282
283static mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
Damiena3dcd9e2013-12-17 21:35:38 +0000284 for (int i = 0; i < n_args; i++) {
285 if (i > 0) {
286 printf(" ");
287 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200288 mp_obj_print(args[i], PRINT_STR);
Damiena3dcd9e2013-12-17 21:35:38 +0000289 }
290 printf("\n");
Damiend99b0522013-12-21 18:17:45 +0000291 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000292}
293
Damien George23005372014-01-13 19:39:01 +0000294MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_print_obj, 0, mp_builtin_print);
295
296static mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) {
297 assert(1 <= n_args && n_args <= 3);
Damien660365e2013-12-17 18:27:24 +0000298 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000299 case 1: return mp_obj_new_range(0, mp_obj_get_int(args[0]), 1);
300 case 2: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), 1);
Damien George23005372014-01-13 19:39:01 +0000301 default: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]));
Damien660365e2013-12-17 18:27:24 +0000302 }
303}
Damiena3dcd9e2013-12-17 21:35:38 +0000304
Damien George23005372014-01-13 19:39:01 +0000305MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range);
306
307static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
308 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000309 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000310 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000311 case 1: value = mp_obj_new_int(0); break;
Damien George23005372014-01-13 19:39:01 +0000312 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000313 }
Damiend99b0522013-12-21 18:17:45 +0000314 mp_obj_t iterable = rt_getiter(args[0]);
315 mp_obj_t item;
316 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
Damiena3dcd9e2013-12-17 21:35:38 +0000317 value = rt_binary_op(RT_BINARY_OP_ADD, value, item);
318 }
319 return value;
320}
Damien George23005372014-01-13 19:39:01 +0000321MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000322
John R. Lenton5c768392014-01-13 05:12:50 +0000323static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) {
324 mp_obj_t *args_items = NULL;
325 uint args_len = 0;
326
327 assert(MP_OBJ_IS_TYPE(args, &tuple_type));
328 mp_obj_tuple_get(args, &args_len, &args_items);
329 assert(args_len >= 1);
330 if (args_len > 1) {
331 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
332 "must use keyword argument for key function"));
333 }
John R. Lenton2ded68d2014-01-13 19:52:28 +0000334 mp_obj_t self = list_type.make_new((mp_obj_t)&list_type, 1, args_items);
John R. Lenton5c768392014-01-13 05:12:50 +0000335 mp_obj_t new_args = rt_build_tuple(1, &self);
Damien George0f592032014-01-14 23:18:35 +0000336 mp_obj_list_sort(new_args, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000337
338 return self;
339}
John R. Lenton88cb1e62014-01-13 19:55:18 +0000340MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);