Mercurial > baum
view baum.c @ 9:c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
author | meillo@marmaro.de |
---|---|
date | Sat, 09 Feb 2008 16:41:41 +0100 |
parents | 495a56e706dc |
children | 0e15841ae111 |
line wrap: on
line source
/* * baum - an esoteric programming language * * (c) markus schnalke <meillo@marmaro.de> * and julian forster * */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "baum.h" #include "actions.h" struct Node* root = 0; struct Listitem* list = NULL; void logit(char* text) { fprintf(stderr, "[%s]\n", text); } /* new */ struct Node* newNode(char* name, unsigned char value) { struct Node* node; node = (struct Node*) malloc(sizeof(struct Node)); strcpy(node->name, name); node->value = value; node->right = 0; node->down = 0; return node; } void setValue(struct Node* node, unsigned char value) { node->value = value; } struct Node* nextNode(struct Node* node) { return node->right; } struct Node* lastNode(struct Node* node) { while (node->right != NULL) { node = node->right; } return node; } void insertLast(struct Node* node, struct Node* insert) { node = lastNode(node); node->right = insert; } /* delete */ void delete(struct Node* node) { if (node != NULL) { if (node->down != NULL) { delete(node->down); } if (node->right != NULL) { delete(node->right); } free(node); node=0; } } /* print */ void printNode(struct Node* node) { if (node != NULL) { fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value); } } void printTree(struct Node* root) { if (root != NULL) { printNode(root); fprintf(stderr, " down: "); if (root->down != NULL) { printTree(root->down); } else { fprintf(stderr, "NULL\n"); } fprintf(stderr, " right: "); if (root->right != NULL) { printTree(root->right); } else { fprintf(stderr, "NULL\n"); } } } /* traverse */ void traverse(struct Node* root) { /* each node controlls the nodes below itself */ action(root); } void push(struct Node* node) { struct Listitem* tmp; struct Listitem* new; new = (struct Listitem*) malloc(sizeof(struct Listitem)); new->node = node; tmp = list; list = new; list->next = tmp; } struct Node* pull() { if (list == NULL) { return NULL; } struct Listitem* tmp; struct Node* node; tmp = list; list = list->next; node = tmp->node; free(tmp); tmp=0; return node; } /* read input */ void read_input() { int c; int indent; char name[256]; int value; int last_indent; struct Node* last_node; struct Node* node; indent = 0; strcpy(name, ""); value = 0; last_indent = -1; root = newNode("blackhole", 0); last_node = root; while ((c = getchar()) != EOF) { if (c == '#') { /* comment */ while ((c = getchar()) != '\n') { } } if (c == ' ' || c == '\t') { /* indent if at start of line */ if (strlen(name) == 0) { indent++; } } if (c == '\n') { /* end of line: create node */ if (strlen(name) > 0) { fprintf(stderr, " %d - %s - %d\n", indent, name, value); /* create node */ node = newNode((char*) name, value); if (indent > last_indent) { /* down */ last_node->down = node; push(last_node); } else if (indent == last_indent) { /* right */ last_node->right = node; } else if (indent < last_indent) { /* up */ /* FIXME what if it goes more than one level up? */ last_node = pull(); last_node->right = node; } last_indent = indent; last_node = node; } indent = 0; strcpy(name, ""); value = 0; } if (c >= 'a' && c <= 'z') { /* name */ int i = 1; name[0] = (char) c; while ((c = getchar()) != '(') { name[i] = (char) c; i++; if (i > 255) { break; } } name[i] = '\0'; } if (c == '(') { /* value */ scanf("%d)", &value); } } } /* main */ int main(int argc, char* argv[]) { unsigned char shell_return = 0; read_input(); printTree(root); shell_return = action(root); printTree(root); delete(root); exit(shell_return); }