Mercurial > baum
comparison baum.c @ 26:f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
author | meillo@marmaro.de |
---|---|
date | Tue, 19 Feb 2008 22:23:37 +0100 |
parents | e2048e569891 |
children | 1c3dd1e88bdf |
comparison
equal
deleted
inserted
replaced
25:6f2c1f9dc08f | 26:f0856c177403 |
---|---|
19 int option_check = 0; | 19 int option_check = 0; |
20 int option_verbose = 0; | 20 int option_verbose = 0; |
21 | 21 |
22 struct Node* root = 0; | 22 struct Node* root = 0; |
23 struct Stackitem* stack = NULL; | 23 struct Stackitem* stack = NULL; |
24 | |
25 | |
26 void printNode(struct Node* node); | |
27 void printTree(struct Node* root); | |
28 struct Node* lastNode(struct Node* node); | |
29 void delete(struct Node* node); | |
30 | |
24 | 31 |
25 | 32 |
26 void logit(char* text) { | 33 void logit(char* text) { |
27 if (option_verbose) { | 34 if (option_verbose) { |
28 fprintf(stderr, "[%s]\n", text); | 35 fprintf(stderr, "[%s]\n", text); |
40 node->down = 0; | 47 node->down = 0; |
41 return node; | 48 return node; |
42 } | 49 } |
43 | 50 |
44 | 51 |
45 void setValue(struct Node* node, unsigned char value) { | |
46 node->value = value; | |
47 } | |
48 | |
49 | |
50 struct Node* nextNode(struct Node* node) { | |
51 return node->right; | |
52 } | |
53 | 52 |
54 struct Node* lastNode(struct Node* node) { | 53 struct Node* lastNode(struct Node* node) { |
55 while (node->right != NULL) { | 54 while (node->right != NULL) { |
56 node = node->right; | 55 node = node->right; |
57 } | 56 } |
62 node = lastNode(node); | 61 node = lastNode(node); |
63 node->right = insert; | 62 node->right = insert; |
64 return insert; | 63 return insert; |
65 } | 64 } |
66 | 65 |
66 | |
67 /* delete */ | 67 /* delete */ |
68 void delete(struct Node* node) { | 68 void delete(struct Node* node) { |
69 if (node != NULL) { | 69 if (node == NULL) { |
70 if (node->down != NULL) { | 70 return; |
71 delete(node->down); | 71 } |
72 } | 72 if (node->down != NULL) { |
73 if (node->right != NULL) { | 73 delete(node->down); |
74 delete(node->right); | 74 } |
75 } | 75 if (node->right != NULL) { |
76 free(node); node=0; | 76 delete(node->right); |
77 } | 77 } |
78 free(node); node=0; | |
78 } | 79 } |
79 | 80 |
80 | 81 |
81 /* print */ | 82 /* print */ |
82 void printNode(struct Node* node) { | 83 void printNode(struct Node* node) { |
84 fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value); | 85 fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value); |
85 } | 86 } |
86 } | 87 } |
87 | 88 |
88 void printTree(struct Node* root) { | 89 void printTree(struct Node* root) { |
89 if (root != NULL) { | 90 if (root == NULL) { |
90 printNode(root); | 91 return; |
91 fprintf(stderr, " down: "); | 92 } |
92 if (root->down != NULL) { | 93 |
93 printTree(root->down); | 94 printNode(root); |
94 } else { | 95 fprintf(stderr, " down: "); |
95 fprintf(stderr, "NULL\n"); | 96 if (root->down != NULL) { |
96 } | 97 printTree(root->down); |
97 fprintf(stderr, " right: "); | 98 } else { |
98 if (root->right != NULL) { | 99 fprintf(stderr, "NULL\n"); |
99 printTree(root->right); | 100 } |
100 } else { | 101 fprintf(stderr, " right: "); |
101 fprintf(stderr, "NULL\n"); | 102 if (root->right != NULL) { |
102 } | 103 printTree(root->right); |
103 } | 104 } else { |
104 } | 105 fprintf(stderr, "NULL\n"); |
105 | 106 } |
106 | 107 } |
107 | 108 |
108 /* traverse */ | 109 |
109 void traverse(struct Node* root) { | 110 |
110 /* each node controlls the nodes below itself */ | 111 |
111 action(root); | 112 |
112 } | 113 /* read tree stack */ |
113 | |
114 | |
115 | |
116 | |
117 void push(struct Node* node) { | 114 void push(struct Node* node) { |
118 struct Stackitem* tmp; | 115 struct Stackitem* tmp; |
119 struct Stackitem* new; | 116 struct Stackitem* new; |
120 new = (struct Stackitem*) malloc(sizeof(struct Stackitem)); | 117 new = (struct Stackitem*) malloc(sizeof(struct Stackitem)); |
121 new->node = node; | 118 new->node = node; |
175 fprintf(stderr, " %d - %s - %d\n", indent, name, value); | 172 fprintf(stderr, " %d - %s - %d\n", indent, name, value); |
176 } | 173 } |
177 /* create node */ | 174 /* create node */ |
178 node = newNode((char*) name, value); | 175 node = newNode((char*) name, value); |
179 if (indent > last_indent) { /* down */ | 176 if (indent > last_indent) { /* down */ |
177 /* FIXME if it goes more than one level down -> error */ | |
180 last_node->down = node; | 178 last_node->down = node; |
181 push(last_node); | 179 push(last_node); |
182 } else if (indent == last_indent) { /* right */ | 180 } else if (indent == last_indent) { /* right */ |
183 last_node->right = node; | 181 last_node->right = node; |
184 } else if (indent < last_indent) { /* up */ | 182 } else if (indent < last_indent) { /* up */ |
185 /* FIXME what if it goes more than one level up? */ | 183 /* FIXME handle if it goes more than one level up */ |
186 last_node = pull(); | 184 last_node = pull(); |
187 last_node->right = node; | 185 last_node->right = node; |
188 } | 186 } |
189 last_indent = indent; | 187 last_indent = indent; |
190 last_node = node; | 188 last_node = node; |
212 } | 210 } |
213 | 211 |
214 } | 212 } |
215 | 213 |
216 fclose(file); | 214 fclose(file); |
217 | 215 } |
218 } | 216 |
217 | |
219 | 218 |
220 /* main */ | 219 /* main */ |
221 int main(int argc, char* argv[]) { | 220 int main(int argc, char* argv[]) { |
222 unsigned char shell_return = 0; | 221 unsigned char shell_return = 0; |
223 | 222 |
224 while (--argc > 0 && (*++argv)[0] == '-') { | 223 while (--argc > 0 && (*++argv)[0] == '-') { |
225 if (strcmp(argv[0], "--version") == 0) { | 224 if (strcmp(argv[0], "--version") == 0) { |
226 printf("baum %s\n\ | 225 printf("\ |
226 baum %s\n\ | |
227 an esoteric programming language\n\ | 227 an esoteric programming language\n\ |
228 by markus schnalke and julian forster\n\ | 228 by markus schnalke and julian forster\n\ |
229 http://prog.marmaro.de/baum\n", VERSION); | 229 http://prog.marmaro.de/baum\n\ |
230 ", VERSION); | |
230 exit(0); | 231 exit(0); |
231 } else if (strcmp(argv[0], "--help") == 0) { | 232 } else if (strcmp(argv[0], "--help") == 0) { |
232 printf("\ | 233 printf("\ |
233 baum --version print version information and exit\n\ | 234 baum --version print version information and exit\n\ |
234 baum --help print this output\n\ | 235 baum --help print this output\n\ |
235 baum [-v] -c <file> (verbosly) check file and return 1 if invalid\n\ | 236 baum [-v] -c <file> (verbosly) check file and return 1 if invalid\n\ |
236 baum [-v] <file> (verbosly) run file\n\ | 237 baum [-v] <file> (verbosly) run file\n\ |
237 "); | 238 "); |
238 exit(0); | 239 exit(0); |
239 } else if (strcmp(argv[0], "-c") == 0) { | 240 } else if (strcmp(argv[0], "-c") == 0) { |
240 option_check = 1; | 241 option_check = 1; |
241 } else if (strcmp(argv[0], "-v") == 0) { | 242 } else if (strcmp(argv[0], "-v") == 0) { |
242 option_verbose = 1; | 243 option_verbose = 1; |