baum

view baum.c @ 13:bf660b45bba9

added commandline options (-v, -c, file); logging only if -v is set
author meillo@marmaro.de
date Wed, 13 Feb 2008 09:13:02 +0100
parents 8e34daa80f64
children e2048e569891
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 logit(char* text) {
27 if (option_verbose) {
28 fprintf(stderr, "[%s]\n", text);
29 }
30 }
33 /* new */
34 struct Node* newNode(char* name, unsigned char value) {
35 struct Node* node;
36 node = (struct Node*) malloc(sizeof(struct Node));
37 strcpy(node->name, name);
38 node->value = value;
39 node->right = 0;
40 node->down = 0;
41 return node;
42 }
45 void setValue(struct Node* node, unsigned char value) {
46 node->value = value;
47 }
50 struct Node* nextNode(struct Node* node) {
51 return node->right;
52 }
54 struct Node* lastNode(struct Node* node) {
55 while (node->right != NULL) {
56 node = node->right;
57 }
58 return node;
59 }
61 void insertLast(struct Node* node, struct Node* insert) {
62 node = lastNode(node);
63 node->right = insert;
64 }
66 /* delete */
67 void delete(struct Node* node) {
68 if (node != NULL) {
69 if (node->down != NULL) {
70 delete(node->down);
71 }
72 if (node->right != NULL) {
73 delete(node->right);
74 }
75 free(node); node=0;
76 }
77 }
80 /* print */
81 void printNode(struct Node* node) {
82 if (node != NULL) {
83 fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value);
84 }
85 }
87 void printTree(struct Node* root) {
88 if (root != NULL) {
89 printNode(root);
90 fprintf(stderr, " down: ");
91 if (root->down != NULL) {
92 printTree(root->down);
93 } else {
94 fprintf(stderr, "NULL\n");
95 }
96 fprintf(stderr, " right: ");
97 if (root->right != NULL) {
98 printTree(root->right);
99 } else {
100 fprintf(stderr, "NULL\n");
101 }
102 }
103 }
107 /* traverse */
108 void traverse(struct Node* root) {
109 /* each node controlls the nodes below itself */
110 action(root);
111 }
116 void push(struct Node* node) {
117 struct Stackitem* tmp;
118 struct Stackitem* new;
119 new = (struct Stackitem*) malloc(sizeof(struct Stackitem));
120 new->node = node;
121 tmp = stack;
122 stack = new;
123 stack->next = tmp;
124 }
125 struct Node* pull() {
126 if (stack == NULL) {
127 return NULL;
128 }
129 struct Stackitem* tmp;
130 struct Node* node;
131 tmp = stack;
132 stack = stack->next;
133 node = tmp->node;
134 free(tmp); tmp=0;
135 return node;
136 }
140 /* read input */
141 void read_input(char* filename) {
142 int c;
143 int indent;
144 char name[256];
145 int value;
146 int last_indent;
147 struct Node* last_node;
148 struct Node* node;
149 FILE* file;
151 indent = 0;
152 strcpy(name, "");
153 value = 0;
154 last_indent = -1;
155 root = newNode("blackhole", 0);
156 last_node = root;
157 file = fopen(filename, "r");
159 while ((c = getc(file)) != EOF) {
160 if (c == '#') { /* comment */
161 while ((c = getc(file)) != '\n') {
162 }
163 }
165 if (c == ' ' || c == '\t') { /* indent if at start of line */
166 if (strlen(name) == 0) {
167 indent++;
168 }
169 }
171 if (c == '\n') { /* end of line: create node */
172 if (strlen(name) > 0) {
173 if (option_verbose) {
174 fprintf(stderr, " %d - %s - %d\n", indent, name, value);
175 }
176 /* create node */
177 node = newNode((char*) name, value);
178 if (indent > last_indent) { /* down */
179 last_node->down = node;
180 push(last_node);
181 } else if (indent == last_indent) { /* right */
182 last_node->right = node;
183 } else if (indent < last_indent) { /* up */
184 /* FIXME what if it goes more than one level up? */
185 last_node = pull();
186 last_node->right = node;
187 }
188 last_indent = indent;
189 last_node = node;
190 }
191 indent = 0;
192 strcpy(name, "");
193 value = 0;
194 }
196 if (c >= 'a' && c <= 'z') { /* name */
197 int i = 1;
198 name[0] = (char) c;
199 while ((c = getc(file)) != '(') {
200 name[i] = (char) c;
201 i++;
202 if (i > 255) {
203 break;
204 }
205 }
206 name[i] = '\0';
207 }
209 if (c == '(') { /* value */
210 fscanf(file, "%d)", &value);
211 }
213 }
215 fclose(file);
217 }
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("baum %s\n\
226 an esoteric programming language\n\
227 by markus schnalke and julian forster\n\
228 http://prog.marmaro.de/baum\n", VERSION);
229 exit(0);
230 } else if (strcmp(argv[0], "--help") == 0) {
231 printf("\
232 baum --version print version information and exit\n\
233 baum --help print this output\n\
234 baum [-v] -c <file> (verbosly) check file and return 1 if invalid\n\
235 baum [-v] <file> (verbosly) run file\n\
236 ");
237 exit(0);
238 } else if (strcmp(argv[0], "-c") == 0) {
239 option_check = 1;
240 } else if (strcmp(argv[0], "-v") == 0) {
241 option_verbose = 1;
242 /*
243 } else if (strcmp(argv[0], "-W") == 0) {
244 / * TODO: catch if no value given * /
245 iDWarn = atoi((++argv)[0]);
246 argc--;
247 */
248 } else {
249 fprintf(stderr, "unknown option: %s\n", argv[0]);
250 exit(127);
251 }
252 }
254 if (argc != 1) {
255 fprintf(stderr, "%d source files given, please specify one.\n", argc);
256 exit(3);
257 }
259 read_input(argv[0]);
261 if (option_verbose) {
262 printTree(root);
263 }
265 shell_return = action(root);
267 if (option_verbose) {
268 printTree(root);
269 }
271 delete(root);
272 exit(shell_return);
273 }