aboutsummaryrefslogtreecommitdiff
path: root/src/tree.c
blob: 48c3acf514a2aabaa7ff69fd9dc8b1320c5abcd2 (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "include/tree.h"

tree_t* tree_init(int type) {
   tree_t* tree;

   tree = emalloc(sizeof(tree_t));

   tree->type = type;
   
   switch (tree->type) {
      case TREE_TYPE_BLOCK:
         tree->data.block.val = NULL;
         tree->data.block.nxt = NULL;
         break;
      case TREE_TYPE_EXPR:
         tree->data.expr.val = NULL;
         break;
      case TREE_TYPE_LINT:
         tree->data.lint.val = 0;
         break;
      case TREE_TYPE_LSTR:
         tree->data.lstr.val = NULL;
         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_TYPE_CARG:
         tree->data.carg.val = NULL;
         tree->data.carg.nxt = NULL;
         break;
      case TREE_TYPE_DEF:
         tree->data.def.tag = NULL;
         tree->data.def.arg = NULL;
         tree->data.def.val = NULL;
         break;
      case TREE_TYPE_CALL:
         tree->data.call.target = NULL;
         tree->data.call.arg = NULL;
         break;
   }

   return tree;
}

void tree_destroy(tree_t* tree) {
   if (!tree) { return; }

   switch (tree->type) {
      case TREE_TYPE_BLOCK:
         tree_destroy(tree->data.block.val);
         tree_destroy(tree->data.block.nxt);
         break;
      case TREE_TYPE_EXPR:
         tree_destroy(tree->data.expr.val);
         break;
      case TREE_TYPE_LINT:
         break;
      case TREE_TYPE_LSTR:
         free(tree->data.lstr.val);
         break;
      case TREE_TYPE_TAG:
         free(tree->data.tag.val);
         tree_destroy(tree->data.tag.nxt);
         break;
      case TREE_TYPE_DARG:
         tree_destroy(tree->data.darg.tag);
         tree_destroy(tree->data.darg.nxt);
         break;
      case TREE_TYPE_CARG:
         tree_destroy(tree->data.carg.val);
         tree_destroy(tree->data.carg.nxt);
         break;
      case TREE_TYPE_DEF:
         tree_destroy(tree->data.def.tag);
         tree_destroy(tree->data.def.arg);
         tree_destroy(tree->data.def.val);
         break;
      case TREE_TYPE_CALL:
         free(tree->data.call.target);
         tree_destroy(tree->data.call.arg);
         break;
   }

   free(tree);
}

void tree_print(tree_t* tree, int nest) {
   char* spaces;
   int i;

   /* Aaahhh. */
   for(i=0,spaces=ecalloc(nest+1,sizeof(char)),spaces[nest]='\0';i<=nest-1;spaces[i++]=' ');

   #define NEST(TEXT) log_raw("%s"TEXT"\n",spaces);
   #define NEST2(TEXT) log_raw("%s"TEXT,spaces);

   if (!tree) {
      NEST("NULL");
      goto tree_print_exit;
   }

   switch (tree->type) {
      case TREE_TYPE_BLOCK:
         NEST("[block]");
         NEST("val:");
            tree_print(tree->data.block.val, nest + 1);
         NEST("nxt:");
            tree_print(tree->data.block.nxt, nest + 1);
         break;
      case TREE_TYPE_EXPR:
         NEST("[expression]");
         NEST("val:");
            tree_print(tree->data.expr.val, nest + 1);
         break;
      case TREE_TYPE_LINT:
         NEST("[lint]");
         NEST("val:");
            log_raw("%s %d\n", spaces, tree->data.lint.val);
         break;
      case TREE_TYPE_LSTR:
         NEST("[lstr]");
         NEST("val:");
            log_raw("%s %s\n", spaces, tree->data.lstr.val);
         NEST("len:");
            log_raw("%s %d\n", spaces, tree->data.lstr.len);
         break;
      case TREE_TYPE_CALL:
         NEST("[call]");
         NEST("target:");
            log_raw("%s %s\n", spaces, tree->data.call.target);
         NEST("arg:")
            tree_print(tree->data.call.arg, nest + 1);
         break;
      case TREE_TYPE_CARG:
         NEST("[carg]");
         NEST("val:");
            tree_print(tree->data.carg.val, nest + 1);
         NEST("nxt:");
            tree_print(tree->data.carg.nxt, nest + 1);
         break;
      case TREE_TYPE_DEF:
         NEST("[def]");
         NEST("tag:");
            tree_print(tree->data.def.tag, nest + 1);
         NEST("arg:");
            tree_print(tree->data.def.arg, nest + 1);
         NEST("val:");
            tree_print(tree->data.def.val, nest + 1);
         break;
      case TREE_TYPE_TAG:
         NEST("[tag]");
         NEST("val:");
            log_raw("%s %s\n", spaces, tree->data.call.target);
         NEST("nxt:");
            tree_print(tree->data.tag.nxt, nest + 1);
         break;
      case TREE_TYPE_DARG:
         NEST("[darg]");
         NEST("tag:");
            tree_print(tree->data.darg.tag, nest + 1);
         NEST("nxt:");
            tree_print(tree->data.darg.nxt, nest + 1);
         break;
      default:
         log_err("%d", __LINE__);
   }

   tree_print_exit: free(spaces); return;
}