baum

view baum.c @ 6:ab87b154a96b

first tries for the read input function
author meillo@marmaro.de
date Fri, 08 Feb 2008 21:44:07 +0100
parents c202ccccedb5
children 6a6152cf63f7
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* fp;
186 char c;
187 char* line;
188 int indent;
189 char* name;
190 int value;
192 fp = fopen(filename, "r");
193 indent = 0;
195 while ((c = getc(fp)) != EOF) {
196 if (c == ' ' || c == '\t') {
197 indent++;
198 } else if (c == '(') {
199 fscanf(fp, "%d", &value);
200 } else if (c == '\n') {
202 if (strcmp(name, "") != 0) {
203 printf("\nindent: %d\nname: %s\nvalue: %d\n", indent, name, value);
204 }
205 indent = 0;
206 strcpy(name, "");
207 value = 0;
208 } else {
209 fscanf(fp, "%[a-z]", name);
210 }
211 }
212 }
214 /* main */
215 int main(int argc, char* argv[]) {
216 unsigned char shell_return;
217 /*
218 init();
220 printTree(root);
221 fprintf(stderr, "\n\n");
223 shell_return = action(root);
225 fprintf(stderr, "\n\n");
226 printTree(root);
228 delete(root);
229 */
231 read_input("./input_addition");
232 exit(shell_return);
233 }