~siborgium/Regex

6179ec7aac45656269b75483bb9d632e49644e93 — Sergey Smirnykh 2 years ago d20279e
ast_normalize uses memory manager to free unused AST nodes

Actual ast_free is not ready yet, but the code using it is.
4 files changed, 21 insertions(+), 16 deletions(-)

M Include/ast_normalize.h
M Src/ast_normalize.c
M Src/main.c
M Src/test_parser.c
M Include/ast_normalize.h => Include/ast_normalize.h +1 -1
@@ 6,4 6,4 @@
/// @brief Приводит AST в нормальную форму
/// @param a -- приводимое AST
/// @return nonzero on error, zero on success
int ast_normalize(struct ast* a);
int ast_normalize(struct ast* a, struct mem_mgr* mgr);

M Src/ast_normalize.c => Src/ast_normalize.c +18 -13
@@ 6,6 6,13 @@
#include <string.h>
#include <check.h>

static inline int ast_free(struct ast* a, struct mem_mgr* m) {
    (void)a;
    (void)m;
    // TODO: proper ast_free
    return 0;
}

static void safe_sum(unsigned* a, unsigned b) {
    if (*a != UINT_MAX && b != UINT_MAX && (*a + b) >= *a) {
        *a = *a + b;


@@ 14,7 21,7 @@ static void safe_sum(unsigned* a, unsigned b) {
    *a = UINT_MAX;
}

static int merge_equivalent_entries(struct ast* a) {
static int merge_equivalent_entries(struct ast* a, struct mem_mgr* mgr) {
    assert(a->type == AST_TYPE_SEQ);

    struct ast* current = a->store.children.first;


@@ 40,7 47,7 @@ static int merge_equivalent_entries(struct ast* a) {
                while (t != current) {
                    struct ast* q = t->next;
                    // FIXME: proper free once memory mgr is ready
                    //free(t);
                    ast_free(t, mgr);
                    t = q;
                }
                equiv_range_begin->next = current->next;


@@ 61,8 68,7 @@ static int merge_equivalent_entries(struct ast* a) {
        struct ast* t = equiv_range_begin->next;
        while (t) {
            struct ast* q = t->next;
            // FIXME: proper free once memory mgr is ready
            //free(t);
            CHECK(ast_free(t, mgr));
            t = q;
        }
        equiv_range_begin->next = NULL;


@@ 71,7 77,7 @@ static int merge_equivalent_entries(struct ast* a) {
    return 0;
}

static int remove_equivalent_branches(struct ast* a) {
static int remove_equivalent_branches(struct ast* a, struct mem_mgr* mgr) {
    // OR should be at least 1 elements long
    assert(a);
    if (!a->next) {


@@ 100,16 106,15 @@ static int remove_equivalent_branches(struct ast* a) {
    a->store.children.last = p;

    while (frees) {
        // FIXME: proper free once mem mgr is finished
        struct ast* t = frees->next;
        free(frees);
        CHECK(ast_free(frees, mgr));
        frees = t;
    }

    return 0;
}

int ast_normalize(struct ast* a) {
int ast_normalize(struct ast* a, struct mem_mgr* mgr) {
    if (!a) {
        return -1;
    }


@@ 117,19 122,19 @@ int ast_normalize(struct ast* a) {
    switch (a->type) {
        case AST_TYPE_OR:
        {
            FOR_AST(a, a1, CHECK(ast_normalize(a1)));
            return remove_equivalent_branches(a);
            FOR_AST(a, a1, CHECK(ast_normalize(a1, mgr)));
            return remove_equivalent_branches(a, mgr);
        }
        case AST_TYPE_PAREN:
        {
            CHECK(ast_normalize(a->store.child));
            CHECK(ast_normalize(a->store.child, mgr));
            break;
        }
        case AST_TYPE_SEQ:
        {
            assert(a->store.children.first && a->store.children.first->next);
            FOR_AST(a, a1, CHECK(ast_normalize(a1)));
            CHECK(merge_equivalent_entries(a));
            FOR_AST(a, a1, CHECK(ast_normalize(a1, mgr)));
            CHECK(merge_equivalent_entries(a, mgr));
            break;
        }
        default:

M Src/main.c => Src/main.c +1 -1
@@ 128,7 128,7 @@ int main(int argc, char* argv[]) {
    CHECK(write_tree(stdout, ctx.result_ast));

    puts("=== normalized AST ===");
    CHECK(ast_normalize(ctx.result_ast));
    CHECK(ast_normalize(ctx.result_ast, &mgr));
    CHECK(write_tree(stdout, ctx.result_ast));

    puts("=== transitions dump ===");

M Src/test_parser.c => Src/test_parser.c +1 -1
@@ 44,7 44,7 @@ int main(int argc, char* argv[]) {
    CHECK(write_tree(stdout, ctx.result_ast));

    puts("=== normalized AST ===");
    CHECK(ast_normalize(ctx.result_ast));
    CHECK(ast_normalize(ctx.result_ast, &mgr));
    CHECK(write_tree(stdout, ctx.result_ast));
    return 0;
}