baum

view actions.c @ 32:3903effae5ef

new debian package
author meillo@marmaro.de
date Fri, 22 Feb 2008 15:58:25 +0100
parents cd979b979610
children b11ac43f3917
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "baum.h"
5 #include "actions.h"
7 unsigned char action_print(struct Node* node);
8 unsigned char action_sum(struct Node* node);
9 unsigned char action_number(struct Node* node);
10 unsigned char action_input(struct Node* node);
11 unsigned char action_times(struct Node* node);
12 unsigned char action_blackhole(struct Node* node);
16 unsigned char action(struct Node* node) {
17 if (node == NULL) {
18 fprintf(stderr, "action of non existing node\n");
19 return 0;
20 }
22 logit(node->name);
24 if (strcmp(node->name, "print") == 0) {
25 return action_print(node);
27 } else if (strcmp(node->name, "sum") == 0) {
28 return action_sum(node);
30 } else if (strcmp(node->name, "number") == 0) {
31 return action_number(node);
33 } else if (strcmp(node->name, "input") == 0) {
34 return action_input(node);
36 } else if (strcmp(node->name, "times") == 0) {
37 return action_times(node);
39 } else if (strcmp(node->name, "blackhole") == 0) {
40 return action_blackhole(node);
42 } else {
43 fprintf(stderr, "unknown kind of node\n");
44 exit(4);
45 }
46 }
50 unsigned char action_print(struct Node* node) {
51 unsigned char result;
52 result = action(node->down);
53 if (node->value == 'c') {
54 printf("%c", result);
55 } else {
56 printf("%d", result);
57 }
58 return result;
59 }
62 unsigned char action_sum(struct Node* node) {
63 struct Node* tp;
64 tp = node->down;
65 while (tp != NULL) {
66 node->value += action(tp);
67 tp = tp->right;
68 }
69 return node->value;
70 }
73 unsigned char action_number(struct Node* node) {
74 return node->value;
75 }
78 unsigned char action_input(struct Node* node) {
79 /* reads a number which is treated as ASCII value */
80 int input;
81 printf("input: ");
82 scanf("%d", &input);
83 input = input % 256;
84 insertLast(node, newNode("number", (char) input));
86 return 0;
87 }
90 unsigned char action_times(struct Node* node) {
91 struct Node* tp;
92 struct Node* last;
93 unsigned char i;
94 tp = node->down;
95 for (i = 0; i < node->value; i++) {
96 /* FIXME deep copy */
97 last = insertLast(node, newNode(tp->name, tp->value));
98 if (tp->down != NULL) {
99 last->down = newNode(tp->down->name, tp->down->value);
100 }
102 }
103 return 0;
104 }
107 unsigned char action_blackhole(struct Node* node) {
108 action(node->down);
109 return 0;
110 }