blob: ce555efa6dbe1ea05775514a47e53e42806c9bed [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 George55baff42014-01-21 21:40:13 +000011#include "qstr.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 George5fa93b62014-01-22 14:35:10 +000017mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in) {
Damien Georgeb97669a2014-01-08 11:47:55 +000018 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) {
Damien George5fa93b62014-01-22 14:35:10 +000029 return mp_obj_get_type(o_in)->name;
Damien660365e2013-12-17 18:27:24 +000030}
31
32void printf_wrapper(void *env, const char *fmt, ...) {
33 va_list args;
34 va_start(args, fmt);
35 vprintf(fmt, args);
36 va_end(args);
37}
38
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020039void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Damien George5fa93b62014-01-22 14:35:10 +000040 mp_obj_type_t *type = mp_obj_get_type(o_in);
41 if (type->print != NULL) {
42 type->print(print, env, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000043 } else {
Damien George5fa93b62014-01-22 14:35:10 +000044 print(env, "<%s>", type->name);
Damien660365e2013-12-17 18:27:24 +000045 }
46}
47
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020048void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
49 mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000050}
51
Damien George136b1492014-01-19 12:38:49 +000052// helper function to print an exception with traceback
53void mp_obj_print_exception(mp_obj_t exc) {
54 if (MP_OBJ_IS_TYPE(exc, &exception_type)) {
55 machine_uint_t n, *values;
56 mp_obj_exception_get_traceback(exc, &n, &values);
57 if (n > 0) {
58 printf("Traceback (most recent call last):\n");
59 for (int i = n - 3; i >= 0; i -= 3) {
Damien George62ad1892014-01-29 21:51:51 +000060#if MICROPY_ENABLE_SOURCE_LINE
Damien George136b1492014-01-19 12:38:49 +000061 printf(" File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
Damien George62ad1892014-01-29 21:51:51 +000062#else
63 printf(" File \"%s\", in %s\n", qstr_str(values[i]), qstr_str(values[i + 2]));
64#endif
Damien George136b1492014-01-19 12:38:49 +000065 }
66 }
67 }
68 mp_obj_print(exc, PRINT_REPR);
69 printf("\n");
70}
71
Damiend99b0522013-12-21 18:17:45 +000072bool mp_obj_is_callable(mp_obj_t o_in) {
Damien Georgeb051e7d2014-01-23 18:13:53 +000073 return mp_obj_get_type(o_in)->call != NULL;
Damien660365e2013-12-17 18:27:24 +000074}
75
Damiend99b0522013-12-21 18:17:45 +000076machine_int_t mp_obj_hash(mp_obj_t o_in) {
77 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +000078 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +000079 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +000080 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +000081 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
82 return MP_OBJ_SMALL_INT_VALUE(o_in);
Damien George5fa93b62014-01-22 14:35:10 +000083 } else if (MP_OBJ_IS_STR(o_in)) {
84 return mp_obj_str_get_hash(o_in);
Damiend99b0522013-12-21 18:17:45 +000085 } else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
Damien660365e2013-12-17 18:27:24 +000086 return (machine_int_t)o_in;
Damien660365e2013-12-17 18:27:24 +000087 } else {
88 assert(0);
89 return 0;
90 }
91}
92
93// this function implements the '==' operator (and so the inverse of '!=')
94// from the python language reference:
95// "The objects need not have the same type. If both are numbers, they are converted
96// to a common type. Otherwise, the == and != operators always consider objects of
97// different types to be unequal."
98// note also that False==0 and True==1 are true expressions
Damiend99b0522013-12-21 18:17:45 +000099bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +0000100 if (o1 == o2) {
101 return true;
Damiend99b0522013-12-21 18:17:45 +0000102 } else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
103 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
Damien660365e2013-12-17 18:27:24 +0000104 return false;
105 } else {
Damiend99b0522013-12-21 18:17:45 +0000106 if (MP_OBJ_IS_SMALL_INT(o2)) {
107 mp_obj_t temp = o1; o1 = o2; o2 = temp;
Damien660365e2013-12-17 18:27:24 +0000108 }
109 // o1 is the SMALL_INT, o2 is not
Damiend99b0522013-12-21 18:17:45 +0000110 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1);
111 if (o2 == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000112 return val == 0;
Damiend99b0522013-12-21 18:17:45 +0000113 } else if (o2 == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000114 return val == 1;
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200115 } else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
116 // If o2 is long int, dispatch to its virtual methods
117 mp_obj_base_t *o = o2;
118 if (o->type->binary_op != NULL) {
119 mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1);
120 return r == mp_const_true ? true : false;
121 }
Damien660365e2013-12-17 18:27:24 +0000122 }
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200123 return false;
Damien660365e2013-12-17 18:27:24 +0000124 }
Damien George5fa93b62014-01-22 14:35:10 +0000125 } else if (MP_OBJ_IS_STR(o1) && MP_OBJ_IS_STR(o2)) {
126 return mp_obj_str_equal(o1, o2);
Damien660365e2013-12-17 18:27:24 +0000127 } else {
Paul Sokolovskycc57bd22014-01-12 01:55:50 +0200128 mp_obj_base_t *o = o1;
129 if (o->type->binary_op != NULL) {
130 mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o1, o2);
131 if (r != MP_OBJ_NULL) {
132 return r == mp_const_true ? true : false;
133 }
134 }
Paul Sokolovskyd6f27fe2014-01-10 04:14:10 +0200135 // TODO: Debugging helper
136 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 +0000137 assert(0);
138 return false;
139 }
140}
141
Damiend99b0522013-12-21 18:17:45 +0000142bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
143 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
144 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
145 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
Damiena3dcd9e2013-12-17 21:35:38 +0000146 return i1 < i2;
147 } else {
148 assert(0);
149 return false;
150 }
151}
152
Damiend99b0522013-12-21 18:17:45 +0000153machine_int_t mp_obj_get_int(mp_obj_t arg) {
154 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000155 return 0;
Damiend99b0522013-12-21 18:17:45 +0000156 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000157 return 1;
Damiend99b0522013-12-21 18:17:45 +0000158 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
159 return MP_OBJ_SMALL_INT_VALUE(arg);
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200160 } else if (MP_OBJ_IS_TYPE(arg, &int_type)) {
161 return mp_obj_int_get_checked(arg);
Damien660365e2013-12-17 18:27:24 +0000162 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000163 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 +0000164 }
165}
166
167#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000168machine_float_t mp_obj_get_float(mp_obj_t arg) {
169 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000170 return 0;
Damiend99b0522013-12-21 18:17:45 +0000171 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000172 return 1;
Damiend99b0522013-12-21 18:17:45 +0000173 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
174 return MP_OBJ_SMALL_INT_VALUE(arg);
175 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
176 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000177 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000178 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 +0000179 }
180}
181
Damiend99b0522013-12-21 18:17:45 +0000182void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
183 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000184 *real = 0;
185 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000186 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000187 *real = 1;
188 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000189 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
190 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000191 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000192 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
193 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000194 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000195 } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
196 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000197 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000198 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 +0000199 }
200}
201#endif
202
Damiend99b0522013-12-21 18:17:45 +0000203mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
204 if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
205 uint seq_len;
206 mp_obj_t *seq_items;
207 if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
208 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
209 } else {
210 mp_obj_list_get(o_in, &seq_len, &seq_items);
Damien660365e2013-12-17 18:27:24 +0000211 }
Damiend99b0522013-12-21 18:17:45 +0000212 if (seq_len != n) {
Damien George6c73ca12014-01-08 18:11:23 +0000213 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 +0000214 }
215 return seq_items;
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, "object '%s' is not a tuple or list", mp_obj_get_type_str(o_in)));
Damien660365e2013-12-17 18:27:24 +0000218 }
219}
220
Damiend99b0522013-12-21 18:17:45 +0000221uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) {
222 // TODO False and True are considered 0 and 1 for indexing purposes
223 if (MP_OBJ_IS_SMALL_INT(index)) {
224 int i = MP_OBJ_SMALL_INT_VALUE(index);
225 if (i < 0) {
226 i += len;
227 }
228 if (i < 0 || i >= len) {
Damien George6c73ca12014-01-08 18:11:23 +0000229 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
Damiend99b0522013-12-21 18:17:45 +0000230 }
231 return i;
232 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000233 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 +0000234 }
Damien660365e2013-12-17 18:27:24 +0000235}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000236
Damien Georgeeae16442014-01-11 19:22:29 +0000237// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000238mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
239 mp_small_int_t len = 0;
Damien George5fa93b62014-01-22 14:35:10 +0000240 if (MP_OBJ_IS_STR(o_in)) {
241 len = mp_obj_str_get_len(o_in);
John R. Lenton4bee76e2014-01-10 11:25:03 +0000242 } else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
243 uint seq_len;
244 mp_obj_t *seq_items;
245 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
246 len = seq_len;
247 } else if (MP_OBJ_IS_TYPE(o_in, &list_type)) {
248 uint seq_len;
249 mp_obj_t *seq_items;
250 mp_obj_list_get(o_in, &seq_len, &seq_items);
251 len = seq_len;
252 } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
253 len = mp_obj_dict_len(o_in);
Paul Sokolovsky33996682014-01-21 23:30:10 +0200254 } else if (MP_OBJ_IS_TYPE(o_in, &array_type)) {
255 len = mp_obj_array_len(o_in);
John R. Lenton4bee76e2014-01-10 11:25:03 +0000256 } else {
Damien Georgeeae16442014-01-11 19:22:29 +0000257 return MP_OBJ_NULL;
John R. Lenton4bee76e2014-01-10 11:25:03 +0000258 }
259 return MP_OBJ_NEW_SMALL_INT(len);
260}
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200261
262// Return input argument. Useful as .getiter for objects which are
263// their own iterators, etc.
264mp_obj_t mp_identity(mp_obj_t self) {
265 return self;
266}