47 lines
1.1 KiB
C
47 lines
1.1 KiB
C
#include <compiler/fzl_analyzer.h>
|
|
#include <compiler/utils/fzl_symtable.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// Nest context
|
|
fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type)
|
|
{
|
|
if (!analyzer)
|
|
return NULL;
|
|
|
|
fzl_an_ctx_t *ctx = malloc(sizeof(fzl_an_ctx_t));
|
|
if (!ctx)
|
|
return NULL;
|
|
|
|
memset(ctx, 0, sizeof(fzl_an_ctx_t));
|
|
|
|
ctx->type = type;
|
|
if (analyzer->ctx != NULL)
|
|
ctx->parent = analyzer->ctx;
|
|
else
|
|
ctx->parent = NULL;
|
|
|
|
ctx->symbols = fzl_mksymt();
|
|
ctx->symbols_level = analyzer->symbols_top;
|
|
analyzer->ctx = ctx;
|
|
analyzer->symbols[analyzer->symbols_top++] = ctx->symbols;
|
|
|
|
return ctx;
|
|
}
|
|
|
|
// Close context and return parent
|
|
fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer)
|
|
{
|
|
if (!analyzer || analyzer->ctx == NULL)
|
|
return NULL;
|
|
|
|
fzl_an_ctx_t *current = analyzer->ctx;
|
|
fzl_an_ctx_t *parent = current->parent;
|
|
analyzer->ctx = parent;
|
|
|
|
free(current);
|
|
fzl_freesymt(analyzer->symbols[--analyzer->symbols_top]);
|
|
|
|
return parent;
|
|
}
|