AST-reorg iterate

This commit is contained in:
John Kerl 2016-01-24 12:21:09 -05:00
parent b7732d35c2
commit 3dcc8a967c
2 changed files with 36 additions and 4 deletions

View file

@ -2,6 +2,15 @@
#include "lib/mlrutil.h"
#include "containers/mlr_dsl_ast.h"
// ----------------------------------------------------------------
mlr_dsl_ast_root_t* mlr_dsl_ast_root_alloc() {
mlr_dsl_ast_root_t* proot = mlr_malloc_or_die(sizeof(mlr_dsl_ast_root_t));
proot->pbegin_statements = sllv_alloc();
proot->pmain_statements = sllv_alloc();
proot->pend_statements = sllv_alloc();
return proot;
}
// ----------------------------------------------------------------
mlr_dsl_ast_node_t* mlr_dsl_ast_node_alloc(char* text, int type) {
mlr_dsl_ast_node_t* pnode = (mlr_dsl_ast_node_t*)mlr_malloc_or_die(
@ -121,6 +130,20 @@ char* mlr_dsl_ast_node_describe_type(int type) {
}
}
// ----------------------------------------------------------------
static void mlr_dsl_ast_root_free_statement_list(sllv_t* plist) {
for (sllve_t* pe = plist->phead; pe != NULL; pe = pe->pnext)
mlr_dsl_ast_node_free(pe->pvvalue);
sllv_free(plist);
}
void mlr_dsl_ast_root_free(mlr_dsl_ast_root_t* proot) {
mlr_dsl_ast_root_free_statement_list(proot->pbegin_statements);
mlr_dsl_ast_root_free_statement_list(proot->pmain_statements);
mlr_dsl_ast_root_free_statement_list(proot->pend_statements);
free(proot);
}
// ----------------------------------------------------------------
void mlr_dsl_ast_node_free(mlr_dsl_ast_node_t* pnode) {
if (pnode->pchildren) {

View file

@ -23,20 +23,25 @@
#define MLR_DSL_AST_NODE_TYPE_BEGIN 0xff22
#define MLR_DSL_AST_NODE_TYPE_END 0xff44
typedef struct _mlr_dsl_ast_root_t {
sllv_t* pbegin_statements;
sllv_t* pmain_statements;
sllv_t* pend_statements;
} mlr_dsl_ast_root_t;
typedef struct _mlr_dsl_ast_node_t {
char* text;
int type;
sllv_t* pchildren;
} mlr_dsl_ast_node_t;
mlr_dsl_ast_node_t* mlr_dsl_ast_node_alloc(char* text, int type);
mlr_dsl_ast_root_t* mlr_dsl_ast_root_alloc();
mlr_dsl_ast_node_t* mlr_dsl_ast_node_copy(mlr_dsl_ast_node_t* pother);
mlr_dsl_ast_node_t* mlr_dsl_ast_node_alloc(char* text, int type);
mlr_dsl_ast_node_t* mlr_dsl_ast_node_alloc_zary(char* text, int type);
mlr_dsl_ast_node_t* mlr_dsl_ast_node_alloc_unary(char* text, int type,
mlr_dsl_ast_node_t* pa);
mlr_dsl_ast_node_t* mlr_dsl_ast_node_alloc_unary(char* text, int type, mlr_dsl_ast_node_t* pa);
mlr_dsl_ast_node_t* mlr_dsl_ast_node_alloc_binary(char* text, int type,
mlr_dsl_ast_node_t* pa, mlr_dsl_ast_node_t* pb);
@ -44,6 +49,8 @@ mlr_dsl_ast_node_t* mlr_dsl_ast_node_alloc_binary(char* text, int type,
mlr_dsl_ast_node_t* mlr_dsl_ast_node_alloc_ternary(char* text, int type,
mlr_dsl_ast_node_t* pa, mlr_dsl_ast_node_t* pb, mlr_dsl_ast_node_t* pc);
mlr_dsl_ast_node_t* mlr_dsl_ast_node_copy(mlr_dsl_ast_node_t* pother);
// See comments in mlr_dsl_parse.y for this seemingly awkward syntax wherein
// we change the function name after having set it up. This is a consequence of
// bottom-up DSL parsing.
@ -57,4 +64,6 @@ char* mlr_dsl_ast_node_describe_type(int type);
void mlr_dsl_ast_node_free(mlr_dsl_ast_node_t* pnode);
void mlr_dsl_ast_root_free(mlr_dsl_ast_root_t* proot);
#endif // MLR_DSL_AST_H