baum

view actions.c @ 12:8e34daa80f64

input is now read from file again (because of input node)
author meillo@marmaro.de
date Tue, 12 Feb 2008 15:53:08 +0100
parents 0e15841ae111
children 15e11eea1c66
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(1);
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 scanf("%d", &input);
78 input = input % 256;
79 insertLast(node, newNode("number", (char) input));
81 return 0;
82 }
85 unsigned char action_times(struct Node* node) {
86 struct Node* tp;
87 unsigned char i;
88 tp = node->down;
89 for (i = 0; i < node->value; i++) {
90 /* deep copy */
91 }
92 return 0;
93 }
96 unsigned char action_blackhole(struct Node* node) {
97 action(node->down);
98 return 0;
99 }