diff options
-rw-r--r-- | src/ast.c | 22 | ||||
-rw-r--r-- | src/include/visitor.h | 2 | ||||
-rw-r--r-- | src/lexer.c | 16 | ||||
-rw-r--r-- | src/macros.c | 8 | ||||
-rw-r--r-- | src/main.c | 3 | ||||
-rw-r--r-- | src/parser.c | 27 | ||||
-rw-r--r-- | src/visitor.c | 7 |
7 files changed, 35 insertions, 50 deletions
@@ -96,18 +96,20 @@ bool is_proper_list(ast_t *e) { } void ast_free(ast_t *e) { - if (e->cdr != NULL) { - ast_free(e->cdr); - } - if (e->car != NULL) { - ast_free(e->car); - } - - if (e->type == AST_ROOT) { + if (e->type == AST_PAIR) { + ast_t *cur = e; + ast_t *tmp; + while (cur != NULL) { + tmp = cur; + cur = cur->cdr; + free(tmp); + } + } else if (e->type == AST_ROOT) { for (int i = 0; i < e->root_size; i++) { ast_free(e->subnodes[i]); } + free(e); + } else { + free(e); } - - free(e); } diff --git a/src/include/visitor.h b/src/include/visitor.h index de73248..f055d99 100644 --- a/src/include/visitor.h +++ b/src/include/visitor.h @@ -27,4 +27,6 @@ ast_t *eval_list(visitor_t *v, ast_t *e); ast_t *eval_expr(visitor_t *v, ast_t *e); ast_t *eval(visitor_t *v); + +void visitor_free(visitor_t *v); #endif diff --git a/src/lexer.c b/src/lexer.c index 4e03a02..7ca484d 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -72,10 +72,12 @@ token_t *lexer_collect_bool(lexer_t *lexer) { token_t *lexer_collect_id(lexer_t *lexer) { char *ret = (char *)malloc(1); ret[0] = '\0'; - + char merge[2]; + merge[1] = '\0'; while (is_valid_id_char(lexer->c)) { ret = realloc(ret, (strlen(ret) + 2)); - strcat(ret, char_to_string(lexer->c)); + merge[0] = lexer->c; + strcat(ret, merge); lexer_move(lexer); } return init_token(TOKEN_ID, ret, lexer->row, lexer->col); @@ -85,11 +87,14 @@ token_t *lexer_collect_num(lexer_t *lexer) { char *ret = (char *)malloc(1); ret[0] = '\0'; bool is_float = false; + char merge[2]; + merge[1] = '\0'; while (isdigit(lexer->c) || (lexer->c == '.' && !is_float)) { if (lexer->c == '.') is_float = true; + merge[0] = lexer->c; ret = realloc(ret, (strlen(ret) + 2)); - strcat(ret, char_to_string(lexer->c)); + strcat(ret, merge); lexer_move(lexer); } if (is_float) @@ -100,10 +105,13 @@ token_t *lexer_collect_num(lexer_t *lexer) { token_t *lexer_collect_string(lexer_t *lexer) { char *ret = (char *)malloc(1); ret[0] = '\0'; + char merge[2]; + merge[1] = '\0'; lexer_move(lexer); while (lexer->c != '"') { ret = realloc(ret, (strlen(ret) + 2)); - strcat(ret, char_to_string(lexer->c)); + merge[0] = lexer->c; + strcat(ret, merge); lexer_move(lexer); } lexer_move(lexer); diff --git a/src/macros.c b/src/macros.c index 1643168..670e640 100644 --- a/src/macros.c +++ b/src/macros.c @@ -10,8 +10,8 @@ void die(char *message) { } char *char_to_string(char c) { - char *s = malloc(2); - s[0] = c; - s[1] = '\0'; - return s; + char *str = malloc(2); + str[0] = c; + str[1] = '\0'; + return str; } @@ -167,7 +167,8 @@ int main(int argc, char **argv) { ast_t *root = eval(visitor); /* print_root(root); */ ast_free(root); - free(visitor); + ast_free(visitor->root); + visitor_free(visitor); hash_table_free(parser->symbol_table); parser_free(parser); free(lexer); diff --git a/src/parser.c b/src/parser.c index dc949fe..a6a5b82 100644 --- a/src/parser.c +++ b/src/parser.c @@ -46,33 +46,6 @@ parser_t *init_parser_copy_hash(lexer_t *lexer, hash_table_t *h) { parser_t *init_parser(lexer_t *lexer) { return init_parser_copy_hash(lexer, init_hash_table(100)); } -/* parser_t *init_parser(lexer_t *lexer) { */ -/* parser_t *p = (parser_t *)malloc(sizeof(parser_t)); */ -/* if (p == NULL) */ -/* die("malloc on parser"); */ - -/* p->i = 0; */ -/* p->tokens = malloc(sizeof(token_t *)); */ -/* p->symbol_table = init_hash_table(100); */ -/* p->finished = false; */ -/* if (p->tokens == NULL) */ -/* die("malloc on p->tokens"); */ - -/* int size = 1; */ -/* token_t *t = lexer_collect_next(lexer); */ -/* p->tokens[size - 1] = t; */ - -/* while (true) { */ -/* t = lexer_collect_next(lexer); */ -/* size++; */ -/* p->tokens = realloc(p->tokens, size * sizeof(token_t *)); */ -/* p->tokens[size - 1] = t; */ -/* if (t == NULL) */ -/* break; */ -/* } */ -/* p->size = size; */ -/* return p; */ -/* } */ void parser_move(parser_t *parser) { if (parser->i != parser->size - 1) diff --git a/src/visitor.c b/src/visitor.c index bcea8e4..ffd6048 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -537,8 +537,8 @@ ast_t *eval_list(visitor_t *v, ast_t *e) { } stack_push(v->stack_frame, stack_frame); ast_t *res = eval_expr(v, function->cdr); - stack_pop(v->stack_frame); - hash_table_free_some(stack_frame); + hash_table_t *tmp = stack_pop(v->stack_frame); + hash_table_free_some(tmp); return res; } @@ -576,8 +576,7 @@ void eval_error(visitor_t *v, ast_t *e) { } void visitor_free(visitor_t *v) { - ast_free(v->root); - hash_table_free(v->eval_table); + hash_table_free_some(v->eval_table); stack_free(v->stack_frame); free(v); } |