From 76e952ec1756deae78d9a88a67b97eff3550959e Mon Sep 17 00:00:00 2001 From: s-over-4 Date: Fri, 23 Jun 2023 01:07:48 -0400 Subject: basic tree init/destroy --- src/include/tree.h | 25 +++++++++++++++++++------ src/tree.c | 55 +++++++++++++++++++----------------------------------- 2 files changed, 38 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/include/tree.h b/src/include/tree.h index 06f4ee3..4ffefcf 100644 --- a/src/include/tree.h +++ b/src/include/tree.h @@ -7,22 +7,35 @@ typedef struct TREE_STRUC { enum { TREE_DEF, TREE_CALL, - TREE_STRING, - TREE_INT, + TREE_TYPE_STR, + TREE_TYPE_INT, } type; union { - struct def { + struct { // === DEFINITIONS === char* name; // name of definition int mutability; // mutability of definition struct TREE_STRUC* value; // value of definition - }; + } def; - struct call { + struct { // === CALLS === char* target; // name of definition being called struct TREE_STRUC** args; // arguments passed to definition - }; + size_t args_size; // size of arguments + } call; + + // === TYPES === + struct { // strings + char* value; + } type_str; + + struct { // integers + int* value; + } type_int; } data; } tree_t; +tree_t* tree_init(int type); +void tree_destroy(tree_t*); + #endif diff --git a/src/tree.c b/src/tree.c index bb162cd..1f57d5b 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1,50 +1,33 @@ #include "include/tree.h" - -prim_t* prim_init(int type) { - prim_t* prim = calloc(1, sizeof(struct PRIM_STRUC)); - - prim->type = type; - prim->is_mutable = 0; - - switch (type) { - case STR: - prim->val.prim_str.len = NULL; - prim->val.prim_str.val = NULL; - break; - case INT: - prim->val.prim_int.val = NULL; - default: - prim->val.prim_unkwn.val = NULL; - } - - return prim; -} - - tree_t* tree_init(int type) { - tree_t* tree = calloc(1, sizeof(struct TREE_STRUC)); + tree_t* tree; + tree = calloc(1, sizeof(struct TREE_STRUC)); tree->type = type; - + switch (type) { - case TREE_PRIM: - tree->oftype.prim.val = NULL; - break; - case TREE_SUBTREE: - tree->oftype.subtree.size = 0; - tree->oftype.subtree.val = NULL; - break; case TREE_DEF: - tree->oftype.def.args = NULL; - tree->oftype.def.name = NULL; - tree->oftype.def.val = NULL; + tree->data.def.mutability = 0; + tree->data.def.name = (void*) 0; + tree->data.def.value = (void*) 0; break; case TREE_CALL: - tree->oftype.call.args = NULL; - tree->oftype.call.name = NULL; + tree->data.call.args = (void*) 0; + tree->data.call.args_size = 0; + tree->data.call.target = (void*) 0; + break; + case TREE_TYPE_STR: + tree->data.type_str.value = (void*) 0; + break; + case TREE_TYPE_INT: + tree->data.type_int.value = (void*) 0; break; } return tree; } + +void tree_destroy(tree_t* tree) { + free(tree); +} -- cgit v1.2.3