summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-02 16:01:17 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-02 16:01:17 +0000
commit40563d56bdabf270f0f2296dcc1b2f64b0c238c0 (patch)
treedc10ac2c6b010d3967cc5c1e07c60f5056f4ec16 /py
parent210a02e1051d9f5b6f03c24714ab86ac936bc41d (diff)
downloadmicropython-40563d56bdabf270f0f2296dcc1b2f64b0c238c0.tar.gz
micropython-40563d56bdabf270f0f2296dcc1b2f64b0c238c0.zip
py: Add framework for built-in "type()" function.
Diffstat (limited to 'py')
-rw-r--r--py/builtin.c13
-rw-r--r--py/builtin.h1
-rw-r--r--py/objtype.c4
-rw-r--r--py/runtime.c1
4 files changed, 17 insertions, 2 deletions
diff --git a/py/builtin.c b/py/builtin.c
index 96a9fa328b..2b94163f16 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -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);
diff --git a/py/builtin.h b/py/builtin.h
index 2d95e3a913..9806624206 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -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);
diff --git a/py/objtype.c b/py/objtype.c
index 4358148c04..83ae48d2d1 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -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
diff --git a/py/runtime.c b/py/runtime.c
index 372cdd8656..c3b3d74259 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -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