baum

changeset 5:c202ccccedb5

added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
author meillo@marmaro.de
date Fri, 08 Feb 2008 20:45:17 +0100
parents 24a697f37e7c
children ab87b154a96b
files actions.c actions.h baum.c
diffstat 3 files changed, 135 insertions(+), 41 deletions(-) [+]
line diff
     1.1 --- a/actions.c	Thu Feb 07 18:05:03 2008 +0100
     1.2 +++ b/actions.c	Fri Feb 08 20:45:17 2008 +0100
     1.3 @@ -6,6 +6,11 @@
     1.4  
     1.5  
     1.6  unsigned char action(struct Node* node) {
     1.7 +	if (node == NULL) {
     1.8 +		fprintf(stderr, "action of non existing node\n");
     1.9 +		return 0;
    1.10 +	}
    1.11 +
    1.12  	if (strcmp(node->name, "print") == 0) {
    1.13  		logit("print-node");
    1.14  		return action_print(node);
    1.15 @@ -14,10 +19,6 @@
    1.16  		logit("sum-node");
    1.17  		return action_sum(node);
    1.18  
    1.19 -	} else if (strcmp(node->name, "printchar") == 0) {
    1.20 -		logit("printchar-node");
    1.21 -		return action_printchar(node);
    1.22 -
    1.23  	} else if (strcmp(node->name, "number") == 0) {
    1.24  		logit("number-node");
    1.25  		return action_number(node);
    1.26 @@ -26,6 +27,14 @@
    1.27  		logit("input-node");
    1.28  		return action_input(node);
    1.29  
    1.30 +	} else if (strcmp(node->name, "times") == 0) {
    1.31 +		logit("times-node");
    1.32 +		return action_times(node);
    1.33 +
    1.34 +	} else if (strcmp(node->name, "blackhole") == 0) {
    1.35 +		logit("blackhole-node");
    1.36 +		return action_blackhole(node);
    1.37 +
    1.38  	} else {
    1.39  		fprintf(stderr, "unknown kind of node");
    1.40  		exit(1);
    1.41 @@ -35,14 +44,14 @@
    1.42  
    1.43  
    1.44  unsigned char action_print(struct Node* node) {
    1.45 -	printf("%d\n", action(node->down));
    1.46 -	return 0;
    1.47 -}
    1.48 -
    1.49 -
    1.50 -unsigned char action_printchar(struct Node* node) {
    1.51 -	printf("%c\n", action(node->down));
    1.52 -	return 0;
    1.53 +	unsigned char result;
    1.54 +	result = action(node->down);
    1.55 +	if (node->value == 'c') {
    1.56 +		printf("%c", result);
    1.57 +	} else {
    1.58 +		printf("%d", result);
    1.59 +	}
    1.60 +	return result;
    1.61  }
    1.62  
    1.63  
    1.64 @@ -63,10 +72,34 @@
    1.65  
    1.66  
    1.67  unsigned char action_input(struct Node* node) {
    1.68 +	/*
    1.69  	unsigned char input = (unsigned char) getchar();
    1.70  	getchar();  /* catches the newline */
    1.71 -	insertLast(node, newNode("number", input));
    1.72 +
    1.73 +	/* reads a number which is treated as ASCII value */
    1.74 +	int input;
    1.75 +	scanf("%d", &input);
    1.76 +	input = input % 256;
    1.77 +	insertLast(node, newNode("number", (char) input));
    1.78 +
    1.79  	return 0;
    1.80  }
    1.81  
    1.82  
    1.83 +unsigned char action_times(struct Node* node) {
    1.84 +	struct Node* tp;
    1.85 +	unsigned char i;
    1.86 +	tp = node->down;
    1.87 +	for (i; i < node->value; i++) {
    1.88 +		/* deep copy */
    1.89 +	}
    1.90 +	return 0;
    1.91 +}
    1.92 +
    1.93 +
    1.94 +unsigned char action_blackhole(struct Node* node) {
    1.95 +	action(node->down);
    1.96 +	return 0;
    1.97 +}
    1.98 +
    1.99 +
     2.1 --- a/actions.h	Thu Feb 07 18:05:03 2008 +0100
     2.2 +++ b/actions.h	Fri Feb 08 20:45:17 2008 +0100
     2.3 @@ -2,8 +2,9 @@
     2.4  unsigned char action(struct Node* node);
     2.5  
     2.6  unsigned char action_print(struct Node* node);
     2.7 -unsigned char action_printchar(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	Thu Feb 07 18:05:03 2008 +0100
     3.2 +++ b/baum.c	Fri Feb 08 20:45:17 2008 +0100
     3.3 @@ -15,7 +15,7 @@
     3.4  #include "actions.h"
     3.5  
     3.6  
     3.7 -struct Node* root;
     3.8 +struct Node* root = 0;
     3.9  
    3.10  
    3.11  void logit(char* text) {
    3.12 @@ -58,34 +58,40 @@
    3.13  
    3.14  /* delete */
    3.15  void delete(struct Node* node) {
    3.16 -	if (node->down != NULL) {
    3.17 -		delete(node->down);
    3.18 +	if (node != NULL) {
    3.19 +		if (node->down != NULL) {
    3.20 +			delete(node->down);
    3.21 +		}
    3.22 +		if (node->right != NULL) {
    3.23 +			delete(node->right);
    3.24 +		}
    3.25 +		free(node); node=0;
    3.26  	}
    3.27 -	if (node->right != NULL) {
    3.28 -		delete(node->right);
    3.29 -	}
    3.30 -	free(node); node=0;
    3.31  }
    3.32  
    3.33  
    3.34  /* print */
    3.35  void printNode(struct Node* node) {
    3.36 -	printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value);
    3.37 +	if (node != NULL) {
    3.38 +		fprintf(stderr, "Node: %20s (%d|%c)\n", node->name, node->value, node->value);
    3.39 +	}
    3.40  }
    3.41  
    3.42  void printTree(struct Node* root) {
    3.43 -	printNode(root);
    3.44 -	printf("  down: ");
    3.45 -	if (root->down != NULL) {
    3.46 -		printTree(root->down);
    3.47 -	} else {
    3.48 -		printf("NULL\n");
    3.49 -	}
    3.50 -	printf("  right: ");
    3.51 -	if (root->right != NULL) {
    3.52 -		printTree(root->right);
    3.53 -	} else {
    3.54 -		printf("NULL\n");
    3.55 +	if (root != NULL) {
    3.56 +		printNode(root);
    3.57 +		fprintf(stderr, "  down: ");
    3.58 +		if (root->down != NULL) {
    3.59 +			printTree(root->down);
    3.60 +		} else {
    3.61 +			fprintf(stderr, "NULL\n");
    3.62 +		}
    3.63 +		fprintf(stderr, "  right: ");
    3.64 +		if (root->right != NULL) {
    3.65 +			printTree(root->right);
    3.66 +		} else {
    3.67 +			fprintf(stderr, "NULL\n");
    3.68 +		}
    3.69  	}
    3.70  }
    3.71  
    3.72 @@ -100,7 +106,7 @@
    3.73  /* init */
    3.74  void init() {
    3.75  	/* add some numbers
    3.76 -	root = newNode("print", 0);
    3.77 +	root = newNode("print", 'n');
    3.78  	root->down = newNode("sum", 0);
    3.79  
    3.80  	root->down->down = newNode("number", 60);
    3.81 @@ -112,28 +118,82 @@
    3.82  	root->down->down->right->right->down->right = newNode("number", 5);
    3.83  	*/
    3.84  
    3.85 -	/* input numbers and add them */
    3.86 -	root = newNode("print", 0);
    3.87 +	/* input numbers and add them
    3.88 +	root = newNode("print", 'n');
    3.89  	root->down = newNode("sum", 0);
    3.90  	root->down->down = newNode("input", 0);
    3.91  	root->down->down->right = newNode("input", 0);
    3.92  	root->down->down->right->right = newNode("input", 0);
    3.93 +	*/
    3.94 +
    3.95 +	/* prints HelloWorld
    3.96 +	*/
    3.97 +	struct Node* np = 0;
    3.98 +
    3.99 +	root = newNode("blackhole", 0);
   3.100 +	root->down = newNode("sum", 0);
   3.101 +	root->down->down = newNode("print", 'c');
   3.102 +	np = root->down->down;
   3.103 +	np->down = newNode("number", 'H');
   3.104 +
   3.105 +	np->right = newNode("print", 'c');
   3.106 +	np->right->down = newNode("number", 'e');
   3.107 +	np = np->right;
   3.108 +
   3.109 +	np->right = newNode("print", 'c');
   3.110 +	np->right->down = newNode("number", 'l');
   3.111 +	np = np->right;
   3.112 +
   3.113 +	np->right = newNode("print", 'c');
   3.114 +	np->right->down = newNode("number", 'l');
   3.115 +	np = np->right;
   3.116 +
   3.117 +	np->right = newNode("print", 'c');
   3.118 +	np->right->down = newNode("number", 'o');
   3.119 +	np = np->right;
   3.120 +
   3.121 +	np->right = newNode("print", 'c');
   3.122 +	np->right->down = newNode("number", ' ');
   3.123 +	np = np->right;
   3.124 +
   3.125 +	np->right = newNode("print", 'c');
   3.126 +	np->right->down = newNode("number", 'L');
   3.127 +	np = np->right;
   3.128 +
   3.129 +	np->right = newNode("print", 'c');
   3.130 +	np->right->down = newNode("number", 'y');
   3.131 +	np = np->right;
   3.132 +
   3.133 +	np->right = newNode("print", 'c');
   3.134 +	np->right->down = newNode("number", 'd');
   3.135 +	np = np->right;
   3.136 +
   3.137 +	np->right = newNode("print", 'c');
   3.138 +	np->right->down = newNode("number", 'i');
   3.139 +	np = np->right;
   3.140 +
   3.141 +	np->right = newNode("print", 'c');
   3.142 +	np->right->down = newNode("number", 10);
   3.143 +	np = np->right;
   3.144 +
   3.145 +
   3.146  }
   3.147  
   3.148  
   3.149  /* main */
   3.150  int main(int argc, char* argv[]) {
   3.151 +	unsigned char shell_return;
   3.152  	init();
   3.153  
   3.154  	printTree(root);
   3.155 -	printf("\n\n");
   3.156 +	fprintf(stderr, "\n\n");
   3.157  
   3.158 -	action(root);
   3.159 +	shell_return = action(root);
   3.160  
   3.161 -	printf("\n\n");
   3.162 +	fprintf(stderr, "\n\n");
   3.163  	printTree(root);
   3.164  
   3.165  	delete(root);
   3.166  	
   3.167 -	return(0);
   3.168 +	exit(shell_return);
   3.169  }