baum
view baum.c @ 5:c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
author | meillo@marmaro.de |
---|---|
date | Fri, 08 Feb 2008 20:45:17 +0100 |
parents | 24a697f37e7c |
children | ab87b154a96b |
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 /* main */
184 int main(int argc, char* argv[]) {
185 unsigned char shell_return;
186 init();
188 printTree(root);
189 fprintf(stderr, "\n\n");
191 shell_return = action(root);
193 fprintf(stderr, "\n\n");
194 printTree(root);
196 delete(root);
198 exit(shell_return);
199 }