baum
view actions.c @ 41:1ad3d7305e5d
reduced output; cleanups
author | meillo@marmaro.de |
---|---|
date | Sat, 01 Mar 2008 20:35:00 +0100 |
parents | ff01f0f076e4 |
children | 0b82169d4129 |
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 if (strcmp(node->name, "print") == 0) {
24 return action_print(node);
26 } else if (strcmp(node->name, "sum") == 0) {
27 return action_sum(node);
29 } else if (strcmp(node->name, "number") == 0) {
30 return action_number(node);
32 } else if (strcmp(node->name, "input") == 0) {
33 return action_input(node);
35 } else if (strcmp(node->name, "times") == 0) {
36 return action_times(node);
38 } else {
39 fprintf(stderr, "unknown kind of node\n");
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 if (node->down != NULL) {
71 action(node->down);
72 }
73 return node->value;
74 }
77 unsigned char action_input(struct Node* node) {
78 /* reads a number which is treated as ASCII value */
79 int input;
80 printf("input: ");
81 scanf("%d", &input);
82 input = input % 256;
83 insertLast(node, newNode("number", (char) input));
85 return 0;
86 }
89 unsigned char action_times(struct Node* node) {
90 struct Node* tp;
91 struct Node* last;
92 unsigned char i;
93 tp = node->down;
94 for (i = 0; i < node->value; i++) {
95 /* FIXME deep copy */
96 last = insertLast(node, newNode(tp->name, tp->value));
97 if (tp->down != NULL) {
98 last->down = newNode(tp->down->name, tp->down->value);
99 }
101 }
102 return 0;
103 }