2013-12-17 13:27:24 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2014-01-10 06:25:03 -05:00
|
|
|
#include <string.h>
|
2013-12-17 13:27:24 -05:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "mpconfig.h"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "obj.h"
|
|
|
|
#include "runtime0.h"
|
2013-12-17 13:27:24 -05:00
|
|
|
#include "runtime.h"
|
|
|
|
#include "map.h"
|
|
|
|
|
2014-01-22 09:35:10 -05:00
|
|
|
mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in) {
|
2014-01-08 06:47:55 -05:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(o_in)) {
|
|
|
|
return (mp_obj_t)&int_type;
|
2014-01-08 12:33:12 -05:00
|
|
|
} else if (MP_OBJ_IS_QSTR(o_in)) {
|
|
|
|
return (mp_obj_t)&str_type;
|
2014-01-08 06:47:55 -05:00
|
|
|
} else {
|
|
|
|
mp_obj_base_t *o = o_in;
|
|
|
|
return (mp_obj_t)o->type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
const char *mp_obj_get_type_str(mp_obj_t o_in) {
|
2014-01-22 09:35:10 -05:00
|
|
|
return mp_obj_get_type(o_in)->name;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void printf_wrapper(void *env, const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vprintf(fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2014-01-13 12:19:16 -05:00
|
|
|
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
2014-01-22 09:35:10 -05:00
|
|
|
mp_obj_type_t *type = mp_obj_get_type(o_in);
|
|
|
|
if (type->print != NULL) {
|
|
|
|
type->print(print, env, o_in, kind);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-01-22 09:35:10 -05:00
|
|
|
print(env, "<%s>", type->name);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-13 12:19:16 -05:00
|
|
|
void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
|
|
|
|
mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2014-01-19 07:38:49 -05:00
|
|
|
// helper function to print an exception with traceback
|
|
|
|
void mp_obj_print_exception(mp_obj_t exc) {
|
|
|
|
if (MP_OBJ_IS_TYPE(exc, &exception_type)) {
|
|
|
|
machine_uint_t n, *values;
|
|
|
|
mp_obj_exception_get_traceback(exc, &n, &values);
|
|
|
|
if (n > 0) {
|
|
|
|
printf("Traceback (most recent call last):\n");
|
|
|
|
for (int i = n - 3; i >= 0; i -= 3) {
|
2014-01-29 16:51:51 -05:00
|
|
|
#if MICROPY_ENABLE_SOURCE_LINE
|
2014-01-19 07:38:49 -05:00
|
|
|
printf(" File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
|
2014-01-29 16:51:51 -05:00
|
|
|
#else
|
|
|
|
printf(" File \"%s\", in %s\n", qstr_str(values[i]), qstr_str(values[i + 2]));
|
|
|
|
#endif
|
2014-01-19 07:38:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_obj_print(exc, PRINT_REPR);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
bool mp_obj_is_callable(mp_obj_t o_in) {
|
2014-01-23 13:13:53 -05:00
|
|
|
return mp_obj_get_type(o_in)->call != NULL;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
machine_int_t mp_obj_hash(mp_obj_t o_in) {
|
|
|
|
if (o_in == mp_const_false) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 0; // needs to hash to same as the integer 0, since False==0
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (o_in == mp_const_true) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 1; // needs to hash to same as the integer 1, since True==1
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(o_in)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(o_in);
|
2014-01-22 09:35:10 -05:00
|
|
|
} else if (MP_OBJ_IS_STR(o_in)) {
|
|
|
|
return mp_obj_str_get_hash(o_in);
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return (machine_int_t)o_in;
|
|
|
|
} else {
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this function implements the '==' operator (and so the inverse of '!=')
|
|
|
|
// from the python language reference:
|
|
|
|
// "The objects need not have the same type. If both are numbers, they are converted
|
|
|
|
// to a common type. Otherwise, the == and != operators always consider objects of
|
|
|
|
// different types to be unequal."
|
|
|
|
// note also that False==0 and True==1 are true expressions
|
2013-12-21 13:17:45 -05:00
|
|
|
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
|
2013-12-17 13:27:24 -05:00
|
|
|
if (o1 == o2) {
|
|
|
|
return true;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return false;
|
|
|
|
} else {
|
2013-12-21 13:17:45 -05:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(o2)) {
|
|
|
|
mp_obj_t temp = o1; o1 = o2; o2 = temp;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
// o1 is the SMALL_INT, o2 is not
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1);
|
|
|
|
if (o2 == mp_const_false) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return val == 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (o2 == mp_const_true) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return val == 1;
|
2014-01-13 09:29:14 -05:00
|
|
|
} else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
|
|
|
|
// If o2 is long int, dispatch to its virtual methods
|
|
|
|
mp_obj_base_t *o = o2;
|
|
|
|
if (o->type->binary_op != NULL) {
|
|
|
|
mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1);
|
|
|
|
return r == mp_const_true ? true : false;
|
|
|
|
}
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-01-13 09:29:14 -05:00
|
|
|
return false;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-01-22 09:35:10 -05:00
|
|
|
} else if (MP_OBJ_IS_STR(o1) && MP_OBJ_IS_STR(o2)) {
|
|
|
|
return mp_obj_str_equal(o1, o2);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-01-11 18:55:50 -05:00
|
|
|
mp_obj_base_t *o = o1;
|
|
|
|
if (o->type->binary_op != NULL) {
|
|
|
|
mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o1, o2);
|
|
|
|
if (r != MP_OBJ_NULL) {
|
|
|
|
return r == mp_const_true ? true : false;
|
|
|
|
}
|
|
|
|
}
|
2014-01-09 21:14:10 -05:00
|
|
|
// TODO: Debugging helper
|
|
|
|
printf("Equality for '%s' and '%s' types not yet implemented\n", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2));
|
2013-12-17 13:27:24 -05:00
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) {
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
|
|
|
|
mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1);
|
|
|
|
mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2);
|
2013-12-17 16:35:38 -05:00
|
|
|
return i1 < i2;
|
|
|
|
} else {
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
machine_int_t mp_obj_get_int(mp_obj_t arg) {
|
|
|
|
if (arg == mp_const_false) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (arg == mp_const_true) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 1;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(arg);
|
2014-01-18 09:07:16 -05:00
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &int_type)) {
|
|
|
|
return mp_obj_int_get_checked(arg);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-01-08 13:11:23 -05:00
|
|
|
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MICROPY_ENABLE_FLOAT
|
2013-12-21 13:17:45 -05:00
|
|
|
machine_float_t mp_obj_get_float(mp_obj_t arg) {
|
|
|
|
if (arg == mp_const_false) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (arg == mp_const_true) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 1;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(arg);
|
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
|
|
|
|
return mp_obj_float_get(arg);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-01-08 13:11:23 -05:00
|
|
|
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg)));
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
|
|
|
|
if (arg == mp_const_false) {
|
2013-12-17 13:27:24 -05:00
|
|
|
*real = 0;
|
|
|
|
*imag = 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (arg == mp_const_true) {
|
2013-12-17 13:27:24 -05:00
|
|
|
*real = 1;
|
|
|
|
*imag = 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
|
|
|
|
*real = MP_OBJ_SMALL_INT_VALUE(arg);
|
2013-12-17 13:27:24 -05:00
|
|
|
*imag = 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
|
|
|
|
*real = mp_obj_float_get(arg);
|
2013-12-17 13:27:24 -05:00
|
|
|
*imag = 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
|
|
|
|
mp_obj_complex_get(arg, real, imag);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-01-08 13:11:23 -05:00
|
|
|
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg)));
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_t *mp_obj_get_array_fixed_n(mp_obj_t o_in, machine_int_t n) {
|
|
|
|
if (MP_OBJ_IS_TYPE(o_in, &tuple_type) || MP_OBJ_IS_TYPE(o_in, &list_type)) {
|
|
|
|
uint seq_len;
|
|
|
|
mp_obj_t *seq_items;
|
|
|
|
if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
|
|
|
|
mp_obj_tuple_get(o_in, &seq_len, &seq_items);
|
|
|
|
} else {
|
|
|
|
mp_obj_list_get(o_in, &seq_len, &seq_items);
|
|
|
|
}
|
|
|
|
if (seq_len != n) {
|
2014-01-08 13:11:23 -05:00
|
|
|
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "requested length %d but object has length %d", n, seq_len));
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
return seq_items;
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2014-01-08 13:11:23 -05:00
|
|
|
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)));
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index) {
|
|
|
|
// TODO False and True are considered 0 and 1 for indexing purposes
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(index)) {
|
|
|
|
int i = MP_OBJ_SMALL_INT_VALUE(index);
|
|
|
|
if (i < 0) {
|
|
|
|
i += len;
|
|
|
|
}
|
|
|
|
if (i < 0 || i >= len) {
|
2014-01-08 13:11:23 -05:00
|
|
|
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
} else {
|
2014-01-08 13:11:23 -05:00
|
|
|
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)));
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-10 06:25:03 -05:00
|
|
|
|
2014-01-11 14:22:29 -05:00
|
|
|
// may return MP_OBJ_NULL
|
2014-01-10 06:25:03 -05:00
|
|
|
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
|
2014-01-22 09:35:10 -05:00
|
|
|
if (MP_OBJ_IS_STR(o_in)) {
|
2014-01-30 05:05:33 -05:00
|
|
|
return MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_str_get_len(o_in));
|
2014-01-10 06:25:03 -05:00
|
|
|
} else {
|
2014-01-29 21:37:19 -05:00
|
|
|
mp_obj_type_t *type = mp_obj_get_type(o_in);
|
|
|
|
if (type->unary_op != NULL) {
|
2014-01-30 05:05:33 -05:00
|
|
|
return type->unary_op(RT_UNARY_OP_LEN, o_in);
|
|
|
|
} else {
|
|
|
|
return MP_OBJ_NULL;
|
2014-01-29 21:37:19 -05:00
|
|
|
}
|
2014-01-10 06:25:03 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-20 11:37:30 -05:00
|
|
|
|
|
|
|
// Return input argument. Useful as .getiter for objects which are
|
|
|
|
// their own iterators, etc.
|
|
|
|
mp_obj_t mp_identity(mp_obj_t self) {
|
|
|
|
return self;
|
|
|
|
}
|