baum

view baum.c @ 8:495a56e706dc

input is now read from stdin
author meillo@marmaro.de
date Sat, 09 Feb 2008 12:42:11 +0100
parents 6a6152cf63f7
children c020b0d1cfca
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() {
185 int c;
186 int indent;
187 char name[256];
188 int value;
190 indent = 0;
191 strcpy(name, "");
192 value = 0;
194 while ((c = getchar()) != EOF) {
195 if (c == '#') { /* comment */
196 while ((c = getchar()) != '\n') {
197 }
198 }
200 if (c == ' ' || c == '\t') { /* indent if at start of line */
201 if (strlen(name) == 0) {
202 indent++;
203 }
204 }
206 if (c == '\n') { /* end of line: create node */
207 if (strlen(name) > 0) {
208 printf(" %d - %s - %d\n", indent, name, value);
209 /* create node */
210 }
211 indent = 0;
212 strcpy(name, "");
213 value = 0;
214 }
216 if (c >= 'a' && c <= 'z') { /* name */
217 int i = 1;
218 name[0] = (char) c;
219 while ((c = getchar()) != '(') {
220 name[i] = (char) c;
221 i++;
222 if (i > 255) {
223 break;
224 }
225 }
226 name[i] = '\0';
227 }
229 if (c == '(') { /* value */
230 scanf("%d)", &value);
231 }
233 }
235 }
237 /* main */
238 int main(int argc, char* argv[]) {
239 unsigned char shell_return = 0;
240 /*
241 init();
243 printTree(root);
244 fprintf(stderr, "\n\n");
246 shell_return = action(root);
248 fprintf(stderr, "\n\n");
249 printTree(root);
251 delete(root);
252 */
254 read_input();
255 exit(shell_return);
256 }