py: Add framework for built-in "type()" function.
This commit is contained in:
parent
210a02e105
commit
40563d56bd
13
py/builtin.c
13
py/builtin.c
@ -410,3 +410,16 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
|
|||||||
}
|
}
|
||||||
return value;
|
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);
|
||||||
|
@ -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_obj_t mp_builtin_range(int n_args, const mp_obj_t *args);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_set_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_set_obj);
|
||||||
mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args);
|
mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args);
|
||||||
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_type_obj);
|
||||||
|
@ -7,12 +7,12 @@
|
|||||||
#include "obj.h"
|
#include "obj.h"
|
||||||
|
|
||||||
void type_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
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 = {
|
const mp_obj_type_t mp_const_type = {
|
||||||
{ &mp_const_type },
|
{ &mp_const_type },
|
||||||
"type?",
|
"<a type>",
|
||||||
type_print, // print
|
type_print, // print
|
||||||
NULL, // call_n
|
NULL, // call_n
|
||||||
NULL, // unary_op
|
NULL, // unary_op
|
||||||
|
@ -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("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("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("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
|
next_unique_code_id = 2; // 1 is reserved for the __main__ module scope
|
||||||
|
Loading…
x
Reference in New Issue
Block a user