Commit 36c4499d authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

Implement str() and repr() builtin functions.

parent 76d982ef
...@@ -338,3 +338,19 @@ static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) { ...@@ -338,3 +338,19 @@ 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);
mp_obj_t mp_builtin_str(mp_obj_t o) {
vstr_t *vstr = vstr_new();
mp_obj_print_helper(vstr_printf_wrapper, vstr, o, PRINT_STR);
return mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc));
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_str_obj, mp_builtin_str);
mp_obj_t mp_builtin_repr(mp_obj_t o) {
vstr_t *vstr = vstr_new();
mp_obj_print_helper(vstr_printf_wrapper, vstr, o, PRINT_REPR);
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);
...@@ -23,3 +23,5 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj); ...@@ -23,3 +23,5 @@ 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_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);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_str_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_repr_obj);
...@@ -81,6 +81,8 @@ void vstr_add_strn(vstr_t *vstr, const char *str, int len); ...@@ -81,6 +81,8 @@ void vstr_add_strn(vstr_t *vstr, const char *str, int len);
//void vstr_add_le32(vstr_t *vstr, unsigned int v); //void vstr_add_le32(vstr_t *vstr, unsigned int v);
void vstr_cut_tail(vstr_t *vstr, int len); void vstr_cut_tail(vstr_t *vstr, int len);
void vstr_printf(vstr_t *vstr, const char *fmt, ...); void vstr_printf(vstr_t *vstr, const char *fmt, ...);
// TODO: do we need wrapper at all?
void vstr_printf_wrapper(void *env, const char *fmt, ...);
#ifdef va_start #ifdef va_start
void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap); void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
......
...@@ -56,9 +56,11 @@ Q(ord) ...@@ -56,9 +56,11 @@ 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)
Q(str)
Q(tuple) Q(tuple)
Q(type) Q(type)
Q(zip) Q(zip)
......
...@@ -136,6 +136,8 @@ void rt_init(void) { ...@@ -136,6 +136,8 @@ void rt_init(void) {
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_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);
mp_map_add_qstr(&map_builtins, MP_QSTR_str, (mp_obj_t)&mp_builtin_str_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj);
next_unique_code_id = 1; // 0 indicates "no code" next_unique_code_id = 1; // 0 indicates "no code"
unique_codes_alloc = 0; unique_codes_alloc = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment