blob: 0558c38a7e2c5132c5fa227ab80cca01417e3a9a [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
Damiend99b0522013-12-21 18:17:45 +000044void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
45 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) {
50 o->type->print(print, env, o_in);
51 } else {
52 print(env, "<%s>", o->type->name);
Damien660365e2013-12-17 18:27:24 +000053 }
54 }
55}
56
Damiend99b0522013-12-21 18:17:45 +000057void mp_obj_print(mp_obj_t o_in) {
58 mp_obj_print_helper(printf_wrapper, NULL, o_in);
Damien660365e2013-12-17 18:27:24 +000059}
60
Damiend99b0522013-12-21 18:17:45 +000061bool mp_obj_is_callable(mp_obj_t o_in) {
62 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien660365e2013-12-17 18:27:24 +000063 return false;
64 } else {
Damiend99b0522013-12-21 18:17:45 +000065 mp_obj_base_t *o = o_in;
66 return o->type->call_n != NULL;
Damien660365e2013-12-17 18:27:24 +000067 }
68}
69
Damiend99b0522013-12-21 18:17:45 +000070machine_int_t mp_obj_hash(mp_obj_t o_in) {
71 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +000072 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +000073 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +000074 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +000075 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
76 return MP_OBJ_SMALL_INT_VALUE(o_in);
Damien George38a2da62014-01-08 17:33:12 +000077 } else if (MP_OBJ_IS_QSTR(o_in)) {
78 return MP_OBJ_QSTR_VALUE(o_in);
Damiend99b0522013-12-21 18:17:45 +000079 } else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
Damien660365e2013-12-17 18:27:24 +000080 return (machine_int_t)o_in;
Damiend99b0522013-12-21 18:17:45 +000081 } else if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
82 return mp_obj_str_get(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;
111 } else {
112 return false;
113 }
114 }
Damien George38a2da62014-01-08 17:33:12 +0000115 } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
116 return false;
Damiend99b0522013-12-21 18:17:45 +0000117 } else if (MP_OBJ_IS_TYPE(o1, &str_type) && MP_OBJ_IS_TYPE(o2, &str_type)) {
118 return mp_obj_str_get(o1) == mp_obj_str_get(o2);
Damien660365e2013-12-17 18:27:24 +0000119 } else {
120 assert(0);
121 return false;
122 }
123}
124
Damiend99b0522013-12-21 18:17:45 +0000125bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
126 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
127 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
128 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
Damiena3dcd9e2013-12-17 21:35:38 +0000129 return i1 < i2;
130 } else {
131 assert(0);
132 return false;
133 }
134}
135
Damiend99b0522013-12-21 18:17:45 +0000136machine_int_t mp_obj_get_int(mp_obj_t arg) {
137 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000138 return 0;
Damiend99b0522013-12-21 18:17:45 +0000139 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000140 return 1;
Damiend99b0522013-12-21 18:17:45 +0000141 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
142 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George71c51812014-01-04 20:21:15 +0000143#if MICROPY_ENABLE_FLOAT
144 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
145 // TODO work out if this should be floor, ceil or trunc
146 return (machine_int_t)mp_obj_float_get(arg);
147#endif
Damien660365e2013-12-17 18:27:24 +0000148 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000149 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 +0000150 }
151}
152
153#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000154machine_float_t mp_obj_get_float(mp_obj_t arg) {
155 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000156 return 0;
Damiend99b0522013-12-21 18:17:45 +0000157 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000158 return 1;
Damiend99b0522013-12-21 18:17:45 +0000159 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
160 return MP_OBJ_SMALL_INT_VALUE(arg);
161 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
162 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000163 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000164 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 +0000165 }
166}
167
Damiend99b0522013-12-21 18:17:45 +0000168void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
169 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000170 *real = 0;
171 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000172 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000173 *real = 1;
174 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000175 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
176 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000177 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000178 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
179 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000180 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000181 } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
182 mp_obj_complex_get(arg, real, imag);
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 complex", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000185 }
186}
187#endif
188
Damiend99b0522013-12-21 18:17:45 +0000189qstr mp_obj_get_qstr(mp_obj_t arg) {
190 if (MP_OBJ_IS_TYPE(arg, &str_type)) {
191 return mp_obj_str_get(arg);
Damien660365e2013-12-17 18:27:24 +0000192 } else {
193 assert(0);
194 return 0;
195 }
196}
197
Damiend99b0522013-12-21 18:17:45 +0000198mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
199 if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
200 uint seq_len;
201 mp_obj_t *seq_items;
202 if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
203 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
204 } else {
205 mp_obj_list_get(o_in, &seq_len, &seq_items);
Damien660365e2013-12-17 18:27:24 +0000206 }
Damiend99b0522013-12-21 18:17:45 +0000207 if (seq_len != n) {
Damien George6c73ca12014-01-08 18:11:23 +0000208 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 +0000209 }
210 return seq_items;
Damien660365e2013-12-17 18:27:24 +0000211 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000212 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 +0000213 }
214}
215
Damiend99b0522013-12-21 18:17:45 +0000216uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) {
217 // TODO False and True are considered 0 and 1 for indexing purposes
218 if (MP_OBJ_IS_SMALL_INT(index)) {
219 int i = MP_OBJ_SMALL_INT_VALUE(index);
220 if (i < 0) {
221 i += len;
222 }
223 if (i < 0 || i >= len) {
Damien George6c73ca12014-01-08 18:11:23 +0000224 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
Damiend99b0522013-12-21 18:17:45 +0000225 }
226 return i;
227 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000228 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 +0000229 }
Damien660365e2013-12-17 18:27:24 +0000230}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000231
232// may return NULL
233mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
234 mp_small_int_t len = 0;
235 if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
236 len = strlen(qstr_str(mp_obj_str_get(o_in)));
237 } else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
238 uint seq_len;
239 mp_obj_t *seq_items;
240 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
241 len = seq_len;
242 } else if (MP_OBJ_IS_TYPE(o_in, &list_type)) {
243 uint seq_len;
244 mp_obj_t *seq_items;
245 mp_obj_list_get(o_in, &seq_len, &seq_items);
246 len = seq_len;
247 } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
248 len = mp_obj_dict_len(o_in);
249 } else {
250 return NULL;
251 }
252 return MP_OBJ_NEW_SMALL_INT(len);
253}