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