meillo@0: /* meillo@0: * baum - an esoteric programming language meillo@0: * meillo@0: * (c) markus schnalke meillo@0: * and julian forster meillo@0: * meillo@0: */ meillo@0: meillo@0: meillo@0: #include meillo@0: #include meillo@1: #include meillo@0: meillo@1: #include "baum.h" meillo@2: #include "actions.h" meillo@0: meillo@0: meillo@0: struct Node* root; meillo@0: meillo@0: meillo@1: void logit(char* text) { meillo@1: fprintf(stderr, "[%s]\n", text); meillo@1: } meillo@1: meillo@1: meillo@1: /* new */ meillo@3: struct Node* newNode(char* name, unsigned char value) { meillo@0: struct Node* node; meillo@0: node = (struct Node*) malloc(sizeof(struct Node)); meillo@0: node->name = name; meillo@3: node->value = value; meillo@0: node->right = 0; meillo@0: node->down = 0; meillo@0: return node; meillo@0: } meillo@0: meillo@0: meillo@3: void setValue(struct Node* node, unsigned char value) { meillo@3: node->value = value; meillo@3: } meillo@3: meillo@3: meillo@3: struct Node* nextNode(struct Node* node) { meillo@3: return node->right; meillo@3: } meillo@3: meillo@3: struct Node* lastNode(struct Node* node) { meillo@3: while (node->right != NULL) { meillo@3: node = node->right; meillo@3: } meillo@3: return node; meillo@3: } meillo@3: meillo@3: void insertLast(struct Node* node, struct Node* insert) { meillo@3: node = lastNode(node); meillo@3: node->right = insert; meillo@3: } meillo@3: meillo@1: /* delete */ meillo@1: void delete(struct Node* node) { meillo@0: if (node->down != NULL) { meillo@1: delete(node->down); meillo@0: } meillo@0: if (node->right != NULL) { meillo@1: delete(node->right); meillo@0: } meillo@0: free(node); node=0; meillo@0: } meillo@0: meillo@1: meillo@1: /* print */ meillo@0: void printNode(struct Node* node) { meillo@2: printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value); meillo@0: } meillo@0: meillo@0: void printTree(struct Node* root) { meillo@0: printNode(root); meillo@1: printf(" down: "); meillo@0: if (root->down != NULL) { meillo@0: printTree(root->down); meillo@1: } else { meillo@1: printf("NULL\n"); meillo@0: } meillo@1: printf(" right: "); meillo@0: if (root->right != NULL) { meillo@0: printTree(root->right); meillo@1: } else { meillo@1: printf("NULL\n"); meillo@0: } meillo@0: } meillo@0: meillo@0: meillo@1: meillo@1: /* traverse */ meillo@1: void traverse(struct Node* root) { meillo@1: /* each node controlls the nodes below itself */ meillo@1: action(root); meillo@1: } meillo@1: meillo@1: /* init */ meillo@1: void init() { meillo@3: /* add some numbers meillo@3: root = newNode("print", 0); meillo@3: root->down = newNode("sum", 0); meillo@3: meillo@3: root->down->down = newNode("number", 60); meillo@3: meillo@3: root->down->down->right = newNode("number", 50); meillo@3: meillo@3: root->down->down->right->right = newNode("sum", 0); meillo@3: root->down->down->right->right->down = newNode("number", 1); meillo@3: root->down->down->right->right->down->right = newNode("number", 5); meillo@3: */ meillo@3: meillo@3: /* input numbers and add them */ meillo@3: root = newNode("print", 0); meillo@3: root->down = newNode("sum", 0); meillo@3: root->down->down = newNode("input", 0); meillo@3: root->down->down->right = newNode("input", 0); meillo@3: root->down->down->right->right = newNode("input", 0); meillo@1: } meillo@1: meillo@1: meillo@1: /* main */ meillo@0: int main(int argc, char* argv[]) { meillo@0: init(); meillo@0: printTree(root); meillo@1: meillo@1: action(root); meillo@1: meillo@3: printTree(root); meillo@1: delete(root); meillo@0: meillo@0: return(0); meillo@0: }