Add parse_node_free_struct() and use it to free parse tree after compilation.

TODO: Check lexer/parse/compile error path for leaks too.
This commit is contained in:
Paul Sokolovsky 2014-01-25 00:56:19 +02:00
parent c1d200ef74
commit aee2ba70de
3 changed files with 16 additions and 0 deletions

View File

@ -135,6 +135,20 @@ mp_parse_node_struct_t *parse_node_new_struct(int src_line, int rule_id, int num
return pn; return pn;
} }
int parse_node_free_struct(mp_parse_node_t pn_in) {
int cnt = 0;
if (MP_PARSE_NODE_IS_STRUCT(pn_in)) {
mp_parse_node_struct_t *pn = (mp_parse_node_struct_t *)pn_in;
int n = pn->kind_num_nodes >> 8;
for (int i = 0; i < n; i++) {
cnt += parse_node_free_struct(pn->nodes[i]);
}
m_del_var(mp_parse_node_struct_t, mp_parse_node_t, n, pn);
cnt++;
}
return cnt;
}
#if MICROPY_DEBUG_PRINTERS #if MICROPY_DEBUG_PRINTERS
void mp_parse_node_print(mp_parse_node_t pn, int indent) { void mp_parse_node_print(mp_parse_node_t pn, int indent) {
if (MP_PARSE_NODE_IS_STRUCT(pn)) { if (MP_PARSE_NODE_IS_STRUCT(pn)) {

View File

@ -53,6 +53,7 @@ typedef struct _mp_parse_node_struct_t {
#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)
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);
int parse_node_free_struct(mp_parse_node_t pn_in);
void mp_parse_node_print(mp_parse_node_t pn, int indent); void mp_parse_node_print(mp_parse_node_t pn, int indent);

View File

@ -62,6 +62,7 @@ static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
*/ */
mp_obj_t module_fun = mp_compile(pn, source_name, is_repl); mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
parse_node_free_struct(pn);
if (module_fun == mp_const_none) { if (module_fun == mp_const_none) {
// compile error // compile error