baum

view actions.c @ 14:15e11eea1c66

input says now that it wants an input
author meillo@marmaro.de
date Wed, 13 Feb 2008 09:16:01 +0100
parents 8a8da74530dd
children e2048e569891
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 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 unsigned char i;
89 tp = node->down;
90 for (i = 0; i < node->value; i++) {
91 /* deep copy */
92 }
93 return 0;
94 }
97 unsigned char action_blackhole(struct Node* node) {
98 action(node->down);
99 return 0;
100 }