baum
view baum.c @ 2:557fa4df2bcd
added difference between char and number
author | meillo@marmaro.de |
---|---|
date | Thu, 07 Feb 2008 14:46:27 +0100 |
parents | 3da0ff17c8e7 |
children | 15d7d6b9766f |
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) {
28 struct Node* node;
29 node = (struct Node*) malloc(sizeof(struct Node));
30 node->name = name;
31 node->value = 0;
32 node->right = 0;
33 node->down = 0;
34 return node;
35 }
38 /* delete */
39 void delete(struct Node* node) {
40 if (node->down != NULL) {
41 delete(node->down);
42 }
43 if (node->right != NULL) {
44 delete(node->right);
45 }
46 free(node); node=0;
47 }
50 /* print */
51 void printNode(struct Node* node) {
52 printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value);
53 }
55 void printTree(struct Node* root) {
56 printNode(root);
57 printf(" down: ");
58 if (root->down != NULL) {
59 printTree(root->down);
60 } else {
61 printf("NULL\n");
62 }
63 printf(" right: ");
64 if (root->right != NULL) {
65 printTree(root->right);
66 } else {
67 printf("NULL\n");
68 }
69 }
73 /* traverse */
74 void traverse(struct Node* root) {
75 /* each node controlls the nodes below itself */
76 action(root);
77 }
79 /* init */
80 void init() {
81 root = newNode("printchar");
82 root->down = newNode("number");
83 root->down = newNode("sum");
84 root->down->down = newNode("number");
85 root->down->down->value = 70; /* 'F' */
86 root->down->down->right = newNode("number");
87 root->down->down->right->value = 50; /* '2' */
88 /* result should be 'x' */
89 }
92 /* main */
93 int main(int argc, char* argv[]) {
94 init();
95 printTree(root);
97 action(root);
99 delete(root);
101 return(0);
102 }