baum
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 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 +