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