baum

view baum.c @ 26:f0856c177403

removed obsolete stuff; only relevant stuff is extern now; refactoring
author meillo@marmaro.de
date Tue, 19 Feb 2008 22:23:37 +0100
parents e2048e569891
children 1c3dd1e88bdf
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.1"
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 fclose(file);
215 }
219 /* main */
220 int main(int argc, char* argv[]) {
221 unsigned char shell_return = 0;
223 while (--argc > 0 && (*++argv)[0] == '-') {
224 if (strcmp(argv[0], "--version") == 0) {
225 printf("\
226 baum %s\n\
227 an esoteric programming language\n\
228 by markus schnalke and julian forster\n\
229 http://prog.marmaro.de/baum\n\
230 ", VERSION);
231 exit(0);
232 } else if (strcmp(argv[0], "--help") == 0) {
233 printf("\
234 baum --version print version information and exit\n\
235 baum --help print this output\n\
236 baum [-v] -c <file> (verbosly) check file and return 1 if invalid\n\
237 baum [-v] <file> (verbosly) run file\n\
238 ");
239 exit(0);
240 } else if (strcmp(argv[0], "-c") == 0) {
241 option_check = 1;
242 } else if (strcmp(argv[0], "-v") == 0) {
243 option_verbose = 1;
244 /*
245 } else if (strcmp(argv[0], "-W") == 0) {
246 / * TODO: catch if no value given * /
247 iDWarn = atoi((++argv)[0]);
248 argc--;
249 */
250 } else {
251 fprintf(stderr, "unknown option: %s\n", argv[0]);
252 exit(127);
253 }
254 }
256 if (argc != 1) {
257 fprintf(stderr, "%d source files given, please specify one.\n", argc);
258 exit(3);
259 }
261 read_input(argv[0]);
263 if (option_verbose) {
264 printTree(root);
265 }
267 shell_return = action(root);
269 if (option_verbose) {
270 printTree(root);
271 }
273 delete(root);
274 exit(shell_return);
275 }