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