py: Add framework for built-in "type()" function.

This commit is contained in:
Damien George 2014-01-02 16:01:17 +00:00
parent 210a02e105
commit 40563d56bd
4 changed files with 17 additions and 2 deletions

View File

@ -410,3 +410,16 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
}
return value;
}
static mp_obj_t mp_builtin_type(mp_obj_t o_in) {
// TODO implement the 3 argument version of type()
if (MP_OBJ_IS_SMALL_INT(o_in)) {
// TODO implement int-type
return mp_const_none;
} else {
mp_obj_base_t *o = o_in;
return (mp_obj_t)o->type;
}
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_type_obj, mp_builtin_type);

View File

@ -31,3 +31,4 @@ mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args);
mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_set_obj);
mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_type_obj);

View File

@ -7,12 +7,12 @@
#include "obj.h"
void type_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
print(env, "type?");
print(env, "<a type>");
}
const mp_obj_type_t mp_const_type = {
{ &mp_const_type },
"type?",
"<a type>",
type_print, // print
NULL, // call_n
NULL, // unary_op

View File

@ -147,6 +147,7 @@ void rt_init(void) {
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("range"), true)->value = rt_make_function_var(1, mp_builtin_range);
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("set"), true)->value = (mp_obj_t)&mp_builtin_set_obj;
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("sum"), true)->value = rt_make_function_var(1, mp_builtin_sum);
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("type"), true)->value = (mp_obj_t)&mp_builtin_type_obj;
next_unique_code_id = 2; // 1 is reserved for the __main__ module scope