baum
annotate 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 |
rev | line source |
---|---|
meillo@2 | 1 #include <stdio.h> |
meillo@2 | 2 #include <stdlib.h> |
meillo@2 | 3 #include <string.h> |
meillo@2 | 4 #include "baum.h" |
meillo@2 | 5 #include "actions.h" |
meillo@2 | 6 |
meillo@2 | 7 |
meillo@2 | 8 char action(struct Node* node) { |
meillo@2 | 9 if (strcmp(node->name, "print") == 0) { |
meillo@2 | 10 logit("print-node"); |
meillo@2 | 11 return action_print(node); |
meillo@2 | 12 } else if (strcmp(node->name, "sum") == 0) { |
meillo@2 | 13 logit("sum-node"); |
meillo@2 | 14 return action_sum(node); |
meillo@2 | 15 } else if (strcmp(node->name, "printchar") == 0) { |
meillo@2 | 16 logit("printchar-node"); |
meillo@2 | 17 return action_printchar(node); |
meillo@2 | 18 } else if (strcmp(node->name, "number") == 0) { |
meillo@2 | 19 logit("number-node"); |
meillo@2 | 20 return action_number(node); |
meillo@2 | 21 } else { |
meillo@2 | 22 fprintf(stderr, "unknown kind of node"); |
meillo@2 | 23 exit(1); |
meillo@2 | 24 } |
meillo@2 | 25 } |
meillo@2 | 26 |
meillo@2 | 27 |
meillo@2 | 28 |
meillo@2 | 29 char action_print(struct Node* node) { |
meillo@2 | 30 printf("%d\n", action(node->down)); |
meillo@2 | 31 return 0; |
meillo@2 | 32 } |
meillo@2 | 33 |
meillo@2 | 34 |
meillo@2 | 35 char action_printchar(struct Node* node) { |
meillo@2 | 36 printf("%c\n", action(node->down)); |
meillo@2 | 37 return 0; |
meillo@2 | 38 } |
meillo@2 | 39 |
meillo@2 | 40 |
meillo@2 | 41 char action_sum(struct Node* node) { |
meillo@2 | 42 struct Node* tp; |
meillo@2 | 43 tp = node->down; |
meillo@2 | 44 while (tp != NULL) { |
meillo@2 | 45 node->value += action(tp); |
meillo@2 | 46 tp = tp->right; |
meillo@2 | 47 } |
meillo@2 | 48 return node->value; |
meillo@2 | 49 } |
meillo@2 | 50 |
meillo@2 | 51 |
meillo@2 | 52 char action_number(struct Node* node) { |
meillo@2 | 53 return node->value; |
meillo@2 | 54 } |
meillo@2 | 55 |