baum
view baum.c @ 3:15d7d6b9766f
added input; added nextNode, lastNode, insertLast
author | meillo@marmaro.de |
---|---|
date | Thu, 07 Feb 2008 16:15:07 +0100 |
parents | 557fa4df2bcd |
children | 24a697f37e7c |
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;
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->down != NULL) {
62 delete(node->down);
63 }
64 if (node->right != NULL) {
65 delete(node->right);
66 }
67 free(node); node=0;
68 }
71 /* print */
72 void printNode(struct Node* node) {
73 printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value);
74 }
76 void printTree(struct Node* root) {
77 printNode(root);
78 printf(" down: ");
79 if (root->down != NULL) {
80 printTree(root->down);
81 } else {
82 printf("NULL\n");
83 }
84 printf(" right: ");
85 if (root->right != NULL) {
86 printTree(root->right);
87 } else {
88 printf("NULL\n");
89 }
90 }
94 /* traverse */
95 void traverse(struct Node* root) {
96 /* each node controlls the nodes below itself */
97 action(root);
98 }
100 /* init */
101 void init() {
102 /* add some numbers
103 root = newNode("print", 0);
104 root->down = newNode("sum", 0);
106 root->down->down = newNode("number", 60);
108 root->down->down->right = newNode("number", 50);
110 root->down->down->right->right = newNode("sum", 0);
111 root->down->down->right->right->down = newNode("number", 1);
112 root->down->down->right->right->down->right = newNode("number", 5);
113 */
115 /* input numbers and add them */
116 root = newNode("print", 0);
117 root->down = newNode("sum", 0);
118 root->down->down = newNode("input", 0);
119 root->down->down->right = newNode("input", 0);
120 root->down->down->right->right = newNode("input", 0);
121 }
124 /* main */
125 int main(int argc, char* argv[]) {
126 init();
127 printTree(root);
129 action(root);
131 printTree(root);
132 delete(root);
134 return(0);
135 }