baum

view actions.c @ 37:29172b6e802a

switched to next version number, updated man page
author meillo@marmaro.de
date Sat, 01 Mar 2008 17:55:42 +0100
parents 4e60d96265f0
children ff01f0f076e4
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 fprintf(stderr, "action of non existing node\n");
18 return 0;
19 }
21 logit(node->name);
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 action(node->down);
71 return node->value;
72 }
75 unsigned char action_input(struct Node* node) {
76 /* reads a number which is treated as ASCII value */
77 int input;
78 printf("input: ");
79 scanf("%d", &input);
80 input = input % 256;
81 insertLast(node, newNode("number", (char) input));
83 return 0;
84 }
87 unsigned char action_times(struct Node* node) {
88 struct Node* tp;
89 struct Node* last;
90 unsigned char i;
91 tp = node->down;
92 for (i = 0; i < node->value; i++) {
93 /* FIXME deep copy */
94 last = insertLast(node, newNode(tp->name, tp->value));
95 if (tp->down != NULL) {
96 last->down = newNode(tp->down->name, tp->down->value);
97 }
99 }
100 return 0;
101 }