@@ -56,6 +56,7 @@ typedef enum {
5656#include "grammar.h"
5757#undef DEF_RULE
5858 PN_maximum_number_of ,
59+ PN_string , // special node for non-interned string
5960} pn_kind_t ;
6061
6162#define EMIT (fun ) (comp->emit_method_table->fun(comp->emit))
@@ -177,6 +178,8 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
177178 }
178179 break ;
179180#endif
181+ case PN_string :
182+ return pn ;
180183 }
181184
182185 // fold arguments
@@ -426,6 +429,9 @@ void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
426429
427430#if MICROPY_EMIT_CPYTHON
428431STATIC bool cpython_c_tuple_is_const (mp_parse_node_t pn ) {
432+ if (MP_PARSE_NODE_IS_STRUCT_KIND (pn , PN_string )) {
433+ return true;
434+ }
429435 if (!MP_PARSE_NODE_IS_LEAF (pn )) {
430436 return false;
431437 }
@@ -435,9 +441,7 @@ STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
435441 return true;
436442}
437443
438- STATIC void cpython_c_print_quoted_str (vstr_t * vstr , qstr qstr , bool bytes ) {
439- uint len ;
440- const byte * str = qstr_data (qstr , & len );
444+ STATIC void cpython_c_print_quoted_str (vstr_t * vstr , const char * str , uint len , bool bytes ) {
441445 bool has_single_quote = false;
442446 bool has_double_quote = false;
443447 for (int i = 0 ; i < len ; i ++ ) {
@@ -476,6 +480,12 @@ STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
476480}
477481
478482STATIC void cpython_c_tuple_emit_const (compiler_t * comp , mp_parse_node_t pn , vstr_t * vstr ) {
483+ if (MP_PARSE_NODE_IS_STRUCT_KIND (pn , PN_string )) {
484+ mp_parse_node_struct_t * pns = (mp_parse_node_struct_t * )pn ;
485+ cpython_c_print_quoted_str (vstr , (const char * )pns -> nodes [0 ], (machine_uint_t )pns -> nodes [1 ], false);
486+ return ;
487+ }
488+
479489 assert (MP_PARSE_NODE_IS_LEAF (pn ));
480490 if (MP_PARSE_NODE_IS_SMALL_INT (pn )) {
481491 vstr_printf (vstr , INT_FMT , MP_PARSE_NODE_LEAF_SMALL_INT (pn ));
@@ -487,8 +497,13 @@ STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vst
487497 case MP_PARSE_NODE_ID : assert (0 );
488498 case MP_PARSE_NODE_INTEGER : vstr_printf (vstr , "%s" , qstr_str (arg )); break ;
489499 case MP_PARSE_NODE_DECIMAL : vstr_printf (vstr , "%s" , qstr_str (arg )); break ;
490- case MP_PARSE_NODE_STRING : cpython_c_print_quoted_str (vstr , arg , false); break ;
491- case MP_PARSE_NODE_BYTES : cpython_c_print_quoted_str (vstr , arg , true); break ;
500+ case MP_PARSE_NODE_STRING :
501+ case MP_PARSE_NODE_BYTES : {
502+ uint len ;
503+ const byte * str = qstr_data (arg , & len );
504+ cpython_c_print_quoted_str (vstr , (const char * )str , len , MP_PARSE_NODE_LEAF_KIND (pn ) == MP_PARSE_NODE_BYTES );
505+ break ;
506+ }
492507 case MP_PARSE_NODE_TOKEN :
493508 switch (arg ) {
494509 case MP_TOKEN_KW_FALSE : vstr_printf (vstr , "False" ); break ;
@@ -2058,7 +2073,8 @@ void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
20582073
20592074 } else {
20602075 // for non-REPL, evaluate then discard the expression
2061- if (MP_PARSE_NODE_IS_LEAF (pns -> nodes [0 ]) && !MP_PARSE_NODE_IS_ID (pns -> nodes [0 ])) {
2076+ if ((MP_PARSE_NODE_IS_LEAF (pns -> nodes [0 ]) && !MP_PARSE_NODE_IS_ID (pns -> nodes [0 ]))
2077+ || MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [0 ], PN_string )) {
20622078 // do nothing with a lonely constant
20632079 } else {
20642080 compile_node (comp , pns -> nodes [0 ]); // just an expression
@@ -2498,26 +2514,40 @@ void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
24982514 int n_bytes = 0 ;
24992515 int string_kind = MP_PARSE_NODE_NULL ;
25002516 for (int i = 0 ; i < n ; i ++ ) {
2501- assert (MP_PARSE_NODE_IS_LEAF (pns -> nodes [i ]));
2502- int pn_kind = MP_PARSE_NODE_LEAF_KIND (pns -> nodes [i ]);
2503- assert (pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES );
2517+ int pn_kind ;
2518+ if (MP_PARSE_NODE_IS_LEAF (pns -> nodes [i ])) {
2519+ pn_kind = MP_PARSE_NODE_LEAF_KIND (pns -> nodes [i ]);
2520+ assert (pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES );
2521+ n_bytes += qstr_len (MP_PARSE_NODE_LEAF_ARG (pns -> nodes [i ]));
2522+ } else {
2523+ assert (MP_PARSE_NODE_IS_STRUCT (pns -> nodes [i ]));
2524+ mp_parse_node_struct_t * pns_string = (mp_parse_node_struct_t * )pns -> nodes [i ];
2525+ assert (MP_PARSE_NODE_STRUCT_KIND (pns_string ) == PN_string );
2526+ pn_kind = MP_PARSE_NODE_STRING ;
2527+ n_bytes += (machine_uint_t )pns_string -> nodes [1 ];
2528+ }
25042529 if (i == 0 ) {
25052530 string_kind = pn_kind ;
25062531 } else if (pn_kind != string_kind ) {
25072532 compile_syntax_error (comp , (mp_parse_node_t )pns , "cannot mix bytes and nonbytes literals" );
25082533 return ;
25092534 }
2510- n_bytes += qstr_len (MP_PARSE_NODE_LEAF_ARG (pns -> nodes [i ]));
25112535 }
25122536
25132537 // concatenate string/bytes
25142538 byte * q_ptr ;
25152539 byte * s_dest = qstr_build_start (n_bytes , & q_ptr );
25162540 for (int i = 0 ; i < n ; i ++ ) {
2517- uint s_len ;
2518- const byte * s = qstr_data (MP_PARSE_NODE_LEAF_ARG (pns -> nodes [i ]), & s_len );
2519- memcpy (s_dest , s , s_len );
2520- s_dest += s_len ;
2541+ if (MP_PARSE_NODE_IS_LEAF (pns -> nodes [i ])) {
2542+ uint s_len ;
2543+ const byte * s = qstr_data (MP_PARSE_NODE_LEAF_ARG (pns -> nodes [i ]), & s_len );
2544+ memcpy (s_dest , s , s_len );
2545+ s_dest += s_len ;
2546+ } else {
2547+ mp_parse_node_struct_t * pns_string = (mp_parse_node_struct_t * )pns -> nodes [i ];
2548+ memcpy (s_dest , (const char * )pns_string -> nodes [0 ], (machine_uint_t )pns_string -> nodes [1 ]);
2549+ s_dest += (machine_uint_t )pns_string -> nodes [1 ];
2550+ }
25212551 }
25222552 qstr q = qstr_build_end (q_ptr );
25232553
@@ -2848,15 +2878,19 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
28482878 } else {
28492879 mp_parse_node_struct_t * pns = (mp_parse_node_struct_t * )pn ;
28502880 EMIT_ARG (set_line_number , pns -> source_line );
2851- compile_function_t f = compile_function [MP_PARSE_NODE_STRUCT_KIND (pns )];
2852- if (f == NULL ) {
2853- printf ("node %u cannot be compiled\n" , (uint )MP_PARSE_NODE_STRUCT_KIND (pns ));
2881+ if (MP_PARSE_NODE_STRUCT_KIND (pns ) == PN_string ) {
2882+ EMIT_ARG (load_const_str , qstr_from_strn ((const char * )pns -> nodes [0 ], (machine_uint_t )pns -> nodes [1 ]), false);
2883+ } else {
2884+ compile_function_t f = compile_function [MP_PARSE_NODE_STRUCT_KIND (pns )];
2885+ if (f == NULL ) {
2886+ printf ("node %u cannot be compiled\n" , (uint )MP_PARSE_NODE_STRUCT_KIND (pns ));
28542887#if MICROPY_DEBUG_PRINTERS
2855- mp_parse_node_print (pn , 0 );
2888+ mp_parse_node_print (pn , 0 );
28562889#endif
2857- compile_syntax_error (comp , pn , "internal compiler error" );
2858- } else {
2859- f (comp , pns );
2890+ compile_syntax_error (comp , pn , "internal compiler error" );
2891+ } else {
2892+ f (comp , pns );
2893+ }
28602894 }
28612895 }
28622896}
@@ -3033,13 +3067,13 @@ STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
30333067 // check the first statement for a doc string
30343068 if (MP_PARSE_NODE_IS_STRUCT_KIND (pn , PN_expr_stmt )) {
30353069 mp_parse_node_struct_t * pns = (mp_parse_node_struct_t * )pn ;
3036- if (MP_PARSE_NODE_IS_LEAF (pns -> nodes [0 ])) {
3037- int kind = MP_PARSE_NODE_LEAF_KIND (pns -> nodes [0 ]);
3038- if (kind == MP_PARSE_NODE_STRING ) {
3039- compile_node (comp , pns -> nodes [0 ]); // a doc string
3040- // store doc string
3070+ if ((MP_PARSE_NODE_IS_LEAF (pns -> nodes [0 ])
3071+ && MP_PARSE_NODE_LEAF_KIND (pns -> nodes [0 ]) == MP_PARSE_NODE_STRING )
3072+ || MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [0 ], PN_string )) {
3073+ // compile the doc string
3074+ compile_node (comp , pns -> nodes [0 ]);
3075+ // store the doc string
30413076 EMIT_ARG (store_id , MP_QSTR___doc__ );
3042- }
30433077 }
30443078 }
30453079#endif
0 commit comments