Mercurial > baum
annotate actions.c @ 30:cd979b979610
fixed multiple (un)indentions in read_input; some better comments
author | meillo@marmaro.de |
---|---|
date | Fri, 22 Feb 2008 14:47:47 +0100 |
parents | f0856c177403 |
children | 4e60d96265f0 |
rev | line source |
---|---|
2 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include "baum.h" | |
5 #include "actions.h" | |
6 | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
7 unsigned char action_print(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
8 unsigned char action_sum(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
9 unsigned char action_number(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
10 unsigned char action_input(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
11 unsigned char action_times(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
12 unsigned char action_blackhole(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
13 |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
14 |
2 | 15 |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
16 unsigned char action(struct Node* node) { |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
17 if (node == NULL) { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
18 fprintf(stderr, "action of non existing node\n"); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
19 return 0; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
20 } |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
21 |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
22 logit(node->name); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
23 |
2 | 24 if (strcmp(node->name, "print") == 0) { |
25 return action_print(node); | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
26 |
2 | 27 } else if (strcmp(node->name, "sum") == 0) { |
28 return action_sum(node); | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
29 |
2 | 30 } else if (strcmp(node->name, "number") == 0) { |
31 return action_number(node); | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
32 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
33 } else if (strcmp(node->name, "input") == 0) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
34 return action_input(node); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
35 |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
36 } else if (strcmp(node->name, "times") == 0) { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
37 return action_times(node); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
38 |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
39 } else if (strcmp(node->name, "blackhole") == 0) { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
40 return action_blackhole(node); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
41 |
2 | 42 } else { |
43 fprintf(stderr, "unknown kind of node"); | |
16
b62288419c1c
added README and LICENSE; changed error code of invalid node
meillo@marmaro.de
parents:
15
diff
changeset
|
44 exit(4); |
2 | 45 } |
46 } | |
47 | |
48 | |
49 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
50 unsigned char action_print(struct Node* node) { |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
51 unsigned char result; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
52 result = action(node->down); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
53 if (node->value == 'c') { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
54 printf("%c", result); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
55 } else { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
56 printf("%d", result); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
57 } |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
58 return result; |
2 | 59 } |
60 | |
61 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
62 unsigned char action_sum(struct Node* node) { |
2 | 63 struct Node* tp; |
64 tp = node->down; | |
65 while (tp != NULL) { | |
66 node->value += action(tp); | |
67 tp = tp->right; | |
68 } | |
69 return node->value; | |
70 } | |
71 | |
72 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
73 unsigned char action_number(struct Node* node) { |
2 | 74 return node->value; |
75 } | |
76 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
77 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
78 unsigned char action_input(struct Node* node) { |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
79 /* reads a number which is treated as ASCII value */ |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
80 int input; |
14 | 81 printf("input: "); |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
82 scanf("%d", &input); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
83 input = input % 256; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
84 insertLast(node, newNode("number", (char) input)); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
85 |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
86 return 0; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
87 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
88 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
89 |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
90 unsigned char action_times(struct Node* node) { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
91 struct Node* tp; |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
92 struct Node* last; |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
93 unsigned char i; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
94 tp = node->down; |
10 | 95 for (i = 0; i < node->value; i++) { |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
26
diff
changeset
|
96 /* FIXME deep copy */ |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
97 last = insertLast(node, newNode(tp->name, tp->value)); |
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
98 if (tp->down != NULL) { |
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
99 last->down = newNode(tp->down->name, tp->down->value); |
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
100 } |
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
14
diff
changeset
|
101 |
5
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
102 } |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
103 return 0; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
104 } |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
105 |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
106 |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
107 unsigned char action_blackhole(struct Node* node) { |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
108 action(node->down); |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
109 return 0; |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
110 } |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
111 |
c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents:
3
diff
changeset
|
112 |