baum

view baum.c @ 7:6a6152cf63f7

new implementation of the read input function
author meillo@marmaro.de
date Sat, 09 Feb 2008 11:57:16 +0100
parents ab87b154a96b
children 495a56e706dc
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"
18 struct Node* root = 0;
21 void logit(char* text) {
22 fprintf(stderr, "[%s]\n", text);
23 }
26 /* new */
27 struct Node* newNode(char* name, unsigned char value) {
28 struct Node* node;
29 node = (struct Node*) malloc(sizeof(struct Node));
30 node->name = name;
31 node->value = value;
32 node->right = 0;
33 node->down = 0;
34 return node;
35 }
38 void setValue(struct Node* node, unsigned char value) {
39 node->value = value;
40 }
43 struct Node* nextNode(struct Node* node) {
44 return node->right;
45 }
47 struct Node* lastNode(struct Node* node) {
48 while (node->right != NULL) {
49 node = node->right;
50 }
51 return node;
52 }
54 void insertLast(struct Node* node, struct Node* insert) {
55 node = lastNode(node);
56 node->right = insert;
57 }
59 /* delete */
60 void delete(struct Node* node) {
61 if (node != NULL) {
62 if (node->down != NULL) {
63 delete(node->down);
64 }
65 if (node->right != NULL) {
66 delete(node->right);
67 }
68 free(node); node=0;
69 }
70 }
73 /* print */
74 void printNode(struct Node* node) {
75 if (node != NULL) {
76 fprintf(stderr, "Node: %20s (%d|%c)\n", node->name, node->value, node->value);
77 }
78 }
80 void printTree(struct Node* root) {
81 if (root != NULL) {
82 printNode(root);
83 fprintf(stderr, " down: ");
84 if (root->down != NULL) {
85 printTree(root->down);
86 } else {
87 fprintf(stderr, "NULL\n");
88 }
89 fprintf(stderr, " right: ");
90 if (root->right != NULL) {
91 printTree(root->right);
92 } else {
93 fprintf(stderr, "NULL\n");
94 }
95 }
96 }
100 /* traverse */
101 void traverse(struct Node* root) {
102 /* each node controlls the nodes below itself */
103 action(root);
104 }
106 /* init */
107 void init() {
108 /* add some numbers
109 root = newNode("print", 'n');
110 root->down = newNode("sum", 0);
112 root->down->down = newNode("number", 60);
114 root->down->down->right = newNode("number", 50);
116 root->down->down->right->right = newNode("sum", 0);
117 root->down->down->right->right->down = newNode("number", 1);
118 root->down->down->right->right->down->right = newNode("number", 5);
119 */
121 /* input numbers and add them
122 root = newNode("print", 'n');
123 root->down = newNode("sum", 0);
124 root->down->down = newNode("input", 0);
125 root->down->down->right = newNode("input", 0);
126 root->down->down->right->right = newNode("input", 0);
127 */
129 /* prints HelloWorld
130 */
131 struct Node* np = 0;
133 root = newNode("blackhole", 0);
134 root->down = newNode("sum", 0);
135 root->down->down = newNode("print", 'c');
136 np = root->down->down;
137 np->down = newNode("number", 'H');
139 np->right = newNode("print", 'c');
140 np->right->down = newNode("number", 'e');
141 np = np->right;
143 np->right = newNode("print", 'c');
144 np->right->down = newNode("number", 'l');
145 np = np->right;
147 np->right = newNode("print", 'c');
148 np->right->down = newNode("number", 'l');
149 np = np->right;
151 np->right = newNode("print", 'c');
152 np->right->down = newNode("number", 'o');
153 np = np->right;
155 np->right = newNode("print", 'c');
156 np->right->down = newNode("number", ' ');
157 np = np->right;
159 np->right = newNode("print", 'c');
160 np->right->down = newNode("number", 'L');
161 np = np->right;
163 np->right = newNode("print", 'c');
164 np->right->down = newNode("number", 'y');
165 np = np->right;
167 np->right = newNode("print", 'c');
168 np->right->down = newNode("number", 'd');
169 np = np->right;
171 np->right = newNode("print", 'c');
172 np->right->down = newNode("number", 'i');
173 np = np->right;
175 np->right = newNode("print", 'c');
176 np->right->down = newNode("number", 10);
177 np = np->right;
180 }
183 /* read input */
184 void read_input(char* filename) {
185 FILE* file;
186 int c;
187 char* cp;
188 int indent = 0;
189 char name[256];
190 int value;
192 file = fopen(filename, "r");
195 while ((c = getc(file)) != EOF) {
196 if (c == '#') {
197 printf("c\n");
198 while ((c = getc(file)) != '\n') {
199 }
200 }
202 if (c == ' ' || c == '\t') {
203 indent++;
204 }
206 if (c == '\n') {
207 if (strlen(name) > 0) {
208 printf(" %d - %s - %d\n", indent, name, value);
209 /*
210 */
211 } else {
212 printf("comment\n");
213 }
214 indent = 0;
215 strcpy(name, "");
216 value = 0;
217 }
219 if (c >= 'a' && c <= 'z') {
220 int i = 1;
221 name[0] = (char) c;
222 while ((c = getc(file)) != '(') {
223 /*putc(c, stdout);*/
224 name[i] = (char) c;
225 i++;
226 }
227 name[i] = '\0';
228 }
230 if (c == '(') {
231 fscanf(file, "%d)", &value);
232 }
234 }
236 fclose(file);
237 }
239 /* main */
240 int main(int argc, char* argv[]) {
241 unsigned char shell_return = 0;
242 /*
243 init();
245 printTree(root);
246 fprintf(stderr, "\n\n");
248 shell_return = action(root);
250 fprintf(stderr, "\n\n");
251 printTree(root);
253 delete(root);
254 */
256 read_input("./input_addition");
257 exit(shell_return);
258 }