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@3
|
8 unsigned char action(struct Node* node) {
|
meillo@5
|
9 if (node == NULL) {
|
meillo@5
|
10 fprintf(stderr, "action of non existing node\n");
|
meillo@5
|
11 return 0;
|
meillo@5
|
12 }
|
meillo@5
|
13
|
meillo@2
|
14 if (strcmp(node->name, "print") == 0) {
|
meillo@2
|
15 logit("print-node");
|
meillo@2
|
16 return action_print(node);
|
meillo@3
|
17
|
meillo@2
|
18 } else if (strcmp(node->name, "sum") == 0) {
|
meillo@2
|
19 logit("sum-node");
|
meillo@2
|
20 return action_sum(node);
|
meillo@3
|
21
|
meillo@2
|
22 } else if (strcmp(node->name, "number") == 0) {
|
meillo@2
|
23 logit("number-node");
|
meillo@2
|
24 return action_number(node);
|
meillo@3
|
25
|
meillo@3
|
26 } else if (strcmp(node->name, "input") == 0) {
|
meillo@3
|
27 logit("input-node");
|
meillo@3
|
28 return action_input(node);
|
meillo@3
|
29
|
meillo@5
|
30 } else if (strcmp(node->name, "times") == 0) {
|
meillo@5
|
31 logit("times-node");
|
meillo@5
|
32 return action_times(node);
|
meillo@5
|
33
|
meillo@5
|
34 } else if (strcmp(node->name, "blackhole") == 0) {
|
meillo@5
|
35 logit("blackhole-node");
|
meillo@5
|
36 return action_blackhole(node);
|
meillo@5
|
37
|
meillo@2
|
38 } else {
|
meillo@2
|
39 fprintf(stderr, "unknown kind of node");
|
meillo@2
|
40 exit(1);
|
meillo@2
|
41 }
|
meillo@2
|
42 }
|
meillo@2
|
43
|
meillo@2
|
44
|
meillo@2
|
45
|
meillo@3
|
46 unsigned char action_print(struct Node* node) {
|
meillo@5
|
47 unsigned char result;
|
meillo@5
|
48 result = action(node->down);
|
meillo@5
|
49 if (node->value == 'c') {
|
meillo@5
|
50 printf("%c", result);
|
meillo@5
|
51 } else {
|
meillo@5
|
52 printf("%d", result);
|
meillo@5
|
53 }
|
meillo@5
|
54 return result;
|
meillo@2
|
55 }
|
meillo@2
|
56
|
meillo@2
|
57
|
meillo@3
|
58 unsigned char action_sum(struct Node* node) {
|
meillo@2
|
59 struct Node* tp;
|
meillo@2
|
60 tp = node->down;
|
meillo@2
|
61 while (tp != NULL) {
|
meillo@2
|
62 node->value += action(tp);
|
meillo@2
|
63 tp = tp->right;
|
meillo@2
|
64 }
|
meillo@2
|
65 return node->value;
|
meillo@2
|
66 }
|
meillo@2
|
67
|
meillo@2
|
68
|
meillo@3
|
69 unsigned char action_number(struct Node* node) {
|
meillo@2
|
70 return node->value;
|
meillo@2
|
71 }
|
meillo@2
|
72
|
meillo@3
|
73
|
meillo@3
|
74 unsigned char action_input(struct Node* node) {
|
meillo@5
|
75 /* reads a number which is treated as ASCII value */
|
meillo@5
|
76 int input;
|
meillo@5
|
77 scanf("%d", &input);
|
meillo@5
|
78 input = input % 256;
|
meillo@5
|
79 insertLast(node, newNode("number", (char) input));
|
meillo@5
|
80
|
meillo@3
|
81 return 0;
|
meillo@3
|
82 }
|
meillo@3
|
83
|
meillo@3
|
84
|
meillo@5
|
85 unsigned char action_times(struct Node* node) {
|
meillo@5
|
86 struct Node* tp;
|
meillo@5
|
87 unsigned char i;
|
meillo@5
|
88 tp = node->down;
|
meillo@10
|
89 for (i = 0; i < node->value; i++) {
|
meillo@5
|
90 /* deep copy */
|
meillo@5
|
91 }
|
meillo@5
|
92 return 0;
|
meillo@5
|
93 }
|
meillo@5
|
94
|
meillo@5
|
95
|
meillo@5
|
96 unsigned char action_blackhole(struct Node* node) {
|
meillo@5
|
97 action(node->down);
|
meillo@5
|
98 return 0;
|
meillo@5
|
99 }
|
meillo@5
|
100
|
meillo@5
|
101
|