From fa69b0278d11ef954f1d382fea134e10d0a43481 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 1 Dec 2013 10:12:27 -0500 Subject: [PATCH] Relocate AST stuff --- ast.c | 31 +++++++++++++++++++++++++- ast.h | 11 +++++++++ parser.c | 68 -------------------------------------------------------- 3 files changed, 41 insertions(+), 69 deletions(-) diff --git a/ast.c b/ast.c index f7a4fb3..3cd92b8 100644 --- a/ast.c +++ b/ast.c @@ -3447,7 +3447,36 @@ error: return false; } -/* iterator functions */ +/* AST iterator + iterator functions */ +void ast_iterator_begin(ast_iterator *iter, ast_node *start) { + iter->head = start; + iter->at = &iter->head; + iter->path = NULL; + vec_push(iter->path, iter->at); +} + +void ast_iterator_delete(ast_iterator *iter) { + if (iter->path) + vec_free(iter->path); +} + +ast_node *ast_iterator_next(ast_iterator *iter) { + size_t depth = vec_size(iter->path); + while (depth) { + ast_node **last = vec_last(iter->path); + ast_node **next = ast_next_child(*last, iter->at); + if (next) { + vec_push(iter->path, next); + iter->at = next; + return *next; + } + iter->at = last; + vec_pop(iter->path); + --depth; + } + return NULL; +} + static ast_node** ast_member_next_child(ast_member *self, ast_node **cur) { (void)self; (void)cur; return NULL; diff --git a/ast.h b/ast.h index e99b378..0f8105f 100644 --- a/ast.h +++ b/ast.h @@ -708,4 +708,15 @@ bool ast_generate_accessors(ast_value *asvalue, ir_builder *ir); * all the flags. */ typedef int static_assert_is_ast_flag_safe [((AST_FLAG_LAST) <= (ast_flag_t)(-1)) ? 1 : -1]; + +/* AST iterator */ +typedef struct { + ast_node ***path; + ast_node **at; + ast_node *head; +} ast_iterator; + +void ast_iterator_begin(ast_iterator *iter, ast_node *start); +void ast_iterator_delete(ast_iterator *iter); +ast_node *ast_iterator_next(ast_iterator *iter); #endif diff --git a/parser.c b/parser.c index f6f02fe..004d8a3 100644 --- a/parser.c +++ b/parser.c @@ -6155,73 +6155,6 @@ void parser_cleanup(parser_t *parser) mem_d(parser); } -typedef struct { - ast_node ***path; - ast_node **at; - ast_node *head; -} ast_iterator; - -static void ast_iterator_begin(ast_iterator *iter, ast_node *start) -{ - iter->head = start; - iter->at = &iter->head; - iter->path = NULL; - vec_push(iter->path, iter->at); -} - -static void ast_iterator_delete(ast_iterator *iter) -{ - if (iter->path) { - vec_free(iter->path); - } -} - -static ast_node* ast_iterator_next(ast_iterator *iter) -{ - size_t depth = vec_size(iter->path); - while (depth) { - ast_node **last = vec_last(iter->path); - ast_node **next = ast_next_child(*last, iter->at); - if (next) { - vec_push(iter->path, next); - iter->at = next; - return *next; - } - /* back up */ - iter->at = last; - vec_pop(iter->path); - --depth; - } - return NULL; -} - -static void traverse_that_thing(ast_function *fun) -{ -#if 1 - ast_node *at; - size_t depth; - ast_iterator iter; - ast_iterator_begin(&iter, (ast_node*)fun); - - for (at = (ast_node*)fun; - at; - at = ast_iterator_next(&iter)) - { - for (depth = vec_size(iter.path); depth; --depth) - con_out("> "); - con_out("ast_%s (%p)\n", ast_node_type_name[at->nodetype], at); - } - - ast_iterator_delete(&iter); -#else - ast_iterator iter = { NULL, (ast_node*)fun }; - ast_iterator_begin(&iter, (ast_node*)fun); - if (!fun) - ast_iterator_next(&iter); - ast_iterator_delete(&iter); -#endif -} - bool parser_finish(parser_t *parser, const char *output) { size_t i; @@ -6368,7 +6301,6 @@ bool parser_finish(parser_t *parser, const char *output) ir_builder_delete(ir); return false; } - traverse_that_thing(parser->functions[i]); } parser_remove_ast(parser); -- 2.39.2