summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/builtin.c95
-rw-r--r--py/builtin.h8
-rw-r--r--py/obj.c7
-rw-r--r--py/runtime.c9
4 files changed, 80 insertions, 39 deletions
diff --git a/py/builtin.c b/py/builtin.c
index f39ab03d45..96a9fa328b 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -115,41 +115,38 @@ mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
#if MICROPY_ENABLE_FLOAT
mp_obj_t mp_builtin_complex(int n_args, const mp_obj_t *args) {
- switch (n_args) {
- case 0:
- return mp_obj_new_complex(0, 0);
+ assert(0 <= n_args && n_args <= 2);
- case 1:
- // TODO allow string as first arg
- if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
- return args[0];
- } else {
- return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
- }
-
- case 2:
- {
- mp_float_t real, imag;
- if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
- mp_obj_get_complex(args[0], &real, &imag);
- } else {
- real = mp_obj_get_float(args[0]);
- imag = 0;
- }
- if (MP_OBJ_IS_TYPE(args[1], &complex_type)) {
- mp_float_t real2, imag2;
- mp_obj_get_complex(args[1], &real2, &imag2);
- real -= imag2;
- imag += real2;
- } else {
- imag += mp_obj_get_float(args[1]);
- }
- return mp_obj_new_complex(real, imag);
+ if (n_args == 0) {
+ return mp_obj_new_complex(0, 0);
+ } else if (n_args == 1) {
+ // TODO allow string as first arg and parse it
+ if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
+ return args[0];
+ } else {
+ return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
}
-
- default: nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "comlpex() takes at most 2 arguments (%d given)", (void*)(machine_int_t)n_args));
+ } else {
+ mp_float_t real, imag;
+ if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
+ mp_obj_get_complex(args[0], &real, &imag);
+ } else {
+ real = mp_obj_get_float(args[0]);
+ imag = 0;
+ }
+ if (MP_OBJ_IS_TYPE(args[1], &complex_type)) {
+ mp_float_t real2, imag2;
+ mp_obj_get_complex(args[1], &real2, &imag2);
+ real -= imag2;
+ imag += real2;
+ } else {
+ imag += mp_obj_get_float(args[1]);
+ }
+ return mp_obj_new_complex(real, imag);
}
}
+
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_complex_obj, 0, 2, mp_builtin_complex);
#endif
mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
@@ -182,6 +179,25 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
}
}
+#if MICROPY_ENABLE_FLOAT
+static mp_obj_t mp_builtin_float(int n_args, const mp_obj_t *args) {
+ assert(0 <= n_args && n_args <= 1);
+
+ if (n_args == 0) {
+ return mp_obj_new_float(0);
+ } else {
+ // TODO allow string as arg and parse it
+ if (MP_OBJ_IS_TYPE(args[0], &float_type)) {
+ return args[0];
+ } else {
+ return mp_obj_new_float(mp_obj_get_float(args[0]));
+ }
+ }
+}
+
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_float_obj, 0, 1, mp_builtin_float);
+#endif
+
static mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
// TODO hash will generally overflow small integer; can we safely truncate it?
return mp_obj_new_int(mp_obj_hash(o_in));
@@ -189,6 +205,23 @@ static mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
+static mp_obj_t mp_builtin_int(int n_args, const mp_obj_t *args) {
+ assert(0 <= n_args && n_args <= 2);
+
+ if (n_args == 0) {
+ return MP_OBJ_NEW_SMALL_INT(0);
+ } else if (n_args == 1) {
+ // TODO if arg is a string then parse it
+ return mp_obj_new_int(mp_obj_get_int(args[0]));
+ } else { // n_args == 2
+ // TODO, parse with given base
+ assert(0);
+ return MP_OBJ_NEW_SMALL_INT(0);
+ }
+}
+
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_int_obj, 0, 2, mp_builtin_int);
+
static mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
return rt_getiter(o_in);
}
diff --git a/py/builtin.h b/py/builtin.h
index c164a76611..2d95e3a913 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -8,11 +8,17 @@ mp_obj_t mp_builtin_all(mp_obj_t o_in);
mp_obj_t mp_builtin_any(mp_obj_t o_in);
mp_obj_t mp_builtin_bool(int n_args, const mp_obj_t *args);
mp_obj_t mp_builtin_callable(mp_obj_t o_in);
-mp_obj_t mp_builtin_complex(int n_args, const mp_obj_t *args);
+#if MICROPY_ENABLE_FLOAT
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_complex_obj);
+#endif
mp_obj_t mp_builtin_chr(mp_obj_t o_in);
mp_obj_t mp_builtin_dict(void);
mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in);
+#if MICROPY_ENABLE_FLOAT
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_float_obj);
+#endif
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_int_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_iter_obj);
mp_obj_t mp_builtin_len(mp_obj_t o_in);
mp_obj_t mp_builtin_list(int n_args, const mp_obj_t *args);
diff --git a/py/obj.c b/py/obj.c
index ff49492bf8..d88d0ac3d7 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -144,8 +144,7 @@ machine_float_t mp_obj_get_float(mp_obj_t arg) {
} else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
return mp_obj_float_get(arg);
} else {
- assert(0);
- return 0;
+ nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg)));
}
}
@@ -165,9 +164,7 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
} else if (MP_OBJ_IS_TYPE(arg, &complex_type)) {
mp_obj_complex_get(arg, real, imag);
} else {
- assert(0);
- *real = 0;
- *imag = 0;
+ nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg)));
}
}
#endif
diff --git a/py/runtime.c b/py/runtime.c
index c3e1f5d9cf..372cdd8656 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -124,13 +124,17 @@ void rt_init(void) {
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("any"), true)->value = rt_make_function_1(mp_builtin_any);
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("bool"), true)->value = rt_make_function_var(0, mp_builtin_bool);
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("callable"), true)->value = rt_make_function_1(mp_builtin_callable);
+ mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("chr"), true)->value = rt_make_function_1(mp_builtin_chr);
#if MICROPY_ENABLE_FLOAT
- mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("complex"), true)->value = rt_make_function_var(0, mp_builtin_complex);
+ mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("complex"), true)->value = (mp_obj_t)&mp_builtin_complex_obj;
#endif
- mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("chr"), true)->value = rt_make_function_1(mp_builtin_chr);
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("dict"), true)->value = rt_make_function_0(mp_builtin_dict);
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("divmod"), true)->value = rt_make_function_2(mp_builtin_divmod);
+#if MICROPY_ENABLE_FLOAT
+ mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("float"), true)->value = (mp_obj_t)&mp_builtin_float_obj;
+#endif
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("hash"), true)->value = (mp_obj_t)&mp_builtin_hash_obj;
+ mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("int"), true)->value = (mp_obj_t)&mp_builtin_int_obj;
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("iter"), true)->value = (mp_obj_t)&mp_builtin_iter_obj;
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("len"), true)->value = rt_make_function_1(mp_builtin_len);
mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("list"), true)->value = rt_make_function_var(0, mp_builtin_list);
@@ -144,6 +148,7 @@ void rt_init(void) {
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);
+
next_unique_code_id = 2; // 1 is reserved for the __main__ module scope
unique_codes = NULL;