@@ -2789,14 +2789,17 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2789
2789
2790
2790
void 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 ) {
2791
2791
// 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 ;
2793
2794
mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL ;
2794
2795
if (MP_PARSE_NODE_IS_ID (pn )) {
2795
2796
param_name = MP_PARSE_NODE_LEAF_ARG (pn );
2796
2797
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 ;
2798
2800
} 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 ;
2800
2803
}
2801
2804
} else {
2802
2805
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
2822
2825
}
2823
2826
*/
2824
2827
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 ;
2826
2830
} 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 ;
2828
2833
}
2829
B422
2834
} else if (MP_PARSE_NODE_STRUCT_KIND (pns ) == pn_star ) {
2830
2835
comp -> have_star = true;
2836
+ param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM ;
2831
2837
if (MP_PARSE_NODE_IS_NULL (pns -> nodes [0 ])) {
2832
2838
// bare star
2833
2839
// 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
2848
2854
}
2849
2855
} else if (MP_PARSE_NODE_STRUCT_KIND (pns ) == pn_dbl_star ) {
2850
2856
param_name = MP_PARSE_NODE_LEAF_ARG (pns -> nodes [0 ]);
2857
+ param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM ;
2851
2858
if (allow_annotations && !MP_PARSE_NODE_IS_NULL (pns -> nodes [1 ])) {
2852
2859
// this parameter has an annotation
2853
2860
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
2859
2866
}
2860
2867
}
2861
2868
2862
- if (param_name != 0 ) {
2869
+ if (param_name != MP_QSTR_NULL ) {
2863
2870
if (!MP_PARSE_NODE_IS_NULL (pn_annotation )) {
2864
2871
// TODO this parameter has an annotation
2865
2872
}
@@ -2870,15 +2877,15 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
2870
2877
return ;
2871
2878
}
2872
2879
id_info -> kind = ID_INFO_KIND_LOCAL ;
2873
- id_info -> flags |= ID_FLAG_IS_PARAM ;
2880
+ id_info -> flags = param_flag ;
2874
2881
}
2875
2882
}
2876
2883
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 ) {
2878
2885
compile_scope_func_lambda_param (comp , pn , PN_typedargslist_name , PN_typedargslist_star , PN_typedargslist_dbl_star , true);
2879
2886
}
2880
2887
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 ) {
2882
2889
compile_scope_func_lambda_param (comp , pn , PN_varargslist_name , PN_varargslist_star , PN_varargslist_dbl_star , false);
2883
2890
}
2884
2891
@@ -3051,7 +3058,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3051
3058
id_info_t * id_info = scope_find_or_add_id (comp -> scope_cur , qstr_arg , & added );
3052
3059
assert (added );
3053
3060
id_info -> kind = ID_INFO_KIND_LOCAL ;
3054
- scope -> num_params = 1 ;
3061
+ scope -> num_pos_args = 1 ;
3055
3062
}
3056
3063
3057
3064
if (scope -> kind == SCOPE_LIST_COMP ) {
@@ -3144,7 +3151,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
3144
3151
if (comp -> pass == PASS_2 ) {
3145
3152
mp_parse_node_t * pn_params ;
3146
3153
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 );
3148
3155
}
3149
3156
3150
3157
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
3235
3242
#endif
3236
3243
3237
3244
STATIC 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
+
3238
3264
// in functions, turn implicit globals into explicit globals
3239
3265
// compute the index of each local
3240
3266
scope -> num_locals = 0 ;
@@ -3247,10 +3273,9 @@ STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3247
3273
if (scope -> kind >= SCOPE_FUNCTION && scope -> kind <= SCOPE_GEN_EXPR && id -> kind == ID_INFO_KIND_GLOBAL_IMPLICIT ) {
3248
3274
id -> kind = ID_INFO_KIND_GLOBAL_EXPLICIT ;
3249
3275
}
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
3251
3277
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 ++ ;
3254
3279
}
3255
3280
}
3256
3281
@@ -3309,7 +3334,7 @@ STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3309
3334
id -> local_num += num_free ;
3310
3335
}
3311
3336
}
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
3313
3338
scope -> num_locals += num_free ;
3314
3339
}
3315
3340
#endif
0 commit comments