Mercurial > baum
annotate baum.c @ 4:24a697f37e7c
trivial output improvement
author | meillo@marmaro.de |
---|---|
date | Thu, 07 Feb 2008 18:05:03 +0100 |
parents | 15d7d6b9766f |
children | c202ccccedb5 |
rev | line source |
---|---|
0 | 1 /* |
2 * baum - an esoteric programming language | |
3 * | |
4 * (c) markus schnalke <meillo@marmaro.de> | |
5 * and julian forster | |
6 * | |
7 */ | |
8 | |
9 | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
12 #include <string.h> |
0 | 13 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
14 #include "baum.h" |
2 | 15 #include "actions.h" |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
16 |
0 | 17 |
18 struct Node* root; | |
19 | |
20 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
21 void logit(char* text) { |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
22 fprintf(stderr, "[%s]\n", text); |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
23 } |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
24 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
25 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
26 /* new */ |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
27 struct Node* newNode(char* name, unsigned char value) { |
0 | 28 struct Node* node; |
29 node = (struct Node*) malloc(sizeof(struct Node)); | |
30 node->name = name; | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
31 node->value = value; |
0 | 32 node->right = 0; |
33 node->down = 0; | |
34 return node; | |
35 } | |
36 | |
37 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
38 void setValue(struct Node* node, unsigned char value) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
39 node->value = value; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
40 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
41 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
42 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
43 struct Node* nextNode(struct Node* node) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
44 return node->right; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
45 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
46 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
47 struct Node* lastNode(struct Node* node) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
48 while (node->right != NULL) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
49 node = node->right; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
50 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
51 return node; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
52 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
53 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
54 void insertLast(struct Node* node, struct Node* insert) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
55 node = lastNode(node); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
56 node->right = insert; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
57 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
58 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
59 /* delete */ |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
60 void delete(struct Node* node) { |
0 | 61 if (node->down != NULL) { |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
62 delete(node->down); |
0 | 63 } |
64 if (node->right != NULL) { | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
65 delete(node->right); |
0 | 66 } |
67 free(node); node=0; | |
68 } | |
69 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
70 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
71 /* print */ |
0 | 72 void printNode(struct Node* node) { |
2 | 73 printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value); |
0 | 74 } |
75 | |
76 void printTree(struct Node* root) { | |
77 printNode(root); | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
78 printf(" down: "); |
0 | 79 if (root->down != NULL) { |
80 printTree(root->down); | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
81 } else { |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
82 printf("NULL\n"); |
0 | 83 } |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
84 printf(" right: "); |
0 | 85 if (root->right != NULL) { |
86 printTree(root->right); | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
87 } else { |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
88 printf("NULL\n"); |
0 | 89 } |
90 } | |
91 | |
92 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
93 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
94 /* traverse */ |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
95 void traverse(struct Node* root) { |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
96 /* each node controlls the nodes below itself */ |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
97 action(root); |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
98 } |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
99 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
100 /* init */ |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
101 void init() { |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
102 /* add some numbers |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
103 root = newNode("print", 0); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
104 root->down = newNode("sum", 0); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
105 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
106 root->down->down = newNode("number", 60); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
107 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
108 root->down->down->right = newNode("number", 50); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
109 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
110 root->down->down->right->right = newNode("sum", 0); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
111 root->down->down->right->right->down = newNode("number", 1); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
112 root->down->down->right->right->down->right = newNode("number", 5); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
113 */ |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
114 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
115 /* input numbers and add them */ |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
116 root = newNode("print", 0); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
117 root->down = newNode("sum", 0); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
118 root->down->down = newNode("input", 0); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
119 root->down->down->right = newNode("input", 0); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
120 root->down->down->right->right = newNode("input", 0); |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
121 } |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
122 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
123 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
124 /* main */ |
0 | 125 int main(int argc, char* argv[]) { |
126 init(); | |
4 | 127 |
0 | 128 printTree(root); |
4 | 129 printf("\n\n"); |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
130 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
131 action(root); |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
132 |
4 | 133 printf("\n\n"); |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
134 printTree(root); |
4 | 135 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
136 delete(root); |
0 | 137 |
138 return(0); | |
139 } |