blob: 76a4d8f9b2e56525c25cc10d80d350f078860cc1 [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) {
60 printf(" File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
61 }
62 }
63 }
64 mp_obj_print(exc, PRINT_REPR);
65 printf("\n");
66}
67
Damiend99b0522013-12-21 18:17:45 +000068bool mp_obj_is_callable(mp_obj_t o_in) {
Damien Georgeb051e7d2014-01-23 18:13:53 +000069 return mp_obj_get_type(o_in)->call != NULL;
Damien660365e2013-12-17 18:27:24 +000070}
71
Damiend99b0522013-12-21 18:17:45 +000072machine_int_t mp_obj_hash(mp_obj_t o_in) {
73 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +000074 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +000075 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +000076 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +000077 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
78 return MP_OBJ_SMALL_INT_VALUE(o_in);
Damien George5fa93b62014-01-22 14:35:10 +000079 } else if (MP_OBJ_IS_STR(o_in)) {
80 return mp_obj_str_get_hash(o_in);
Damiend99b0522013-12-21 18:17:45 +000081 } else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
Damien660365e2013-12-17 18:27:24 +000082 return (machine_int_t)o_in;
Damien660365e2013-12-17 18:27:24 +000083 } else {
84 assert(0);
85 return 0;
86 }
87}
88
89// this function implements the '==' operator (and so the inverse of '!=')
90// from the python language reference:
91// "The objects need not have the same type. If both are numbers, they are converted
92// to a common type. Otherwise, the == and != operators always consider objects of
93// different types to be unequal."
94// note also that False==0 and True==1 are true expressions
Damiend99b0522013-12-21 18:17:45 +000095bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +000096 if (o1 == o2) {
97 return true;
Damiend99b0522013-12-21 18:17:45 +000098 } else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
99 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
Damien660365e2013-12-17 18:27:24 +0000100 return false;
101 } else {
Damiend99b0522013-12-21 18:17:45 +0000102 if (MP_OBJ_IS_SMALL_INT(o2)) {
103 mp_obj_t temp = o1; o1 = o2; o2 = temp;
Damien660365e2013-12-17 18:27:24 +0000104 }
105 // o1 is the SMALL_INT, o2 is not
Damiend99b0522013-12-21 18:17:45 +0000106 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1);
107 if (o2 == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000108 return val == 0;
Damiend99b0522013-12-21 18:17:45 +0000109 } else if (o2 == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000110 return val == 1;
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200111 } else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
112 // If o2 is long int, dispatch to its virtual methods
113 mp_obj_base_t *o = o2;
114 if (o->type->binary_op != NULL) {
115 mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1);
116 return r == mp_const_true ? true : false;
117 }
Damien660365e2013-12-17 18:27:24 +0000118 }
Paul Sokolovskyca318bb2014-01-13 16:29:14 +0200119 return false;
Damien660365e2013-12-17 18:27:24 +0000120 }
Damien George5fa93b62014-01-22 14:35:10 +0000121 } else if (MP_OBJ_IS_STR(o1) && MP_OBJ_IS_STR(o2)) {
122 return mp_obj_str_equal(o1, o2);
Damien660365e2013-12-17 18:27:24 +0000123 } else {
Paul Sokolovskycc57bd22014-01-12 01:55:50 +0200124 mp_obj_base_t *o = o1;
125 if (o->type->binary_op != NULL) {
126 mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o1, o2);
127 if (r != MP_OBJ_NULL) {
128 return r == mp_const_true ? true : false;
129 }
130 }
Paul Sokolovskyd6f27fe2014-01-10 04:14:10 +0200131 // TODO: Debugging helper
132 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 +0000133 assert(0);
134 return false;
135 }
136}
137
Damiend99b0522013-12-21 18:17:45 +0000138bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
139 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
140 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
141 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
Damiena3dcd9e2013-12-17 21:35:38 +0000142 return i1 < i2;
143 } else {
144 assert(0);
145 return false;
146 }
147}
148
Damiend99b0522013-12-21 18:17:45 +0000149machine_int_t mp_obj_get_int(mp_obj_t arg) {
150 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000151 return 0;
Damiend99b0522013-12-21 18:17:45 +0000152 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000153 return 1;
Damiend99b0522013-12-21 18:17:45 +0000154 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
155 return MP_OBJ_SMALL_INT_VALUE(arg);
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200156 } else if (MP_OBJ_IS_TYPE(arg, &int_type)) {
157 return mp_obj_int_get_checked(arg);
Damien660365e2013-12-17 18:27:24 +0000158 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000159 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 +0000160 }
161}
162
163#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000164machine_float_t mp_obj_get_float(mp_obj_t arg) {
165 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000166 return 0;
Damiend99b0522013-12-21 18:17:45 +0000167 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000168 return 1;
Damiend99b0522013-12-21 18:17:45 +0000169 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
170 return MP_OBJ_SMALL_INT_VALUE(arg);
171 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
172 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000173 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000174 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 +0000175 }
176}
177
Damiend99b0522013-12-21 18:17:45 +0000178void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
179 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000180 *real = 0;
181 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000182 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000183 *real = 1;
184 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000185 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
186 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000187 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000188 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
189 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000190 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000191 } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
192 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000193 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000194 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 +0000195 }
196}
197#endif
198
Damiend99b0522013-12-21 18:17:45 +0000199mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
200 if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
201 uint seq_len;
202 mp_obj_t *seq_items;
203 if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
204 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
205 } else {
206 mp_obj_list_get(o_in, &seq_len, &seq_items);
Damien660365e2013-12-17 18:27:24 +0000207 }
Damiend99b0522013-12-21 18:17:45 +0000208 if (seq_len != n) {
Damien George6c73ca12014-01-08 18:11:23 +0000209 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 +0000210 }
211 return seq_items;
Damien660365e2013-12-17 18:27:24 +0000212 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000213 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 +0000214 }
215}
216
Damiend99b0522013-12-21 18:17:45 +0000217uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) {
218 // TODO False and True are considered 0 and 1 for indexing purposes
219 if (MP_OBJ_IS_SMALL_INT(index)) {
220 int i = MP_OBJ_SMALL_INT_VALUE(index);
221 if (i < 0) {
222 i += len;
223 }
224 if (i < 0 || i >= len) {
Damien George6c73ca12014-01-08 18:11:23 +0000225 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
Damiend99b0522013-12-21 18:17:45 +0000226 }
227 return i;
228 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000229 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 +0000230 }
Damien660365e2013-12-17 18:27:24 +0000231}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000232
Damien Georgeeae16442014-01-11 19:22:29 +0000233// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000234mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
235 mp_small_int_t len = 0;
Damien George5fa93b62014-01-22 14:35:10 +0000236 if (MP_OBJ_IS_STR(o_in)) {
237 len = mp_obj_str_get_len(o_in);
John R. Lenton4bee76e2014-01-10 11:25:03 +0000238 } else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
239 uint seq_len;
240 mp_obj_t *seq_items;
241 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
242 len = seq_len;
243 } else if (MP_OBJ_IS_TYPE(o_in, &list_type)) {
244 uint seq_len;
245 mp_obj_t *seq_items;
246 mp_obj_list_get(o_in, &seq_len, &seq_items);
247 len = seq_len;
248 } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
249 len = mp_obj_dict_len(o_in);
Paul Sokolovsky33996682014-01-21 23:30:10 +0200250 } else if (MP_OBJ_IS_TYPE(o_in, &array_type)) {
251 len = mp_obj_array_len(o_in);
John R. Lenton4bee76e2014-01-10 11:25:03 +0000252 } else {
Damien Georgeeae16442014-01-11 19:22:29 +0000253 return MP_OBJ_NULL;
John R. Lenton4bee76e2014-01-10 11:25:03 +0000254 }
255 return MP_OBJ_NEW_SMALL_INT(len);
256}
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200257
258// Return input argument. Useful as .getiter for objects which are
259// their own iterators, etc.
260mp_obj_t mp_identity(mp_obj_t self) {
261 return self;
262}