Implement repr.

This commit is contained in:
Damien George 2014-01-15 21:40:48 +00:00
parent 9528cd66d7
commit e2fb2baaa4
5 changed files with 17 additions and 7 deletions

View File

@ -140,10 +140,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
static mp_obj_t mp_builtin_chr(mp_obj_t o_in) { static mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
int ord = mp_obj_get_int(o_in); int ord = mp_obj_get_int(o_in);
if (0 <= ord && ord <= 0x10ffff) { if (0 <= ord && ord <= 0x10ffff) {
char *str = m_new(char, 2); char str[2] = {ord, '\0'};
str[0] = ord; return mp_obj_new_str(qstr_from_strn_copy(str, 1));
str[1] = '\0';
return mp_obj_new_str(qstr_from_str_take(str, 2));
} else { } else {
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "chr() arg not in range(0x110000)")); nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "chr() arg not in range(0x110000)"));
} }
@ -310,6 +308,14 @@ static mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) {
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range);
static mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
vstr_t *vstr = vstr_new();
mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in);
return mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc));
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
assert(1 <= n_args && n_args <= 2); assert(1 <= n_args && n_args <= 2);
mp_obj_t value; mp_obj_t value;
@ -324,6 +330,7 @@ static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
} }
return value; return value;
} }
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) { static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) {
@ -343,4 +350,5 @@ static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) {
return self; return self;
} }
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted); MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);

View File

@ -21,5 +21,6 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_range_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_range_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_repr_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);

View File

@ -57,6 +57,7 @@ Q(ord)
Q(pow) Q(pow)
Q(print) Q(print)
Q(range) Q(range)
Q(repr)
Q(set) Q(set)
Q(sorted) Q(sorted)
Q(sum) Q(sum)

View File

@ -134,6 +134,7 @@ void rt_init(void) {
mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj);

View File

@ -1,3 +1,2 @@
# TODO: requires repr() a = IndexError(1, "test", [100, 200])
#a = IndexError(1, "test", [100, 200]) print(repr(a))
#print(repr(a))