blob: 2759437fd7c89b63f1edb2b31c3ca8d7d87704cf [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 {
Paul Sokolovskycc57bd22014-01-12 01:55:50 +0200120 mp_obj_base_t *o = o1;
121 if (o->type->binary_op != NULL) {
122 mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o1, o2);
123 if (r != MP_OBJ_NULL) {
124 return r == mp_const_true ? true : false;
125 }
126 }
Paul Sokolovskyd6f27fe2014-01-10 04:14:10 +0200127 // TODO: Debugging helper
128 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 +0000129 assert(0);
130 return false;
131 }
132}
133
Damiend99b0522013-12-21 18:17:45 +0000134bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
135 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
136 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
137 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
Damiena3dcd9e2013-12-17 21:35:38 +0000138 return i1 < i2;
139 } else {
140 assert(0);
141 return false;
142 }
143}
144
Damiend99b0522013-12-21 18:17:45 +0000145machine_int_t mp_obj_get_int(mp_obj_t arg) {
146 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000147 return 0;
Damiend99b0522013-12-21 18:17:45 +0000148 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000149 return 1;
Damiend99b0522013-12-21 18:17:45 +0000150 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
151 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George71c51812014-01-04 20:21:15 +0000152#if MICROPY_ENABLE_FLOAT
153 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
154 // TODO work out if this should be floor, ceil or trunc
155 return (machine_int_t)mp_obj_float_get(arg);
156#endif
Damien660365e2013-12-17 18:27:24 +0000157 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000158 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 +0000159 }
160}
161
162#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000163machine_float_t mp_obj_get_float(mp_obj_t arg) {
164 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000165 return 0;
Damiend99b0522013-12-21 18:17:45 +0000166 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000167 return 1;
Damiend99b0522013-12-21 18:17:45 +0000168 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
169 return MP_OBJ_SMALL_INT_VALUE(arg);
170 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
171 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000172 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000173 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 +0000174 }
175}
176
Damiend99b0522013-12-21 18:17:45 +0000177void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
178 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000179 *real = 0;
180 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000181 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000182 *real = 1;
183 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000184 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
185 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000186 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000187 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
188 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000189 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000190 } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
191 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000192 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000193 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 +0000194 }
195}
196#endif
197
Damiend99b0522013-12-21 18:17:45 +0000198qstr mp_obj_get_qstr(mp_obj_t arg) {
199 if (MP_OBJ_IS_TYPE(arg, &str_type)) {
200 return mp_obj_str_get(arg);
Damien660365e2013-12-17 18:27:24 +0000201 } else {
202 assert(0);
203 return 0;
204 }
205}
206
Damiend99b0522013-12-21 18:17:45 +0000207mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
208 if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
209 uint seq_len;
210 mp_obj_t *seq_items;
211 if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
212 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
213 } else {
214 mp_obj_list_get(o_in, &seq_len, &seq_items);
Damien660365e2013-12-17 18:27:24 +0000215 }
Damiend99b0522013-12-21 18:17:45 +0000216 if (seq_len != n) {
Damien George6c73ca12014-01-08 18:11:23 +0000217 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 +0000218 }
219 return seq_items;
Damien660365e2013-12-17 18:27:24 +0000220 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000221 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 +0000222 }
223}
224
Damiend99b0522013-12-21 18:17:45 +0000225uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) {
226 // TODO False and True are considered 0 and 1 for indexing purposes
227 if (MP_OBJ_IS_SMALL_INT(index)) {
228 int i = MP_OBJ_SMALL_INT_VALUE(index);
229 if (i < 0) {
230 i += len;
231 }
232 if (i < 0 || i >= len) {
Damien George6c73ca12014-01-08 18:11:23 +0000233 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
Damiend99b0522013-12-21 18:17:45 +0000234 }
235 return i;
236 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000237 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 +0000238 }
Damien660365e2013-12-17 18:27:24 +0000239}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000240
Damien Georgeeae16442014-01-11 19:22:29 +0000241// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000242mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
243 mp_small_int_t len = 0;
244 if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
245 len = strlen(qstr_str(mp_obj_str_get(o_in)));
246 } else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
247 uint seq_len;
248 mp_obj_t *seq_items;
249 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
250 len = seq_len;
251 } else if (MP_OBJ_IS_TYPE(o_in, &list_type)) {
252 uint seq_len;
253 mp_obj_t *seq_items;
254 mp_obj_list_get(o_in, &seq_len, &seq_items);
255 len = seq_len;
256 } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
257 len = mp_obj_dict_len(o_in);
258 } else {
Damien Georgeeae16442014-01-11 19:22:29 +0000259 return MP_OBJ_NULL;
John R. Lenton4bee76e2014-01-10 11:25:03 +0000260 }
261 return MP_OBJ_NEW_SMALL_INT(len);
262}