baum

diff baum.c @ 26:f0856c177403

removed obsolete stuff; only relevant stuff is extern now; refactoring
author meillo@marmaro.de
date Tue, 19 Feb 2008 22:23:37 +0100
parents e2048e569891
children 1c3dd1e88bdf
line diff
     1.1 --- a/baum.c	Wed Feb 13 22:04:50 2008 +0100
     1.2 +++ b/baum.c	Tue Feb 19 22:23:37 2008 +0100
     1.3 @@ -23,6 +23,13 @@
     1.4  struct Stackitem* stack = NULL;
     1.5  
     1.6  
     1.7 +void printNode(struct Node* node);
     1.8 +void printTree(struct Node* root);
     1.9 +struct Node* lastNode(struct Node* node);
    1.10 +void delete(struct Node* node);
    1.11 +
    1.12 +
    1.13 +
    1.14  void logit(char* text) {
    1.15  	if (option_verbose) {
    1.16  		fprintf(stderr, "[%s]\n", text);
    1.17 @@ -42,14 +49,6 @@
    1.18  }
    1.19  
    1.20  
    1.21 -void setValue(struct Node* node, unsigned char value) {
    1.22 -	node->value = value;
    1.23 -}
    1.24 -
    1.25 -
    1.26 -struct Node* nextNode(struct Node* node) {
    1.27 -	return node->right;
    1.28 -}
    1.29  
    1.30  struct Node* lastNode(struct Node* node) {
    1.31  	while (node->right != NULL) {
    1.32 @@ -64,17 +63,19 @@
    1.33  	return insert;
    1.34  }
    1.35  
    1.36 +
    1.37  /* delete */
    1.38  void delete(struct Node* node) {
    1.39 -	if (node != NULL) {
    1.40 -		if (node->down != NULL) {
    1.41 -			delete(node->down);
    1.42 -		}
    1.43 -		if (node->right != NULL) {
    1.44 -			delete(node->right);
    1.45 -		}
    1.46 -		free(node); node=0;
    1.47 +	if (node == NULL) {
    1.48 +		return;
    1.49  	}
    1.50 +	if (node->down != NULL) {
    1.51 +		delete(node->down);
    1.52 +	}
    1.53 +	if (node->right != NULL) {
    1.54 +		delete(node->right);
    1.55 +	}
    1.56 +	free(node); node=0;
    1.57  }
    1.58  
    1.59  
    1.60 @@ -86,34 +87,30 @@
    1.61  }
    1.62  
    1.63  void printTree(struct Node* root) {
    1.64 -	if (root != NULL) {
    1.65 -		printNode(root);
    1.66 -		fprintf(stderr, "  down: ");
    1.67 -		if (root->down != NULL) {
    1.68 -			printTree(root->down);
    1.69 -		} else {
    1.70 -			fprintf(stderr, "NULL\n");
    1.71 -		}
    1.72 -		fprintf(stderr, "  right: ");
    1.73 -		if (root->right != NULL) {
    1.74 -			printTree(root->right);
    1.75 -		} else {
    1.76 -			fprintf(stderr, "NULL\n");
    1.77 -		}
    1.78 +	if (root == NULL) {
    1.79 +		return;
    1.80 +	}
    1.81 +
    1.82 +	printNode(root);
    1.83 +	fprintf(stderr, "  down: ");
    1.84 +	if (root->down != NULL) {
    1.85 +		printTree(root->down);
    1.86 +	} else {
    1.87 +		fprintf(stderr, "NULL\n");
    1.88 +	}
    1.89 +	fprintf(stderr, "  right: ");
    1.90 +	if (root->right != NULL) {
    1.91 +		printTree(root->right);
    1.92 +	} else {
    1.93 +		fprintf(stderr, "NULL\n");
    1.94  	}
    1.95  }
    1.96  
    1.97  
    1.98  
    1.99 -/* traverse */
   1.100 -void traverse(struct Node* root) {
   1.101 -	/* each node controlls the nodes below itself */
   1.102 -	action(root);
   1.103 -}
   1.104  
   1.105  
   1.106 -
   1.107 -
   1.108 +/* read tree stack */
   1.109  void push(struct Node* node) {
   1.110  	struct Stackitem* tmp;
   1.111  	struct Stackitem* new;
   1.112 @@ -177,12 +174,13 @@
   1.113  				/* create node */
   1.114  				node = newNode((char*) name, value);
   1.115  				if (indent > last_indent) { /* down */
   1.116 +					/* FIXME if it goes more than one level down -> error */
   1.117  					last_node->down = node;
   1.118  					push(last_node);
   1.119  				} else if (indent == last_indent) { /* right */
   1.120  					last_node->right = node;
   1.121  				} else if (indent < last_indent) { /* up */
   1.122 -					/* FIXME what if it goes more than one level up? */
   1.123 +					/* FIXME handle if it goes more than one level up */
   1.124  					last_node = pull();
   1.125  					last_node->right = node;
   1.126  				}
   1.127 @@ -214,8 +212,9 @@
   1.128  	}
   1.129  
   1.130  	fclose(file);
   1.131 +}
   1.132  
   1.133 -}
   1.134 +
   1.135  
   1.136  /* main */
   1.137  int main(int argc, char* argv[]) {
   1.138 @@ -223,10 +222,12 @@
   1.139  	
   1.140  	while (--argc > 0 && (*++argv)[0] == '-') {
   1.141  		if (strcmp(argv[0], "--version") == 0) {
   1.142 -			printf("baum %s\n\
   1.143 +			printf("\
   1.144 +baum %s\n\
   1.145  an esoteric programming language\n\
   1.146  by markus schnalke and julian forster\n\
   1.147 -http://prog.marmaro.de/baum\n", VERSION);
   1.148 +http://prog.marmaro.de/baum\n\
   1.149 +", VERSION);
   1.150  			exit(0);
   1.151  		} else if (strcmp(argv[0], "--help") == 0) {
   1.152  			printf("\
   1.153 @@ -234,7 +235,7 @@
   1.154  baum --help           print this output\n\
   1.155  baum [-v] -c <file>   (verbosly) check file and return 1 if invalid\n\
   1.156  baum [-v] <file>      (verbosly) run file\n\
   1.157 -					");
   1.158 +");
   1.159  			exit(0);
   1.160  		} else if (strcmp(argv[0], "-c") == 0) {
   1.161  			option_check = 1;