Mercurial > baum
annotate actions.c @ 53:036779d5da75
"sum" node clears its value now everytime it gets called
author | meillo@marmaro.de |
---|---|
date | Sun, 02 Mar 2008 16:32:52 +0100 |
parents | 201b4603671a |
children | 6279e5b14d9e |
rev | line source |
---|---|
2 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include "baum.h" | |
5 | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
6 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
|
7 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
|
8 unsigned char action_number(struct Node* node); |
51
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
9 unsigned char action_create(struct Node* node); |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
16
diff
changeset
|
10 unsigned char action_times(struct Node* node); |
50
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
11 unsigned char action_if(struct Node* node); |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
12 unsigned char action_while(struct Node* node); |
26
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) { |
38
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
18 if (option_verbose) { |
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
19 fprintf(stderr, "warning: action of non existing node\n"); |
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
20 } |
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
|
21 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
|
22 } |
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
|
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 |
51
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
33 } else if (strcmp(node->name, "create") == 0) { |
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
34 return action_create(node); |
3
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 |
50
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
39 } else if (strcmp(node->name, "if") == 0) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
40 return action_if(node); |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
41 |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
42 } else if (strcmp(node->name, "while") == 0) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
43 return action_while(node); |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
44 |
2 | 45 } else { |
31
4e60d96265f0
removed -c option completely; updated man page; new error code 5
meillo@marmaro.de
parents:
30
diff
changeset
|
46 fprintf(stderr, "unknown kind of node\n"); |
16
b62288419c1c
added README and LICENSE; changed error code of invalid node
meillo@marmaro.de
parents:
15
diff
changeset
|
47 exit(4); |
2 | 48 } |
49 } | |
50 | |
51 | |
52 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 } 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
|
59 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
|
60 } |
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
|
61 return result; |
2 | 62 } |
63 | |
64 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
65 unsigned char action_sum(struct Node* node) { |
2 | 66 struct Node* tp; |
53
036779d5da75
"sum" node clears its value now everytime it gets called
meillo@marmaro.de
parents:
52
diff
changeset
|
67 |
036779d5da75
"sum" node clears its value now everytime it gets called
meillo@marmaro.de
parents:
52
diff
changeset
|
68 node->value = 0; |
2 | 69 tp = node->down; |
70 while (tp != NULL) { | |
71 node->value += action(tp); | |
72 tp = tp->right; | |
73 } | |
74 return node->value; | |
75 } | |
76 | |
77 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
78 unsigned char action_number(struct Node* node) { |
38
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
79 if (node->down != NULL) { |
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
80 action(node->down); |
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
36
diff
changeset
|
81 } |
2 | 82 return node->value; |
83 } | |
84 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
85 |
51
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
86 unsigned char action_create(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
|
87 int input; |
51
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
88 |
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
89 if (node->down == NULL) { |
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
90 /* user input */ |
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
91 printf("input: "); |
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
92 scanf("%d", &input); |
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
93 input = input % 256; |
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
94 } else { |
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
95 /* return value */ |
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
96 input = action(node->down); |
7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
meillo@marmaro.de
parents:
50
diff
changeset
|
97 } |
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
|
98 insertLast(node, newNode("number", (char) input)); |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
99 return 0; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
100 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
101 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
102 |
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
|
103 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
|
104 unsigned char i; |
10 | 105 for (i = 0; i < node->value; i++) { |
46 | 106 insertLast(node, copyTree(node->down)); |
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
|
107 } |
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 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
|
109 } |
50
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
110 |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
111 |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
112 unsigned char action_if(struct Node* node) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
113 int result = 0; |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
114 char return1; |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
115 char return2; |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
116 |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
117 if (node->down == NULL || node->down->right == NULL) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
118 fprintf(stderr, "error: while requires two sons"); |
52
201b4603671a
documented "if" and "while" in the man page; new error code 7
meillo@marmaro.de
parents:
51
diff
changeset
|
119 exit(7); |
50
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
120 } |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
121 |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
122 return1 = action(node->down); |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
123 return2 = action(node->down->right); |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
124 |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
125 if (node->value == '!') { /* 33 */ |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
126 if (return1 != return2) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
127 result = 1; |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
128 } |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
129 } else if (node->value == '<') { /* 60 */ |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
130 if (return1 < return2) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
131 result = 1; |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
132 } |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
133 } else if (node->value == '>') { /* 62 */ |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
134 if (return1 > return2) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
135 result = 1; |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
136 } |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
137 } else { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
138 if (return1 == return2) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
139 result = 1; |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
140 } |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
141 } |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
142 return result; |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
143 } |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
144 |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
145 |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
146 unsigned char action_while(struct Node* node) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
147 if (node->down == NULL || node->down->right == NULL) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
148 fprintf(stderr, "error: while requires two sons"); |
52
201b4603671a
documented "if" and "while" in the man page; new error code 7
meillo@marmaro.de
parents:
51
diff
changeset
|
149 exit(7); |
50
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
150 } |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
151 while (action(node->down)) { |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
152 action(node->down->right); |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
153 } |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
154 return 0; |
0870e261bf28
added first implementation of "if" and "while" (but they are not perfect yet)
meillo@marmaro.de
parents:
48
diff
changeset
|
155 } |