blob: 866be227aa9b0bde1e9337f5fa2166e5a0e7c2dd [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 {
119 assert(0);
120 return false;
121 }
122}
123
Damiend99b0522013-12-21 18:17:45 +0000124bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
125 if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
126 mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
127 mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
Damiena3dcd9e2013-12-17 21:35:38 +0000128 return i1 < i2;
129 } else {
130 assert(0);
131 return false;
132 }
133}
134
Damiend99b0522013-12-21 18:17:45 +0000135machine_int_t mp_obj_get_int(mp_obj_t arg) {
136 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000137 return 0;
Damiend99b0522013-12-21 18:17:45 +0000138 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000139 return 1;
Damiend99b0522013-12-21 18:17:45 +0000140 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
141 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George71c51812014-01-04 20:21:15 +0000142#if MICROPY_ENABLE_FLOAT
143 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
144 // TODO work out if this should be floor, ceil or trunc
145 return (machine_int_t)mp_obj_float_get(arg);
146#endif
Damien660365e2013-12-17 18:27:24 +0000147 } else {
Damien George71c51812014-01-04 20:21:15 +0000148 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000149 }
150}
151
152#if MICROPY_ENABLE_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000153machine_float_t mp_obj_get_float(mp_obj_t arg) {
154 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000155 return 0;
Damiend99b0522013-12-21 18:17:45 +0000156 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000157 return 1;
Damiend99b0522013-12-21 18:17:45 +0000158 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
159 return MP_OBJ_SMALL_INT_VALUE(arg);
160 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
161 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000162 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000163 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000164 }
165}
166
Damiend99b0522013-12-21 18:17:45 +0000167void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
168 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000169 *real = 0;
170 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000171 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000172 *real = 1;
173 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000174 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
175 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000176 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000177 } else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
178 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000179 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000180 } else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
181 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000182 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000183 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000184 }
185}
186#endif
187
Damiend99b0522013-12-21 18:17:45 +0000188qstr mp_obj_get_qstr(mp_obj_t arg) {
189 if (MP_OBJ_IS_TYPE(arg, &str_type)) {
190 return mp_obj_str_get(arg);
Damien660365e2013-12-17 18:27:24 +0000191 } else {
192 assert(0);
193 return 0;
194 }
195}
196
Damiend99b0522013-12-21 18:17:45 +0000197mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
198 if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
199 uint seq_len;
200 mp_obj_t *seq_items;
201 if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
202 mp_obj_tuple_get(o_in, &seq_len, &seq_items);
203 } else {
204 mp_obj_list_get(o_in, &seq_len, &seq_items);
Damien660365e2013-12-17 18:27:24 +0000205 }
Damiend99b0522013-12-21 18:17:45 +0000206 if (seq_len != n) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000207 nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_IndexError, "requested length %d but object has length %d", (void*)n, (void*)(machine_uint_t)seq_len));
Damiend99b0522013-12-21 18:17:45 +0000208 }
209 return seq_items;
Damien660365e2013-12-17 18:27:24 +0000210 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000211 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o_in)));
Damien660365e2013-12-17 18:27:24 +0000212 }
213}
214
Damiend99b0522013-12-21 18:17:45 +0000215uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) {
216 // TODO False and True are considered 0 and 1 for indexing purposes
217 if (MP_OBJ_IS_SMALL_INT(index)) {
218 int i = MP_OBJ_SMALL_INT_VALUE(index);
219 if (i < 0) {
220 i += len;
221 }
222 if (i < 0 || i >= len) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000223 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_IndexError, "%s index out of range", type->name));
Damiend99b0522013-12-21 18:17:45 +0000224 }
225 return i;
226 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000227 nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, "%s indices must be integers, not %s", type->name, mp_obj_get_type_str(index)));
Damien660365e2013-12-17 18:27:24 +0000228 }
Damien660365e2013-12-17 18:27:24 +0000229}