@@ -2789,14 +2789,17 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
27892789
27902790void compile_scope_func_lambda_param (compiler_t * comp , mp_parse_node_t pn , pn_kind_t pn_name , pn_kind_t pn_star , pn_kind_t pn_dbl_star , bool allow_annotations ) {
27912791 // TODO verify that *k and **k are last etc
2792- qstr param_name = 0 ;
2792+ qstr param_name = MP_QSTR_NULL ;
2793+ uint param_flag = ID_FLAG_IS_PARAM ;
27932794 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL ;
27942795 if (MP_PARSE_NODE_IS_ID (pn )) {
27952796 param_name = MP_PARSE_NODE_LEAF_ARG (pn );
27962797 if (comp -> have_star ) {
2797- // comes after a bare star, so doesn't count as a parameter
2798+ // comes after a star, so counts as a keyword-only parameter
2799+ comp -> scope_cur -> num_kwonly_args += 1 ;
27982800 } else {
2799- comp -> scope_cur -> num_params += 1 ;
2801+ // comes before a star, so counts as a positional parameter
2802+ comp -> scope_cur -> num_pos_args += 1 ;
28002803 }
28012804 } else {
28022805 assert (MP_PARSE_NODE_IS_STRUCT (pn ));
@@ -2822,12 +2825,15 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
28222825 }
28232826 */
28242827 if (comp -> have_star ) {
2825- // comes after a bare star, so doesn't count as a parameter
2828+ // comes after a star, so counts as a keyword-only parameter
2829+ comp -> scope_cur -> num_kwonly_args += 1 ;
28262830 } else {
2827- comp -> scope_cur -> num_params += 1 ;
2831+ // comes before a star, so counts as a positional parameter
2832+ comp -> scope_cur -> num_pos_args += 1 ;
28282833 }
28292834 } else if (MP_PARSE_NODE_STRUCT_KIND (pns ) == pn_star ) {
28302835 comp -> have_star = true;
2836+ param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM ;
28312837 if (MP_PARSE_NODE_IS_NULL (pns -> nodes [0 ])) {
28322838 // bare star
28332839 // TODO see http://www.python.org/dev/peps/pep-3102/
@@ -2848,6 +2854,7 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
28482854 }
28492855 } else if (MP_PARSE_NODE_STRUCT_KIND (pns ) == pn_dbl_star ) {
28502856 param_name = MP_PARSE_NODE_LEAF_ARG (pns -> nodes [0 ]);
2857+ param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM ;
28512858 if (allow_annotations && !MP_PARSE_NODE_IS_NULL (pns -> nodes [1 ])) {
28522859 // this parameter has an annotation
28532860 pn_annotation = pns -> nodes [1 ];
@@ -2859,7 +2866,7 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
28592866 }
28602867 }
28612868
2862- if (param_name != 0 ) {
2869+ if (param_name != MP_QSTR_NULL ) {
28632870 if (!MP_PARSE_NODE_IS_NULL (pn_annotation )) {
28642871 // TODO this parameter has an annotation
28652872 }
@@ -2870,15 +2877,15 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
28702877 return ;
28712878 }
28722879 id_info -> kind = ID_INFO_KIND_LOCAL ;
2873- id_info -> flags |= ID_FLAG_IS_PARAM ;
2880+ id_info -> flags = param_flag ;
28742881 }
28752882}
28762883
2877- void compile_scope_func_param (compiler_t * comp , mp_parse_node_t pn ) {
2884+ STATIC void compile_scope_func_param (compiler_t * comp , mp_parse_node_t pn ) {
28782885 compile_scope_func_lambda_param (comp , pn , PN_typedargslist_name , PN_typedargslist_star , PN_typedargslist_dbl_star , true);
28792886}
28802887
2881- void compile_scope_lambda_param (compiler_t * comp , mp_parse_node_t pn ) {
2888+ STATIC void compile_scope_lambda_param (compiler_t * comp , mp_parse_node_t pn ) {
28822889 compile_scope_func_lambda_param (comp , pn , PN_varargslist_name , PN_varargslist_star , PN_varargslist_dbl_star , false);
28832890}
28842891
@@ -3051,7 +3058,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
30513058 id_info_t * id_info = scope_find_or_add_id (comp -> scope_cur , qstr_arg , & added );
30523059 assert (added );
30533060 id_info -> kind = ID_INFO_KIND_LOCAL ;
3054- scope -> num_params = 1 ;
3061+ scope -> num_pos_args = 1 ;
30553062 }
30563063
30573064 if (scope -> kind == SCOPE_LIST_COMP ) {
@@ -3144,7 +3151,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
31443151 if (comp -> pass == PASS_2 ) {
31453152 mp_parse_node_t * pn_params ;
31463153 int n_params = list_get (& pns -> nodes [1 ], PN_typedargslist , & pn_params );
3147- scope -> num_params = EMIT_INLINE_ASM_ARG (count_params , n_params , pn_params );
3154+ scope -> num_pos_args = EMIT_INLINE_ASM_ARG (count_params , n_params , pn_params );
31483155 }
31493156
31503157 assert (MP_PARSE_NODE_IS_NULL (pns -> nodes [2 ])); // type
@@ -3235,6 +3242,25 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
32353242#endif
32363243
32373244STATIC void compile_scope_compute_things (compiler_t * comp , scope_t * scope ) {
3245+ #if !MICROPY_EMIT_CPYTHON
3246+ // in Micro Python we put the *x parameter after all other parameters (except **y)
3247+ if (scope -> scope_flags & MP_SCOPE_FLAG_VARARGS ) {
3248+ id_info_t * id_param = NULL ;
3249+ for (int i = scope -> id_info_len - 1 ; i >= 0 ; i -- ) {
3250+ id_info_t * id = & scope -> id_info [i ];
3251+ if (id -> flags & ID_FLAG_IS_STAR_PARAM ) {
3252+ if (id_param != NULL ) {
3253+ // swap star param with last param
3254+ id_info_t temp = * id_param ; * id_param = * id ; * id = temp ;
3255+ }
3256+ break ;
3257+ } else if (id_param == NULL && id -> flags == ID_FLAG_IS_PARAM ) {
3258+ id_param = id ;
3259+ }
3260+ }
3261+ }
3262+ #endif
3263+
32383264 // in functions, turn implicit globals into explicit globals
32393265 // compute the index of each local
32403266 scope -> num_locals = 0 ;
@@ -3247,10 +3273,9 @@ STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
32473273 if (scope -> kind >= SCOPE_FUNCTION && scope -> kind <= SCOPE_GEN_EXPR && id -> kind == ID_INFO_KIND_GLOBAL_IMPLICIT ) {
32483274 id -> kind = ID_INFO_KIND_GLOBAL_EXPLICIT ;
32493275 }
3250- // note: params always count for 1 local, even if they are a cell
3276+ // params always count for 1 local, even if they are a cell
32513277 if (id -> kind == ID_INFO_KIND_LOCAL || (id -> flags & ID_FLAG_IS_PARAM )) {
3252- id -> local_num = scope -> num_locals ;
3253- scope -> num_locals += 1 ;
3278+ id -> local_num = scope -> num_locals ++ ;
32543279 }
32553280 }
32563281
@@ -3309,7 +3334,7 @@ STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
33093334 id -> local_num += num_free ;
33103335 }
33113336 }
3312- scope -> num_params += num_free ; // free vars are counted as params for passing them into the function
3337+ scope -> num_pos_args += num_free ; // free vars are counted as params for passing them into the function
33133338 scope -> num_locals += num_free ;
33143339 }
33153340#endif
0 commit comments