baum

view actions.c @ 3:15d7d6b9766f

added input; added nextNode, lastNode, insertLast
author meillo@marmaro.de
date Thu, 07 Feb 2008 16:15:07 +0100
parents 557fa4df2bcd
children c202ccccedb5
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 (strcmp(node->name, "print") == 0) {
10 logit("print-node");
11 return action_print(node);
13 } else if (strcmp(node->name, "sum") == 0) {
14 logit("sum-node");
15 return action_sum(node);
17 } else if (strcmp(node->name, "printchar") == 0) {
18 logit("printchar-node");
19 return action_printchar(node);
21 } else if (strcmp(node->name, "number") == 0) {
22 logit("number-node");
23 return action_number(node);
25 } else if (strcmp(node->name, "input") == 0) {
26 logit("input-node");
27 return action_input(node);
29 } else {
30 fprintf(stderr, "unknown kind of node");
31 exit(1);
32 }
33 }
37 unsigned char action_print(struct Node* node) {
38 printf("%d\n", action(node->down));
39 return 0;
40 }
43 unsigned char action_printchar(struct Node* node) {
44 printf("%c\n", action(node->down));
45 return 0;
46 }
49 unsigned char action_sum(struct Node* node) {
50 struct Node* tp;
51 tp = node->down;
52 while (tp != NULL) {
53 node->value += action(tp);
54 tp = tp->right;
55 }
56 return node->value;
57 }
60 unsigned char action_number(struct Node* node) {
61 return node->value;
62 }
65 unsigned char action_input(struct Node* node) {
66 unsigned char input = (unsigned char) getchar();
67 getchar(); /* catches the newline */
68 insertLast(node, newNode("number", input));
69 return 0;
70 }