comparison baum.c @ 9:c020b0d1cfca

read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
author meillo@marmaro.de
date Sat, 09 Feb 2008 16:41:41 +0100
parents 495a56e706dc
children 0e15841ae111
comparison
equal deleted inserted replaced
8:495a56e706dc 9:c020b0d1cfca
14 #include "baum.h" 14 #include "baum.h"
15 #include "actions.h" 15 #include "actions.h"
16 16
17 17
18 struct Node* root = 0; 18 struct Node* root = 0;
19 struct Listitem* list = NULL;
19 20
20 21
21 void logit(char* text) { 22 void logit(char* text) {
22 fprintf(stderr, "[%s]\n", text); 23 fprintf(stderr, "[%s]\n", text);
23 } 24 }
25 26
26 /* new */ 27 /* new */
27 struct Node* newNode(char* name, unsigned char value) { 28 struct Node* newNode(char* name, unsigned char value) {
28 struct Node* node; 29 struct Node* node;
29 node = (struct Node*) malloc(sizeof(struct Node)); 30 node = (struct Node*) malloc(sizeof(struct Node));
30 node->name = name; 31 strcpy(node->name, name);
31 node->value = value; 32 node->value = value;
32 node->right = 0; 33 node->right = 0;
33 node->down = 0; 34 node->down = 0;
34 return node; 35 return node;
35 } 36 }
71 72
72 73
73 /* print */ 74 /* print */
74 void printNode(struct Node* node) { 75 void printNode(struct Node* node) {
75 if (node != NULL) { 76 if (node != NULL) {
76 fprintf(stderr, "Node: %20s (%d|%c)\n", node->name, node->value, node->value); 77 fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value);
77 } 78 }
78 } 79 }
79 80
80 void printTree(struct Node* root) { 81 void printTree(struct Node* root) {
81 if (root != NULL) { 82 if (root != NULL) {
101 void traverse(struct Node* root) { 102 void traverse(struct Node* root) {
102 /* each node controlls the nodes below itself */ 103 /* each node controlls the nodes below itself */
103 action(root); 104 action(root);
104 } 105 }
105 106
106 /* init */ 107
107 void init() { 108
108 /* add some numbers 109
109 root = newNode("print", 'n'); 110 void push(struct Node* node) {
110 root->down = newNode("sum", 0); 111 struct Listitem* tmp;
111 112 struct Listitem* new;
112 root->down->down = newNode("number", 60); 113 new = (struct Listitem*) malloc(sizeof(struct Listitem));
113 114 new->node = node;
114 root->down->down->right = newNode("number", 50); 115 tmp = list;
115 116 list = new;
116 root->down->down->right->right = newNode("sum", 0); 117 list->next = tmp;
117 root->down->down->right->right->down = newNode("number", 1); 118 }
118 root->down->down->right->right->down->right = newNode("number", 5); 119 struct Node* pull() {
119 */ 120 if (list == NULL) {
120 121 return NULL;
121 /* input numbers and add them 122 }
122 root = newNode("print", 'n'); 123 struct Listitem* tmp;
123 root->down = newNode("sum", 0); 124 struct Node* node;
124 root->down->down = newNode("input", 0); 125 tmp = list;
125 root->down->down->right = newNode("input", 0); 126 list = list->next;
126 root->down->down->right->right = newNode("input", 0); 127 node = tmp->node;
127 */ 128 free(tmp); tmp=0;
128 129 return node;
129 /* prints HelloWorld 130 }
130 */ 131
131 struct Node* np = 0;
132
133 root = newNode("blackhole", 0);
134 root->down = newNode("sum", 0);
135 root->down->down = newNode("print", 'c');
136 np = root->down->down;
137 np->down = newNode("number", 'H');
138
139 np->right = newNode("print", 'c');
140 np->right->down = newNode("number", 'e');
141 np = np->right;
142
143 np->right = newNode("print", 'c');
144 np->right->down = newNode("number", 'l');
145 np = np->right;
146
147 np->right = newNode("print", 'c');
148 np->right->down = newNode("number", 'l');
149 np = np->right;
150
151 np->right = newNode("print", 'c');
152 np->right->down = newNode("number", 'o');
153 np = np->right;
154
155 np->right = newNode("print", 'c');
156 np->right->down = newNode("number", ' ');
157 np = np->right;
158
159 np->right = newNode("print", 'c');
160 np->right->down = newNode("number", 'L');
161 np = np->right;
162
163 np->right = newNode("print", 'c');
164 np->right->down = newNode("number", 'y');
165 np = np->right;
166
167 np->right = newNode("print", 'c');
168 np->right->down = newNode("number", 'd');
169 np = np->right;
170
171 np->right = newNode("print", 'c');
172 np->right->down = newNode("number", 'i');
173 np = np->right;
174
175 np->right = newNode("print", 'c');
176 np->right->down = newNode("number", 10);
177 np = np->right;
178
179
180 }
181 132
182 133
183 /* read input */ 134 /* read input */
184 void read_input() { 135 void read_input() {
185 int c; 136 int c;
186 int indent; 137 int indent;
187 char name[256]; 138 char name[256];
188 int value; 139 int value;
140 int last_indent;
141 struct Node* last_node;
142 struct Node* node;
189 143
190 indent = 0; 144 indent = 0;
191 strcpy(name, ""); 145 strcpy(name, "");
192 value = 0; 146 value = 0;
147 last_indent = -1;
148 root = newNode("blackhole", 0);
149 last_node = root;
193 150
194 while ((c = getchar()) != EOF) { 151 while ((c = getchar()) != EOF) {
195 if (c == '#') { /* comment */ 152 if (c == '#') { /* comment */
196 while ((c = getchar()) != '\n') { 153 while ((c = getchar()) != '\n') {
197 } 154 }
203 } 160 }
204 } 161 }
205 162
206 if (c == '\n') { /* end of line: create node */ 163 if (c == '\n') { /* end of line: create node */
207 if (strlen(name) > 0) { 164 if (strlen(name) > 0) {
208 printf(" %d - %s - %d\n", indent, name, value); 165 fprintf(stderr, " %d - %s - %d\n", indent, name, value);
209 /* create node */ 166 /* create node */
167 node = newNode((char*) name, value);
168 if (indent > last_indent) { /* down */
169 last_node->down = node;
170 push(last_node);
171 } else if (indent == last_indent) { /* right */
172 last_node->right = node;
173 } else if (indent < last_indent) { /* up */
174 /* FIXME what if it goes more than one level up? */
175 last_node = pull();
176 last_node->right = node;
177 }
178 last_indent = indent;
179 last_node = node;
210 } 180 }
211 indent = 0; 181 indent = 0;
212 strcpy(name, ""); 182 strcpy(name, "");
213 value = 0; 183 value = 0;
214 } 184 }
235 } 205 }
236 206
237 /* main */ 207 /* main */
238 int main(int argc, char* argv[]) { 208 int main(int argc, char* argv[]) {
239 unsigned char shell_return = 0; 209 unsigned char shell_return = 0;
240 /*
241 init();
242
243 printTree(root);
244 fprintf(stderr, "\n\n");
245
246 shell_return = action(root);
247
248 fprintf(stderr, "\n\n");
249 printTree(root);
250
251 delete(root);
252 */
253 210
254 read_input(); 211 read_input();
212 printTree(root);
213
214 shell_return = action(root);
215
216 printTree(root);
217 delete(root);
218
255 exit(shell_return); 219 exit(shell_return);
256 } 220 }