baum

view baum.c @ 24:69a7cf2f0c06

make dist includes now a stripped executable (should we include the exe at all?)
author meillo@marmaro.de
date Wed, 13 Feb 2008 22:04:12 +0100
parents bf660b45bba9
children f0856c177403
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 struct Node* insertLast(struct Node* node, struct Node* insert) {
62 node = lastNode(node);
63 node->right = insert;
64 return insert;
65 }
67 /* delete */
68 void delete(struct Node* node) {
69 if (node != NULL) {
70 if (node->down != NULL) {
71 delete(node->down);
72 }
73 if (node->right != NULL) {
74 delete(node->right);
75 }
76 free(node); node=0;
77 }
78 }
81 /* print */
82 void printNode(struct Node* node) {
83 if (node != NULL) {
84 fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value);
85 }
86 }
88 void printTree(struct Node* root) {
89 if (root != NULL) {
90 printNode(root);
91 fprintf(stderr, " down: ");
92 if (root->down != NULL) {
93 printTree(root->down);
94 } else {
95 fprintf(stderr, "NULL\n");
96 }
97 fprintf(stderr, " right: ");
98 if (root->right != NULL) {
99 printTree(root->right);
100 } else {
101 fprintf(stderr, "NULL\n");
102 }
103 }
104 }
108 /* traverse */
109 void traverse(struct Node* root) {
110 /* each node controlls the nodes below itself */
111 action(root);
112 }
117 void push(struct Node* node) {
118 struct Stackitem* tmp;
119 struct Stackitem* new;
120 new = (struct Stackitem*) malloc(sizeof(struct Stackitem));
121 new->node = node;
122 tmp = stack;
123 stack = new;
124 stack->next = tmp;
125 }
126 struct Node* pull() {
127 if (stack == NULL) {
128 return NULL;
129 }
130 struct Stackitem* tmp;
131 struct Node* node;
132 tmp = stack;
133 stack = stack->next;
134 node = tmp->node;
135 free(tmp); tmp=0;
136 return node;
137 }
141 /* read input */
142 void read_input(char* filename) {
143 int c;
144 int indent;
145 char name[256];
146 int value;
147 int last_indent;
148 struct Node* last_node;
149 struct Node* node;
150 FILE* file;
152 indent = 0;
153 strcpy(name, "");
154 value = 0;
155 last_indent = -1;
156 root = newNode("blackhole", 0);
157 last_node = root;
158 file = fopen(filename, "r");
160 while ((c = getc(file)) != EOF) {
161 if (c == '#') { /* comment */
162 while ((c = getc(file)) != '\n') {
163 }
164 }
166 if (c == ' ' || c == '\t') { /* indent if at start of line */
167 if (strlen(name) == 0) {
168 indent++;
169 }
170 }
172 if (c == '\n') { /* end of line: create node */
173 if (strlen(name) > 0) {
174 if (option_verbose) {
175 fprintf(stderr, " %d - %s - %d\n", indent, name, value);
176 }
177 /* create node */
178 node = newNode((char*) name, value);
179 if (indent > last_indent) { /* down */
180 last_node->down = node;
181 push(last_node);
182 } else if (indent == last_indent) { /* right */
183 last_node->right = node;
184 } else if (indent < last_indent) { /* up */
185 /* FIXME what if it goes more than one level up? */
186 last_node = pull();
187 last_node->right = node;
188 }
189 last_indent = indent;
190 last_node = node;
191 }
192 indent = 0;
193 strcpy(name, "");
194 value = 0;
195 }
197 if (c >= 'a' && c <= 'z') { /* name */
198 int i = 1;
199 name[0] = (char) c;
200 while ((c = getc(file)) != '(') {
201 name[i] = (char) c;
202 i++;
203 if (i > 255) {
204 break;
205 }
206 }
207 name[i] = '\0';
208 }
210 if (c == '(') { /* value */
211 fscanf(file, "%d)", &value);
212 }
214 }
216 fclose(file);
218 }
220 /* main */
221 int main(int argc, char* argv[]) {
222 unsigned char shell_return = 0;
224 while (--argc > 0 && (*++argv)[0] == '-') {
225 if (strcmp(argv[0], "--version") == 0) {
226 printf("baum %s\n\
227 an esoteric programming language\n\
228 by markus schnalke and julian forster\n\
229 http://prog.marmaro.de/baum\n", VERSION);
230 exit(0);
231 } else if (strcmp(argv[0], "--help") == 0) {
232 printf("\
233 baum --version print version information and exit\n\
234 baum --help print this output\n\
235 baum [-v] -c <file> (verbosly) check file and return 1 if invalid\n\
236 baum [-v] <file> (verbosly) run file\n\
237 ");
238 exit(0);
239 } else if (strcmp(argv[0], "-c") == 0) {
240 option_check = 1;
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);
264 }
266 shell_return = action(root);
268 if (option_verbose) {
269 printTree(root);
270 }
272 delete(root);
273 exit(shell_return);
274 }