baum
annotate 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 |
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@0 | 27 struct Node* newNode(char* name) { |
meillo@0 | 28 struct Node* node; |
meillo@0 | 29 node = (struct Node*) malloc(sizeof(struct Node)); |
meillo@0 | 30 node->name = name; |
meillo@0 | 31 node->value = 0; |
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@1 | 38 /* delete */ |
meillo@1 | 39 void delete(struct Node* node) { |
meillo@0 | 40 if (node->down != NULL) { |
meillo@1 | 41 delete(node->down); |
meillo@0 | 42 } |
meillo@0 | 43 if (node->right != NULL) { |
meillo@1 | 44 delete(node->right); |
meillo@0 | 45 } |
meillo@0 | 46 free(node); node=0; |
meillo@0 | 47 } |
meillo@0 | 48 |
meillo@1 | 49 |
meillo@1 | 50 /* print */ |
meillo@0 | 51 void printNode(struct Node* node) { |
meillo@2 | 52 printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value); |
meillo@0 | 53 } |
meillo@0 | 54 |
meillo@0 | 55 void printTree(struct Node* root) { |
meillo@0 | 56 printNode(root); |
meillo@1 | 57 printf(" down: "); |
meillo@0 | 58 if (root->down != NULL) { |
meillo@0 | 59 printTree(root->down); |
meillo@1 | 60 } else { |
meillo@1 | 61 printf("NULL\n"); |
meillo@0 | 62 } |
meillo@1 | 63 printf(" right: "); |
meillo@0 | 64 if (root->right != NULL) { |
meillo@0 | 65 printTree(root->right); |
meillo@1 | 66 } else { |
meillo@1 | 67 printf("NULL\n"); |
meillo@0 | 68 } |
meillo@0 | 69 } |
meillo@0 | 70 |
meillo@0 | 71 |
meillo@1 | 72 |
meillo@1 | 73 /* traverse */ |
meillo@1 | 74 void traverse(struct Node* root) { |
meillo@1 | 75 /* each node controlls the nodes below itself */ |
meillo@1 | 76 action(root); |
meillo@1 | 77 } |
meillo@1 | 78 |
meillo@1 | 79 /* init */ |
meillo@1 | 80 void init() { |
meillo@2 | 81 root = newNode("printchar"); |
meillo@1 | 82 root->down = newNode("number"); |
meillo@1 | 83 root->down = newNode("sum"); |
meillo@1 | 84 root->down->down = newNode("number"); |
meillo@1 | 85 root->down->down->value = 70; /* 'F' */ |
meillo@1 | 86 root->down->down->right = newNode("number"); |
meillo@1 | 87 root->down->down->right->value = 50; /* '2' */ |
meillo@1 | 88 /* result should be 'x' */ |
meillo@1 | 89 } |
meillo@1 | 90 |
meillo@1 | 91 |
meillo@1 | 92 /* main */ |
meillo@0 | 93 int main(int argc, char* argv[]) { |
meillo@0 | 94 init(); |
meillo@0 | 95 printTree(root); |
meillo@1 | 96 |
meillo@1 | 97 action(root); |
meillo@1 | 98 |
meillo@1 | 99 delete(root); |
meillo@0 | 100 |
meillo@0 | 101 return(0); |
meillo@0 | 102 } |