@@ -156,22 +156,23 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
156156#if MICROPY_COMP_CONST
157157 case PN_expr_stmt :
158158 if (!MP_PARSE_NODE_IS_NULL (pns -> nodes [1 ])) {
159- mp_parse_node_struct_t * pns1 = (mp_parse_node_struct_t * )pns -> nodes [1 ];
160- if (MP_PARSE_NODE_STRUCT_KIND (pns1 ) == PN_expr_stmt_assign ) {
159+ if (!(MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [1 ], PN_expr_stmt_augassign )
160+ || MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [1 ], PN_expr_stmt_assign_list ))) {
161+ // this node is of the form <x> = <y>
161162 if (MP_PARSE_NODE_IS_ID (pns -> nodes [0 ])
162- && MP_PARSE_NODE_IS_STRUCT_KIND (pns1 -> nodes [0 ], PN_power )
163- && MP_PARSE_NODE_IS_ID (((mp_parse_node_struct_t * )pns1 -> nodes [0 ])-> nodes [0 ])
164- && MP_PARSE_NODE_LEAF_ARG (((mp_parse_node_struct_t * )pns1 -> nodes [0 ])-> nodes [0 ]) == MP_QSTR_const
165- && MP_PARSE_NODE_IS_STRUCT_KIND (((mp_parse_node_struct_t * )pns1 -> nodes [0 ])-> nodes [1 ], PN_trailer_paren )
166- && MP_PARSE_NODE_IS_NULL (((mp_parse_node_struct_t * )pns1 -> nodes [0 ])-> nodes [2 ])
163+ && MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [1 ], PN_power )
164+ && MP_PARSE_NODE_IS_ID (((mp_parse_node_struct_t * )pns -> nodes [1 ])-> nodes [0 ])
165+ && MP_PARSE_NODE_LEAF_ARG (((mp_parse_node_struct_t * )pns -> nodes [1 ])-> nodes [0 ]) == MP_QSTR_const
166+ && MP_PARSE_NODE_IS_STRUCT_KIND (((mp_parse_node_struct_t * )pns -> nodes [1 ])-> nodes [1 ], PN_trailer_paren )
167+ && MP_PARSE_NODE_IS_NULL (((mp_parse_node_struct_t * )pns -> nodes [1 ])-> nodes [2 ])
167168 ) {
168169 // code to assign dynamic constants: id = const(value)
169170
170171 // get the id
171172 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG (pns -> nodes [0 ]);
172173
173174 // get the value
174- mp_parse_node_t pn_value = ((mp_parse_node_struct_t * )((mp_parse_node_struct_t * )pns1 -> nodes [0 ])-> nodes [1 ])-> nodes [0 ];
175+ mp_parse_node_t pn_value = ((mp_parse_node_struct_t * )((mp_parse_node_struct_t * )pns -> nodes [1 ])-> nodes [1 ])-> nodes [0 ];
175176 pn_value = fold_constants (comp , pn_value , consts );
176177 if (!MP_PARSE_NODE_IS_SMALL_INT (pn_value )) {
177178 compile_syntax_error (comp , (mp_parse_node_t )pns , "constant must be an integer" );
@@ -188,7 +189,7 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
188189 elem -> value = MP_OBJ_NEW_SMALL_INT (value );
189190
190191 // replace const(value) with value
191- pns1 -> nodes [0 ] = pn_value ;
192+ pns -> nodes [1 ] = pn_value ;
192193
193194 // finished folding this assignment
194195 return pn ;
@@ -1885,7 +1886,7 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
18851886 EMIT (pop_top ); // discard last result since this is a statement and leaves nothing on the stack
18861887 }
18871888 }
1888- } else {
1889+ } else if ( MP_PARSE_NODE_IS_STRUCT ( pns -> nodes [ 1 ])) {
18891890 mp_parse_node_struct_t * pns1 = (mp_parse_node_struct_t * )pns -> nodes [1 ];
18901891 int kind = MP_PARSE_NODE_STRUCT_KIND (pns1 );
18911892 if (kind == PN_expr_stmt_augassign ) {
@@ -1911,7 +1912,7 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
19111912 c_assign (comp , pns -> nodes [0 ], ASSIGN_AUG_STORE ); // lhs store for aug assign
19121913 } else if (kind == PN_expr_stmt_assign_list ) {
19131914 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES (pns1 ) - 1 ;
1914- compile_node (comp , (( mp_parse_node_struct_t * ) pns1 -> nodes [rhs ]) -> nodes [ 0 ]); // rhs
1915+ compile_node (comp , pns1 -> nodes [rhs ]); // rhs
19151916 // following CPython, we store left-most first
19161917 if (rhs > 0 ) {
19171918 EMIT (dup_top );
@@ -1921,17 +1922,17 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
19211922 if (i + 1 < rhs ) {
19221923 EMIT (dup_top );
19231924 }
1924- c_assign (comp , (( mp_parse_node_struct_t * ) pns1 -> nodes [i ]) -> nodes [ 0 ], ASSIGN_STORE ); // middle store
1925+ c_assign (comp , pns1 -> nodes [i ], ASSIGN_STORE ); // middle store
19251926 }
19261927 } else {
1927- assert ( kind == PN_expr_stmt_assign ); // should be
1928+ plain_assign :
19281929 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
1929- && MP_PARSE_NODE_IS_STRUCT_KIND (pns1 -> nodes [0 ], PN_testlist_star_expr )
1930+ && MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [1 ], PN_testlist_star_expr )
19301931 && MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [0 ], PN_testlist_star_expr )
1931- && MP_PARSE_NODE_STRUCT_NUM_NODES ((mp_parse_node_struct_t * )pns1 -> nodes [0 ]) == 2
1932+ && MP_PARSE_NODE_STRUCT_NUM_NODES ((mp_parse_node_struct_t * )pns -> nodes [1 ]) == 2
19321933 && MP_PARSE_NODE_STRUCT_NUM_NODES ((mp_parse_node_struct_t * )pns -> nodes [0 ]) == 2 ) {
19331934 // optimisation for a, b = c, d
1934- mp_parse_node_struct_t * pns10 = (mp_parse_node_struct_t * )pns1 -> nodes [0 ];
1935+ mp_parse_node_struct_t * pns10 = (mp_parse_node_struct_t * )pns -> nodes [1 ];
19351936 mp_parse_node_struct_t * pns0 = (mp_parse_node_struct_t * )pns -> nodes [0 ];
19361937 if (MP_PARSE_NODE_IS_STRUCT_KIND (pns0 -> nodes [0 ], PN_star_expr )
19371938 || MP_PARSE_NODE_IS_STRUCT_KIND (pns0 -> nodes [1 ], PN_star_expr )) {
@@ -1944,12 +1945,12 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
19441945 c_assign (comp , pns0 -> nodes [0 ], ASSIGN_STORE ); // lhs store
19451946 c_assign (comp , pns0 -> nodes [1 ], ASSIGN_STORE ); // lhs store
19461947 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
1947- && MP_PARSE_NODE_IS_STRUCT_KIND (pns1 -> nodes [0 ], PN_testlist_star_expr )
1948+ && MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [1 ], PN_testlist_star_expr )
19481949 && MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [0 ], PN_testlist_star_expr )
1949- && MP_PARSE_NODE_STRUCT_NUM_NODES ((mp_parse_node_struct_t * )pns1 -> nodes [0 ]) == 3
1950+ && MP_PARSE_NODE_STRUCT_NUM_NODES ((mp_parse_node_struct_t * )pns -> nodes [1 ]) == 3
19501951 && MP_PARSE_NODE_STRUCT_NUM_NODES ((mp_parse_node_struct_t * )pns -> nodes [0 ]) == 3 ) {
19511952 // optimisation for a, b, c = d, e, f
1952- mp_parse_node_struct_t * pns10 = (mp_parse_node_struct_t * )pns1 -> nodes [0 ];
1953+ mp_parse_node_struct_t * pns10 = (mp_parse_node_struct_t * )pns -> nodes [1 ];
19531954 mp_parse_node_struct_t * pns0 = (mp_parse_node_struct_t * )pns -> nodes [0 ];
19541955 if (MP_PARSE_NODE_IS_STRUCT_KIND (pns0 -> nodes [0 ], PN_star_expr )
19551956 || MP_PARSE_NODE_IS_STRUCT_KIND (pns0 -> nodes [1 ], PN_star_expr )
@@ -1967,10 +1968,12 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
19671968 c_assign (comp , pns0 -> nodes [2 ], ASSIGN_STORE ); // lhs store
19681969 } else {
19691970 no_optimisation :
1970- compile_node (comp , pns1 -> nodes [0 ]); // rhs
1971+ compile_node (comp , pns -> nodes [1 ]); // rhs
19711972 c_assign (comp , pns -> nodes [0 ], ASSIGN_STORE ); // lhs store
19721973 }
19731974 }
1975+ } else {
1976+ goto plain_assign ;
19741977 }
19751978}
19761979
@@ -2228,18 +2231,15 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar
22282231 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE ;
22292232 dblstar_args_node = pns_arg ;
22302233 } else if (MP_PARSE_NODE_STRUCT_KIND (pns_arg ) == PN_argument ) {
2231- assert (MP_PARSE_NODE_IS_STRUCT (pns_arg -> nodes [1 ])); // should always be
2232- mp_parse_node_struct_t * pns2 = (mp_parse_node_struct_t * )pns_arg -> nodes [1 ];
2233- if (MP_PARSE_NODE_STRUCT_KIND (pns2 ) == PN_argument_3 ) {
2234+ if (!MP_PARSE_NODE_IS_STRUCT_KIND (pns_arg -> nodes [1 ], PN_comp_for )) {
22342235 if (!MP_PARSE_NODE_IS_ID (pns_arg -> nodes [0 ])) {
22352236 compile_syntax_error (comp , (mp_parse_node_t )pns_arg , "LHS of keyword arg must be an id" );
22362237 return ;
22372238 }
22382239 EMIT_ARG (load_const_str , MP_PARSE_NODE_LEAF_ARG (pns_arg -> nodes [0 ]));
2239- compile_node (comp , pns2 -> nodes [0 ]);
2240+ compile_node (comp , pns_arg -> nodes [1 ]);
22402241 n_keyword += 1 ;
22412242 } else {
2242- assert (MP_PARSE_NODE_STRUCT_KIND (pns2 ) == PN_comp_for ); // should always be
22432243 compile_comprehension (comp , pns_arg , SCOPE_GEN_EXPR );
22442244 n_positional ++ ;
22452245 }
0 commit comments