baum

view actions.c @ 38:ff01f0f076e4

option_verbose is now global; stuff about warning when (expected) nodes are not there
author meillo@marmaro.de
date Sat, 01 Mar 2008 20:04:08 +0100
parents b11ac43f3917
children 1ad3d7305e5d
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);
15 unsigned char action(struct Node* node) {
16 if (node == NULL) {
17 if (option_verbose) {
18 fprintf(stderr, "warning: action of non existing node\n");
19 }
20 return 0;
21 }
23 logit(node->name);
25 if (strcmp(node->name, "print") == 0) {
26 return action_print(node);
28 } else if (strcmp(node->name, "sum") == 0) {
29 return action_sum(node);
31 } else if (strcmp(node->name, "number") == 0) {
32 return action_number(node);
34 } else if (strcmp(node->name, "input") == 0) {
35 return action_input(node);
37 } else if (strcmp(node->name, "times") == 0) {
38 return action_times(node);
40 } else {
41 fprintf(stderr, "unknown kind of node\n");
42 exit(4);
43 }
44 }
48 unsigned char action_print(struct Node* node) {
49 unsigned char result;
50 result = action(node->down);
51 if (node->value == 'c') {
52 printf("%c", result);
53 } else {
54 printf("%d", result);
55 }
56 return result;
57 }
60 unsigned char action_sum(struct Node* node) {
61 struct Node* tp;
62 tp = node->down;
63 while (tp != NULL) {
64 node->value += action(tp);
65 tp = tp->right;
66 }
67 return node->value;
68 }
71 unsigned char action_number(struct Node* node) {
72 if (node->down != NULL) {
73 action(node->down);
74 }
75 return node->value;
76 }
79 unsigned char action_input(struct Node* node) {
80 /* reads a number which is treated as ASCII value */
81 int input;
82 printf("input: ");
83 scanf("%d", &input);
84 input = input % 256;
85 insertLast(node, newNode("number", (char) input));
87 return 0;
88 }
91 unsigned char action_times(struct Node* node) {
92 struct Node* tp;
93 struct Node* last;
94 unsigned char i;
95 tp = node->down;
96 for (i = 0; i < node->value; i++) {
97 /* FIXME deep copy */
98 last = insertLast(node, newNode(tp->name, tp->value));
99 if (tp->down != NULL) {
100 last->down = newNode(tp->down->name, tp->down->value);
101 }
103 }
104 return 0;
105 }