diff actions.c @ 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 15d7d6b9766f
children 0e15841ae111
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;
+}
+
+