baum
view 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 |
line source
1 /*
2 * baum - an esoteric programming language
3 *
4 * (c) markus schnalke <meillo@marmaro.de>
5 * and julian forster
6 *
7 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include "baum.h"
15 #include "actions.h"
18 struct Node* root = 0;
19 struct Listitem* list = NULL;
22 void logit(char* text) {
23 fprintf(stderr, "[%s]\n", text);
24 }
27 /* new */
28 struct Node* newNode(char* name, unsigned char value) {
29 struct Node* node;
30 node = (struct Node*) malloc(sizeof(struct Node));
31 strcpy(node->name, name);
32 node->value = value;
33 node->right = 0;
34 node->down = 0;
35 return node;
36 }
39 void setValue(struct Node* node, unsigned char value) {
40 node->value = value;
41 }
44 struct Node* nextNode(struct Node* node) {
45 return node->right;
46 }
48 struct Node* lastNode(struct Node* node) {
49 while (node->right != NULL) {
50 node = node->right;
51 }
52 return node;
53 }
55 void insertLast(struct Node* node, struct Node* insert) {
56 node = lastNode(node);
57 node->right = insert;
58 }
60 /* delete */
61 void delete(struct Node* node) {
62 if (node != NULL) {
63 if (node->down != NULL) {
64 delete(node->down);
65 }
66 if (node->right != NULL) {
67 delete(node->right);
68 }
69 free(node); node=0;
70 }
71 }
74 /* print */
75 void printNode(struct Node* node) {
76 if (node != NULL) {
77 fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value);
78 }
79 }
81 void printTree(struct Node* root) {
82 if (root != NULL) {
83 printNode(root);
84 fprintf(stderr, " down: ");
85 if (root->down != NULL) {
86 printTree(root->down);
87 } else {
88 fprintf(stderr, "NULL\n");
89 }
90 fprintf(stderr, " right: ");
91 if (root->right != NULL) {
92 printTree(root->right);
93 } else {
94 fprintf(stderr, "NULL\n");
95 }
96 }
97 }
101 /* traverse */
102 void traverse(struct Node* root) {
103 /* each node controlls the nodes below itself */
104 action(root);
105 }
110 void push(struct Node* node) {
111 struct Listitem* tmp;
112 struct Listitem* new;
113 new = (struct Listitem*) malloc(sizeof(struct Listitem));
114 new->node = node;
115 tmp = list;
116 list = new;
117 list->next = tmp;
118 }
119 struct Node* pull() {
120 if (list == NULL) {
121 return NULL;
122 }
123 struct Listitem* tmp;
124 struct Node* node;
125 tmp = list;
126 list = list->next;
127 node = tmp->node;
128 free(tmp); tmp=0;
129 return node;
130 }
134 /* read input */
135 void read_input() {
136 int c;
137 int indent;
138 char name[256];
139 int value;
140 int last_indent;
141 struct Node* last_node;
142 struct Node* node;
144 indent = 0;
145 strcpy(name, "");
146 value = 0;
147 last_indent = -1;
148 root = newNode("blackhole", 0);
149 last_node = root;
151 while ((c = getchar()) != EOF) {
152 if (c == '#') { /* comment */
153 while ((c = getchar()) != '\n') {
154 }
155 }
157 if (c == ' ' || c == '\t') { /* indent if at start of line */
158 if (strlen(name) == 0) {
159 indent++;
160 }
161 }
163 if (c == '\n') { /* end of line: create node */
164 if (strlen(name) > 0) {
165 fprintf(stderr, " %d - %s - %d\n", indent, name, value);
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;
180 }
181 indent = 0;
182 strcpy(name, "");
183 value = 0;
184 }
186 if (c >= 'a' && c <= 'z') { /* name */
187 int i = 1;
188 name[0] = (char) c;
189 while ((c = getchar()) != '(') {
190 name[i] = (char) c;
191 i++;
192 if (i > 255) {
193 break;
194 }
195 }
196 name[i] = '\0';
197 }
199 if (c == '(') { /* value */
200 scanf("%d)", &value);
201 }
203 }
205 }
207 /* main */
208 int main(int argc, char* argv[]) {
209 unsigned char shell_return = 0;
211 read_input();
212 printTree(root);
214 shell_return = action(root);
216 printTree(root);
217 delete(root);
219 exit(shell_return);
220 }