Commit 40563d56 authored by Damien George's avatar Damien George
Browse files

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

parent 210a02e1
......@@ -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);
......@@ -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);
......@@ -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
......
......@@ -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
......
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