py: add int() and float() built-ins, partially implemented.
This commit is contained in:
parent
31aa47d56a
commit
209d1b1835
55
py/builtin.c
55
py/builtin.c
@ -115,20 +115,18 @@ 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 (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);
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
} else {
|
||||
mp_float_t real, imag;
|
||||
if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
|
||||
mp_obj_get_complex(args[0], &real, &imag);
|
||||
@ -146,10 +144,9 @@ mp_obj_t mp_builtin_complex(int n_args, const mp_obj_t *args) {
|
||||
}
|
||||
return mp_obj_new_complex(real, imag);
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
7
py/obj.c
7
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
|
||||
|
11
py/runtime.c
11
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);
|
||||
#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);
|
||||
#endif
|
||||
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 = (mp_obj_t)&mp_builtin_complex_obj;
|
||||
#endif
|
||||
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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user