baum

view actions.c @ 18:711aab4863cc

added man page
author meillo@marmaro.de
date Wed, 13 Feb 2008 20:44:04 +0100
parents e2048e569891
children f0856c177403
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "baum.h"
5 #include "actions.h"
8 unsigned char action(struct Node* node) {
9 if (node == NULL) {
10 fprintf(stderr, "action of non existing node\n");
11 return 0;
12 }
14 if (strcmp(node->name, "print") == 0) {
15 logit("print-node");
16 return action_print(node);
18 } else if (strcmp(node->name, "sum") == 0) {
19 logit("sum-node");
20 return action_sum(node);
22 } else if (strcmp(node->name, "number") == 0) {
23 logit("number-node");
24 return action_number(node);
26 } else if (strcmp(node->name, "input") == 0) {
27 logit("input-node");
28 return action_input(node);
30 } else if (strcmp(node->name, "times") == 0) {
31 logit("times-node");
32 return action_times(node);
34 } else if (strcmp(node->name, "blackhole") == 0) {
35 logit("blackhole-node");
36 return action_blackhole(node);
38 } else {
39 fprintf(stderr, "unknown kind of node");
40 exit(4);
41 }
42 }
46 unsigned char action_print(struct Node* node) {
47 unsigned char result;
48 result = action(node->down);
49 if (node->value == 'c') {
50 printf("%c", result);
51 } else {
52 printf("%d", result);
53 }
54 return result;
55 }
58 unsigned char action_sum(struct Node* node) {
59 struct Node* tp;
60 tp = node->down;
61 while (tp != NULL) {
62 node->value += action(tp);
63 tp = tp->right;
64 }
65 return node->value;
66 }
69 unsigned char action_number(struct Node* node) {
70 return node->value;
71 }
74 unsigned char action_input(struct Node* node) {
75 /* reads a number which is treated as ASCII value */
76 int input;
77 printf("input: ");
78 scanf("%d", &input);
79 input = input % 256;
80 insertLast(node, newNode("number", (char) input));
82 return 0;
83 }
86 unsigned char action_times(struct Node* node) {
87 struct Node* tp;
88 struct Node* last;
89 unsigned char i;
90 tp = node->down;
91 for (i = 0; i < node->value; i++) {
92 /* deep copy */
93 last = insertLast(node, newNode(tp->name, tp->value));
94 if (tp->down != NULL) {
95 last->down = newNode(tp->down->name, tp->down->value);
96 }
98 }
99 return 0;
100 }
103 unsigned char action_blackhole(struct Node* node) {
104 action(node->down);
105 return 0;
106 }