blob: 7dd9cc0a24cda1c571188d8f6d60c9810ad6020a [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>
John R. Lenton4bee76e2014-01-10 11:25:03 +00005#include <string.h>
Damien660365e2013-12-17 18:27:24 +00006#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"
15#include "map.h"
Damien660365e2013-12-17 18:27:24 +000016
Damien Georgeb97669a2014-01-08 11:47:55 +000017mp_obj_t mp_obj_get_type(mp_obj_t o_in) {
18 if (MP_OBJ_IS_SMALL_INT(o_in)) {
19 return (mp_obj_t)&int_type;
Damien George38a2da62014-01-08 17:33:12 +000020 } else if (MP_OBJ_IS_QSTR(o_in)) {
21 return (mp_obj_t)&str_type;
Damien Georgeb97669a2014-01-08 11:47:55 +000022 } else {
23 mp_obj_base_t *o = o_in;
24 return (mp_obj_t)o->type;
25 }
26}
27
Damiend99b0522013-12-21 18:17:45 +000028const char *mp_obj_get_type_str(mp_obj_t o_in) {
29 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien660365e2013-12-17 18:27:24 +000030 return "int";
31 } else {
Damiend99b0522013-12-21 18:17:45 +000032 mp_obj_base_t *o = o_in;
33 return o->type->name;
Damien660365e2013-12-17 18:27:24 +000034 }
35}
36
37void printf_wrapper(void *env, const char *fmt, ...) {
38 va_list args;
39 va_start(args, fmt);
40 vprintf(fmt, args);
41 va_end(args);
42}
43
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020044void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000045 if (MP_OBJ_IS_SMALL_INT(o_in)) {
46 print(env, "%d", (int)MP_OBJ_SMALL_INT_VALUE(o_in));
Paul Sokolovskybb33cc62014-01-20 00:59:25 +020047 } else if (MP_OBJ_IS_QSTR(o_in)) {
48 mp_obj_str_print_qstr(print, env, MP_OBJ_QSTR_VALUE(o_in), kind);
Damien660365e2013-12-17 18:27:24 +000049 } else {
Damiend99b0522013-12-21 18:17:45 +000050 mp_obj_base_t *o = o_in;
51 if (o->type->print != NULL) {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020052 o->type->print(print, env, o_in, kind);
Damiend99b0522013-12-21 18:17:45 +000053 } else {
54 print(env, "<%s>", o->type->name);
Damien660365e2013-12-17 18:27:24 +000055 }
56 }
57}
58
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020059void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
60 mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000061}
62
Damien George136b1492014-01-19 12:38:49 +000063// helper function to print an exception with traceback
64void mp_obj_print_exception(mp_obj_t exc) {
65 if (MP_OBJ_IS_TYPE(exc, &exception_type)) {
66 machine_uint_t n, *values;
67 mp_obj_exception_get_traceback(exc, &n, &values);
68 if (n > 0) {
69 printf("Traceback (most recent call last):\n");
70 for (int i = n - 3; i >= 0; i -= 3) {
71 printf(" File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
72 }
73 }
74 }
75 mp_obj_print(exc, PRINT_REPR);
76 printf("\n");
77}
78
Damiend99b0522013-12-21 18:17:45 +000079bool mp_obj_is_callable(mp_obj_t o_in) {
80 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien660365e2013-12-17 18:27:24 +000081 return false;
82 } else {
Damiend99b0522013-12-21 18:17:45 +000083 mp_obj_base_t *o = o_in;
Damien George20006db2014-01-18 14:10:48 +000084 return o->type->call != NULL;
Damien660365e2013-12-17 18:27:24 +000085 }
86}
87
Damiend99b0522013-12-21 18:17:45 +000088machine_int_t mp_obj_hash(mp_obj_t o_in) {
89 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +000090 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +000091 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +000092 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +000093 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
94 return MP_OBJ_SMALL_INT_VALUE(o_in);
Damien George38a2da62014-01-08 17:33:12 +000095 } else if (MP_OBJ_IS_QSTR(o_in)) {
96 return MP_OBJ_QSTR_VALUE(o_in);
Damiend99b0522013-12-21 18:17:45 +000097 } else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
Damien660365e2013-12-17 18:27:24 +000098 return (machine_int_t)o_in;
Damiend99b0522013-12-21 18:17:45 +000099 } else if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
100 return mp_obj_str_get(o_in);
Damien660365e2013-12-17 18:27:24 +0000101 } else {
102 assert(0);
103 return 0;
104 }
105}
106
107// this function implements the '==' operator (and so the inverse of '!=')
108// from the python language reference:
109// "The objects need not have the same type. If both are numbers, they are converted
110// to a common type. Otherwise, the == and != operators always consider objects of
111// different types to be unequal."
112// note also that False==0 and True==1 are true expressions
Damiend99b0522013-12-21 18:17:45 +0000113bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +0000114 if (o1 == o2) {
115 return true;
Damiend99b0522013-12-21 18:17:45 +0000116 } else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
117 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
Damien660365e2013-12-17 18:27:24 +0000118 return false;
119 } else {
Damiend99b0522013-12-21 18:17:45 +0000120 if (MP_OBJ_IS_SMALL_INT(o2)) {
121 mp_obj_t temp = o1; o1 = o2; o2 = temp;
Damien660365e2013-12-17 18:27:24 +0000122 }
123 // o1 is the SMALL_INT, o2 is not
Damiend99b0522013-12-21 18:17:45 +0000124 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1);
125 if (o2 == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000126 return val == 0;
Damiend99b0522013-12-21 18:17:45 +0000127 } else if (o2 == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000128 return val == 1;
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200129 } else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
130 // If o2 is long int, dispatch to its virtual methods
131 mp_obj_base_t *o = o2;
132 if (o->type->binary_op != NULL) {
133 mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1);
134 return r == mp_const_true ? true : false;
135 }
Damien660365e2013-12-17 18:27:24 +0000136 }
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200137 return false;
Damien660365e2013-12-17 18:27:24 +0000138 }
Damien George38a2da62014-01-08 17:33:12 +0000139 } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
140 return false;
Damiend99b0522013-12-21 18:17:45 +0000141 } else if (MP_OBJ_IS_TYPE(o1, &str_type) && MP_OBJ_IS_TYPE(o2, &str_type)) {
142 return mp_obj_str_get(o1) == mp_obj_str_get(o2);
Damien660365e2013-12-17 18:27:24 +0000143 } else {
Paul Sokolovskycc57bd22014-01-12 01:55:50 +0200144 mp_obj_base_t *o = o1;
145 if (o->type->binary_op != NULL) {
146 mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o1, o2);
147 if (r != MP_OBJ_NULL) {
148 return r == mp_const_true ? true : false;
149 }
150 }
Paul Sokolovskyd6f27fe2014-01-10 04:14:10 +0200151 // TODO: Debugging helper
152 printf("Equality for '%s' and '%s' types not yet implemented\n", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2));
Damien660365e2013-12-17 18:27:24 +0000153 assert(0);
154 return false;
155 }
156}
157
Damiend99b0522013-12-21 18:17:45 +0000158bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
159 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
160 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
161 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
Damiena3dcd9e2013-12-17 21:35:38 +0000162 return i1 < i2;
163 } else {
164 assert(0);
165 return false;
166 }
167}
168
Damiend99b0522013-12-21 18:17:45 +0000169machine_int_t mp_obj_get_int(mp_obj_t arg) {
170 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000171 return 0;
Damiend99b0522013-12-21 18:17:45 +0000172 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000173 return 1;
Damiend99b0522013-12-21 18:17:45 +0000174 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
175 return MP_OBJ_SMALL_INT_VALUE(arg);
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200176 } else if (MP_OBJ_IS_TYPE(arg, &int_type)) {
177 return mp_obj_int_get_checked(arg);
Damien George71c51812014-01-04 20:21:15 +0000178#if MICROPY_ENABLE_FLOAT
179 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
180 // TODO work out if this should be floor, ceil or trunc
181 return (machine_int_t)mp_obj_float_get(arg);
182#endif
Damien660365e2013-12-17 18:27:24 +0000183 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000184 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000185 }
186}
187
188#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000189machine_float_t mp_obj_get_float(mp_obj_t arg) {
190 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000191 return 0;
Damiend99b0522013-12-21 18:17:45 +0000192 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000193 return 1;
Damiend99b0522013-12-21 18:17:45 +0000194 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
195 return MP_OBJ_SMALL_INT_VALUE(arg);
196 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
197 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000198 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000199 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000200 }
201}
202
Damiend99b0522013-12-21 18:17:45 +0000203void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
204 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000205 *real = 0;
206 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000207 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000208 *real = 1;
209 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000210 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
211 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000212 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000213 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
214 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000215 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000216 } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
217 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000218 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000219 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000220 }
221}
222#endif
223
Damiend99b0522013-12-21 18:17:45 +0000224qstr mp_obj_get_qstr(mp_obj_t arg) {
Paul Sokolovsky3754c4a2014-01-20 18:20:57 +0200225 if (MP_OBJ_IS_QSTR(arg)) {
226 return MP_OBJ_QSTR_VALUE(arg);
227 } else if (MP_OBJ_IS_TYPE(arg, &str_type)) {
Damiend99b0522013-12-21 18:17:45 +0000228 return mp_obj_str_get(arg);
Damien660365e2013-12-17 18:27:24 +0000229 } else {
230 assert(0);
231 return 0;
232 }
233}
234
Damiend99b0522013-12-21 18:17:45 +0000235mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
236 if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
237 uint seq_len;
238 mp_obj_t *seq_items;
239 if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
240 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
241 } else {
242 mp_obj_list_get(o_in, &seq_len, &seq_items);
Damien660365e2013-12-17 18:27:24 +0000243 }
Damiend99b0522013-12-21 18:17:45 +0000244 if (seq_len != n) {
Damien George6c73ca12014-01-08 18:11:23 +0000245 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "requested length %d but object has length %d", n, seq_len));
Damiend99b0522013-12-21 18:17:45 +0000246 }
247 return seq_items;
Damien660365e2013-12-17 18:27:24 +0000248 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000249 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o_in)));
Damien660365e2013-12-17 18:27:24 +0000250 }
251}
252
Damiend99b0522013-12-21 18:17:45 +0000253uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) {
254 // TODO False and True are considered 0 and 1 for indexing purposes
255 if (MP_OBJ_IS_SMALL_INT(index)) {
256 int i = MP_OBJ_SMALL_INT_VALUE(index);
257 if (i < 0) {
258 i += len;
259 }
260 if (i < 0 || i >= len) {
Damien George6c73ca12014-01-08 18:11:23 +0000261 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
Damiend99b0522013-12-21 18:17:45 +0000262 }
263 return i;
264 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000265 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "%s indices must be integers, not %s", type->name, mp_obj_get_type_str(index)));
Damien660365e2013-12-17 18:27:24 +0000266 }
Damien660365e2013-12-17 18:27:24 +0000267}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000268
Damien Georgeeae16442014-01-11 19:22:29 +0000269// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000270mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
271 mp_small_int_t len = 0;
272 if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
273 len = strlen(qstr_str(mp_obj_str_get(o_in)));
274 } else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
275 uint seq_len;
276 mp_obj_t *seq_items;
277 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
278 len = seq_len;
279 } else if (MP_OBJ_IS_TYPE(o_in, &list_type)) {
280 uint seq_len;
281 mp_obj_t *seq_items;
282 mp_obj_list_get(o_in, &seq_len, &seq_items);
283 len = seq_len;
284 } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
285 len = mp_obj_dict_len(o_in);
286 } else {
Damien Georgeeae16442014-01-11 19:22:29 +0000287 return MP_OBJ_NULL;
John R. Lenton4bee76e2014-01-10 11:25:03 +0000288 }
289 return MP_OBJ_NEW_SMALL_INT(len);
290}
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200291
292// Return input argument. Useful as .getiter for objects which are
293// their own iterators, etc.
294mp_obj_t mp_identity(mp_obj_t self) {
295 return self;
296}