-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.h
74 lines (62 loc) · 1.82 KB
/
tree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* tree.h - simple tree data structure and utils for them
*
* This source code is licensed under the MIT License.
* See the file COPYING for more details.
*
* @author: Taku Fukushima <tfukushima@dcl.info.waseda.ac.jp>
*/
#ifndef PSH_TREE_H_
#define PSH_TREE_H_
#include <stddef.h>
typedef struct tree_head {
struct tree_head *left;
struct tree_head *right;
} tree_t;
typedef struct node {
token_t *token;
// tree_t *head;
struct node *left;
struct node *right;
int oldstreamfd;
} node_t;
/*
* conteiner_of - macro to get parent structure
*/
#define container_of(ptr, type, member) ({ \
const typeof ( ((type *)0)->member )*__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type, member) );})
/*
* _is_abstract_node - check whether the node is abstract node or not.
*/
static inline bool _is_abstract_node(const node_t *node) {
return (strlen(node->token->element) == 0)? true : false;
}
/**
* init_node - initialize tree node
* @token: token by which node will be initialized
*/
node_t *init_node(const token_t *token);
/**
* init_root - initialize tree root
* @root: the root of syntax tree
*/
node_t *init_root();
/**
* init_abstract_node - initialize tree node with it's spec and without it's token
* @spec: token's specifier
*/
node_t *init_abstract_node(token_spec_t spec);
/**
* create_tree - create tree from parent which left is `left` and right is `right`
* @parent: parent node
* @left: left side child of parent node
* @right: right side child of parent node
*/
node_t *create_tree(node_t *parent, const node_t *left, const node_t *right);
/**
* free_nodes - free nodes which are children of `current` node recursively
* @current: current node which will be freed
*/
void free_nodes(node_t *current, node_t *root);
#endif // PSH_TREE_H_