From 897fe0c0d0e29875cfa32939237c9a6255c5d044 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 15 Apr 2014 22:03:55 +0100 Subject: [PATCH] py: Add builtin functions bin and oct, and some tests for them. --- py/builtin.c | 14 ++++++++++++++ py/builtin.h | 2 ++ py/builtintables.c | 2 ++ py/makeqstrdata.py | 2 ++ py/objstr.c | 4 ++-- py/objstr.h | 2 ++ py/qstrdefs.h | 4 ++++ tests/basics/builtin-bin.py | 12 ++++++++++++ tests/basics/builtin-hex.py | 12 ++++++++++++ tests/basics/builtin-oct.py | 12 ++++++++++++ 10 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/basics/builtin-bin.py create mode 100644 tests/basics/builtin-hex.py create mode 100644 tests/basics/builtin-oct.py diff --git a/py/builtin.c b/py/builtin.c index 5911fa6342..5e20154520 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -6,6 +6,7 @@ #include "mpconfig.h" #include "qstr.h" #include "obj.h" +#include "objstr.h" #include "runtime0.h" #include "runtime.h" #include "builtin.h" @@ -125,6 +126,13 @@ STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any); +STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) { + mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_b_brace_close_), o_in }; + return mp_obj_str_format(sizeof(args) / sizeof(args[0]), args); +} + +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin); + STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) { if (mp_obj_is_callable(o_in)) { return mp_const_true; @@ -299,6 +307,12 @@ STATIC mp_obj_t mp_builtin_next(mp_obj_t o) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next); +STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) { + return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in); +} + +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct); + STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) { uint len; const char *str = mp_obj_str_get_data(o_in, &len); diff --git a/py/builtin.h b/py/builtin.h index 51a74a3103..36b9f38814 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -6,6 +6,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin___repl_print___obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_abs_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_all_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_bin_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_dir_obj); @@ -26,6 +27,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_locals_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_max_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_min_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_next_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_oct_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj); diff --git a/py/builtintables.c b/py/builtintables.c index 6cf12dffa9..6bdae24002 100644 --- a/py/builtintables.c +++ b/py/builtintables.c @@ -51,6 +51,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_abs), (mp_obj_t)&mp_builtin_abs_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_all), (mp_obj_t)&mp_builtin_all_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_any), (mp_obj_t)&mp_builtin_any_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_bin), (mp_obj_t)&mp_builtin_bin_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_callable), (mp_obj_t)&mp_builtin_callable_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_chr), (mp_obj_t)&mp_builtin_chr_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_dir), (mp_obj_t)&mp_builtin_dir_obj }, @@ -70,6 +71,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_max), (mp_obj_t)&mp_builtin_max_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_min), (mp_obj_t)&mp_builtin_min_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_next), (mp_obj_t)&mp_builtin_next_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_oct), (mp_obj_t)&mp_builtin_oct_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_ord), (mp_obj_t)&mp_builtin_ord_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_builtin_pow_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_print), (mp_obj_t)&mp_builtin_print_obj }, diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py index 8620af5673..afde772054 100644 --- a/py/makeqstrdata.py +++ b/py/makeqstrdata.py @@ -18,6 +18,8 @@ codepoint2name[ord(':')] = 'colon' codepoint2name[ord('/')] = 'slash' codepoint2name[ord('%')] = 'percent' codepoint2name[ord('#')] = 'hash' +codepoint2name[ord('{')] = 'brace_open' +codepoint2name[ord('}')] = 'brace_close' # this must match the equivalent function in qstr.c def compute_hash(qstr): diff --git a/py/objstr.c b/py/objstr.c index fc1e1c5695..0edc49909e 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -579,7 +579,7 @@ static mp_obj_t arg_as_int(mp_obj_t arg) { return arg; } -mp_obj_t str_format(uint n_args, const mp_obj_t *args) { +mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) { assert(MP_OBJ_IS_STR(args[0])); GET_STR_DATA_LEN(args[0], str, len); @@ -1346,7 +1346,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split); STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip); -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, mp_obj_str_format); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count); STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition); diff --git a/py/objstr.h b/py/objstr.h index a32ba53b1d..800568de8b 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -8,3 +8,5 @@ typedef struct _mp_obj_str_t { } mp_obj_str_t; #define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte*)str}; + +mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args); diff --git a/py/qstrdefs.h b/py/qstrdefs.h index d18882f69e..20c4db75f0 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -83,6 +83,8 @@ Q(all) Q(any) Q(args) Q(array) +Q(bin) +Q({:#b}) Q(bool) Q(bytearray) Q(bytes) @@ -122,6 +124,8 @@ Q(max) Q(min) Q(namedtuple) Q(next) +Q(oct) +Q(%#o) Q(open) Q(ord) Q(path) diff --git a/tests/basics/builtin-bin.py b/tests/basics/builtin-bin.py new file mode 100644 index 0000000000..f6b6079de5 --- /dev/null +++ b/tests/basics/builtin-bin.py @@ -0,0 +1,12 @@ +# test builtin bin function + +print(bin(1)) +print(bin(-1)) +print(bin(15)) +print(bin(-15)) + +print(bin(12345)) +print(bin(0b10101)) + +print(bin(12345678901234567890)) +print(bin(0b10101010101010101010)) diff --git a/tests/basics/builtin-hex.py b/tests/basics/builtin-hex.py new file mode 100644 index 0000000000..7d1c98a7a9 --- /dev/null +++ b/tests/basics/builtin-hex.py @@ -0,0 +1,12 @@ +# test builtin hex function + +print(hex(1)) +print(hex(-1)) +print(hex(15)) +print(hex(-15)) + +print(hex(12345)) +print(hex(0x12345)) + +print(hex(12345678901234567890)) +print(hex(0x12345678901234567890)) diff --git a/tests/basics/builtin-oct.py b/tests/basics/builtin-oct.py new file mode 100644 index 0000000000..d8ba8e4346 --- /dev/null +++ b/tests/basics/builtin-oct.py @@ -0,0 +1,12 @@ +# test builtin oct function + +print(oct(1)) +print(oct(-1)) +print(oct(15)) +print(oct(-15)) + +print(oct(12345)) +print(oct(0o12345)) + +print(oct(12345678901234567890)) +print(oct(0o12345670123456701234))