baum

view baum.c @ 12:8e34daa80f64

input is now read from file again (because of input node)
author meillo@marmaro.de
date Tue, 12 Feb 2008 15:53:08 +0100
parents 0e15841ae111
children bf660b45bba9
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 Stackitem* stack = 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 Stackitem* tmp;
112 struct Stackitem* new;
113 new = (struct Stackitem*) malloc(sizeof(struct Stackitem));
114 new->node = node;
115 tmp = stack;
116 stack = new;
117 stack->next = tmp;
118 }
119 struct Node* pull() {
120 if (stack == NULL) {
121 return NULL;
122 }
123 struct Stackitem* tmp;
124 struct Node* node;
125 tmp = stack;
126 stack = stack->next;
127 node = tmp->node;
128 free(tmp); tmp=0;
129 return node;
130 }
134 /* read input */
135 void read_input(char* filename) {
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;
143 FILE* file;
145 indent = 0;
146 strcpy(name, "");
147 value = 0;
148 last_indent = -1;
149 root = newNode("blackhole", 0);
150 last_node = root;
151 file = fopen(filename, "r");
153 while ((c = getc(file)) != EOF) {
154 if (c == '#') { /* comment */
155 while ((c = getc(file)) != '\n') {
156 }
157 }
159 if (c == ' ' || c == '\t') { /* indent if at start of line */
160 if (strlen(name) == 0) {
161 indent++;
162 }
163 }
165 if (c == '\n') { /* end of line: create node */
166 if (strlen(name) > 0) {
167 fprintf(stderr, " %d - %s - %d\n", indent, name, value);
168 /* create node */
169 node = newNode((char*) name, value);
170 if (indent > last_indent) { /* down */
171 last_node->down = node;
172 push(last_node);
173 } else if (indent == last_indent) { /* right */
174 last_node->right = node;
175 } else if (indent < last_indent) { /* up */
176 /* FIXME what if it goes more than one level up? */
177 last_node = pull();
178 last_node->right = node;
179 }
180 last_indent = indent;
181 last_node = node;
182 }
183 indent = 0;
184 strcpy(name, "");
185 value = 0;
186 }
188 if (c >= 'a' && c <= 'z') { /* name */
189 int i = 1;
190 name[0] = (char) c;
191 while ((c = getc(file)) != '(') {
192 name[i] = (char) c;
193 i++;
194 if (i > 255) {
195 break;
196 }
197 }
198 name[i] = '\0';
199 }
201 if (c == '(') { /* value */
202 fscanf(file, "%d)", &value);
203 }
205 }
207 fclose(file);
209 }
211 /* main */
212 int main(int argc, char* argv[]) {
213 unsigned char shell_return = 0;
215 read_input("./input_sum3input");
216 printTree(root);
218 shell_return = action(root);
220 printTree(root);
221 delete(root);
223 exit(shell_return);
224 }