baum

view actions.c @ 10:0e15841ae111

s/list/stack/g because thats what it is
author meillo@marmaro.de
date Sat, 09 Feb 2008 16:49:29 +0100
parents c202ccccedb5
children 8a8da74530dd
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 /*
76 unsigned char input = (unsigned char) getchar();
77 getchar();
78 */
80 /* reads a number which is treated as ASCII value */
81 int input;
82 scanf("%d", &input);
83 input = input % 256;
84 insertLast(node, newNode("number", (char) input));
86 return 0;
87 }
90 unsigned char action_times(struct Node* node) {
91 struct Node* tp;
92 unsigned char i;
93 tp = node->down;
94 for (i = 0; i < node->value; i++) {
95 /* deep copy */
96 }
97 return 0;
98 }
101 unsigned char action_blackhole(struct Node* node) {
102 action(node->down);
103 return 0;
104 }