diff --git a/logo/vector-logo-3.png b/logo/vector-logo-3.png new file mode 100644 index 000000000..ba75b0562 Binary files /dev/null and b/logo/vector-logo-3.png differ diff --git a/logo/vector-logo-inkscape_master.svg b/logo/vector-logo-inkscape_master.svg index bbfca4780..ddcf74117 100644 --- a/logo/vector-logo-inkscape_master.svg +++ b/logo/vector-logo-inkscape_master.svg @@ -15,8 +15,8 @@ height="779.328" id="svg2" inkscape:version="0.48.4 r9939" - sodipodi:docname="vector-logo-inkscape_master.svg" - inkscape:export-filename="R:\Mark_Sector\!!Projects-2\microPython\Logo\vector-logo-4.png" + sodipodi:docname="vector-logo-inkscape_master-2.svg" + inkscape:export-filename="C:\Users\user3\Documents\GitHub\micropython\logo\vector-logo-2.png" inkscape:export-xdpi="90" inkscape:export-ydpi="90"> + inkscape:current-layer="layer3" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -373,7 +2243,7 @@ image/svg+xml - + @@ -736,176 +2606,191 @@ inkscape:connector-curvature="0" sodipodi:nodetypes="cccccccccccccccccccscccccccccccccccscsccsscccsccccscsccscccccsscssscsssscsscscssscssssscssscscccsssscscccccsccssccssssscccccccccccccccccccccccsccssscsscccsssccsscsccscccccccsscssccssccsssccssccccccsssscsssscssssccscscsccscscscsccsscsccscccccccccccccccccccccccccccssccssccscssccssscccccccccccccscsssscssssscsscsssssssscsccssccscccccsssssssssccscc" /> + id="g5271" + transform="translate(-18.659191,-7.4636765)"> - + d="m 92.180672,637.64386 -22.366085,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5323);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-4" + d="m 108.64278,643.66805 -22.366085,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5325);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-5" + d="m 124.83462,649.62094 -22.36609,12.91306 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5327);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-0" + d="m 141.26457,655.81193 -22.36608,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5329);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-9" + d="m 157.69453,662.00293 -22.36608,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5331);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-7" + d="m 173.64825,667.9558 -22.36608,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5333);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-41" + d="m 190.07821,673.67057 -22.36608,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5335);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-2" + d="m 207.22251,679.38534 -22.36609,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5337);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-21" + d="m 223.17623,685.33822 -22.36609,12.91306 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5339);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-6" + d="m 239.8443,691.2911 -22.36609,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5341);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-59" + d="m 256.27425,697.4821 -22.36609,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5343);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-1" + d="m 272.70421,702.95875 -22.36609,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5345);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-42" + d="m 288.89604,709.38786 -22.36609,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5347);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-73" + d="m 304.84977,715.57886 -22.36609,12.91307 0,30.03328" /> + style="fill:none;stroke:url(#linearGradient5349);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-72" + d="m 321.75595,720.57928 -22.36609,12.91307 0,30.03328" /> - - - - - - - - - - - - - - - + style="fill:none;stroke:url(#linearGradient5351);stroke-width:9.60000038;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + id="path3292-8-3" + d="m 338.18591,726.77028 -22.36609,12.91307 0,30.03328" /> + + + + + + + + + + + + + + + + + + + + 0) { printf(" "); @@ -273,21 +297,25 @@ mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) { return mp_const_none; } -mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) { +MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_print_obj, 0, mp_builtin_print); + +static mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) { + assert(1 <= n_args && n_args <= 3); switch (n_args) { case 1: return mp_obj_new_range(0, mp_obj_get_int(args[0]), 1); case 2: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), 1); - case 3: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2])); - default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "range expected at most 3 arguments, got %d", n_args)); + default: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2])); } } -mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range); + +static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { + assert(1 <= n_args && n_args <= 2); mp_obj_t value; switch (n_args) { case 1: value = mp_obj_new_int(0); break; - case 2: value = args[1]; break; - default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "sum expected at most 2 arguments, got %d", n_args)); + default: value = args[1]; break; } mp_obj_t iterable = rt_getiter(args[0]); mp_obj_t item; @@ -296,3 +324,5 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { } return value; } + +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum); diff --git a/py/builtin.h b/py/builtin.h index db7d517a0..0198d63bf 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -1,25 +1,24 @@ -// TODO convert all these to objects using MP_DECLARE and MP_DEFINE +mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj); -mp_obj_t mp_builtin___import__(int n, mp_obj_t *args); -mp_obj_t mp_builtin___repl_print__(mp_obj_t o); -mp_obj_t mp_builtin_abs(mp_obj_t o_in); -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_callable(mp_obj_t o_in); -mp_obj_t mp_builtin_chr(mp_obj_t o_in); -mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in); +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_callable_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_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); -mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args); -mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_len_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_list_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_obj_t mp_builtin_ord(mp_obj_t o_in); -mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args); -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_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args); +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); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_range_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj); diff --git a/py/builtinimport.c b/py/builtinimport.c index 90a0fc339..33576e3f0 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -18,7 +18,7 @@ #include "map.h" #include "builtin.h" -mp_obj_t mp_builtin___import__(int n, mp_obj_t *args) { +mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) { /* printf("import:\n"); for (int i = 0; i < n; i++) { diff --git a/py/mpconfig.h b/py/mpconfig.h index ada4aa2ea..5d8c57692 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -91,7 +91,7 @@ typedef long long mp_longint_impl_t; #define BITS_PER_BYTE (8) #define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD) // machine_int_t value with most significant bit set -#define WORD_MSBIT_HIGH (1 << (BYTES_PER_WORD * 8 - 1)) +#define WORD_MSBIT_HIGH (((machine_uint_t)1) << (BYTES_PER_WORD * 8 - 1)) // printf format spec to use for machine_int_t and friends #ifndef INT_FMT diff --git a/py/obj.c b/py/obj.c index 2759437fd..206bf7a24 100644 --- a/py/obj.c +++ b/py/obj.c @@ -108,9 +108,15 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) { return val == 0; } else if (o2 == mp_const_true) { return val == 1; - } else { - return false; + } else if (MP_OBJ_IS_TYPE(o2, &int_type)) { + // If o2 is long int, dispatch to its virtual methods + mp_obj_base_t *o = o2; + if (o->type->binary_op != NULL) { + mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1); + return r == mp_const_true ? true : false; + } } + return false; } } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) { return false; diff --git a/py/objfun.c b/py/objfun.c index afac3889f..ba3ce6279 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -98,38 +98,13 @@ const mp_obj_type_t fun_native_type = { .call_n_kw = fun_native_call_n_kw, }; -mp_obj_t rt_make_function_0(mp_fun_0_t fun) { +// fun must have the correct signature for n_args fixed arguments +mp_obj_t rt_make_function_n(int n_args, void *fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; - o->n_args_min = 0; - o->n_args_max = 0; - o->fun = fun; - return o; -} - -mp_obj_t rt_make_function_1(mp_fun_1_t fun) { - mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); - o->base.type = &fun_native_type; - o->n_args_min = 1; - o->n_args_max = 1; - o->fun = fun; - return o; -} - -mp_obj_t rt_make_function_2(mp_fun_2_t fun) { - mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); - o->base.type = &fun_native_type; - o->n_args_min = 2; - o->n_args_max = 2; - o->fun = fun; - return o; -} - -mp_obj_t rt_make_function_3(mp_fun_3_t fun) { - mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); - o->base.type = &fun_native_type; - o->n_args_min = 3; - o->n_args_max = 3; + o->is_kw = false; + o->n_args_min = n_args; + o->n_args_max = n_args; o->fun = fun; return o; } @@ -137,6 +112,7 @@ mp_obj_t rt_make_function_3(mp_fun_3_t fun) { mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; + o->is_kw = false; o->n_args_min = n_args_min; o->n_args_max = ~((machine_uint_t)0); o->fun = fun; @@ -147,6 +123,7 @@ mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) { mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; + o->is_kw = false; o->n_args_min = n_args_min; o->n_args_max = n_args_max; o->fun = fun; diff --git a/py/objint.c b/py/objint.c index 26d3c0e33..c0bf756f1 100644 --- a/py/objint.c +++ b/py/objint.c @@ -8,16 +8,7 @@ #include "mpconfig.h" #include "mpqstr.h" #include "obj.h" - -typedef struct _mp_obj_int_t { - mp_obj_base_t base; -#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE - mp_longint_impl_t val; -#endif -} mp_obj_int_t; - -void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in); -mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in); +#include "objint.h" // This dispatcher function is expected to be independent of the implementation // of long int @@ -54,11 +45,13 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj // This is called only for non-SMALL_INT mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { assert(0); + return mp_const_none; } // This is called only with strings whose value doesn't fit in SMALL_INT mp_obj_t mp_obj_new_int_from_long_str(const char *s) { assert(0); + return mp_const_none; } mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) { @@ -69,6 +62,7 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) { } // TODO: Raise exception assert(0); + return mp_const_none; } mp_obj_t mp_obj_new_int(machine_int_t value) { @@ -77,5 +71,6 @@ mp_obj_t mp_obj_new_int(machine_int_t value) { } // TODO: Raise exception assert(0); + return mp_const_none; } #endif diff --git a/py/objint.h b/py/objint.h new file mode 100644 index 000000000..14cf977be --- /dev/null +++ b/py/objint.h @@ -0,0 +1,9 @@ +typedef struct _mp_obj_int_t { + mp_obj_base_t base; +#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE + mp_longint_impl_t val; +#endif +} mp_obj_int_t; + +void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in); +mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in); diff --git a/py/objstr.c b/py/objstr.c index 4151cf007..8b7ab9692 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -178,8 +178,8 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) { const char* haystack = qstr_str(((mp_obj_str_t*)args[0])->qstr); const char* needle = qstr_str(((mp_obj_str_t*)args[1])->qstr); - ssize_t haystack_len = strlen(haystack); - ssize_t needle_len = strlen(needle); + size_t haystack_len = strlen(haystack); + size_t needle_len = strlen(needle); size_t start = 0; size_t end = haystack_len; @@ -192,14 +192,17 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) { } char *p = strstr(haystack + start, needle); - ssize_t pos = -1; - if (p) { - pos = p - haystack; + if (p == NULL) { + // not found + return MP_OBJ_NEW_SMALL_INT(-1); + } else { + // found + machine_int_t pos = p - haystack; if (pos + needle_len > end) { pos = -1; } + return MP_OBJ_NEW_SMALL_INT(pos); } - return MP_OBJ_NEW_SMALL_INT(pos); } mp_obj_t str_strip(int n_args, const mp_obj_t *args) { diff --git a/py/runtime.c b/py/runtime.c index 50ee37ec0..0610fd99b 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -97,7 +97,7 @@ void rt_init(void) { // built-in core functions mp_map_add_qstr(&map_builtins, MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj); - mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, rt_make_function_1(mp_builtin___repl_print__)); + mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj); // built-in types mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type); @@ -114,26 +114,26 @@ void rt_init(void) { mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type); mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type); - // built-in user functions; TODO covert all to &mp_builtin_xxx's - mp_map_add_qstr(&map_builtins, MP_QSTR_abs, rt_make_function_1(mp_builtin_abs)); - mp_map_add_qstr(&map_builtins, MP_QSTR_all, rt_make_function_1(mp_builtin_all)); - mp_map_add_qstr(&map_builtins, MP_QSTR_any, rt_make_function_1(mp_builtin_any)); - mp_map_add_qstr(&map_builtins, MP_QSTR_callable, rt_make_function_1(mp_builtin_callable)); - mp_map_add_qstr(&map_builtins, MP_QSTR_chr, rt_make_function_1(mp_builtin_chr)); - mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, rt_make_function_2(mp_builtin_divmod)); + // built-in user functions + mp_map_add_qstr(&map_builtins, MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj); - mp_map_add_qstr(&map_builtins, MP_QSTR_len, rt_make_function_1(mp_builtin_len)); - mp_map_add_qstr(&map_builtins, MP_QSTR_max, rt_make_function_var(1, mp_builtin_max)); - mp_map_add_qstr(&map_builtins, MP_QSTR_min, rt_make_function_var(1, mp_builtin_min)); + mp_map_add_qstr(&map_builtins, MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj); - mp_map_add_qstr(&map_builtins, MP_QSTR_ord, rt_make_function_1(mp_builtin_ord)); - mp_map_add_qstr(&map_builtins, MP_QSTR_pow, rt_make_function_var(2, mp_builtin_pow)); - mp_map_add_qstr(&map_builtins, MP_QSTR_print, rt_make_function_var(0, mp_builtin_print)); - mp_map_add_qstr(&map_builtins, MP_QSTR_range, rt_make_function_var(1, mp_builtin_range)); - mp_map_add_qstr(&map_builtins, MP_QSTR_sum, rt_make_function_var(1, mp_builtin_sum)); + mp_map_add_qstr(&map_builtins, MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj); next_unique_code_id = 1; // 0 indicates "no code" unique_codes_alloc = 0; @@ -622,12 +622,7 @@ mp_obj_t rt_make_function_from_id(int unique_code_id) { fun = mp_obj_new_fun_bc(c->n_args, c->n_locals + c->n_stack, c->u_byte.code); break; case MP_CODE_NATIVE: - switch (c->n_args) { - case 0: fun = rt_make_function_0(c->u_native.fun); break; - case 1: fun = rt_make_function_1((mp_fun_1_t)c->u_native.fun); break; - case 2: fun = rt_make_function_2((mp_fun_2_t)c->u_native.fun); break; - default: assert(0); fun = mp_const_none; - } + fun = rt_make_function_n(c->n_args, c->u_native.fun); break; case MP_CODE_INLINE_ASM: fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun); diff --git a/py/runtime.h b/py/runtime.h index ac53e1411..32cb47684 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -12,10 +12,8 @@ void rt_store_global(qstr qstr, mp_obj_t obj); mp_obj_t rt_unary_op(int op, mp_obj_t arg); mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs); mp_obj_t rt_make_function_from_id(int unique_code_id); -mp_obj_t rt_make_function_0(mp_fun_0_t f); -mp_obj_t rt_make_function_1(mp_fun_1_t f); -mp_obj_t rt_make_function_2(mp_fun_2_t f); -mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t f); +mp_obj_t rt_make_function_n(int n_args, void *fun); // fun must have the correct signature for n_args fixed arguments +mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun); mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun); // min and max are inclusive mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple); mp_obj_t rt_call_function_0(mp_obj_t fun); diff --git a/stm/audio.c b/stm/audio.c index 34adefbcd..e2aa32b9f 100644 --- a/stm/audio.c +++ b/stm/audio.c @@ -91,8 +91,8 @@ void audio_init(void) { // Python interface mp_obj_t m = mp_obj_new_module(qstr_from_str_static("audio")); - rt_store_attr(m, qstr_from_str_static("dac"), rt_make_function_1(pyb_audio_dac)); - rt_store_attr(m, qstr_from_str_static("is_full"), rt_make_function_0(pyb_audio_is_full)); - rt_store_attr(m, qstr_from_str_static("fill"), rt_make_function_1(pyb_audio_fill)); + rt_store_attr(m, qstr_from_str_static("dac"), rt_make_function_n(1, pyb_audio_dac)); + rt_store_attr(m, qstr_from_str_static("is_full"), rt_make_function_n(0, pyb_audio_is_full)); + rt_store_attr(m, qstr_from_str_static("fill"), rt_make_function_n(1, pyb_audio_fill)); rt_store_name(qstr_from_str_static("audio"), m); } diff --git a/stm/lcd.c b/stm/lcd.c index 70d1a2642..82e42b779 100644 --- a/stm/lcd.c +++ b/stm/lcd.c @@ -220,13 +220,13 @@ void lcd_init(void) { // Python interface mp_obj_t m = mp_obj_new_module(qstr_from_str_static("lcd")); - rt_store_attr(m, qstr_from_str_static("lcd8"), rt_make_function_2(lcd_draw_pixel_8)); - rt_store_attr(m, qstr_from_str_static("clear"), rt_make_function_0(lcd_pix_clear)); - rt_store_attr(m, qstr_from_str_static("get"), rt_make_function_2(lcd_pix_get)); - rt_store_attr(m, qstr_from_str_static("set"), rt_make_function_2(lcd_pix_set)); - rt_store_attr(m, qstr_from_str_static("reset"), rt_make_function_2(lcd_pix_reset)); - rt_store_attr(m, qstr_from_str_static("show"), rt_make_function_0(lcd_pix_show)); - rt_store_attr(m, qstr_from_str_static("text"), rt_make_function_1(lcd_print)); + rt_store_attr(m, qstr_from_str_static("lcd8"), rt_make_function_n(2, lcd_draw_pixel_8)); + rt_store_attr(m, qstr_from_str_static("clear"), rt_make_function_n(0, lcd_pix_clear)); + rt_store_attr(m, qstr_from_str_static("get"), rt_make_function_n(2, lcd_pix_get)); + rt_store_attr(m, qstr_from_str_static("set"), rt_make_function_n(2, lcd_pix_set)); + rt_store_attr(m, qstr_from_str_static("reset"), rt_make_function_n(2, lcd_pix_reset)); + rt_store_attr(m, qstr_from_str_static("show"), rt_make_function_n(0, lcd_pix_show)); + rt_store_attr(m, qstr_from_str_static("text"), rt_make_function_n(1, lcd_print)); rt_store_name(qstr_from_str_static("lcd"), m); } diff --git a/stm/main.c b/stm/main.c index cdc4432c0..2085182c2 100644 --- a/stm/main.c +++ b/stm/main.c @@ -812,36 +812,36 @@ soft_reset: // add some functions to the python namespace { - rt_store_name(qstr_from_str_static("help"), rt_make_function_0(pyb_help)); + rt_store_name(qstr_from_str_static("help"), rt_make_function_n(0, pyb_help)); mp_obj_t m = mp_obj_new_module(qstr_from_str_static("pyb")); - rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_0(pyb_info)); - rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_0(pyb_sd_test)); - rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_0(pyb_stop)); - rt_store_attr(m, qstr_from_str_static("standby"), rt_make_function_0(pyb_standby)); - rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_1(pyb_source_dir)); - rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_1(pyb_main)); - rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_0(pyb_sync)); - rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_0(pyb_gc)); - rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_1(pyb_delay)); - rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_1(pyb_led)); + rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_n(0, pyb_info)); + rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_n(0, pyb_sd_test)); + rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_n(0, pyb_stop)); + rt_store_attr(m, qstr_from_str_static("standby"), rt_make_function_n(0, pyb_standby)); + rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_n(1, pyb_source_dir)); + rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_n(1, pyb_main)); + rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_n(0, pyb_sync)); + rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_n(0, pyb_gc)); + rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_n(1, pyb_delay)); + rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_n(1, pyb_led)); rt_store_attr(m, qstr_from_str_static("switch"), (mp_obj_t)&pyb_switch_obj); - rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_2(pyb_servo_set)); - rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_2(pyb_pwm_set)); + rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_n(2, pyb_servo_set)); + rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_n(2, pyb_pwm_set)); rt_store_attr(m, qstr_from_str_static("accel"), (mp_obj_t)&pyb_mma_read_obj); rt_store_attr(m, qstr_from_str_static("mma_read"), (mp_obj_t)&pyb_mma_read_all_obj); rt_store_attr(m, qstr_from_str_static("mma_mode"), (mp_obj_t)&pyb_mma_write_mode_obj); - rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_1(pyb_hid_send_report)); - rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_0(pyb_rtc_read)); - rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_0(pyb_rng_get)); - rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_1(pyb_Led)); - rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_1(pyb_Servo)); - rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_2(pyb_I2C)); + rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_n(1, pyb_hid_send_report)); + rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_n(0, pyb_rtc_read)); + rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_n(0, pyb_rng_get)); + rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_n(1, pyb_Led)); + rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_n(1, pyb_Servo)); + rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_n(2, pyb_I2C)); rt_store_attr(m, qstr_from_str_static("gpio"), (mp_obj_t)&pyb_gpio_obj); - rt_store_attr(m, qstr_from_str_static("Usart"), rt_make_function_2(pyb_Usart)); + rt_store_attr(m, qstr_from_str_static("Usart"), rt_make_function_n(2, pyb_Usart)); rt_store_name(qstr_from_str_static("pyb"), m); - rt_store_name(qstr_from_str_static("open"), rt_make_function_2(pyb_io_open)); + rt_store_name(qstr_from_str_static("open"), rt_make_function_n(2, pyb_io_open)); } // print a message to the LCD diff --git a/stm/printf.c b/stm/printf.c index c0fa82e1b..732e83452 100644 --- a/stm/printf.c +++ b/stm/printf.c @@ -213,9 +213,9 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) { // usable. I expect that this will be replaced with something // more appropriate. char dot = '.'; - double d = va_arg(args, double); + mp_float_t d = va_arg(args, double); int left = (int)d; - int right = (int)((d - (double)(int)d) * 1000000.0); + int right = (int)((d - (mp_float_t)(int)d) * 1000000.0); chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width); chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width); chrs += pfenv_print_int(pfenv, right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6); diff --git a/stm/pybwlan.c b/stm/pybwlan.c index 09d642d9c..91d71a652 100644 --- a/stm/pybwlan.c +++ b/stm/pybwlan.c @@ -357,11 +357,11 @@ void pyb_wlan_init(void) { mp_obj_t m = mp_obj_new_module(qstr_from_str_static("wlan")); rt_store_attr(m, qstr_from_str_static("connect"), rt_make_function_var(0, pyb_wlan_connect)); - rt_store_attr(m, qstr_from_str_static("disconnect"), rt_make_function_0(pyb_wlan_disconnect)); - rt_store_attr(m, qstr_from_str_static("ip"), rt_make_function_0(pyb_wlan_get_ip)); - rt_store_attr(m, qstr_from_str_static("get_host"), rt_make_function_1(pyb_wlan_get_host)); - rt_store_attr(m, qstr_from_str_static("http_get"), rt_make_function_2(pyb_wlan_http_get)); - rt_store_attr(m, qstr_from_str_static("serve"), rt_make_function_0(pyb_wlan_serve)); + rt_store_attr(m, qstr_from_str_static("disconnect"), rt_make_function_n(0, pyb_wlan_disconnect)); + rt_store_attr(m, qstr_from_str_static("ip"), rt_make_function_n(0, pyb_wlan_get_ip)); + rt_store_attr(m, qstr_from_str_static("get_host"), rt_make_function_n(1, pyb_wlan_get_host)); + rt_store_attr(m, qstr_from_str_static("http_get"), rt_make_function_n(2, pyb_wlan_http_get)); + rt_store_attr(m, qstr_from_str_static("serve"), rt_make_function_n(0, pyb_wlan_serve)); rt_store_name(qstr_from_str_static("wlan"), m); } diff --git a/stm/std.h b/stm/std.h index 587b9f888..95c606e05 100644 --- a/stm/std.h +++ b/stm/std.h @@ -17,6 +17,8 @@ int strncmp(const char *s1, const char *s2, size_t n); char *strndup(const char *s, size_t n); char *strcpy(char *dest, const char *src); char *strcat(char *dest, const char *src); +char *strchr(const char *s, int c); +char *strstr(const char *haystack, const char *needle); int printf(const char *fmt, ...); int snprintf(char *str, size_t size, const char *fmt, ...); diff --git a/stm/string0.c b/stm/string0.c index 2a5f25597..d67c5f2b1 100644 --- a/stm/string0.c +++ b/stm/string0.c @@ -108,3 +108,31 @@ char *strcat(char *dest, const char *src) { *d = '\0'; return dest; } + +// Public Domain implementation of strchr from: +// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strchr_function +char *strchr(const char *s, int c) +{ + /* Scan s for the character. When this loop is finished, + s will either point to the end of the string or the + character we were looking for. */ + while (*s != '\0' && *s != (char)c) + s++; + return ((*s == c) ? (char *) s : 0); +} + + +// Public Domain implementation of strstr from: +// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strstr_function +char *strstr(const char *haystack, const char *needle) +{ + size_t needlelen; + /* Check for the null needle case. */ + if (*needle == '\0') + return (char *) haystack; + needlelen = strlen(needle); + for (; (haystack = strchr(haystack, *needle)) != 0; haystack++) + if (strncmp(haystack, needle, needlelen) == 0) + return (char *) haystack; + return 0; +} diff --git a/stm/timer.c b/stm/timer.c index 2605d4b4b..c665a461d 100644 --- a/stm/timer.c +++ b/stm/timer.c @@ -72,10 +72,10 @@ void timer_init(void) { // Python interface mp_obj_t m = mp_obj_new_module(qstr_from_str_static("timer")); - rt_store_attr(m, qstr_from_str_static("callback"), rt_make_function_1(timer_py_set_callback)); - rt_store_attr(m, qstr_from_str_static("period"), rt_make_function_1(timer_py_set_period)); - rt_store_attr(m, qstr_from_str_static("prescaler"), rt_make_function_1(timer_py_set_prescaler)); - rt_store_attr(m, qstr_from_str_static("value"), rt_make_function_0(timer_py_get_value)); + rt_store_attr(m, qstr_from_str_static("callback"), rt_make_function_n(1, timer_py_set_callback)); + rt_store_attr(m, qstr_from_str_static("period"), rt_make_function_n(1, timer_py_set_period)); + rt_store_attr(m, qstr_from_str_static("prescaler"), rt_make_function_n(1, timer_py_set_prescaler)); + rt_store_attr(m, qstr_from_str_static("value"), rt_make_function_n(0, timer_py_get_value)); rt_store_name(qstr_from_str_static("timer"), m); }