baum

changeset 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 6f2c1f9dc08f
children 1c3dd1e88bdf
files actions.c actions.h baum.c baum.h
diffstat 4 files changed, 54 insertions(+), 66 deletions(-) [+]
line diff
     1.1 --- a/actions.c	Wed Feb 13 22:04:50 2008 +0100
     1.2 +++ b/actions.c	Tue Feb 19 22:23:37 2008 +0100
     1.3 @@ -4,6 +4,14 @@
     1.4  #include "baum.h"
     1.5  #include "actions.h"
     1.6  
     1.7 +unsigned char action_print(struct Node* node);
     1.8 +unsigned char action_sum(struct Node* node);
     1.9 +unsigned char action_number(struct Node* node);
    1.10 +unsigned char action_input(struct Node* node);
    1.11 +unsigned char action_times(struct Node* node);
    1.12 +unsigned char action_blackhole(struct Node* node);
    1.13 +
    1.14 +
    1.15  
    1.16  unsigned char action(struct Node* node) {
    1.17  	if (node == NULL) {
    1.18 @@ -11,28 +19,24 @@
    1.19  		return 0;
    1.20  	}
    1.21  
    1.22 +	logit(node->name);
    1.23 +
    1.24  	if (strcmp(node->name, "print") == 0) {
    1.25 -		logit("print-node");
    1.26  		return action_print(node);
    1.27  
    1.28  	} else if (strcmp(node->name, "sum") == 0) {
    1.29 -		logit("sum-node");
    1.30  		return action_sum(node);
    1.31  
    1.32  	} else if (strcmp(node->name, "number") == 0) {
    1.33 -		logit("number-node");
    1.34  		return action_number(node);
    1.35  
    1.36  	} else if (strcmp(node->name, "input") == 0) {
    1.37 -		logit("input-node");
    1.38  		return action_input(node);
    1.39  
    1.40  	} else if (strcmp(node->name, "times") == 0) {
    1.41 -		logit("times-node");
    1.42  		return action_times(node);
    1.43  
    1.44  	} else if (strcmp(node->name, "blackhole") == 0) {
    1.45 -		logit("blackhole-node");
    1.46  		return action_blackhole(node);
    1.47  
    1.48  	} else {
     2.1 --- a/actions.h	Wed Feb 13 22:04:50 2008 +0100
     2.2 +++ b/actions.h	Tue Feb 19 22:23:37 2008 +0100
     2.3 @@ -1,10 +1,1 @@
     2.4 -
     2.5  unsigned char action(struct Node* node);
     2.6 -
     2.7 -unsigned char action_print(struct Node* node);
     2.8 -unsigned char action_sum(struct Node* node);
     2.9 -unsigned char action_number(struct Node* node);
    2.10 -unsigned char action_input(struct Node* node);
    2.11 -unsigned char action_times(struct Node* node);
    2.12 -unsigned char action_blackhole(struct Node* node);
    2.13 -
     3.1 --- a/baum.c	Wed Feb 13 22:04:50 2008 +0100
     3.2 +++ b/baum.c	Tue Feb 19 22:23:37 2008 +0100
     3.3 @@ -23,6 +23,13 @@
     3.4  struct Stackitem* stack = NULL;
     3.5  
     3.6  
     3.7 +void printNode(struct Node* node);
     3.8 +void printTree(struct Node* root);
     3.9 +struct Node* lastNode(struct Node* node);
    3.10 +void delete(struct Node* node);
    3.11 +
    3.12 +
    3.13 +
    3.14  void logit(char* text) {
    3.15  	if (option_verbose) {
    3.16  		fprintf(stderr, "[%s]\n", text);
    3.17 @@ -42,14 +49,6 @@
    3.18  }
    3.19  
    3.20  
    3.21 -void setValue(struct Node* node, unsigned char value) {
    3.22 -	node->value = value;
    3.23 -}
    3.24 -
    3.25 -
    3.26 -struct Node* nextNode(struct Node* node) {
    3.27 -	return node->right;
    3.28 -}
    3.29  
    3.30  struct Node* lastNode(struct Node* node) {
    3.31  	while (node->right != NULL) {
    3.32 @@ -64,17 +63,19 @@
    3.33  	return insert;
    3.34  }
    3.35  
    3.36 +
    3.37  /* delete */
    3.38  void delete(struct Node* node) {
    3.39 -	if (node != NULL) {
    3.40 -		if (node->down != NULL) {
    3.41 -			delete(node->down);
    3.42 -		}
    3.43 -		if (node->right != NULL) {
    3.44 -			delete(node->right);
    3.45 -		}
    3.46 -		free(node); node=0;
    3.47 +	if (node == NULL) {
    3.48 +		return;
    3.49  	}
    3.50 +	if (node->down != NULL) {
    3.51 +		delete(node->down);
    3.52 +	}
    3.53 +	if (node->right != NULL) {
    3.54 +		delete(node->right);
    3.55 +	}
    3.56 +	free(node); node=0;
    3.57  }
    3.58  
    3.59  
    3.60 @@ -86,34 +87,30 @@
    3.61  }
    3.62  
    3.63  void printTree(struct Node* root) {
    3.64 -	if (root != NULL) {
    3.65 -		printNode(root);
    3.66 -		fprintf(stderr, "  down: ");
    3.67 -		if (root->down != NULL) {
    3.68 -			printTree(root->down);
    3.69 -		} else {
    3.70 -			fprintf(stderr, "NULL\n");
    3.71 -		}
    3.72 -		fprintf(stderr, "  right: ");
    3.73 -		if (root->right != NULL) {
    3.74 -			printTree(root->right);
    3.75 -		} else {
    3.76 -			fprintf(stderr, "NULL\n");
    3.77 -		}
    3.78 +	if (root == NULL) {
    3.79 +		return;
    3.80 +	}
    3.81 +
    3.82 +	printNode(root);
    3.83 +	fprintf(stderr, "  down: ");
    3.84 +	if (root->down != NULL) {
    3.85 +		printTree(root->down);
    3.86 +	} else {
    3.87 +		fprintf(stderr, "NULL\n");
    3.88 +	}
    3.89 +	fprintf(stderr, "  right: ");
    3.90 +	if (root->right != NULL) {
    3.91 +		printTree(root->right);
    3.92 +	} else {
    3.93 +		fprintf(stderr, "NULL\n");
    3.94  	}
    3.95  }
    3.96  
    3.97  
    3.98  
    3.99 -/* traverse */
   3.100 -void traverse(struct Node* root) {
   3.101 -	/* each node controlls the nodes below itself */
   3.102 -	action(root);
   3.103 -}
   3.104  
   3.105  
   3.106 -
   3.107 -
   3.108 +/* read tree stack */
   3.109  void push(struct Node* node) {
   3.110  	struct Stackitem* tmp;
   3.111  	struct Stackitem* new;
   3.112 @@ -177,12 +174,13 @@
   3.113  				/* create node */
   3.114  				node = newNode((char*) name, value);
   3.115  				if (indent > last_indent) { /* down */
   3.116 +					/* FIXME if it goes more than one level down -> error */
   3.117  					last_node->down = node;
   3.118  					push(last_node);
   3.119  				} else if (indent == last_indent) { /* right */
   3.120  					last_node->right = node;
   3.121  				} else if (indent < last_indent) { /* up */
   3.122 -					/* FIXME what if it goes more than one level up? */
   3.123 +					/* FIXME handle if it goes more than one level up */
   3.124  					last_node = pull();
   3.125  					last_node->right = node;
   3.126  				}
   3.127 @@ -214,8 +212,9 @@
   3.128  	}
   3.129  
   3.130  	fclose(file);
   3.131 +}
   3.132  
   3.133 -}
   3.134 +
   3.135  
   3.136  /* main */
   3.137  int main(int argc, char* argv[]) {
   3.138 @@ -223,10 +222,12 @@
   3.139  	
   3.140  	while (--argc > 0 && (*++argv)[0] == '-') {
   3.141  		if (strcmp(argv[0], "--version") == 0) {
   3.142 -			printf("baum %s\n\
   3.143 +			printf("\
   3.144 +baum %s\n\
   3.145  an esoteric programming language\n\
   3.146  by markus schnalke and julian forster\n\
   3.147 -http://prog.marmaro.de/baum\n", VERSION);
   3.148 +http://prog.marmaro.de/baum\n\
   3.149 +", VERSION);
   3.150  			exit(0);
   3.151  		} else if (strcmp(argv[0], "--help") == 0) {
   3.152  			printf("\
   3.153 @@ -234,7 +235,7 @@
   3.154  baum --help           print this output\n\
   3.155  baum [-v] -c <file>   (verbosly) check file and return 1 if invalid\n\
   3.156  baum [-v] <file>      (verbosly) run file\n\
   3.157 -					");
   3.158 +");
   3.159  			exit(0);
   3.160  		} else if (strcmp(argv[0], "-c") == 0) {
   3.161  			option_check = 1;
     4.1 --- a/baum.h	Wed Feb 13 22:04:50 2008 +0100
     4.2 +++ b/baum.h	Tue Feb 19 22:23:37 2008 +0100
     4.3 @@ -2,17 +2,10 @@
     4.4  void logit(char* text);
     4.5  
     4.6  struct Node* newNode(char* name, unsigned char value);
     4.7 -void setValue(struct Node* node, unsigned char value);
     4.8 -void delete(struct Node* node);
     4.9 -void printNode(struct Node* node);
    4.10 -void printTree(struct Node* root);
    4.11 -void traverse(struct Node* root);
    4.12 -
    4.13 -struct Node* nextNode(struct Node* node);
    4.14 -struct Node* lastNode(struct Node* node);
    4.15  struct Node* insertLast(struct Node* node, struct Node* insert);
    4.16  
    4.17  
    4.18 +/* structs */
    4.19  struct Node {
    4.20  	char name[256];
    4.21  	unsigned char value;
    4.22 @@ -20,7 +13,6 @@
    4.23  	struct Node* right;
    4.24  };
    4.25  
    4.26 -
    4.27  struct Stackitem {
    4.28  	struct Node* node;
    4.29  	struct Stackitem* next;