baum

view baum.c @ 40:5c13e29bc6fd

removed debug output of read_input
author meillo@marmaro.de
date Sat, 01 Mar 2008 20:15:20 +0100
parents ff01f0f076e4
children 1ad3d7305e5d
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"
17 #define VERSION "0.3"
20 struct Node* root = 0;
21 struct Stackitem* stack = NULL;
24 void printNode(struct Node* node, int level);
25 void printTree(struct Node* root, int level);
26 struct Node* lastNode(struct Node* node);
27 void delete(struct Node* node);
31 void logit(char* text) {
32 if (option_verbose) {
33 fprintf(stderr, "[%s]\n", text);
34 }
35 }
38 /* new */
39 struct Node* newNode(char* name, unsigned char value) {
40 struct Node* node;
41 node = (struct Node*) malloc(sizeof(struct Node));
42 strcpy(node->name, name);
43 node->value = value;
44 node->right = 0;
45 node->down = 0;
46 return node;
47 }
51 struct Node* lastNode(struct Node* node) {
52 while (node->right != NULL) {
53 node = node->right;
54 }
55 return node;
56 }
58 struct Node* insertLast(struct Node* node, struct Node* insert) {
59 node = lastNode(node);
60 node->right = insert;
61 return insert;
62 }
65 /* delete */
66 void delete(struct Node* node) {
67 if (node == NULL) {
68 return;
69 }
70 delete(node->down);
71 delete(node->right);
72 free(node); node=0;
73 }
76 /* print */
77 void printNode(struct Node* node, int level) {
78 if (node != NULL) {
79 while (level-- > 0) {
80 fprintf(stderr, "\t");
81 }
82 fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value);
83 }
84 }
86 void printTree(struct Node* root, int level) {
87 if (root == NULL) {
88 return;
89 }
90 printNode(root, level);
91 printTree(root->down, level+1);
92 printTree(root->right, level);
93 }
99 /* stack for read_input */
100 void push(struct Node* node) {
101 struct Stackitem* tmp;
102 struct Stackitem* new;
103 new = (struct Stackitem*) malloc(sizeof(struct Stackitem));
104 new->node = node;
105 tmp = stack;
106 stack = new;
107 stack->next = tmp;
108 }
109 struct Node* pull() {
110 if (stack == NULL) {
111 return NULL;
112 }
113 struct Stackitem* tmp;
114 struct Node* node;
115 tmp = stack;
116 stack = stack->next;
117 node = tmp->node;
118 free(tmp); tmp=0;
119 return node;
120 }
124 /* read input */
125 void read_input(char* filename) {
126 int c;
127 int indent;
128 char name[256];
129 int value;
130 int last_indent;
131 struct Node* last_node;
132 struct Node* node;
133 FILE* file;
135 indent = 0;
136 strcpy(name, "");
137 value = 0;
138 last_indent = -1;
139 last_node = NULL;
140 file = fopen(filename, "r");
142 while ((c = getc(file)) != EOF) {
143 if (c == '#') { /* comment */
144 while ((c = getc(file)) != '\n') {
145 }
146 }
148 if (c == ' ' || c == '\t') { /* indent if at start of line */
149 if (strlen(name) == 0) {
150 indent++;
151 }
152 }
154 if (c == '\n') { /* end of line: create node */
155 if (strlen(name) > 0) {
156 /* create node */
157 node = newNode((char*) name, value);
158 if (indent > last_indent) { /* down */
159 /* if it goes more than one level down -> error */
160 if (indent > last_indent + 1) {
161 fprintf(stderr, "error: Indention over more than one level. Only indent by one!\n");
162 exit(5);
163 }
164 if (last_node == NULL) {
165 root = node;
166 last_node = root;
167 } else {
168 last_node->down = node;
169 }
170 push(last_node);
171 } else if (indent == last_indent) { /* right */
172 last_node->right = node;
173 } else if (indent < last_indent) { /* up */
174 /* handle if it goes more than one level up */
175 while (indent < last_indent) {
176 last_node = pull();
177 last_indent--;
178 }
179 last_node->right = node;
180 }
181 last_indent = indent;
182 last_node = node;
183 }
184 indent = 0;
185 strcpy(name, "");
186 value = 0;
187 }
189 if (c >= 'a' && c <= 'z') { /* name */
190 int i = 1;
191 name[0] = (char) c;
192 while ((c = getc(file)) != '(') {
193 name[i] = (char) c;
194 i++;
195 if (i > 255) {
196 fprintf(stderr, "error: node name too long, or no value given\n");
197 exit(6);
198 }
199 }
200 name[i] = '\0';
201 }
203 if (c == '(') { /* value */
204 fscanf(file, "%d)", &value);
205 }
207 }
209 /* clear stack */
210 while (stack != NULL) {
211 pull();
212 }
214 fclose(file);
215 }
219 /* main */
220 int main(int argc, char* argv[]) {
221 unsigned char shell_return = 0;
223 option_verbose = 0;
225 while (--argc > 0 && (*++argv)[0] == '-') {
226 if (strcmp(argv[0], "--version") == 0) {
227 printf("\
228 baum %s\n\
229 an esoteric programming language\n\
230 by markus schnalke and julian forster\n\
231 http://prog.marmaro.de/baum\n\
232 ", VERSION);
233 exit(0);
234 } else if (strcmp(argv[0], "--help") == 0) {
235 printf("\
236 baum --version print version information and exit\n\
237 baum --help print this output\n\
238 baum [-v] <file> (verbosly) run file\n\
239 ");
240 exit(0);
241 } else if (strcmp(argv[0], "-v") == 0) {
242 option_verbose = 1;
243 /*
244 } else if (strcmp(argv[0], "-W") == 0) {
245 / * TODO: catch if no value given * /
246 iDWarn = atoi((++argv)[0]);
247 argc--;
248 */
249 } else {
250 fprintf(stderr, "unknown option: %s\n", argv[0]);
251 exit(127);
252 }
253 }
255 if (argc != 1) {
256 fprintf(stderr, "%d source files given, please specify one.\n", argc);
257 exit(3);
258 }
260 read_input(argv[0]);
262 if (option_verbose) {
263 printTree(root, 0);
264 }
266 shell_return = action(root);
267 fflush(stdout);
269 if (option_verbose) {
270 printTree(root, 0);
271 }
273 delete(root);
274 exit(shell_return);
275 }