blob: f90c67b0c6c7f9c158391dcaf44f599705e0e1e7 [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));
Damien660365e2013-12-17 18:27:24 +000047 } else {
Damiend99b0522013-12-21 18:17:45 +000048 mp_obj_base_t *o = o_in;
49 if (o->type->print != NULL) {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020050 o->type->print(print, env, o_in, kind);
Damiend99b0522013-12-21 18:17:45 +000051 } else {
52 print(env, "<%s>", o->type->name);
Damien660365e2013-12-17 18:27:24 +000053 }
54 }
55}
56
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020057void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
58 mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000059}
60
Damien George136b1492014-01-19 12:38:49 +000061// helper function to print an exception with traceback
62void mp_obj_print_exception(mp_obj_t exc) {
63 if (MP_OBJ_IS_TYPE(exc, &exception_type)) {
64 machine_uint_t n, *values;
65 mp_obj_exception_get_traceback(exc, &n, &values);
66 if (n > 0) {
67 printf("Traceback (most recent call last):\n");
68 for (int i = n - 3; i >= 0; i -= 3) {
69 printf(" File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
70 }
71 }
72 }
73 mp_obj_print(exc, PRINT_REPR);
74 printf("\n");
75}
76
Damiend99b0522013-12-21 18:17:45 +000077bool mp_obj_is_callable(mp_obj_t o_in) {
78 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien660365e2013-12-17 18:27:24 +000079 return false;
80 } else {
Damiend99b0522013-12-21 18:17:45 +000081 mp_obj_base_t *o = o_in;
Damien George20006db2014-01-18 14:10:48 +000082 return o->type->call != NULL;
Damien660365e2013-12-17 18:27:24 +000083 }
84}
85
Damiend99b0522013-12-21 18:17:45 +000086machine_int_t mp_obj_hash(mp_obj_t o_in) {
87 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +000088 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +000089 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +000090 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +000091 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
92 return MP_OBJ_SMALL_INT_VALUE(o_in);
Damien George38a2da62014-01-08 17:33:12 +000093 } else if (MP_OBJ_IS_QSTR(o_in)) {
94 return MP_OBJ_QSTR_VALUE(o_in);
Damiend99b0522013-12-21 18:17:45 +000095 } else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
Damien660365e2013-12-17 18:27:24 +000096 return (machine_int_t)o_in;
Damiend99b0522013-12-21 18:17:45 +000097 } else if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
98 return mp_obj_str_get(o_in);
Damien660365e2013-12-17 18:27:24 +000099 } else {
100 assert(0);
101 return 0;
102 }
103}
104
105// this function implements the '==' operator (and so the inverse of '!=')
106// from the python language reference:
107// "The objects need not have the same type. If both are numbers, they are converted
108// to a common type. Otherwise, the == and != operators always consider objects of
109// different types to be unequal."
110// note also that False==0 and True==1 are true expressions
Damiend99b0522013-12-21 18:17:45 +0000111bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +0000112 if (o1 == o2) {
113 return true;
Damiend99b0522013-12-21 18:17:45 +0000114 } else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
115 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
Damien660365e2013-12-17 18:27:24 +0000116 return false;
117 } else {
Damiend99b0522013-12-21 18:17:45 +0000118 if (MP_OBJ_IS_SMALL_INT(o2)) {
119 mp_obj_t temp = o1; o1 = o2; o2 = temp;
Damien660365e2013-12-17 18:27:24 +0000120 }
121 // o1 is the SMALL_INT, o2 is not
Damiend99b0522013-12-21 18:17:45 +0000122 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1);
123 if (o2 == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000124 return val == 0;
Damiend99b0522013-12-21 18:17:45 +0000125 } else if (o2 == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000126 return val == 1;
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200127 } else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
128 // If o2 is long int, dispatch to its virtual methods
129 mp_obj_base_t *o = o2;
130 if (o->type->binary_op != NULL) {
131 mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1);
132 return r == mp_const_true ? true : false;
133 }
Damien660365e2013-12-17 18:27:24 +0000134 }
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200135 return false;
Damien660365e2013-12-17 18:27:24 +0000136 }
Damien George38a2da62014-01-08 17:33:12 +0000137 } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
138 return false;
Damiend99b0522013-12-21 18:17:45 +0000139 } else if (MP_OBJ_IS_TYPE(o1, &str_type) && MP_OBJ_IS_TYPE(o2, &str_type)) {
140 return mp_obj_str_get(o1) == mp_obj_str_get(o2);
Damien660365e2013-12-17 18:27:24 +0000141 } else {
Paul Sokolovskycc57bd22014-01-12 01:55:50 +0200142 mp_obj_base_t *o = o1;
143 if (o->type->binary_op != NULL) {
144 mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o1, o2);
145 if (r != MP_OBJ_NULL) {
146 return r == mp_const_true ? true : false;
147 }
148 }
Paul Sokolovskyd6f27fe2014-01-10 04:14:10 +0200149 // TODO: Debugging helper
150 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 +0000151 assert(0);
152 return false;
153 }
154}
155
Damiend99b0522013-12-21 18:17:45 +0000156bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
157 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
158 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
159 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
Damiena3dcd9e2013-12-17 21:35:38 +0000160 return i1 < i2;
161 } else {
162 assert(0);
163 return false;
164 }
165}
166
Damiend99b0522013-12-21 18:17:45 +0000167machine_int_t mp_obj_get_int(mp_obj_t arg) {
168 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000169 return 0;
Damiend99b0522013-12-21 18:17:45 +0000170 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000171 return 1;
Damiend99b0522013-12-21 18:17:45 +0000172 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
173 return MP_OBJ_SMALL_INT_VALUE(arg);
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200174 } else if (MP_OBJ_IS_TYPE(arg, &int_type)) {
175 return mp_obj_int_get_checked(arg);
Damien George71c51812014-01-04 20:21:15 +0000176#if MICROPY_ENABLE_FLOAT
177 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
178 // TODO work out if this should be floor, ceil or trunc
179 return (machine_int_t)mp_obj_float_get(arg);
180#endif
Damien660365e2013-12-17 18:27:24 +0000181 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000182 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 +0000183 }
184}
185
186#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000187machine_float_t mp_obj_get_float(mp_obj_t arg) {
188 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000189 return 0;
Damiend99b0522013-12-21 18:17:45 +0000190 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000191 return 1;
Damiend99b0522013-12-21 18:17:45 +0000192 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
193 return MP_OBJ_SMALL_INT_VALUE(arg);
194 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
195 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000196 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000197 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 +0000198 }
199}
200
Damiend99b0522013-12-21 18:17:45 +0000201void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
202 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000203 *real = 0;
204 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000205 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000206 *real = 1;
207 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000208 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
209 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000210 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000211 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
212 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000213 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000214 } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
215 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000216 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000217 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 +0000218 }
219}
220#endif
221
Damiend99b0522013-12-21 18:17:45 +0000222qstr mp_obj_get_qstr(mp_obj_t arg) {
223 if (MP_OBJ_IS_TYPE(arg, &str_type)) {
224 return mp_obj_str_get(arg);
Damien660365e2013-12-17 18:27:24 +0000225 } else {
226 assert(0);
227 return 0;
228 }
229}
230
Damiend99b0522013-12-21 18:17:45 +0000231mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
232 if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
233 uint seq_len;
234 mp_obj_t *seq_items;
235 if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
236 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
237 } else {
238 mp_obj_list_get(o_in, &seq_len, &seq_items);
Damien660365e2013-12-17 18:27:24 +0000239 }
Damiend99b0522013-12-21 18:17:45 +0000240 if (seq_len != n) {
Damien George6c73ca12014-01-08 18:11:23 +0000241 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 +0000242 }
243 return seq_items;
Damien660365e2013-12-17 18:27:24 +0000244 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000245 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 +0000246 }
247}
248
Damiend99b0522013-12-21 18:17:45 +0000249uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) {
250 // TODO False and True are considered 0 and 1 for indexing purposes
251 if (MP_OBJ_IS_SMALL_INT(index)) {
252 int i = MP_OBJ_SMALL_INT_VALUE(index);
253 if (i < 0) {
254 i += len;
255 }
256 if (i < 0 || i >= len) {
Damien George6c73ca12014-01-08 18:11:23 +0000257 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
Damiend99b0522013-12-21 18:17:45 +0000258 }
259 return i;
260 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000261 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 +0000262 }
Damien660365e2013-12-17 18:27:24 +0000263}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000264
Damien Georgeeae16442014-01-11 19:22:29 +0000265// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000266mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
267 mp_small_int_t len = 0;
268 if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
269 len = strlen(qstr_str(mp_obj_str_get(o_in)));
270 } else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
271 uint seq_len;
272 mp_obj_t *seq_items;
273 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
274 len = seq_len;
275 } else if (MP_OBJ_IS_TYPE(o_in, &list_type)) {
276 uint seq_len;
277 mp_obj_t *seq_items;
278 mp_obj_list_get(o_in, &seq_len, &seq_items);
279 len = seq_len;
280 } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
281 len = mp_obj_dict_len(o_in);
282 } else {
Damien Georgeeae16442014-01-11 19:22:29 +0000283 return MP_OBJ_NULL;
John R. Lenton4bee76e2014-01-10 11:25:03 +0000284 }
285 return MP_OBJ_NEW_SMALL_INT(len);
286}