baum

view baum.c @ 29:88a51653db83

removed binary from dist tarball; new debian package
author meillo@marmaro.de
date Fri, 22 Feb 2008 13:55:57 +0100
parents 1c3dd1e88bdf
children cd979b979610
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.2"
19 int option_check = 0;
20 int option_verbose = 0;
22 struct Node* root = 0;
23 struct Stackitem* stack = NULL;
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);
33 void logit(char* text) {
34 if (option_verbose) {
35 fprintf(stderr, "[%s]\n", text);
36 }
37 }
40 /* new */
41 struct Node* newNode(char* name, unsigned char value) {
42 struct Node* node;
43 node = (struct Node*) malloc(sizeof(struct Node));
44 strcpy(node->name, name);
45 node->value = value;
46 node->right = 0;
47 node->down = 0;
48 return node;
49 }
53 struct Node* lastNode(struct Node* node) {
54 while (node->right != NULL) {
55 node = node->right;
56 }
57 return node;
58 }
60 struct Node* insertLast(struct Node* node, struct Node* insert) {
61 node = lastNode(node);
62 node->right = insert;
63 return insert;
64 }
67 /* delete */
68 void delete(struct Node* node) {
69 if (node == NULL) {
70 return;
71 }
72 if (node->down != NULL) {
73 delete(node->down);
74 }
75 if (node->right != NULL) {
76 delete(node->right);
77 }
78 free(node); node=0;
79 }
82 /* print */
83 void printNode(struct Node* node) {
84 if (node != NULL) {
85 fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value);
86 }
87 }
89 void printTree(struct Node* root) {
90 if (root == NULL) {
91 return;
92 }
94 printNode(root);
95 fprintf(stderr, " down: ");
96 if (root->down != NULL) {
97 printTree(root->down);
98 } else {
99 fprintf(stderr, "NULL\n");
100 }
101 fprintf(stderr, " right: ");
102 if (root->right != NULL) {
103 printTree(root->right);
104 } else {
105 fprintf(stderr, "NULL\n");
106 }
107 }
113 /* read tree stack */
114 void push(struct Node* node) {
115 struct Stackitem* tmp;
116 struct Stackitem* new;
117 new = (struct Stackitem*) malloc(sizeof(struct Stackitem));
118 new->node = node;
119 tmp = stack;
120 stack = new;
121 stack->next = tmp;
122 }
123 struct Node* pull() {
124 if (stack == NULL) {
125 return NULL;
126 }
127 struct Stackitem* tmp;
128 struct Node* node;
129 tmp = stack;
130 stack = stack->next;
131 node = tmp->node;
132 free(tmp); tmp=0;
133 return node;
134 }
138 /* read input */
139 void read_input(char* filename) {
140 int c;
141 int indent;
142 char name[256];
143 int value;
144 int last_indent;
145 struct Node* last_node;
146 struct Node* node;
147 FILE* file;
149 indent = 0;
150 strcpy(name, "");
151 value = 0;
152 last_indent = -1;
153 root = newNode("blackhole", 0);
154 last_node = root;
155 file = fopen(filename, "r");
157 while ((c = getc(file)) != EOF) {
158 if (c == '#') { /* comment */
159 while ((c = getc(file)) != '\n') {
160 }
161 }
163 if (c == ' ' || c == '\t') { /* indent if at start of line */
164 if (strlen(name) == 0) {
165 indent++;
166 }
167 }
169 if (c == '\n') { /* end of line: create node */
170 if (strlen(name) > 0) {
171 if (option_verbose) {
172 fprintf(stderr, " %d - %s - %d\n", indent, name, value);
173 }
174 /* create node */
175 node = newNode((char*) name, value);
176 if (indent > last_indent) { /* down */
177 /* FIXME if it goes more than one level down -> error */
178 last_node->down = node;
179 push(last_node);
180 } else if (indent == last_indent) { /* right */
181 last_node->right = node;
182 } else if (indent < last_indent) { /* up */
183 /* FIXME handle if it goes more than one level up */
184 last_node = pull();
185 last_node->right = node;
186 }
187 last_indent = indent;
188 last_node = node;
189 }
190 indent = 0;
191 strcpy(name, "");
192 value = 0;
193 }
195 if (c >= 'a' && c <= 'z') { /* name */
196 int i = 1;
197 name[0] = (char) c;
198 while ((c = getc(file)) != '(') {
199 name[i] = (char) c;
200 i++;
201 if (i > 255) {
202 break;
203 }
204 }
205 name[i] = '\0';
206 }
208 if (c == '(') { /* value */
209 fscanf(file, "%d)", &value);
210 }
212 }
214 /* empty stack */
215 while (stack != NULL) {
216 pull();
217 }
219 fclose(file);
220 }
224 /* main */
225 int main(int argc, char* argv[]) {
226 unsigned char shell_return = 0;
228 while (--argc > 0 && (*++argv)[0] == '-') {
229 if (strcmp(argv[0], "--version") == 0) {
230 printf("\
231 baum %s\n\
232 an esoteric programming language\n\
233 by markus schnalke and julian forster\n\
234 http://prog.marmaro.de/baum\n\
235 ", VERSION);
236 exit(0);
237 } else if (strcmp(argv[0], "--help") == 0) {
238 printf("\
239 baum --version print version information and exit\n\
240 baum --help print this output\n\
241 baum [-v] -c <file> (verbosly) check file and return 1 if invalid\n\
242 baum [-v] <file> (verbosly) run file\n\
243 ");
244 exit(0);
245 } else if (strcmp(argv[0], "-c") == 0) {
246 option_check = 1;
247 } else if (strcmp(argv[0], "-v") == 0) {
248 option_verbose = 1;
249 /*
250 } else if (strcmp(argv[0], "-W") == 0) {
251 / * TODO: catch if no value given * /
252 iDWarn = atoi((++argv)[0]);
253 argc--;
254 */
255 } else {
256 fprintf(stderr, "unknown option: %s\n", argv[0]);
257 exit(127);
258 }
259 }
261 if (argc != 1) {
262 fprintf(stderr, "%d source files given, please specify one.\n", argc);
263 exit(3);
264 }
266 read_input(argv[0]);
268 if (option_verbose) {
269 printTree(root);
270 }
272 shell_return = action(root);
274 if (option_verbose) {
275 printTree(root);
276 }
278 delete(root);
279 exit(shell_return);
280 }