blob: 9ca3d5d16b44daa8f149fe75560a73291791ca32 [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
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;
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 George38a2da62014-01-08 17:33:12 +0000121 } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
122 return false;
Damiend99b0522013-12-21 18:17:45 +0000123 } else if (MP_OBJ_IS_TYPE(o1, &str_type) && MP_OBJ_IS_TYPE(o2, &str_type)) {
124 return mp_obj_str_get(o1) == mp_obj_str_get(o2);
Damien660365e2013-12-17 18:27:24 +0000125 } else {
Paul Sokolovskycc57bd22014-01-12 01:55:50 +0200126 mp_obj_base_t *o = o1;
127 if (o->type->binary_op != NULL) {
128 mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o1, o2);
129 if (r != MP_OBJ_NULL) {
130 return r == mp_const_true ? true : false;
131 }
132 }
Paul Sokolovskyd6f27fe2014-01-10 04:14:10 +0200133 // TODO: Debugging helper
134 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 +0000135 assert(0);
136 return false;
137 }
138}
139
Damiend99b0522013-12-21 18:17:45 +0000140bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
141 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
142 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
143 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
Damiena3dcd9e2013-12-17 21:35:38 +0000144 return i1 < i2;
145 } else {
146 assert(0);
147 return false;
148 }
149}
150
Damiend99b0522013-12-21 18:17:45 +0000151machine_int_t mp_obj_get_int(mp_obj_t arg) {
152 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000153 return 0;
Damiend99b0522013-12-21 18:17:45 +0000154 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000155 return 1;
Damiend99b0522013-12-21 18:17:45 +0000156 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
157 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George71c51812014-01-04 20:21:15 +0000158#if MICROPY_ENABLE_FLOAT
159 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
160 // TODO work out if this should be floor, ceil or trunc
161 return (machine_int_t)mp_obj_float_get(arg);
162#endif
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 int", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000165 }
166}
167
168#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000169machine_float_t mp_obj_get_float(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);
176 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
177 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000178 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000179 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 +0000180 }
181}
182
Damiend99b0522013-12-21 18:17:45 +0000183void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
184 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000185 *real = 0;
186 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000187 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000188 *real = 1;
189 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000190 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
191 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000192 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000193 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
194 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000195 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000196 } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
197 mp_obj_complex_get(arg, real, imag);
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 complex", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000200 }
201}
202#endif
203
Damiend99b0522013-12-21 18:17:45 +0000204qstr mp_obj_get_qstr(mp_obj_t arg) {
205 if (MP_OBJ_IS_TYPE(arg, &str_type)) {
206 return mp_obj_str_get(arg);
Damien660365e2013-12-17 18:27:24 +0000207 } else {
208 assert(0);
209 return 0;
210 }
211}
212
Damiend99b0522013-12-21 18:17:45 +0000213mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
214 if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
215 uint seq_len;
216 mp_obj_t *seq_items;
217 if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
218 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
219 } else {
220 mp_obj_list_get(o_in, &seq_len, &seq_items);
Damien660365e2013-12-17 18:27:24 +0000221 }
Damiend99b0522013-12-21 18:17:45 +0000222 if (seq_len != n) {
Damien George6c73ca12014-01-08 18:11:23 +0000223 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 +0000224 }
225 return seq_items;
Damien660365e2013-12-17 18:27:24 +0000226 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000227 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 +0000228 }
229}
230
Damiend99b0522013-12-21 18:17:45 +0000231uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) {
232 // TODO False and True are considered 0 and 1 for indexing purposes
233 if (MP_OBJ_IS_SMALL_INT(index)) {
234 int i = MP_OBJ_SMALL_INT_VALUE(index);
235 if (i < 0) {
236 i += len;
237 }
238 if (i < 0 || i >= len) {
Damien George6c73ca12014-01-08 18:11:23 +0000239 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
Damiend99b0522013-12-21 18:17:45 +0000240 }
241 return i;
242 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000243 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 +0000244 }
Damien660365e2013-12-17 18:27:24 +0000245}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000246
Damien Georgeeae16442014-01-11 19:22:29 +0000247// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000248mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
249 mp_small_int_t len = 0;
250 if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
251 len = strlen(qstr_str(mp_obj_str_get(o_in)));
252 } else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
253 uint seq_len;
254 mp_obj_t *seq_items;
255 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
256 len = seq_len;
257 } else if (MP_OBJ_IS_TYPE(o_in, &list_type)) {
258 uint seq_len;
259 mp_obj_t *seq_items;
260 mp_obj_list_get(o_in, &seq_len, &seq_items);
261 len = seq_len;
262 } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
263 len = mp_obj_dict_len(o_in);
264 } else {
Damien Georgeeae16442014-01-11 19:22:29 +0000265 return MP_OBJ_NULL;
John R. Lenton4bee76e2014-01-10 11:25:03 +0000266 }
267 return MP_OBJ_NEW_SMALL_INT(len);
268}