|
38 | 38 | #include "py/runtime0.h"
|
39 | 39 | #include "py/runtime.h"
|
40 | 40 | #include "py/objint.h"
|
| 41 | +#include "py/objstr.h" |
41 | 42 | #include "py/builtin.h"
|
42 | 43 |
|
43 | 44 | #if MICROPY_ENABLE_COMPILER
|
|
75 | 76 | #include "py/grammar.h"
|
76 | 77 | #undef DEF_RULE
|
77 | 78 | #undef DEF_RULE_NC
|
78 |
| - RULE_string, // special node for non-interned string |
79 |
| - RULE_bytes, // special node for non-interned bytes |
80 | 79 | RULE_const_object, // special node for a constant, generic Python object
|
81 | 80 |
|
82 | 81 | // define rules without a compile function
|
@@ -123,8 +122,6 @@ STATIC const rule_t *const rules[] = {
|
123 | 122 | #include "py/grammar.h"
|
124 | 123 | #undef DEF_RULE
|
125 | 124 | #undef DEF_RULE_NC
|
126 |
| - NULL, // RULE_string |
127 |
| - NULL, // RULE_bytes |
128 | 125 | NULL, // RULE_const_object
|
129 | 126 |
|
130 | 127 | // define rules without a compile function
|
@@ -326,11 +323,7 @@ void mp_parse_node_print(mp_parse_node_t pn, size_t indent) {
|
326 | 323 | } else {
|
327 | 324 | // node must be a mp_parse_node_struct_t
|
328 | 325 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
|
329 |
| - if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) { |
330 |
| - printf("literal str(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]); |
331 |
| - } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_bytes) { |
332 |
| - printf("literal bytes(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]); |
333 |
| - } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_const_object) { |
| 326 | + if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_const_object) { |
334 | 327 | #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
|
335 | 328 | printf("literal const(%016llx)\n", (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
|
336 | 329 | #else
|
@@ -392,21 +385,6 @@ STATIC void push_result_node(parser_t *parser, mp_parse_node_t pn) {
|
392 | 385 | parser->result_stack[parser->result_stack_top++] = pn;
|
393 | 386 | }
|
394 | 387 |
|
395 |
| -STATIC mp_parse_node_t make_node_string_bytes(parser_t *parser, size_t src_line, size_t rule_kind, const char *str, size_t len) { |
396 |
| - mp_parse_node_struct_t *pn = parser_alloc(parser, sizeof(mp_parse_node_struct_t) + sizeof(mp_parse_node_t) * 2); |
397 |
| - if (pn == NULL) { |
398 |
| - parser->parse_error = PARSE_ERROR_MEMORY; |
399 |
| - return MP_PARSE_NODE_NULL; |
400 |
| - } |
401 |
| - pn->source_line = src_line; |
402 |
| - pn->kind_num_nodes = rule_kind | (2 << 8); |
403 |
| - char *p = m_new(char, len); |
404 |
| - memcpy(p, str, len); |
405 |
| - pn->nodes[0] = (uintptr_t)p; |
406 |
| - pn->nodes[1] = len; |
407 |
| - return (mp_parse_node_t)pn; |
408 |
| -} |
409 |
| - |
410 | 388 | STATIC mp_parse_node_t make_node_const_object(parser_t *parser, size_t src_line, mp_obj_t obj) {
|
411 | 389 | mp_parse_node_struct_t *pn = parser_alloc(parser, sizeof(mp_parse_node_struct_t) + sizeof(mp_obj_t));
|
412 | 390 | if (pn == NULL) {
|
@@ -473,8 +451,11 @@ STATIC void push_result_token(parser_t *parser, const rule_t *rule) {
|
473 | 451 | // qstr exists, make a leaf node
|
474 | 452 | pn = mp_parse_node_new_leaf(lex->tok_kind == MP_TOKEN_STRING ? MP_PARSE_NODE_STRING : MP_PARSE_NODE_BYTES, qst);
|
475 | 453 | } else {
|
476 |
| - // not interned, make a node holding a pointer to the string/bytes data |
477 |
| - pn = make_node_string_bytes(parser, lex->tok_line, lex->tok_kind == MP_TOKEN_STRING ? RULE_string : RULE_bytes, lex->vstr.buf, lex->vstr.len); |
| 454 | + // not interned, make a node holding a pointer to the string/bytes object |
| 455 | + mp_obj_t o = mp_obj_new_str_of_type( |
| 456 | + lex->tok_kind == MP_TOKEN_STRING ? &mp_type_str : &mp_type_bytes, |
| 457 | + (const byte*)lex->vstr.buf, lex->vstr.len); |
| 458 | + pn = make_node_const_object(parser, lex->tok_line, o); |
478 | 459 | }
|
479 | 460 | } else {
|
480 | 461 | pn = mp_parse_node_new_leaf(MP_PARSE_NODE_TOKEN, lex->tok_kind);
|
@@ -934,15 +915,13 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
|
934 | 915 | // this code discards lonely statements, such as doc strings
|
935 | 916 | if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) {
|
936 | 917 | mp_parse_node_t p = peek_result(&parser, 1);
|
937 |
| - if ((MP_PARSE_NODE_IS_LEAF(p) && !MP_PARSE_NODE_IS_ID(p)) || MP_PARSE_NODE_IS_STRUCT_KIND(p, RULE_string)) { |
| 918 | + if ((MP_PARSE_NODE_IS_LEAF(p) && !MP_PARSE_NODE_IS_ID(p)) |
| 919 | + || MP_PARSE_NODE_IS_STRUCT_KIND(p, RULE_const_object)) { |
938 | 920 | pop_result(&parser); // MP_PARSE_NODE_NULL
|
939 |
| - mp_parse_node_t pn = pop_result(&parser); // possibly RULE_string |
940 |
| - if (MP_PARSE_NODE_IS_STRUCT(pn)) { |
941 |
| - mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn; |
942 |
| - if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) { |
943 |
| - m_del(char, (char*)pns->nodes[0], (size_t)pns->nodes[1]); |
944 |
| - } |
945 |
| - } |
| 921 | + pop_result(&parser); // const expression (leaf or RULE_const_object) |
| 922 | + // Pushing the "pass" rule here will overwrite any RULE_const_object |
| 923 | + // entry that was on the result stack, allowing the GC to reclaim |
| 924 | + // the memory from the const object when needed. |
946 | 925 | push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0);
|
947 | 926 | break;
|
948 | 927 | }
|
|
0 commit comments