blob: 7905f05486efa0cd40b7e5241b88f5c996d924cf [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>
5#include <assert.h>
6
7#include "nlr.h"
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000010#include "mpqstr.h"
Damien660365e2013-12-17 18:27:24 +000011#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000012#include "runtime0.h"
13#include "runtime.h"
14#include "map.h"
Damien660365e2013-12-17 18:27:24 +000015
Damien Georgeb97669a2014-01-08 11:47:55 +000016mp_obj_t mp_obj_get_type(mp_obj_t o_in) {
17 if (MP_OBJ_IS_SMALL_INT(o_in)) {
18 return (mp_obj_t)&int_type;
Damien George38a2da62014-01-08 17:33:12 +000019 } else if (MP_OBJ_IS_QSTR(o_in)) {
20 return (mp_obj_t)&str_type;
Damien Georgeb97669a2014-01-08 11:47:55 +000021 } else {
22 mp_obj_base_t *o = o_in;
23 return (mp_obj_t)o->type;
24 }
25}
26
Damiend99b0522013-12-21 18:17:45 +000027const char *mp_obj_get_type_str(mp_obj_t o_in) {
28 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien660365e2013-12-17 18:27:24 +000029 return "int";
30 } else {
Damiend99b0522013-12-21 18:17:45 +000031 mp_obj_base_t *o = o_in;
32 return o->type->name;
Damien660365e2013-12-17 18:27:24 +000033 }
34}
35
36void printf_wrapper(void *env, const char *fmt, ...) {
37 va_list args;
38 va_start(args, fmt);
39 vprintf(fmt, args);
40 va_end(args);
41}
42
Damiend99b0522013-12-21 18:17:45 +000043void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
44 if (MP_OBJ_IS_SMALL_INT(o_in)) {
45 print(env, "%d", (int)MP_OBJ_SMALL_INT_VALUE(o_in));
Damien660365e2013-12-17 18:27:24 +000046 } else {
Damiend99b0522013-12-21 18:17:45 +000047 mp_obj_base_t *o = o_in;
48 if (o->type->print != NULL) {
49 o->type->print(print, env, o_in);
50 } else {
51 print(env, "<%s>", o->type->name);
Damien660365e2013-12-17 18:27:24 +000052 }
53 }
54}
55
Damiend99b0522013-12-21 18:17:45 +000056void mp_obj_print(mp_obj_t o_in) {
57 mp_obj_print_helper(printf_wrapper, NULL, o_in);
Damien660365e2013-12-17 18:27:24 +000058}
59
Damiend99b0522013-12-21 18:17:45 +000060bool mp_obj_is_callable(mp_obj_t o_in) {
61 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien660365e2013-12-17 18:27:24 +000062 return false;
63 } else {
Damiend99b0522013-12-21 18:17:45 +000064 mp_obj_base_t *o = o_in;
65 return o->type->call_n != NULL;
Damien660365e2013-12-17 18:27:24 +000066 }
67}
68
Damiend99b0522013-12-21 18:17:45 +000069machine_int_t mp_obj_hash(mp_obj_t o_in) {
70 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +000071 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +000072 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +000073 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +000074 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
75 return MP_OBJ_SMALL_INT_VALUE(o_in);
Damien George38a2da62014-01-08 17:33:12 +000076 } else if (MP_OBJ_IS_QSTR(o_in)) {
77 return MP_OBJ_QSTR_VALUE(o_in);
Damiend99b0522013-12-21 18:17:45 +000078 } else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
Damien660365e2013-12-17 18:27:24 +000079 return (machine_int_t)o_in;
Damiend99b0522013-12-21 18:17:45 +000080 } else if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
81 return mp_obj_str_get(o_in);
Damien660365e2013-12-17 18:27:24 +000082 } else {
83 assert(0);
84 return 0;
85 }
86}
87
88// this function implements the '==' operator (and so the inverse of '!=')
89// from the python language reference:
90// "The objects need not have the same type. If both are numbers, they are converted
91// to a common type. Otherwise, the == and != operators always consider objects of
92// different types to be unequal."
93// note also that False==0 and True==1 are true expressions
Damiend99b0522013-12-21 18:17:45 +000094bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +000095 if (o1 == o2) {
96 return true;
Damiend99b0522013-12-21 18:17:45 +000097 } else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
98 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
Damien660365e2013-12-17 18:27:24 +000099 return false;
100 } else {
Damiend99b0522013-12-21 18:17:45 +0000101 if (MP_OBJ_IS_SMALL_INT(o2)) {
102 mp_obj_t temp = o1; o1 = o2; o2 = temp;
Damien660365e2013-12-17 18:27:24 +0000103 }
104 // o1 is the SMALL_INT, o2 is not
Damiend99b0522013-12-21 18:17:45 +0000105 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1);
106 if (o2 == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000107 return val == 0;
Damiend99b0522013-12-21 18:17:45 +0000108 } else if (o2 == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000109 return val == 1;
110 } else {
111 return false;
112 }
113 }
Damien George38a2da62014-01-08 17:33:12 +0000114 } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
115 return false;
Damiend99b0522013-12-21 18:17:45 +0000116 } else if (MP_OBJ_IS_TYPE(o1, &str_type) && MP_OBJ_IS_TYPE(o2, &str_type)) {
117 return mp_obj_str_get(o1) == mp_obj_str_get(o2);
Damien660365e2013-12-17 18:27:24 +0000118 } else {
Paul Sokolovskyd6f27fe2014-01-10 04:14:10 +0200119 // TODO: Debugging helper
120 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 +0000121 assert(0);
122 return false;
123 }
124}
125
Damiend99b0522013-12-21 18:17:45 +0000126bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
127 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
128 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
129 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
Damiena3dcd9e2013-12-17 21:35:38 +0000130 return i1 < i2;
131 } else {
132 assert(0);
133 return false;
134 }
135}
136
Damiend99b0522013-12-21 18:17:45 +0000137machine_int_t mp_obj_get_int(mp_obj_t arg) {
138 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000139 return 0;
Damiend99b0522013-12-21 18:17:45 +0000140 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000141 return 1;
Damiend99b0522013-12-21 18:17:45 +0000142 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
143 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George71c51812014-01-04 20:21:15 +0000144#if MICROPY_ENABLE_FLOAT
145 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
146 // TODO work out if this should be floor, ceil or trunc
147 return (machine_int_t)mp_obj_float_get(arg);
148#endif
Damien660365e2013-12-17 18:27:24 +0000149 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000150 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 +0000151 }
152}
153
154#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000155machine_float_t mp_obj_get_float(mp_obj_t arg) {
156 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000157 return 0;
Damiend99b0522013-12-21 18:17:45 +0000158 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000159 return 1;
Damiend99b0522013-12-21 18:17:45 +0000160 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
161 return MP_OBJ_SMALL_INT_VALUE(arg);
162 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
163 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000164 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000165 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 +0000166 }
167}
168
Damiend99b0522013-12-21 18:17:45 +0000169void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
170 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000171 *real = 0;
172 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000173 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000174 *real = 1;
175 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000176 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
177 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000178 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000179 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
180 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000181 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000182 } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
183 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000184 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000185 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 +0000186 }
187}
188#endif
189
Damiend99b0522013-12-21 18:17:45 +0000190qstr mp_obj_get_qstr(mp_obj_t arg) {
191 if (MP_OBJ_IS_TYPE(arg, &str_type)) {
192 return mp_obj_str_get(arg);
Damien660365e2013-12-17 18:27:24 +0000193 } else {
194 assert(0);
195 return 0;
196 }
197}
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}