Browse Source

Merge branch 'keep-strings-uninterned' of github.com:pfalcon/micropython into pfalcon-keep-strings-uninterned

Conflicts:
	py/parse.c
pull/629/head
Damien George 11 years ago
parent
commit
3aaabd11a0
  1. 31
      py/parse.c
  2. 1
      py/parse.h

31
py/parse.c

@ -28,6 +28,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include <memory.h>
#include "misc.h" #include "misc.h"
#include "mpconfig.h" #include "mpconfig.h"
@ -70,6 +71,7 @@ enum {
#include "grammar.h" #include "grammar.h"
#undef DEF_RULE #undef DEF_RULE
RULE_maximum_number_of, RULE_maximum_number_of,
RULE_string,
}; };
#define or(n) (RULE_ACT_OR | n) #define or(n) (RULE_ACT_OR | n)
@ -218,6 +220,9 @@ void mp_parse_node_print(mp_parse_node_t pn, int indent) {
case MP_PARSE_NODE_TOKEN: printf("tok(" INT_FMT ")\n", arg); break; case MP_PARSE_NODE_TOKEN: printf("tok(" INT_FMT ")\n", arg); break;
default: assert(0); default: assert(0);
} }
} else if (MP_PARSE_NODE_IS_STRING(pn)) {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
printf("literal str(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
} else { } else {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
@ -274,6 +279,8 @@ STATIC void push_result_node(parser_t *parser, mp_parse_node_t pn) {
parser->result_stack[parser->result_stack_top++] = pn; parser->result_stack[parser->result_stack_top++] = pn;
} }
STATIC void push_string(parser_t *parser, int src_line, const char *str, uint len);
STATIC void push_result_token(parser_t *parser, const mp_lexer_t *lex) { STATIC void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
const mp_token_t *tok = mp_lexer_cur(lex); const mp_token_t *tok = mp_lexer_cur(lex);
mp_parse_node_t pn; mp_parse_node_t pn;
@ -319,7 +326,10 @@ STATIC void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_INTEGER, qstr_from_strn(str, len)); pn = mp_parse_node_new_leaf(MP_PARSE_NODE_INTEGER, qstr_from_strn(str, len));
} }
} else if (tok->kind == MP_TOKEN_STRING) { } else if (tok->kind == MP_TOKEN_STRING) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_STRING, qstr_from_strn(tok->str, tok->len)); printf("Pushing string\n");
push_string(parser, mp_lexer_cur(lex)->src_line, tok->str, tok->len);
return;
// pn = mp_parse_node_new_leaf(MP_PARSE_NODE_STRING, qstr_from_strn(tok->str, tok->len));
} else if (tok->kind == MP_TOKEN_BYTES) { } else if (tok->kind == MP_TOKEN_BYTES) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_BYTES, qstr_from_strn(tok->str, tok->len)); pn = mp_parse_node_new_leaf(MP_PARSE_NODE_BYTES, qstr_from_strn(tok->str, tok->len));
} else { } else {
@ -328,6 +338,21 @@ STATIC void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
push_result_node(parser, pn); push_result_node(parser, pn);
} }
STATIC void push_string(parser_t *parser, int src_line, const char *str, uint len) {
mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, 2);
if (pn == NULL) {
memory_error(parser);
return;
}
pn->source_line = src_line;
pn->kind_num_nodes = RULE_string | (2 << 8);
char *p = m_new(char, len);
memcpy(p, str, len);
pn->nodes[0] = (machine_int_t)p;
pn->nodes[1] = len;
push_result_node(parser, (mp_parse_node_t)pn);
}
STATIC void push_result_rule(parser_t *parser, int src_line, const rule_t *rule, int num_args) { STATIC void push_result_rule(parser_t *parser, int src_line, const rule_t *rule, int num_args) {
mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, num_args); mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, num_args);
if (pn == NULL) { if (pn == NULL) {
@ -516,12 +541,12 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
} }
} }
#if 0 && !MICROPY_ENABLE_DOC_STRING #if 1 && !MICROPY_ENABLE_DOC_STRING
// this code discards lonely statement, such as doc strings // this code discards lonely statement, such as doc strings
// problem is that doc strings have already been interned, so this doesn't really help reduce RAM usage // problem is that doc strings have already been interned, so this doesn't really help reduce RAM usage
if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) { if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) {
mp_parse_node_t p = peek_result(&parser, 1); mp_parse_node_t p = peek_result(&parser, 1);
if (MP_PARSE_NODE_IS_LEAF(p) && !MP_PARSE_NODE_IS_ID(p)) { if ((MP_PARSE_NODE_IS_LEAF(p) && !MP_PARSE_NODE_IS_ID(p)) || MP_PARSE_NODE_IS_STRING(p)) {
pop_result(parser); pop_result(parser);
pop_result(parser); pop_result(parser);
push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0); push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0);

1
py/parse.h

@ -80,6 +80,7 @@ typedef struct _mp_parse_node_struct_t {
#define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((machine_int_t)(pn)) >> 1) #define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((machine_int_t)(pn)) >> 1)
#define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff) #define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff)
#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8) #define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8)
#define MP_PARSE_NODE_IS_STRING(pn) (MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == RULE_string)
mp_parse_node_t mp_parse_node_new_leaf(machine_int_t kind, machine_int_t arg); mp_parse_node_t mp_parse_node_new_leaf(machine_int_t kind, machine_int_t arg);
uint mp_parse_node_free(mp_parse_node_t pn); uint mp_parse_node_free(mp_parse_node_t pn);

Loading…
Cancel
Save