Mercurial > baum
annotate baum.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 | c31b5bb6d493 |
children | 6e46b106c334 |
rev | line source |
---|---|
0 | 1 /* |
2 * baum - an esoteric programming language | |
3 * | |
4 * (c) markus schnalke <meillo@marmaro.de> | |
5 * and julian forster | |
6 * | |
7 */ | |
8 | |
9 | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
12 #include <string.h> |
0 | 13 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
14 #include "baum.h" |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
15 |
37
29172b6e802a
switched to next version number, updated man page
meillo@marmaro.de
parents:
34
diff
changeset
|
16 #define VERSION "0.3" |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
17 |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
18 |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
19 void printNode(struct Node* node, int level); |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
20 void printTree(struct Node* root, int level); |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
21 struct Node* lastNode(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
22 void delete(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
23 |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
24 |
41 | 25 struct Stackitem { |
26 struct Node* node; | |
27 struct Stackitem* next; | |
28 }; | |
29 | |
30 | |
31 | |
32 struct Node* root = 0; | |
33 struct Stackitem* stack = NULL; | |
34 | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
35 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
36 void logit(char* text) { |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
37 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
38 fprintf(stderr, "[%s]\n", text); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
39 } |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
40 } |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
41 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
42 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
43 /* new */ |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
44 struct Node* newNode(char* name, unsigned char value) { |
0 | 45 struct Node* node; |
46 node = (struct Node*) malloc(sizeof(struct Node)); | |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
47 strcpy(node->name, name); |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
48 node->value = value; |
0 | 49 node->right = 0; |
50 node->down = 0; | |
51 return node; | |
52 } | |
53 | |
54 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
55 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
56 struct Node* lastNode(struct Node* node) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
57 while (node->right != NULL) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
58 node = node->right; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
59 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
60 return node; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
61 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
62 |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
13
diff
changeset
|
63 struct Node* insertLast(struct Node* node, struct Node* insert) { |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
64 node = lastNode(node); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
65 node->right = insert; |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
13
diff
changeset
|
66 return insert; |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
67 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
68 |
46 | 69 struct Node* copyTree(struct Node* node) { |
70 if (node == NULL) { | |
71 return NULL; | |
72 } | |
73 struct Node* tmp; | |
74 tmp = newNode(node->name, node->value); | |
75 tmp->down = copyTree(node->down); | |
76 tmp->right = copyTree(node->right); | |
77 return tmp; | |
78 } | |
79 | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
80 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
81 /* delete */ |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
82 void delete(struct Node* node) { |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
83 if (node == NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
84 return; |
0 | 85 } |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
86 delete(node->down); |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
87 delete(node->right); |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
88 free(node); node=0; |
0 | 89 } |
90 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
91 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
92 /* print */ |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
93 void printNode(struct Node* node, int level) { |
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:
4
diff
changeset
|
94 if (node != NULL) { |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
95 while (level-- > 0) { |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
96 fprintf(stderr, "\t"); |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
97 } |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
98 fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value); |
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:
4
diff
changeset
|
99 } |
0 | 100 } |
101 | |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
102 void printTree(struct Node* root, int level) { |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
103 if (root == NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
104 return; |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
105 } |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
106 printNode(root, level); |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
107 printTree(root->down, level+1); |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
108 printTree(root->right, level); |
0 | 109 } |
110 | |
111 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
112 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
113 |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
114 |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
115 /* stack for read_input */ |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
116 void push(struct Node* node) { |
10 | 117 struct Stackitem* tmp; |
118 struct Stackitem* new; | |
119 new = (struct Stackitem*) malloc(sizeof(struct Stackitem)); | |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
120 new->node = node; |
10 | 121 tmp = stack; |
122 stack = new; | |
123 stack->next = tmp; | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
124 } |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
125 struct Node* pull() { |
10 | 126 if (stack == NULL) { |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
127 return NULL; |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
128 } |
10 | 129 struct Stackitem* tmp; |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
130 struct Node* node; |
10 | 131 tmp = stack; |
132 stack = stack->next; | |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
133 node = tmp->node; |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
134 free(tmp); tmp=0; |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
135 return node; |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
136 } |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
137 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
138 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
139 |
6 | 140 /* read input */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
141 void read_input(char* filename) { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
142 int c; |
8 | 143 int indent; |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
144 char name[256]; |
6 | 145 int value; |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
146 int last_indent; |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
147 struct Node* last_node; |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
148 struct Node* node; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
149 FILE* file; |
6 | 150 |
8 | 151 indent = 0; |
152 strcpy(name, ""); | |
153 value = 0; | |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
154 last_indent = -1; |
34 | 155 last_node = NULL; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
156 file = fopen(filename, "r"); |
6 | 157 |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
158 while ((c = getc(file)) != EOF) { |
8 | 159 if (c == '#') { /* comment */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
160 while ((c = getc(file)) != '\n') { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
161 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
162 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
163 |
8 | 164 if (c == ' ' || c == '\t') { /* indent if at start of line */ |
165 if (strlen(name) == 0) { | |
166 indent++; | |
167 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
168 } |
6 | 169 |
8 | 170 if (c == '\n') { /* end of line: create node */ |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
171 if (strlen(name) > 0) { |
8 | 172 /* create node */ |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
173 node = newNode((char*) name, value); |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
174 if (indent > last_indent) { /* down */ |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
175 /* if it goes more than one level down -> error */ |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
176 if (indent > last_indent + 1) { |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
177 fprintf(stderr, "error: Indention over more than one level. Only indent by one!\n"); |
31
4e60d96265f0
removed -c option completely; updated man page; new error code 5
meillo@marmaro.de
parents:
30
diff
changeset
|
178 exit(5); |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
179 } |
34 | 180 if (last_node == NULL) { |
181 root = node; | |
182 last_node = root; | |
183 } else { | |
184 last_node->down = node; | |
185 } | |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
186 push(last_node); |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
187 } else if (indent == last_indent) { /* right */ |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
188 last_node->right = node; |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
189 } else if (indent < last_indent) { /* up */ |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
190 /* handle if it goes more than one level up */ |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
191 while (indent < last_indent) { |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
192 last_node = pull(); |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
193 last_indent--; |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
194 } |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
195 last_node->right = node; |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
196 } |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
197 last_indent = indent; |
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
198 last_node = node; |
6 | 199 } |
200 indent = 0; | |
201 strcpy(name, ""); | |
202 value = 0; | |
203 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
204 |
8 | 205 if (c >= 'a' && c <= 'z') { /* name */ |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
206 int i = 1; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
207 name[0] = (char) c; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
208 while ((c = getc(file)) != '(') { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
209 name[i] = (char) c; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
210 i++; |
8 | 211 if (i > 255) { |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
212 fprintf(stderr, "error: node name too long, or no value given\n"); |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
213 exit(6); |
8 | 214 } |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
215 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
216 name[i] = '\0'; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
217 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
218 |
8 | 219 if (c == '(') { /* value */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
220 fscanf(file, "%d)", &value); |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
221 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
222 |
6 | 223 } |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
224 |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
225 /* clear stack */ |
27
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
226 while (stack != NULL) { |
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
227 pull(); |
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
228 } |
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
229 |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
230 fclose(file); |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
231 } |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
232 |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
233 |
6 | 234 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
235 /* main */ |
0 | 236 int main(int argc, char* argv[]) { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
237 unsigned char shell_return = 0; |
38
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
37
diff
changeset
|
238 |
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
37
diff
changeset
|
239 option_verbose = 0; |
9
c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents:
8
diff
changeset
|
240 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
241 while (--argc > 0 && (*++argv)[0] == '-') { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
242 if (strcmp(argv[0], "--version") == 0) { |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
243 printf("\ |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
244 baum %s\n\ |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
245 an esoteric programming language\n\ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
246 by markus schnalke and julian forster\n\ |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
247 http://prog.marmaro.de/baum\n\ |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
248 ", VERSION); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
249 exit(0); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
250 } else if (strcmp(argv[0], "--help") == 0) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
251 printf("\ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
252 baum --version print version information and exit\n\ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
253 baum --help print this output\n\ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
254 baum [-v] <file> (verbosly) run file\n\ |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
255 "); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
256 exit(0); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
257 } else if (strcmp(argv[0], "-v") == 0) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
258 option_verbose = 1; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
259 /* |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
260 } else if (strcmp(argv[0], "-W") == 0) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
261 / * TODO: catch if no value given * / |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
262 iDWarn = atoi((++argv)[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
263 argc--; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
264 */ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
265 } else { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
266 fprintf(stderr, "unknown option: %s\n", argv[0]); |
42
233ac9bea4f1
switched exit code 127 to 126 (because shell returns 127 if command not found)
meillo@marmaro.de
parents:
41
diff
changeset
|
267 exit(126); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
268 } |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
269 } |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
270 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
271 if (argc != 1) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
272 fprintf(stderr, "%d source files given, please specify one.\n", argc); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
273 exit(3); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
274 } |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
275 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
276 read_input(argv[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
277 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
278 if (option_verbose) { |
53
036779d5da75
"sum" node clears its value now everytime it gets called
meillo@marmaro.de
parents:
47
diff
changeset
|
279 fprintf(stderr, "\n\ntree read from input:\n"); |
036779d5da75
"sum" node clears its value now everytime it gets called
meillo@marmaro.de
parents:
47
diff
changeset
|
280 printTree(root, 1); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
281 } |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
282 |
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:
4
diff
changeset
|
283 shell_return = action(root); |
34 | 284 fflush(stdout); |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
285 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
286 if (option_verbose) { |
53
036779d5da75
"sum" node clears its value now everytime it gets called
meillo@marmaro.de
parents:
47
diff
changeset
|
287 fprintf(stderr, "\n\nmodified tree after execution:\n"); |
036779d5da75
"sum" node clears its value now everytime it gets called
meillo@marmaro.de
parents:
47
diff
changeset
|
288 printTree(root, 1); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
289 } |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
290 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
291 delete(root); |
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:
4
diff
changeset
|
292 exit(shell_return); |
0 | 293 } |