blob: a434d318e95c6adf2c8ae3303c9ee8e914f0f69e [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdio.h>
2#include <string.h>
3#include <assert.h>
4
Damience89a212013-10-15 22:25:17 +01005#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01006#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00007#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +00009#include "obj.h"
Damien Georgecaac5422014-03-25 14:18:18 +000010#include "objmodule.h"
Damien George20773972014-02-22 18:12:43 +000011#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000012#include "runtime0.h"
13#include "runtime.h"
Damien George2326d522014-03-27 23:26:35 +000014#include "emitglue.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "map.h"
Damien660365e2013-12-17 18:27:24 +000016#include "builtin.h"
Damien Georgecaac5422014-03-25 14:18:18 +000017#include "builtintables.h"
Damien George5fa93b62014-01-22 14:35:10 +000018#include "bc.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000019#include "intdivmod.h"
Damien660365e2013-12-17 18:27:24 +000020
Damien7f5dacf2013-10-10 11:24:39 +010021#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010022#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020023#define DEBUG_printf DEBUG_printf
Damien George41eb6082014-02-26 22:40:35 +000024#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
Damien7f5dacf2013-10-10 11:24:39 +010025#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000026#define DEBUG_printf(...) (void)0
27#define DEBUG_OP_printf(...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010028#endif
Damien429d7192013-10-04 19:53:11 +010029
Damieneb19efb2013-10-10 22:06:54 +010030// locals and globals need to be pointers because they can be the same in outer module scope
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020031STATIC mp_map_t *map_locals;
32STATIC mp_map_t *map_globals;
33STATIC mp_map_t map_builtins;
Damienbd254452013-10-16 20:39:12 +010034
Damien George38a2da62014-01-08 17:33:12 +000035// a good optimising compiler will inline this if necessary
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020036STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
Damien George38a2da62014-01-08 17:33:12 +000037 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
38}
39
Damien Georged17926d2014-03-30 13:35:08 +010040void mp_init(void) {
Damien George2326d522014-03-27 23:26:35 +000041 mp_emit_glue_init();
42
Damieneb19efb2013-10-10 22:06:54 +010043 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +000044 map_locals = map_globals = mp_map_new(1);
Damien429d7192013-10-04 19:53:11 +010045
Damienb86e3f92013-12-29 17:17:43 +000046 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +000047 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +000048
Damien Georgecaac5422014-03-25 14:18:18 +000049 // init global module stuff
50 mp_module_init();
Damien George0d028742014-01-22 23:59:20 +000051
Damien Georgecaac5422014-03-25 14:18:18 +000052 // add some builtins that can't be done in ROM
53 mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Damien Georgee9906ac2014-01-04 18:44:46 +000054
Paul Sokolovskydcac8802014-01-16 19:19:50 +020055#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +020056 // Precreate sys module, so "import sys" didn't throw exceptions.
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020057 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
58 // Avoid warning of unused var
59 (void)m_sys;
Paul Sokolovskydcac8802014-01-16 19:19:50 +020060#endif
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020061 // init sys.path
62 // for efficiency, left to platform-specific startup code
Damien Georged17926d2014-03-30 13:35:08 +010063 //mp_sys_path = mp_obj_new_list(0, NULL);
64 //mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
Damien429d7192013-10-04 19:53:11 +010065}
66
Damien Georged17926d2014-03-30 13:35:08 +010067void mp_deinit(void) {
Damien George2326d522014-03-27 23:26:35 +000068 mp_map_free(map_globals);
69 mp_map_deinit(&map_builtins);
70 mp_module_deinit();
71 mp_emit_glue_deinit();
Damien429d7192013-10-04 19:53:11 +010072}
73
Damien Georged17926d2014-03-30 13:35:08 +010074mp_obj_t mp_list_append(mp_obj_t self_in, mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +000075 return mp_obj_list_append(self_in, arg);
76}
77
Damien Georged17926d2014-03-30 13:35:08 +010078mp_obj_t mp_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +000079 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +000080 uint len;
81 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +000082 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +000083}
84
Damien Georged17926d2014-03-30 13:35:08 +010085mp_obj_t mp_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +010086 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +000087 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +010088}
89
Damien Georged17926d2014-03-30 13:35:08 +010090mp_obj_t mp_load_const_bytes(qstr qstr) {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020091 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
92 uint len;
93 const byte *data = qstr_data(qstr, &len);
94 return mp_obj_new_bytes(data, len);
95}
96
Damien Georged17926d2014-03-30 13:35:08 +010097mp_obj_t mp_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +010098 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +010099 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000100 mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien Georgeaea532e2014-02-06 22:57:51 +0000101 if (elem != NULL) {
102 return elem->value;
103 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100104 return mp_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100105 }
Damiena3977762013-10-09 23:10:10 +0100106}
107
Damien Georged17926d2014-03-30 13:35:08 +0100108mp_obj_t mp_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100109 // logic: search globals, builtins
110 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000111 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100112 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000113 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100114 if (elem == NULL) {
Damien Georgecaac5422014-03-25 14:18:18 +0000115 mp_obj_t o = mp_builtin_tables_lookup_object(qstr);
116 if (o != MP_OBJ_NULL) {
117 return o;
Damien Georgeaea532e2014-02-06 22:57:51 +0000118 }
Damien Georgec5966122014-02-15 16:10:44 +0000119 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damien429d7192013-10-04 19:53:11 +0100120 }
121 }
122 return elem->value;
123}
124
Damien Georged17926d2014-03-30 13:35:08 +0100125mp_obj_t mp_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100126 DEBUG_OP_printf("load_build_class\n");
Damien Georgecaac5422014-03-25 14:18:18 +0000127 // lookup __build_class__ in dynamic table of builtins first
Damien George38a2da62014-01-08 17:33:12 +0000128 mp_map_elem_t *elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), MP_MAP_LOOKUP);
Damien Georgeaea532e2014-02-06 22:57:51 +0000129 if (elem != NULL) {
Damien Georgecaac5422014-03-25 14:18:18 +0000130 // found user-defined __build_class__, return it
Damien Georgeaea532e2014-02-06 22:57:51 +0000131 return elem->value;
132 } else {
Damien Georgecaac5422014-03-25 14:18:18 +0000133 // no user-defined __build_class__, return builtin one
Damien Georgeaea532e2014-02-06 22:57:51 +0000134 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100135 }
Damien429d7192013-10-04 19:53:11 +0100136}
137
Damien Georged17926d2014-03-30 13:35:08 +0100138mp_obj_t mp_get_cell(mp_obj_t cell) {
Damiend99b0522013-12-21 18:17:45 +0000139 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000140}
141
Damien Georged17926d2014-03-30 13:35:08 +0100142void mp_set_cell(mp_obj_t cell, mp_obj_t val) {
Damiend99b0522013-12-21 18:17:45 +0000143 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000144}
145
Damien Georged17926d2014-03-30 13:35:08 +0100146void mp_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100147 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000148 mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = obj;
Damiena3977762013-10-09 23:10:10 +0100149}
150
Damien Georged17926d2014-03-30 13:35:08 +0100151void mp_delete_name(qstr qstr) {
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200152 DEBUG_OP_printf("delete name %s\n", qstr_str(qstr));
153 mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
154}
155
Damien Georged17926d2014-03-30 13:35:08 +0100156void mp_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100157 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000158 mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = obj;
Damien429d7192013-10-04 19:53:11 +0100159}
160
Damien Georged17926d2014-03-30 13:35:08 +0100161mp_obj_t mp_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000162 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000163
Damiend99b0522013-12-21 18:17:45 +0000164 if (MP_OBJ_IS_SMALL_INT(arg)) {
165 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000166 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100167 case MP_UNARY_OP_BOOL:
Damien George9d68e9c2014-03-12 15:38:15 +0000168 return MP_BOOL(val != 0);
Damien Georged17926d2014-03-30 13:35:08 +0100169 case MP_UNARY_OP_POSITIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000170 return arg;
Damien Georged17926d2014-03-30 13:35:08 +0100171 case MP_UNARY_OP_NEGATIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000172 // check for overflow
173 if (val == MP_SMALL_INT_MIN) {
174 return mp_obj_new_int(-val);
175 } else {
176 return MP_OBJ_NEW_SMALL_INT(-val);
177 }
Damien Georged17926d2014-03-30 13:35:08 +0100178 case MP_UNARY_OP_INVERT:
Damien George9d68e9c2014-03-12 15:38:15 +0000179 return MP_OBJ_NEW_SMALL_INT(~val);
180 default:
181 assert(0);
182 return arg;
Damien7410e442013-11-02 19:47:57 +0000183 }
Damien George1e708fe2014-01-23 18:27:51 +0000184 } else {
185 mp_obj_type_t *type = mp_obj_get_type(arg);
186 if (type->unary_op != NULL) {
187 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000188 if (result != NULL) {
189 return result;
190 }
Damien7410e442013-11-02 19:47:57 +0000191 }
Damiend99b0522013-12-21 18:17:45 +0000192 // TODO specify in error message what the operator is
Damien George0ec6bd42014-03-09 16:29:36 +0000193 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bad operand type for unary operator: '%s'", mp_obj_get_type_str(arg)));
Damien7410e442013-11-02 19:47:57 +0000194 }
Damien429d7192013-10-04 19:53:11 +0100195}
196
Damien Georged17926d2014-03-30 13:35:08 +0100197mp_obj_t mp_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100198 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000199
200 // TODO correctly distinguish inplace operators for mutable objects
201 // lookup logic that CPython uses for +=:
202 // check for implemented +=
203 // then check for implemented +
204 // then check for implemented seq.inplace_concat
205 // then check for implemented seq.concat
206 // then fail
207 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
208
Damien George9aa2a522014-02-01 23:04:09 +0000209 // deal with is
Damien Georged17926d2014-03-30 13:35:08 +0100210 if (op == MP_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200211 return MP_BOOL(lhs == rhs);
212 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200213
Damien Georgebcbeea02014-01-11 10:47:22 +0000214 // deal with == and != for all types
Damien Georged17926d2014-03-30 13:35:08 +0100215 if (op == MP_BINARY_OP_EQUAL || op == MP_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000216 if (mp_obj_equal(lhs, rhs)) {
Damien Georged17926d2014-03-30 13:35:08 +0100217 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000218 return mp_const_true;
219 } else {
220 return mp_const_false;
221 }
222 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100223 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000224 return mp_const_false;
225 } else {
226 return mp_const_true;
227 }
228 }
229 }
230
231 // deal with exception_match for all types
Damien Georged17926d2014-03-30 13:35:08 +0100232 if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000233 // rhs must be issubclass(rhs, BaseException)
234 if (mp_obj_is_exception_type(rhs)) {
235 // if lhs is an instance of an exception, then extract and use its type
236 if (mp_obj_is_exception_instance(lhs)) {
237 lhs = mp_obj_get_type(lhs);
238 }
Damien George71510152014-03-03 22:38:13 +0000239 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000240 return mp_const_true;
241 } else {
242 return mp_const_false;
243 }
244 }
Paul Sokolovsky4b2b7ce2014-03-23 20:49:39 +0200245 assert(0);
246 return mp_const_false;
Damien Georgebcbeea02014-01-11 10:47:22 +0000247 }
248
Damien George1a9951d2014-01-06 22:13:00 +0000249 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000250 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000251 if (MP_OBJ_IS_SMALL_INT(rhs)) {
252 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000253 // This is a binary operation: lhs_val op rhs_val
254 // We need to be careful to handle overflow; see CERT INT32-C
255 // Operations that can overflow:
256 // + result always fits in machine_int_t, then handled by SMALL_INT check
257 // - result always fits in machine_int_t, then handled by SMALL_INT check
258 // * checked explicitly
259 // / if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
260 // % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
261 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000262 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100263 case MP_BINARY_OP_OR:
264 case MP_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
265 case MP_BINARY_OP_XOR:
266 case MP_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
267 case MP_BINARY_OP_AND:
268 case MP_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
269 case MP_BINARY_OP_LSHIFT:
270 case MP_BINARY_OP_INPLACE_LSHIFT: {
Damien George9d68e9c2014-03-12 15:38:15 +0000271 if (rhs_val < 0) {
272 // negative shift not allowed
273 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
274 } else if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
275 // left-shift will overflow, so use higher precision integer
276 lhs = mp_obj_new_int_from_ll(lhs_val);
277 goto generic_binary_op;
278 } else {
279 // use standard precision
280 lhs_val <<= rhs_val;
281 }
282 break;
283 }
Damien Georged17926d2014-03-30 13:35:08 +0100284 case MP_BINARY_OP_RSHIFT:
285 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000286 if (rhs_val < 0) {
287 // negative shift not allowed
288 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
289 } else {
290 // standard precision is enough for right-shift
291 lhs_val >>= rhs_val;
292 }
293 break;
Damien Georged17926d2014-03-30 13:35:08 +0100294 case MP_BINARY_OP_ADD:
295 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
296 case MP_BINARY_OP_SUBTRACT:
297 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
298 case MP_BINARY_OP_MULTIPLY:
299 case MP_BINARY_OP_INPLACE_MULTIPLY: {
Damien George9d68e9c2014-03-12 15:38:15 +0000300
301 // If long long type exists and is larger than machine_int_t, then
302 // we can use the following code to perform overflow-checked multiplication.
303 // Otherwise (eg in x64 case) we must use the branching code below.
304 #if 0
305 // compute result using long long precision
306 long long res = (long long)lhs_val * (long long)rhs_val;
307 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
308 // result overflowed SMALL_INT, so return higher precision integer
309 return mp_obj_new_int_from_ll(res);
310 } else {
311 // use standard precision
312 lhs_val = (mp_small_int_t)res;
313 }
314 #endif
315
316 if (lhs_val > 0) { // lhs_val is positive
317 if (rhs_val > 0) { // lhs_val and rhs_val are positive
318 if (lhs_val > (MP_SMALL_INT_MAX / rhs_val)) {
319 goto mul_overflow;
320 }
321 } else { // lhs_val positive, rhs_val nonpositive
322 if (rhs_val < (MP_SMALL_INT_MIN / lhs_val)) {
323 goto mul_overflow;
324 }
325 } // lhs_val positive, rhs_val nonpositive
326 } else { // lhs_val is nonpositive
327 if (rhs_val > 0) { // lhs_val is nonpositive, rhs_val is positive
328 if (lhs_val < (MP_SMALL_INT_MIN / rhs_val)) {
329 goto mul_overflow;
330 }
331 } else { // lhs_val and rhs_val are nonpositive
332 if (lhs_val != 0 && rhs_val < (MP_SMALL_INT_MAX / lhs_val)) {
333 goto mul_overflow;
334 }
335 } // End if lhs_val and rhs_val are nonpositive
336 } // End if lhs_val is nonpositive
337
338 // use standard precision
339 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
340
341 mul_overflow:
342 // use higher precision
343 lhs = mp_obj_new_int_from_ll(lhs_val);
344 goto generic_binary_op;
345
346 break;
347 }
Damien Georged17926d2014-03-30 13:35:08 +0100348 case MP_BINARY_OP_FLOOR_DIVIDE:
349 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Rachel Dowdall56402792014-03-22 20:19:24 +0000350 {
351 lhs_val = python_floor_divide(lhs_val, rhs_val);
352 break;
353 }
Damien George9d68e9c2014-03-12 15:38:15 +0000354 #if MICROPY_ENABLE_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100355 case MP_BINARY_OP_TRUE_DIVIDE:
356 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000357 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000358
Damien Georged17926d2014-03-30 13:35:08 +0100359 case MP_BINARY_OP_MODULO:
360 case MP_BINARY_OP_INPLACE_MODULO:
Rachel Dowdallcde86312014-03-22 17:29:27 +0000361 {
362 lhs_val = python_modulo(lhs_val, rhs_val);
363 break;
364 }
Damien Georged17926d2014-03-30 13:35:08 +0100365 case MP_BINARY_OP_POWER:
366 case MP_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000367 if (rhs_val < 0) {
368 #if MICROPY_ENABLE_FLOAT
369 lhs = mp_obj_new_float(lhs_val);
370 goto generic_binary_op;
371 #else
372 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
373 #endif
374 } else {
375 // TODO check for overflow
376 machine_int_t ans = 1;
377 while (rhs_val > 0) {
378 if (rhs_val & 1) {
379 ans *= lhs_val;
380 }
381 lhs_val *= lhs_val;
382 rhs_val /= 2;
Damien George1a9951d2014-01-06 22:13:00 +0000383 }
Damien George9d68e9c2014-03-12 15:38:15 +0000384 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000385 }
Damien George1a9951d2014-01-06 22:13:00 +0000386 break;
Damien Georged17926d2014-03-30 13:35:08 +0100387 case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
388 case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
389 case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
390 case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000391
Damien George1a9951d2014-01-06 22:13:00 +0000392 default: assert(0);
393 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200394 // TODO: We just should make mp_obj_new_int() inline and use that
395 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000396 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000397 } else {
398 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000399 }
Damien George3f759b72014-01-31 00:42:12 +0000400#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000401 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien George1a9951d2014-01-06 22:13:00 +0000402 return mp_obj_float_binary_op(op, lhs_val, rhs);
Damien George0c36da02014-03-08 15:24:39 +0000403 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien George1a9951d2014-01-06 22:13:00 +0000404 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000405#endif
Damien429d7192013-10-04 19:53:11 +0100406 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000407 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000408
Damien George9aa2a522014-02-01 23:04:09 +0000409 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000410 *
411 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000412 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000413 */
Damien Georged17926d2014-03-30 13:35:08 +0100414 if (op == MP_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000415 mp_obj_type_t *type = mp_obj_get_type(rhs);
416 if (type->binary_op != NULL) {
417 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000418 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000419 return res;
420 }
421 }
422 if (type->getiter != NULL) {
423 /* second attempt, walk the iterator */
424 mp_obj_t next = NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100425 mp_obj_t iter = mp_getiter(rhs);
426 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000427 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000428 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000429 }
Damien7410e442013-11-02 19:47:57 +0000430 }
Damien George9aa2a522014-02-01 23:04:09 +0000431 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000432 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000433
434 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000435 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000436 mp_obj_get_type_str(rhs)));
437 return mp_const_none;
438 }
439
Damien George5fa93b62014-01-22 14:35:10 +0000440 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000441 mp_obj_type_t *type;
442generic_binary_op:
443 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000444 if (type->binary_op != NULL) {
445 mp_obj_t result = type->binary_op(op, lhs, rhs);
446 if (result != MP_OBJ_NULL) {
447 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000448 }
Damien429d7192013-10-04 19:53:11 +0100449 }
Damiend99b0522013-12-21 18:17:45 +0000450
Damien George5fa93b62014-01-22 14:35:10 +0000451 // TODO implement dispatch for reverse binary ops
452
Damiend99b0522013-12-21 18:17:45 +0000453 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000454 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200455 "unsupported operand types for binary operator: '%s', '%s'",
456 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000457 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100458}
459
Damien Georged17926d2014-03-30 13:35:08 +0100460mp_obj_t mp_call_function_0(mp_obj_t fun) {
461 return mp_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100462}
463
Damien Georged17926d2014-03-30 13:35:08 +0100464mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
465 return mp_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100466}
467
Damien Georged17926d2014-03-30 13:35:08 +0100468mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
Damiend99b0522013-12-21 18:17:45 +0000469 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000470 args[0] = arg1;
471 args[1] = arg2;
Damien Georged17926d2014-03-30 13:35:08 +0100472 return mp_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100473}
474
Damien Georgecd82e022014-02-02 13:11:48 +0000475// wrapper that accepts n_args and n_kw in one argument
476// native emitter can only pass at most 3 arguments to a function
Damien Georged17926d2014-03-30 13:35:08 +0100477mp_obj_t mp_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
478 return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
Damien Georgecd82e022014-02-02 13:11:48 +0000479}
480
Damien George20006db2014-01-18 14:10:48 +0000481// args contains, eg: arg0 arg1 key0 value0 key1 value1
Damien Georged17926d2014-03-30 13:35:08 +0100482mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damiend99b0522013-12-21 18:17:45 +0000483 // TODO improve this: fun object can specify its type and we parse here the arguments,
484 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100485
John R. Lenton9c83ec02014-01-07 23:06:46 +0000486 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
487
Damien George8b56beb2014-01-31 23:49:49 +0000488 // get the type
489 mp_obj_type_t *type = mp_obj_get_type(fun_in);
490
491 // do the call
492 if (type->call != NULL) {
493 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000494 } else {
Damien George0ec6bd42014-03-09 16:29:36 +0000495 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not callable", mp_obj_get_type_str(fun_in)));
John R. Lenton9c83ec02014-01-07 23:06:46 +0000496 }
Damien86c7fc72013-11-26 15:16:41 +0000497}
498
Damien George20006db2014-01-18 14:10:48 +0000499// args contains: fun self/NULL arg(0) ... arg(n_args-2) arg(n_args-1) kw_key(0) kw_val(0) ... kw_key(n_kw-1) kw_val(n_kw-1)
500// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damien Georged17926d2014-03-30 13:35:08 +0100501mp_obj_t mp_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +0000502 DEBUG_OP_printf("call method (fun=%p, self=%p, n_args=%u, n_kw=%u, args=%p)\n", args[0], args[1], n_args, n_kw, args);
503 int adjust = (args[1] == NULL) ? 0 : 1;
Damien Georged17926d2014-03-30 13:35:08 +0100504 return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000505}
506
Damien Georged17926d2014-03-30 13:35:08 +0100507mp_obj_t mp_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000508 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100509}
510
Damien Georged17926d2014-03-30 13:35:08 +0100511mp_obj_t mp_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000512 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100513}
514
Damien Georged17926d2014-03-30 13:35:08 +0100515mp_obj_t mp_build_set(int n_args, mp_obj_t *items) {
Damiend99b0522013-12-21 18:17:45 +0000516 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100517}
518
Damien Georged17926d2014-03-30 13:35:08 +0100519mp_obj_t mp_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000520 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100521 return set;
522}
523
Damien George932bf1c2014-01-18 23:42:49 +0000524// unpacked items are stored in reverse order into the array pointed to by items
Damien Georged17926d2014-03-30 13:35:08 +0100525void mp_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200526 uint seq_len;
Damien George3e1a5c12014-03-29 13:43:38 +0000527 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
Damiend99b0522013-12-21 18:17:45 +0000528 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000529 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000530 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
531 } else {
532 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000533 }
Damiend99b0522013-12-21 18:17:45 +0000534 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200535 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000536 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200537 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000538 }
Damien George932bf1c2014-01-18 23:42:49 +0000539 for (uint i = 0; i < num; i++) {
540 items[i] = seq_items[num - 1 - i];
541 }
Damien86c7fc72013-11-26 15:16:41 +0000542 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100543 mp_obj_t iterable = mp_getiter(seq_in);
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200544
545 for (seq_len = 0; seq_len < num; seq_len++) {
Damien Georged17926d2014-03-30 13:35:08 +0100546 mp_obj_t el = mp_iternext(iterable);
Damien George66eaf842014-03-26 19:27:58 +0000547 if (el == MP_OBJ_NULL) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200548 goto too_short;
549 }
550 items[num - 1 - seq_len] = el;
551 }
Damien Georged17926d2014-03-30 13:35:08 +0100552 if (mp_iternext(iterable) != MP_OBJ_NULL) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200553 goto too_long;
554 }
Damien86c7fc72013-11-26 15:16:41 +0000555 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200556 return;
557
558too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000559 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", seq_len));
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200560too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000561 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "too many values to unpack (expected %d)", num));
Damien86c7fc72013-11-26 15:16:41 +0000562}
563
Damien Georged17926d2014-03-30 13:35:08 +0100564mp_obj_t mp_build_map(int n_args) {
Damiend99b0522013-12-21 18:17:45 +0000565 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100566}
567
Damien Georged17926d2014-03-30 13:35:08 +0100568mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
Damiend99b0522013-12-21 18:17:45 +0000569 // map should always be a dict
570 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100571}
572
Damien Georged17926d2014-03-30 13:35:08 +0100573mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000574 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
575 // use load_method
576 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100577 mp_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000578 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000579 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000580 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000581 } else {
582 // load_method returned a method, so build a bound method object
583 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000584 }
Damiend99b0522013-12-21 18:17:45 +0000585}
586
Damien George7c9c6672014-01-25 00:17:36 +0000587// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
588// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
589// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Damien Georged17926d2014-03-30 13:35:08 +0100590STATIC void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000591 // clear output to indicate no attribute/method found yet
592 dest[0] = MP_OBJ_NULL;
593 dest[1] = MP_OBJ_NULL;
594
595 // get the type
596 mp_obj_type_t *type = mp_obj_get_type(base);
597
598 // if this type can do its own load, then call it
599 if (type->load_attr != NULL) {
600 type->load_attr(base, attr, dest);
601 }
602
603 // if nothing found yet, look for built-in and generic names
Damien George7c9c6672014-01-25 00:17:36 +0000604 if (dest[0] == MP_OBJ_NULL) {
Damien George35e2a4e2014-02-05 00:51:47 +0000605 if (attr == MP_QSTR___class__) {
606 // a.__class__ is equivalent to type(a)
607 dest[0] = type;
608 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000609 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
610 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000611 } else if (type->load_attr == NULL) {
612 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000613 // this is a lookup in the object (ie not class or type)
Damien George9b196cd2014-03-26 21:47:19 +0000614 if (type->locals_dict != NULL) {
Damien George3e1a5c12014-03-29 13:43:38 +0000615 assert(MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)); // Micro Python restriction, for now
Damien George9b196cd2014-03-26 21:47:19 +0000616 mp_map_t *locals_map = mp_obj_dict_get_map(type->locals_dict);
617 mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
618 if (elem != NULL) {
619 // check if the methods are functions, static or class methods
620 // see http://docs.python.org/3.3/howto/descriptor.html
621 if (MP_OBJ_IS_TYPE(elem->value, &mp_type_staticmethod)) {
622 // return just the function
623 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
624 } else if (MP_OBJ_IS_TYPE(elem->value, &mp_type_classmethod)) {
625 // return a bound method, with self being the type of this object
626 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
627 dest[1] = mp_obj_get_type(base);
Damien Georgec3f11262014-03-26 22:35:10 +0000628 } else if (mp_obj_is_callable(elem->value)) {
Damien George9b196cd2014-03-26 21:47:19 +0000629 // return a bound method, with self being this object
Damien Georgec3f11262014-03-26 22:35:10 +0000630 dest[0] = elem->value;
Damien George9b196cd2014-03-26 21:47:19 +0000631 dest[1] = base;
Damien Georgec3f11262014-03-26 22:35:10 +0000632 } else {
633 // class member is a value, so just return that value
634 dest[0] = elem->value;
Damien George062478e2014-01-09 20:57:50 +0000635 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000636 }
Damiend57eba52013-11-02 23:58:14 +0000637 }
638 }
Damiena3977762013-10-09 23:10:10 +0100639 }
Damien George7c9c6672014-01-25 00:17:36 +0000640}
Damiena3977762013-10-09 23:10:10 +0100641
Damien Georged17926d2014-03-30 13:35:08 +0100642void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George7c9c6672014-01-25 00:17:36 +0000643 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
644
Damien Georged17926d2014-03-30 13:35:08 +0100645 mp_load_method_maybe(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000646
647 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000648 // no attribute/method called attr
649 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000650 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
Paul Sokolovsky7f8b3132014-03-25 00:55:39 +0200651 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
652 "type object '%s' has no attribute '%s'", qstr_str(((mp_obj_type_t*)base)->name), qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +0000653 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000654 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +0000655 }
656 }
Damiena3977762013-10-09 23:10:10 +0100657}
658
Damien Georged17926d2014-03-30 13:35:08 +0100659void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100660 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000661 mp_obj_type_t *type = mp_obj_get_type(base);
662 if (type->store_attr != NULL) {
663 if (type->store_attr(base, attr, value)) {
664 return;
665 }
Damiena3977762013-10-09 23:10:10 +0100666 }
Damien Georgec5966122014-02-15 16:10:44 +0000667 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damiena3977762013-10-09 23:10:10 +0100668}
669
Damien Georged17926d2014-03-30 13:35:08 +0100670void mp_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100671 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damien George3e1a5c12014-03-29 13:43:38 +0000672 if (MP_OBJ_IS_TYPE(base, &mp_type_list)) {
Damien429d7192013-10-04 19:53:11 +0100673 // list store
Damiend99b0522013-12-21 18:17:45 +0000674 mp_obj_list_store(base, index, value);
Damien George3e1a5c12014-03-29 13:43:38 +0000675 } else if (MP_OBJ_IS_TYPE(base, &mp_type_dict)) {
Damiend99b0522013-12-21 18:17:45 +0000676 // dict store
677 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100678 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200679 mp_obj_type_t *type = mp_obj_get_type(base);
680 if (type->store_item != NULL) {
681 bool r = type->store_item(base, index, value);
682 if (r) {
683 return;
684 }
685 // TODO: call base classes here?
686 }
Damien Georgec5966122014-02-15 16:10:44 +0000687 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
Damien429d7192013-10-04 19:53:11 +0100688 }
689}
690
Damien Georged17926d2014-03-30 13:35:08 +0100691mp_obj_t mp_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000692 mp_obj_type_t *type = mp_obj_get_type(o_in);
693 if (type->getiter != NULL) {
694 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +0100695 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000696 // check for __iter__ method
Damien George7c9c6672014-01-25 00:17:36 +0000697 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100698 mp_load_method_maybe(o_in, MP_QSTR___iter__, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000699 if (dest[0] != MP_OBJ_NULL) {
Damien George9e6e9352014-03-26 18:37:06 +0000700 // __iter__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100701 return mp_call_method_n_kw(0, 0, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000702 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100703 mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000704 if (dest[0] != MP_OBJ_NULL) {
705 // __getitem__ exists, create an iterator
706 return mp_obj_new_getitem_iter(dest);
707 } else {
708 // object not iterable
709 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
710 }
Damien George7c9c6672014-01-25 00:17:36 +0000711 }
Damience89a212013-10-15 22:25:17 +0100712 }
713}
714
Damien George66eaf842014-03-26 19:27:58 +0000715// may return MP_OBJ_NULL as an optimisation instead of raise StopIteration()
716// may also raise StopIteration()
Damien Georged17926d2014-03-30 13:35:08 +0100717mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000718 mp_obj_type_t *type = mp_obj_get_type(o_in);
719 if (type->iternext != NULL) {
720 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100721 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000722 // check for __next__ method
723 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100724 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000725 if (dest[0] != MP_OBJ_NULL) {
726 // __next__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100727 return mp_call_method_n_kw(0, 0, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000728 } else {
729 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
730 }
Damien Georgec5966122014-02-15 16:10:44 +0000731 }
732}
733
Damien George66eaf842014-03-26 19:27:58 +0000734// will always return MP_OBJ_NULL instead of raising StopIteration() (or any subclass thereof)
735// may raise other exceptions
Damien Georged17926d2014-03-30 13:35:08 +0100736mp_obj_t mp_iternext(mp_obj_t o_in) {
Damien George66eaf842014-03-26 19:27:58 +0000737 mp_obj_type_t *type = mp_obj_get_type(o_in);
738 if (type->iternext != NULL) {
739 return type->iternext(o_in);
740 } else {
741 // check for __next__ method
742 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100743 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George66eaf842014-03-26 19:27:58 +0000744 if (dest[0] != MP_OBJ_NULL) {
745 // __next__ exists, call it and return its result
746 nlr_buf_t nlr;
747 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100748 mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
Damien George66eaf842014-03-26 19:27:58 +0000749 nlr_pop();
750 return ret;
751 } else {
752 if (mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
753 return MP_OBJ_NULL;
754 } else {
755 nlr_jump(nlr.ret_val);
756 }
757 }
758 } else {
759 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
760 }
761 }
762}
763
Damien Georged17926d2014-03-30 13:35:08 +0100764mp_obj_t mp_make_raise_obj(mp_obj_t o) {
Damien Georgec5966122014-02-15 16:10:44 +0000765 DEBUG_printf("raise %p\n", o);
766 if (mp_obj_is_exception_type(o)) {
767 // o is an exception type (it is derived from BaseException (or is BaseException))
768 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +0000769 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
770 // could have const instances in ROM which we return here instead
Damien Georged17926d2014-03-30 13:35:08 +0100771 return mp_call_function_n_kw(o, 0, 0, NULL);
Damien Georgec5966122014-02-15 16:10:44 +0000772 } else if (mp_obj_is_exception_instance(o)) {
773 // o is an instance of an exception, so use it as the exception
774 return o;
775 } else {
776 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
777 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +0100778 }
779}
780
Damien Georged17926d2014-03-30 13:35:08 +0100781mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +0000782 DEBUG_printf("import name %s\n", qstr_str(name));
783
Damiendb4c3612013-12-10 17:27:24 +0000784 // build args array
Damiend99b0522013-12-21 18:17:45 +0000785 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +0000786 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +0000787 args[1] = mp_const_none; // TODO should be globals
788 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000789 args[3] = fromlist;
790 args[4] = level; // must be 0; we don't yet support other values
791
792 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000793 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000794}
795
Damien Georged17926d2014-03-30 13:35:08 +0100796mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +0000797 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
798
Damien Georged17926d2014-03-30 13:35:08 +0100799 mp_obj_t x = mp_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000800 /* TODO convert AttributeError to ImportError
801 if (fail) {
802 (ImportError, "cannot import name %s", qstr_str(name), NULL)
803 }
804 */
805 return x;
806}
807
Damien Georged17926d2014-03-30 13:35:08 +0100808void mp_import_all(mp_obj_t module) {
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200809 DEBUG_printf("import all %p\n", module);
810
811 mp_map_t *map = mp_obj_module_get_globals(module);
812 for (uint i = 0; i < map->alloc; i++) {
813 if (map->table[i].key != MP_OBJ_NULL) {
Damien Georged17926d2014-03-30 13:35:08 +0100814 mp_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200815 }
816 }
817}
818
Damien Georged17926d2014-03-30 13:35:08 +0100819mp_map_t *mp_locals_get(void) {
Damien George66028ab2014-01-03 14:03:48 +0000820 return map_locals;
821}
822
Damien Georged17926d2014-03-30 13:35:08 +0100823void mp_locals_set(mp_map_t *m) {
824 DEBUG_OP_printf("mp_locals_set(%p)\n", m);
Damien George66028ab2014-01-03 14:03:48 +0000825 map_locals = m;
826}
827
Damien Georged17926d2014-03-30 13:35:08 +0100828mp_map_t *mp_globals_get(void) {
Damien George66028ab2014-01-03 14:03:48 +0000829 return map_globals;
830}
831
Damien Georged17926d2014-03-30 13:35:08 +0100832void mp_globals_set(mp_map_t *m) {
833 DEBUG_OP_printf("mp_globals_set(%p)\n", m);
Damien George66028ab2014-01-03 14:03:48 +0000834 map_globals = m;
835}
836
Damien6ba13142013-11-02 20:34:54 +0000837// these must correspond to the respective enum
Damien Georged17926d2014-03-30 13:35:08 +0100838void *const mp_fun_table[MP_F_NUMBER_OF] = {
839 mp_load_const_dec,
840 mp_load_const_str,
841 mp_load_name,
842 mp_load_global,
843 mp_load_build_class,
844 mp_load_attr,
845 mp_load_method,
846 mp_store_name,
847 mp_store_attr,
848 mp_store_subscr,
849 mp_obj_is_true,
850 mp_unary_op,
851 mp_binary_op,
852 mp_build_tuple,
853 mp_build_list,
854 mp_list_append,
855 mp_build_map,
856 mp_store_map,
857 mp_build_set,
858 mp_store_set,
859 mp_make_function_from_id,
860 mp_call_function_n_kw_for_native,
861 mp_call_method_n_kw,
862 mp_getiter,
863 mp_iternext,
Damien429d7192013-10-04 19:53:11 +0100864};
865
866/*
Damien Georged17926d2014-03-30 13:35:08 +0100867void mp_f_vector(mp_fun_kind_t fun_kind) {
868 (mp_f_table[fun_kind])();
Damien429d7192013-10-04 19:53:11 +0100869}
870*/