baum

view actions.c @ 47:c31b5bb6d493

merged header files into only one (removed actions.h)
author meillo@marmaro.de
date Sun, 02 Mar 2008 10:34:09 +0100
parents 22305a6e128d
children f9fc4c4f9e3d
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "baum.h"
6 unsigned char action_print(struct Node* node);
7 unsigned char action_sum(struct Node* node);
8 unsigned char action_number(struct Node* node);
9 unsigned char action_input(struct Node* node);
10 unsigned char action_times(struct Node* node);
14 unsigned char action(struct Node* node) {
15 if (node == NULL) {
16 if (option_verbose) {
17 fprintf(stderr, "warning: action of non existing node\n");
18 }
19 return 0;
20 }
22 if (strcmp(node->name, "print") == 0) {
23 return action_print(node);
25 } else if (strcmp(node->name, "sum") == 0) {
26 return action_sum(node);
28 } else if (strcmp(node->name, "number") == 0) {
29 return action_number(node);
31 } else if (strcmp(node->name, "input") == 0) {
32 return action_input(node);
34 } else if (strcmp(node->name, "times") == 0) {
35 return action_times(node);
37 } else {
38 fprintf(stderr, "unknown kind of node\n");
39 exit(4);
40 }
41 }
45 unsigned char action_print(struct Node* node) {
46 unsigned char result;
47 result = action(node->down);
48 if (node->value == 'c') {
49 printf("%c", result);
50 } else {
51 printf("%d", result);
52 }
53 return result;
54 }
57 unsigned char action_sum(struct Node* node) {
58 struct Node* tp;
59 tp = node->down;
60 while (tp != NULL) {
61 node->value += action(tp);
62 tp = tp->right;
63 }
64 return node->value;
65 }
68 unsigned char action_number(struct Node* node) {
69 if (node->down != NULL) {
70 action(node->down);
71 }
72 return node->value;
73 }
76 unsigned char action_input(struct Node* node) {
77 /* reads a number which is treated as ASCII value */
78 int input;
79 printf("input: ");
80 scanf("%d", &input);
81 input = input % 256;
82 insertLast(node, newNode("number", (char) input));
84 return 0;
85 }
88 unsigned char action_times(struct Node* node) {
89 unsigned char i;
90 for (i = 0; i < node->value; i++) {
91 insertLast(node, copyTree(node->down));
92 }
93 return 0;
94 }