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 wrap: on
line diff
--- a/actions.c	Thu Feb 07 18:05:03 2008 +0100
+++ b/actions.c	Fri Feb 08 20:45:17 2008 +0100
@@ -6,6 +6,11 @@
 
 
 unsigned char action(struct Node* node) {
+	if (node == NULL) {
+		fprintf(stderr, "action of non existing node\n");
+		return 0;
+	}
+
 	if (strcmp(node->name, "print") == 0) {
 		logit("print-node");
 		return action_print(node);
@@ -14,10 +19,6 @@
 		logit("sum-node");
 		return action_sum(node);
 
-	} else if (strcmp(node->name, "printchar") == 0) {
-		logit("printchar-node");
-		return action_printchar(node);
-
 	} else if (strcmp(node->name, "number") == 0) {
 		logit("number-node");
 		return action_number(node);
@@ -26,6 +27,14 @@
 		logit("input-node");
 		return action_input(node);
 
+	} else if (strcmp(node->name, "times") == 0) {
+		logit("times-node");
+		return action_times(node);
+
+	} else if (strcmp(node->name, "blackhole") == 0) {
+		logit("blackhole-node");
+		return action_blackhole(node);
+
 	} else {
 		fprintf(stderr, "unknown kind of node");
 		exit(1);
@@ -35,14 +44,14 @@
 
 
 unsigned char action_print(struct Node* node) {
-	printf("%d\n", action(node->down));
-	return 0;
-}
-
-
-unsigned char action_printchar(struct Node* node) {
-	printf("%c\n", action(node->down));
-	return 0;
+	unsigned char result;
+	result = action(node->down);
+	if (node->value == 'c') {
+		printf("%c", result);
+	} else {
+		printf("%d", result);
+	}
+	return result;
 }
 
 
@@ -63,10 +72,34 @@
 
 
 unsigned char action_input(struct Node* node) {
+	/*
 	unsigned char input = (unsigned char) getchar();
 	getchar();  /* catches the newline */
-	insertLast(node, newNode("number", input));
+
+	/* reads a number which is treated as ASCII value */
+	int input;
+	scanf("%d", &input);
+	input = input % 256;
+	insertLast(node, newNode("number", (char) input));
+
 	return 0;
 }
 
 
+unsigned char action_times(struct Node* node) {
+	struct Node* tp;
+	unsigned char i;
+	tp = node->down;
+	for (i; i < node->value; i++) {
+		/* deep copy */
+	}
+	return 0;
+}
+
+
+unsigned char action_blackhole(struct Node* node) {
+	action(node->down);
+	return 0;
+}
+
+
--- a/actions.h	Thu Feb 07 18:05:03 2008 +0100
+++ b/actions.h	Fri Feb 08 20:45:17 2008 +0100
@@ -2,8 +2,9 @@
 unsigned char action(struct Node* node);
 
 unsigned char action_print(struct Node* node);
-unsigned char action_printchar(struct Node* node);
 unsigned char action_sum(struct Node* node);
 unsigned char action_number(struct Node* node);
 unsigned char action_input(struct Node* node);
+unsigned char action_times(struct Node* node);
+unsigned char action_blackhole(struct Node* node);
 
--- a/baum.c	Thu Feb 07 18:05:03 2008 +0100
+++ b/baum.c	Fri Feb 08 20:45:17 2008 +0100
@@ -15,7 +15,7 @@
 #include "actions.h"
 
 
-struct Node* root;
+struct Node* root = 0;
 
 
 void logit(char* text) {
@@ -58,34 +58,40 @@
 
 /* delete */
 void delete(struct Node* node) {
-	if (node->down != NULL) {
-		delete(node->down);
+	if (node != NULL) {
+		if (node->down != NULL) {
+			delete(node->down);
+		}
+		if (node->right != NULL) {
+			delete(node->right);
+		}
+		free(node); node=0;
 	}
-	if (node->right != NULL) {
-		delete(node->right);
-	}
-	free(node); node=0;
 }
 
 
 /* print */
 void printNode(struct Node* node) {
-	printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value);
+	if (node != NULL) {
+		fprintf(stderr, "Node: %20s (%d|%c)\n", node->name, node->value, node->value);
+	}
 }
 
 void printTree(struct Node* root) {
-	printNode(root);
-	printf("  down: ");
-	if (root->down != NULL) {
-		printTree(root->down);
-	} else {
-		printf("NULL\n");
-	}
-	printf("  right: ");
-	if (root->right != NULL) {
-		printTree(root->right);
-	} else {
-		printf("NULL\n");
+	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");
+		}
 	}
 }
 
@@ -100,7 +106,7 @@
 /* init */
 void init() {
 	/* add some numbers
-	root = newNode("print", 0);
+	root = newNode("print", 'n');
 	root->down = newNode("sum", 0);
 
 	root->down->down = newNode("number", 60);
@@ -112,28 +118,82 @@
 	root->down->down->right->right->down->right = newNode("number", 5);
 	*/
 
-	/* input numbers and add them */
-	root = newNode("print", 0);
+	/* input numbers and add them
+	root = newNode("print", 'n');
 	root->down = newNode("sum", 0);
 	root->down->down = newNode("input", 0);
 	root->down->down->right = newNode("input", 0);
 	root->down->down->right->right = newNode("input", 0);
+	*/
+
+	/* prints HelloWorld
+	*/
+	struct Node* np = 0;
+
+	root = newNode("blackhole", 0);
+	root->down = newNode("sum", 0);
+	root->down->down = newNode("print", 'c');
+	np = root->down->down;
+	np->down = newNode("number", 'H');
+
+	np->right = newNode("print", 'c');
+	np->right->down = newNode("number", 'e');
+	np = np->right;
+
+	np->right = newNode("print", 'c');
+	np->right->down = newNode("number", 'l');
+	np = np->right;
+
+	np->right = newNode("print", 'c');
+	np->right->down = newNode("number", 'l');
+	np = np->right;
+
+	np->right = newNode("print", 'c');
+	np->right->down = newNode("number", 'o');
+	np = np->right;
+
+	np->right = newNode("print", 'c');
+	np->right->down = newNode("number", ' ');
+	np = np->right;
+
+	np->right = newNode("print", 'c');
+	np->right->down = newNode("number", 'L');
+	np = np->right;
+
+	np->right = newNode("print", 'c');
+	np->right->down = newNode("number", 'y');
+	np = np->right;
+
+	np->right = newNode("print", 'c');
+	np->right->down = newNode("number", 'd');
+	np = np->right;
+
+	np->right = newNode("print", 'c');
+	np->right->down = newNode("number", 'i');
+	np = np->right;
+
+	np->right = newNode("print", 'c');
+	np->right->down = newNode("number", 10);
+	np = np->right;
+
+
 }
 
 
 /* main */
 int main(int argc, char* argv[]) {
+	unsigned char shell_return;
 	init();
 
 	printTree(root);
-	printf("\n\n");
+	fprintf(stderr, "\n\n");
 
-	action(root);
+	shell_return = action(root);
 
-	printf("\n\n");
+	fprintf(stderr, "\n\n");
 	printTree(root);
 
 	delete(root);
 	
-	return(0);
+	exit(shell_return);
 }