Mercurial > baum
comparison 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 |
comparison
equal
deleted
inserted
replaced
2:557fa4df2bcd | 3:15d7d6b9766f |
---|---|
3 #include <string.h> | 3 #include <string.h> |
4 #include "baum.h" | 4 #include "baum.h" |
5 #include "actions.h" | 5 #include "actions.h" |
6 | 6 |
7 | 7 |
8 char action(struct Node* node) { | 8 unsigned char action(struct Node* node) { |
9 if (strcmp(node->name, "print") == 0) { | 9 if (strcmp(node->name, "print") == 0) { |
10 logit("print-node"); | 10 logit("print-node"); |
11 return action_print(node); | 11 return action_print(node); |
12 | |
12 } else if (strcmp(node->name, "sum") == 0) { | 13 } else if (strcmp(node->name, "sum") == 0) { |
13 logit("sum-node"); | 14 logit("sum-node"); |
14 return action_sum(node); | 15 return action_sum(node); |
16 | |
15 } else if (strcmp(node->name, "printchar") == 0) { | 17 } else if (strcmp(node->name, "printchar") == 0) { |
16 logit("printchar-node"); | 18 logit("printchar-node"); |
17 return action_printchar(node); | 19 return action_printchar(node); |
20 | |
18 } else if (strcmp(node->name, "number") == 0) { | 21 } else if (strcmp(node->name, "number") == 0) { |
19 logit("number-node"); | 22 logit("number-node"); |
20 return action_number(node); | 23 return action_number(node); |
24 | |
25 } else if (strcmp(node->name, "input") == 0) { | |
26 logit("input-node"); | |
27 return action_input(node); | |
28 | |
21 } else { | 29 } else { |
22 fprintf(stderr, "unknown kind of node"); | 30 fprintf(stderr, "unknown kind of node"); |
23 exit(1); | 31 exit(1); |
24 } | 32 } |
25 } | 33 } |
26 | 34 |
27 | 35 |
28 | 36 |
29 char action_print(struct Node* node) { | 37 unsigned char action_print(struct Node* node) { |
30 printf("%d\n", action(node->down)); | 38 printf("%d\n", action(node->down)); |
31 return 0; | 39 return 0; |
32 } | 40 } |
33 | 41 |
34 | 42 |
35 char action_printchar(struct Node* node) { | 43 unsigned char action_printchar(struct Node* node) { |
36 printf("%c\n", action(node->down)); | 44 printf("%c\n", action(node->down)); |
37 return 0; | 45 return 0; |
38 } | 46 } |
39 | 47 |
40 | 48 |
41 char action_sum(struct Node* node) { | 49 unsigned char action_sum(struct Node* node) { |
42 struct Node* tp; | 50 struct Node* tp; |
43 tp = node->down; | 51 tp = node->down; |
44 while (tp != NULL) { | 52 while (tp != NULL) { |
45 node->value += action(tp); | 53 node->value += action(tp); |
46 tp = tp->right; | 54 tp = tp->right; |
47 } | 55 } |
48 return node->value; | 56 return node->value; |
49 } | 57 } |
50 | 58 |
51 | 59 |
52 char action_number(struct Node* node) { | 60 unsigned char action_number(struct Node* node) { |
53 return node->value; | 61 return node->value; |
54 } | 62 } |
55 | 63 |
64 | |
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 } | |
71 | |
72 |