From 8aac9c7a82027ba9d90c0546be7f091df89a6d1f Mon Sep 17 00:00:00 2001 From: c+1 Date: Sun, 29 Oct 2023 17:12:11 -0400 Subject: AST --- Makefile | 1 - src/include/tree.h | 76 ++++++++++++++++++++++++++++++++++++------------------ src/tree.c | 50 +++++++++++++++++++---------------- 3 files changed, 79 insertions(+), 48 deletions(-) diff --git a/Makefile b/Makefile index b888564..d9602c2 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,6 @@ DBG_CFLAGS := -Og -ggdb -pedantic -Wall -Wno-deprecated-declarations -fsanitize= CFLAGS := ${REG_CFLAGS} SRCS := $(wildcard src/*.c) SRCS := $(filter-out src/parser.c, $(SRCS)) -SRCS := $(filter-out src/tree.c, $(SRCS)) OBJS := $(SRCS:.c=.o) all: halk diff --git a/src/include/tree.h b/src/include/tree.h index 108f646..d19f1f7 100644 --- a/src/include/tree.h +++ b/src/include/tree.h @@ -3,39 +3,65 @@ #include -typedef enum { - LEAF_TYPE_INT, - LEAF_TYPE_STR, -} leaf_t; - -typedef struct ARG_T_STRUCT { - leaf_t type; - char* id; - struct ARG_T_STRUCT* nxt; -} arg_t; - -typedef struct TREE_T_STRUCT { +typedef struct TREE { enum TREE_TYPE { + TREE_TYPE_LINT, + TREE_TYPE_LSTR, + TREE_TYPE_TAG, + TREE_TYPE_DARG, + TREE_TYPE_CARG, TREE_TYPE_DEF, TREE_TYPE_CAL, } type; - union { - struct { - leaf_t type; - char* id; - arg_t* arg; - struct TREE_T_STRUCT* val; - } tree_def; - - struct { - char* id; - arg_t* arg; - } tree_cal; + union TREE_DATA{ + + /* literal integer */ + struct TREE_DATA_LINT { + int val; + } lint; + + /* literal string */ + struct TREE_DATA_LSTR { + size_t len; + char* val; + } lstr; + + /* tags */ + struct TREE_DATA_TAG { + char* val; + struct TREE_DATA_TAG* nxt; + } tag; + + /* definition arguments */ + struct TREE_DATA_DARG { + struct TREE_DATA_TAG* tag; + struct TREE_DATA_DARG* nxt; + } darg; + + /* call arguments */ + struct TREE_DATA_CARG { + struct TREE* val; + struct TREE_DATA_CARG* nxt; + } carg; + + /* definitions */ + struct TREE_DATA_DEF { + struct TREE_DATA_TAG* tag; + struct TREE_DATA_DARG* arg; + struct TREE* val; + } def; + + /* calls */ + struct TREE_DATA_CAL { + char* target; + struct TREE_DATA_CARG* arg; + } cal; + } data; } tree_t; tree_t* tree_init(int type); -void tree_destroy(tree_t*); +void tree_destroy(tree_t* tree); #endif diff --git a/src/tree.c b/src/tree.c index 235d0e6..dc0a9ad 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1,35 +1,41 @@ #include "include/tree.h" +#include "include/util.h" + tree_t* tree_init(int type) { tree_t* tree; - tree = calloc(1, sizeof(struct TREE_STRUC)); + tree = emalloc(sizeof(struct TREE)); + tree->type = type; - switch (type) { - case TREE_COMP: - tree->data.comp.value = NULL; - tree->data.comp.size = 0; + switch (tree->type) { + case TREE_TYPE_LINT: + tree->data.lint.val = 0; + break; + case TREE_TYPE_LSTR: + tree->data.lstr.len = 0; + break; + case TREE_TYPE_TAG: + tree->data.tag.val = NULL; + tree->data.tag.nxt = NULL; + break; + case TREE_TYPE_DARG: + tree->data.darg.tag = NULL; + tree->data.darg.nxt = NULL; break; - case TREE_DEF: - tree->data.def.type = NULL; - tree->data.def.tags = NULL; - tree->data.def.tags_size = 0; - tree->data.def.name = NULL; - tree->data.def.args = NULL; - tree->data.def.tags_size = 0; - tree->data.def.value = NULL; + case TREE_TYPE_CARG: + tree->data.carg.val = NULL; + tree->data.carg.nxt = NULL; break; - case TREE_CALL: - tree->data.call.target = NULL; - tree->data.call.args = NULL; - tree->data.call.args_size = 0; + case TREE_TYPE_DEF: + tree->data.def.tag = NULL; + tree->data.def.arg = NULL; + tree->data.def.val = NULL; break; - case TREE_TYPE_STR: - tree->data.type_str.value = NULL; - break; - case TREE_TYPE_INT: - tree->data.type_int.value = 0; + case TREE_TYPE_CAL: + tree->data.cal.target = NULL; + tree->data.cal.arg = NULL; break; } -- cgit v1.2.3