@@ -56,6 +56,7 @@ typedef enum {
56
56
#include "grammar.h"
57
57
#undef DEF_RULE
58
58
PN_maximum_number_of ,
59
+ PN_string , // special node for non-interned string
59
60
} pn_kind_t ;
60
61
61
62
#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
177
178
}
178
179
break ;
179
180
#endif
181
+ case PN_string :
182
+ return pn ;
180
183
}
181
184
182
185
// fold arguments
@@ -426,6 +429,9 @@ void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
426
429
427
430
#if MICROPY_EMIT_CPYTHON
428
431
STATIC 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
+ }
429
435
if (!MP_PARSE_NODE_IS_LEAF (pn )) {
430
436
return false;
431
437
}
@@ -435,9 +441,7 @@ STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
435
441
return true;
436
442
}
437
443
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 ) {
441
445
bool has_single_quote = false;
442
446
bool has_double_quote = false;
443
447
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) {
476
480
}
477
481
478
482
STATIC 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
+
479
489
assert (MP_PARSE_NODE_IS_LEAF (pn ));
480
490
if (MP_PARSE_NODE_IS_SMALL_INT (pn )) {
481
491
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
487
497
case MP_PARSE_NODE_ID : assert (0 );
488
498
case MP_PARSE_NODE_INTEGER : vstr_printf (vstr , "%s" , qstr_str (arg )); break ;
489
499
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
+ }
492
507
case MP_PARSE_NODE_TOKEN :
493
508
switch (arg ) {
494
509
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) {
2058
2073
2059
2074
} else {
2060
2075
// 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 )) {
2062
2078
// do nothing with a lonely constant
2063
2079
} else {
2064
2080
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) {
2498
2514
int n_bytes = 0 ;
2499
2515
int string_kind = MP_PARSE_NODE_NULL ;
2500
2516
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
+ }
2504
2529
if (i == 0 ) {
2505
2530
string_kind = pn_kind ;
2506
2531
} else if (pn_kind != string_kind ) {
2507
2532
compile_syntax_error (comp , (mp_parse_node_t )pns , "cannot mix bytes and nonbytes literals" );
2508
2533
return ;
2509
2534
}
2510
- n_bytes += qstr_len (MP_PARSE_NODE_LEAF_ARG (pns -> nodes [i ]));
2511
2535
}
2512
2536
2513
2537
// concatenate string/bytes
2514
2538
byte * q_ptr ;
2515
2539
byte * s_dest = qstr_build_start (n_bytes , & q_ptr );
2516
2540
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
+ }
2521
2551
}
2522
2552
qstr q = qstr_build_end (q_ptr );
2523
2553
@@ -2848,15 +2878,19 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2848
2878
} else {
2849
2879
mp_parse_node_struct_t * pns = (mp_parse_node_struct_t * )pn ;
2850
2880
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 ));
2854
2887
#if MICROPY_DEBUG_PRINTERS
2855
- mp_parse_node_print (pn , 0 );
2888
+ mp_parse_node_print (pn , 0 );
2856
2889
#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
+ }
2860
2894
}
2861
2895
}
2862
2896
}
@@ -3033,13 +3067,13 @@ STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3033
3067
// check the first statement for a doc string
3034
3068
if (MP_PARSE_NODE_IS_STRUCT_KIND (pn , PN_expr_stmt )) {
3035
3069
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
3041
3076
EMIT_ARG (store_id , MP_QSTR___doc__ );
3042
- }
3043
3077
}
3044
3078
}
3045
3079
#endif
0 commit comments