baum
diff actions.c @ 2:557fa4df2bcd
added difference between char and number
author | meillo@marmaro.de |
---|---|
date | Thu, 07 Feb 2008 14:46:27 +0100 |
parents | |
children | 15d7d6b9766f |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/actions.c Thu Feb 07 14:46:27 2008 +0100 1.3 @@ -0,0 +1,55 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include "baum.h" 1.8 +#include "actions.h" 1.9 + 1.10 + 1.11 +char action(struct Node* node) { 1.12 + if (strcmp(node->name, "print") == 0) { 1.13 + logit("print-node"); 1.14 + return action_print(node); 1.15 + } else if (strcmp(node->name, "sum") == 0) { 1.16 + logit("sum-node"); 1.17 + return action_sum(node); 1.18 + } else if (strcmp(node->name, "printchar") == 0) { 1.19 + logit("printchar-node"); 1.20 + return action_printchar(node); 1.21 + } else if (strcmp(node->name, "number") == 0) { 1.22 + logit("number-node"); 1.23 + return action_number(node); 1.24 + } else { 1.25 + fprintf(stderr, "unknown kind of node"); 1.26 + exit(1); 1.27 + } 1.28 +} 1.29 + 1.30 + 1.31 + 1.32 +char action_print(struct Node* node) { 1.33 + printf("%d\n", action(node->down)); 1.34 + return 0; 1.35 +} 1.36 + 1.37 + 1.38 +char action_printchar(struct Node* node) { 1.39 + printf("%c\n", action(node->down)); 1.40 + return 0; 1.41 +} 1.42 + 1.43 + 1.44 +char action_sum(struct Node* node) { 1.45 + struct Node* tp; 1.46 + tp = node->down; 1.47 + while (tp != NULL) { 1.48 + node->value += action(tp); 1.49 + tp = tp->right; 1.50 + } 1.51 + return node->value; 1.52 +} 1.53 + 1.54 + 1.55 +char action_number(struct Node* node) { 1.56 + return node->value; 1.57 +} 1.58 +